Example usage for javax.swing JSpinner isEnabled

List of usage examples for javax.swing JSpinner isEnabled

Introduction

In this page you can find the example usage for javax.swing JSpinner isEnabled.

Prototype

public boolean isEnabled() 

Source Link

Document

Determines whether this component is enabled.

Usage

From source file:userinterface.graph.Histogram.java

/**
 * Generates the property dialog for a Histogram. Allows the user to select either a new or an exisitng Histogram
 * to plot data on//from  w  ww  .j  av a  2 s  .c o m
 * 
 * @param defaultSeriesName
 * @param handler instance of {@link GUIGraphHandler}
 * @param minVal the min value in data cache
 * @param maxVal the max value in data cache
 * @return Either a new instance of a Histogram or an old one depending on what the user selects
 */

public static Pair<Histogram, SeriesKey> showPropertiesDialog(String defaultSeriesName, GUIGraphHandler handler,
        double minVal, double maxVal) {

    // make sure that the probabilities are valid
    if (maxVal > 1.0)
        maxVal = 1.0;
    if (minVal < 0.0)
        minVal = 0.0;

    // set properties for the dialog
    JDialog dialog = new JDialog(GUIPrism.getGUI(), "Histogram properties", true);
    dialog.setLayout(new BorderLayout());

    JPanel p1 = new JPanel(new FlowLayout());
    p1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
            "Number of buckets"));

    JPanel p2 = new JPanel(new FlowLayout());
    p2.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
            "Series name"));

    JSpinner buckets = new JSpinner(new SpinnerNumberModel(10, 5, Integer.MAX_VALUE, 1));
    buckets.setToolTipText("Select the number of buckets for this Histogram");

    // provides the ability to select a new or an old histogram to plot the series on
    JTextField seriesName = new JTextField(defaultSeriesName);
    JRadioButton newSeries = new JRadioButton("New Histogram");
    JRadioButton existing = new JRadioButton("Existing Histogram");
    newSeries.setSelected(true);
    JPanel seriesSelectPanel = new JPanel();
    seriesSelectPanel.setLayout(new BoxLayout(seriesSelectPanel, BoxLayout.Y_AXIS));
    JPanel seriesTypeSelect = new JPanel(new FlowLayout());
    JPanel seriesOptionsPanel = new JPanel(new FlowLayout());
    seriesTypeSelect.add(newSeries);
    seriesTypeSelect.add(existing);
    JComboBox<String> seriesOptions = new JComboBox<>();
    seriesOptionsPanel.add(seriesOptions);
    seriesSelectPanel.add(seriesTypeSelect);
    seriesSelectPanel.add(seriesOptionsPanel);
    seriesSelectPanel.setBorder(BorderFactory
            .createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Add series to"));

    // provides ability to select the min/max range of the plot
    JLabel minValsLabel = new JLabel("Min range:");
    JSpinner minVals = new JSpinner(new SpinnerNumberModel(0.0, 0.0, minVal, 0.01));
    minVals.setToolTipText("Does not allow value more than the min value in the probabilities");
    JLabel maxValsLabel = new JLabel("Max range:");
    JSpinner maxVals = new JSpinner(new SpinnerNumberModel(1.0, maxVal, 1.0, 0.01));
    maxVals.setToolTipText("Does not allow value less than the max value in the probabilities");
    JPanel minMaxPanel = new JPanel();
    minMaxPanel.setLayout(new BoxLayout(minMaxPanel, BoxLayout.X_AXIS));
    JPanel leftValsPanel = new JPanel(new BorderLayout());
    JPanel rightValsPanel = new JPanel(new BorderLayout());
    minMaxPanel.setBorder(
            BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Range"));
    leftValsPanel.add(minValsLabel, BorderLayout.WEST);
    leftValsPanel.add(minVals, BorderLayout.CENTER);
    rightValsPanel.add(maxValsLabel, BorderLayout.WEST);
    rightValsPanel.add(maxVals, BorderLayout.CENTER);
    minMaxPanel.add(leftValsPanel);
    minMaxPanel.add(rightValsPanel);

    // fill the old histograms in the property dialog

    boolean found = false;
    for (int i = 0; i < handler.getNumModels(); i++) {

        if (handler.getModel(i) instanceof Histogram) {

            seriesOptions.addItem(handler.getGraphName(i));
            found = true;
        }

    }

    existing.setEnabled(found);
    seriesOptions.setEnabled(false);

    // the bottom panel
    JPanel options = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    JButton ok = new JButton("Plot");
    JButton cancel = new JButton("Cancel");

    // bind keyboard keys to plot and cancel buttons to improve usability

    ok.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "ok");
    ok.getActionMap().put("ok", new AbstractAction() {

        private static final long serialVersionUID = -7324877661936685228L;

        @Override
        public void actionPerformed(ActionEvent e) {

            ok.doClick();

        }
    });

    cancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            "ok");
    cancel.getActionMap().put("ok", new AbstractAction() {

        private static final long serialVersionUID = 2642213543774356676L;

        @Override
        public void actionPerformed(ActionEvent e) {

            cancel.doClick();

        }
    });

    //Action listener for the new series radio button
    newSeries.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            if (newSeries.isSelected()) {

                existing.setSelected(false);
                seriesOptions.setEnabled(false);
                buckets.setEnabled(true);
                buckets.setToolTipText("Select the number of buckets for this Histogram");
                minVals.setEnabled(true);
                maxVals.setEnabled(true);
            }
        }
    });

    //Action listener for the existing series radio button
    existing.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (existing.isSelected()) {

                newSeries.setSelected(false);
                seriesOptions.setEnabled(true);
                buckets.setEnabled(false);
                minVals.setEnabled(false);
                maxVals.setEnabled(false);
                buckets.setToolTipText("Number of buckets can't be changed on an existing Histogram");
            }

        }
    });

    //Action listener for the plot button
    ok.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            dialog.dispose();

            if (newSeries.isSelected()) {

                hist = new Histogram();
                hist.setNumOfBuckets((int) buckets.getValue());
                hist.setIsNew(true);

            } else if (existing.isSelected()) {

                String HistName = (String) seriesOptions.getSelectedItem();
                hist = (Histogram) handler.getModel(HistName);
                hist.setIsNew(false);

            }

            key = hist.addSeries(seriesName.getText());

            if (minVals.isEnabled() && maxVals.isEnabled()) {

                hist.setMinProb((double) minVals.getValue());
                hist.setMaxProb((double) maxVals.getValue());

            }
        }
    });

    //Action listener for the cancel button
    cancel.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
            hist = null;
        }
    });

    dialog.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosed(WindowEvent e) {

            hist = null;
        }
    });

    p1.add(buckets, BorderLayout.CENTER);

    p2.add(seriesName, BorderLayout.CENTER);

    options.add(ok);
    options.add(cancel);

    // add everything to the main panel of the dialog
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    mainPanel.add(seriesSelectPanel);
    mainPanel.add(p1);
    mainPanel.add(p2);
    mainPanel.add(minMaxPanel);

    // add main panel to the dialog
    dialog.add(mainPanel, BorderLayout.CENTER);
    dialog.add(options, BorderLayout.SOUTH);

    // set dialog properties
    dialog.setSize(320, 290);
    dialog.setLocationRelativeTo(GUIPrism.getGUI());
    dialog.setVisible(true);

    // return the user selected Histogram with the properties set
    return new Pair<Histogram, SeriesKey>(hist, key);
}