Search in sources :

Example 6 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class SemDataParamsEditor method setup.

public void setup() {
    // set up text and ties them to the parameters object being edited.
    IntTextField sampleSizeField = new IntTextField(getParams().getInt("sampleSize", 1000), 8);
    sampleSizeField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                getParams().set("sampleSize", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    IntTextField numDataSetsField = new IntTextField(getParams().getInt("numDataSets", 1), 8);
    numDataSetsField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                getParams().set("numDataSets", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    // JCheckBox latentDataSaved = new JCheckBox("Include Latent Variables",
    // Preferences.userRoot().getBoolean("latentDataSaved", getParameters().isIncludeLatents()));
    JCheckBox latentDataSaved = new JCheckBox("Include Latent Variables", getParams().getBoolean("latentDataSaved", false));
    latentDataSaved.setHorizontalTextPosition(SwingConstants.LEFT);
    latentDataSaved.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JCheckBox b = (JCheckBox) e.getSource();
            getParams().set("includeLatents", b.isSelected());
        }
    });
    // JCheckBox positiveOnlyBox = new JCheckBox("Positive Data Only",
    // getParameters().isPositiveDataOnly());
    // positiveOnlyBox.setHorizontalTextPosition(SwingConstants.LEFT);
    // positiveOnlyBox.addActionListener(new ActionListener(){
    // public void actionPerformed(ActionEvent e) {
    // JCheckBox b = (JCheckBox)e.getSource();
    // getParameters().setPositiveDataOnly(b.isSelected());
    // }
    // });
    setLayout(new BorderLayout());
    // continue workbench construction.
    Box b = Box.createVerticalBox();
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Sample size:  "));
    b1.add(Box.createHorizontalGlue());
    b1.add(sampleSizeField);
    b.add(b1);
    Box b1a = Box.createHorizontalBox();
    b1a.add(new JLabel("Num data sets:  "));
    b1a.add(Box.createHorizontalGlue());
    b1a.add(numDataSetsField);
    b.add(b1a);
    Box b2 = Box.createHorizontalBox();
    b2.add(latentDataSaved);
    b.add(b2);
    // Box b3 = Box.createHorizontalBox();
    // b3.add(positiveOnlyBox);
    // b.add(b3);
    add(b, BorderLayout.CENTER);
    setBorder(new EmptyBorder(5, 5, 5, 5));
}
Also used : ActionListener(java.awt.event.ActionListener) IntTextField(edu.cmu.tetradapp.util.IntTextField) ActionEvent(java.awt.event.ActionEvent) EmptyBorder(javax.swing.border.EmptyBorder)

Example 7 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class DataLoaderSettings method basicSettings.

// Step 1 items
public final Box basicSettings() {
    // Data loading params layout
    Box basicSettingsBox = Box.createVerticalBox();
    // File type: Tabular/covariance
    Box fileTypeBox = Box.createHorizontalBox();
    tabularRadioButton = new JRadioButton("Tabular data");
    covarianceRadioButton = new JRadioButton("Covariance data (lower triangle)");
    // We need to group the radio buttons, otherwise all can be selected
    ButtonGroup fileTypeBtnGrp = new ButtonGroup();
    fileTypeBtnGrp.add(tabularRadioButton);
    fileTypeBtnGrp.add(covarianceRadioButton);
    // Tabular data is selected by default
    tabularRadioButton.setSelected(true);
    // Event listener
    tabularRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JRadioButton button = (JRadioButton) actionEvent.getSource();
            // Just enable disabled buttons, do not change the previous selections - Zhou
            if (button.isSelected()) {
                // Enable the discrete/mixed radio button if it's disabled by clicking covariance data
                if (!discRadioButton.isEnabled()) {
                    discRadioButton.setEnabled(true);
                }
                if (!mixedRadioButton.isEnabled()) {
                    mixedRadioButton.setEnabled(true);
                }
                // Enable variable names in first row
                if (!firstRowVarNamesYesRadioButton.isEnabled()) {
                    firstRowVarNamesYesRadioButton.setEnabled(true);
                }
                if (!firstRowVarNamesNoRadioButton.isEnabled()) {
                    firstRowVarNamesNoRadioButton.setEnabled(true);
                }
                // Enable case Id options
                if (!idUnlabeledFirstColRadioButton.isEnabled()) {
                    idUnlabeledFirstColRadioButton.setEnabled(true);
                }
                if (!idLabeledColRadioButton.isEnabled()) {
                    idLabeledColRadioButton.setEnabled(true);
                }
                if (!idStringField.isEnabled()) {
                    idStringField.setEnabled(true);
                }
            }
        }
    });
    // Event listener
    covarianceRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JRadioButton button = (JRadioButton) actionEvent.getSource();
            if (button.isSelected()) {
                // When Covariance data is selected, data type can only be Continuous,
                contRadioButton.setSelected(true);
                // will disallow the users to choose Discrete and mixed data
                discRadioButton.setEnabled(false);
                mixedRadioButton.setEnabled(false);
                // Both Yes and No of Variable names in first row need to be disabled
                // Because the first row should be number of cases
                firstRowVarNamesYesRadioButton.setEnabled(false);
                firstRowVarNamesNoRadioButton.setEnabled(false);
                // select None for Case IDs, disable other options,
                // since no Case ID should be in covariance data
                idNoneRadioButton.setSelected(true);
                idUnlabeledFirstColRadioButton.setEnabled(false);
                idLabeledColRadioButton.setEnabled(false);
                idStringField.setEnabled(false);
            }
        }
    });
    // Add label into this label box to size
    Box fileTypeLabelBox = Box.createHorizontalBox();
    fileTypeLabelBox.setPreferredSize(labelSize);
    fileTypeLabelBox.add(new JLabel("File type:"));
    // Option 1
    Box fileTypeOption1Box = Box.createHorizontalBox();
    fileTypeOption1Box.setPreferredSize(new Dimension(160, 30));
    fileTypeOption1Box.add(tabularRadioButton);
    // Option 2
    Box fileTypeOption2Box = Box.createHorizontalBox();
    // Make this longer since the text is long - Zhou
    fileTypeOption2Box.setPreferredSize(new Dimension(300, 30));
    fileTypeOption2Box.add(covarianceRadioButton);
    // Add to file type box
    fileTypeBox.add(fileTypeLabelBox);
    fileTypeBox.add(Box.createRigidArea(new Dimension(10, 1)));
    fileTypeBox.add(fileTypeOption1Box);
    fileTypeBox.add(fileTypeOption2Box);
    fileTypeBox.add(Box.createHorizontalGlue());
    // Add to format container
    basicSettingsBox.add(fileTypeBox);
    // Add seperator line
    JSeparator separator1 = new JSeparator(SwingConstants.HORIZONTAL);
    separator1.setForeground(separatorColor);
    basicSettingsBox.add(separator1);
    // Vertical gap
    // basicSettingsBox.add(Box.createVerticalStrut(5));
    // Data type - moved from the old Fast tab - Zhou
    Box dataTypeBox = Box.createHorizontalBox();
    // Data type: continuous, discrete, or mixed
    contRadioButton = new JRadioButton("Continuous");
    discRadioButton = new JRadioButton("Discrete");
    mixedRadioButton = new JRadioButton("Mixed");
    ButtonGroup dataTypeBtnGrp = new ButtonGroup();
    dataTypeBtnGrp.add(contRadioButton);
    dataTypeBtnGrp.add(discRadioButton);
    dataTypeBtnGrp.add(mixedRadioButton);
    // Continuous radion button is selected by default
    contRadioButton.setSelected(true);
    // Add label into this label box to size
    Box dataTypeLabelBox = Box.createHorizontalBox();
    dataTypeLabelBox.setPreferredSize(labelSize);
    dataTypeLabelBox.add(new JLabel("Data type:"));
    // Option 1
    Box dataTypeOption1Box = Box.createHorizontalBox();
    dataTypeOption1Box.setPreferredSize(new Dimension(160, 30));
    dataTypeOption1Box.add(contRadioButton);
    // Option 2
    Box dataTypeOption2Box = Box.createHorizontalBox();
    dataTypeOption2Box.setPreferredSize(new Dimension(160, 30));
    dataTypeOption2Box.add(discRadioButton);
    // Option 3
    Box dataTypeOption3Box = Box.createHorizontalBox();
    dataTypeOption3Box.setPreferredSize(new Dimension(320, 30));
    dataTypeOption3Box.add(mixedRadioButton);
    // Threshold label
    JLabel maxDiscCatLabel = new JLabel(", max discrete categories: ");
    // Add info icon next to label to show tooltip on mouseover
    JLabel maxDiscCatLabelInfoIcon = new JLabel(new ImageIcon(ImageUtils.getImage(this, "information_small_white.png")));
    // Add tooltip on mouseover the info icon
    maxDiscCatLabelInfoIcon.setToolTipText("Integral columns with up to N (specify here) distinct values are discrete.");
    // Max number of discrete categories
    maxNumOfDiscCategoriesField = new IntTextField(0, 3);
    // 0 by default
    maxNumOfDiscCategoriesField.setValue(0);
    maxNumOfDiscCategoriesField.setFilter(new IntTextField.Filter() {

        @Override
        public int filter(int value, int oldValue) {
            if (value >= 0) {
                return value;
            } else {
                return oldValue;
            }
        }
    });
    // Event listener
    maxNumOfDiscCategoriesField.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            // Select the "Mixed" radio button when users click the input field
            if (!mixedRadioButton.isSelected()) {
                mixedRadioButton.setSelected(true);
            }
        }
    });
    dataTypeOption3Box.add(maxDiscCatLabel);
    dataTypeOption3Box.add(maxDiscCatLabelInfoIcon);
    dataTypeOption3Box.add(maxNumOfDiscCategoriesField);
    dataTypeBox.add(dataTypeLabelBox);
    dataTypeBox.add(Box.createRigidArea(new Dimension(10, 1)));
    dataTypeBox.add(dataTypeOption1Box);
    dataTypeBox.add(dataTypeOption2Box);
    dataTypeBox.add(dataTypeOption3Box);
    dataTypeBox.add(Box.createHorizontalGlue());
    basicSettingsBox.add(dataTypeBox);
    // Add seperator line
    JSeparator separator2 = new JSeparator(SwingConstants.HORIZONTAL);
    separator2.setForeground(separatorColor);
    basicSettingsBox.add(separator2);
    // basicSettingsBox.add(Box.createVerticalStrut(5));
    // Value Delimiter box
    Box valueDelimiterBox = Box.createHorizontalBox();
    // Value Delimiter
    whitespaceDelimiterRadioButton = new JRadioButton("Whitespace");
    singleCharDelimiterRadioButton = new JRadioButton("Single character: ");
    // Dropdown options for commo box
    String[] singleCharDelimiterOptions = { "Comma", "Colon", "Tab", "Space", "Semicolon", "Pipe" };
    // Create the combo box
    singleCharDelimiterComboBox = new JComboBox(singleCharDelimiterOptions);
    singleCharDelimiterComboBox.setMaximumSize(new Dimension(90, 30));
    // select first item by default, index starts at 0
    singleCharDelimiterComboBox.setSelectedIndex(0);
    ButtonGroup delimiterBtnGrp = new ButtonGroup();
    delimiterBtnGrp.add(whitespaceDelimiterRadioButton);
    delimiterBtnGrp.add(singleCharDelimiterRadioButton);
    // Defaults to whitespace if the inferred delimiter is not in Combo box
    // We can't infer whitespcace.
    // Otherwise, select the inferred delimiter from ComboBox and
    // check the singleCharDelimiterRadioButton
    // Only infer delimiter based on the first file - Zhou
    char inferredDelimiter = getInferredDelimiter(files.get(0));
    switch(inferredDelimiter) {
        case ',':
            singleCharDelimiterRadioButton.setSelected(true);
            singleCharDelimiterComboBox.setSelectedItem("Comma");
            System.out.println("Inferred delimiter: Comma");
            break;
        case '\t':
            singleCharDelimiterRadioButton.setSelected(true);
            singleCharDelimiterComboBox.setSelectedItem("Tab");
            System.out.println("Inferred delimiter: Tab");
            break;
        case ' ':
            // Whitespace covers space, so we use whitespace instead of space here
            whitespaceDelimiterRadioButton.setSelected(true);
            System.out.println("Inferred delimiter defaults: Whitespace");
            break;
        case ':':
            singleCharDelimiterRadioButton.setSelected(true);
            singleCharDelimiterComboBox.setSelectedItem("Colon");
            System.out.println("Inferred delimiter: Colon");
            break;
        case ';':
            singleCharDelimiterRadioButton.setSelected(true);
            singleCharDelimiterComboBox.setSelectedItem("Semicolon");
            System.out.println("Inferred delimiter: Semicolon");
            break;
        case '|':
            singleCharDelimiterRadioButton.setSelected(true);
            singleCharDelimiterComboBox.setSelectedItem("Pipe");
            System.out.println("Inferred delimiter: Pipe");
            break;
        default:
            // Just use whitespace as default if can't infer
            whitespaceDelimiterRadioButton.setSelected(true);
            System.out.println("Inferred delimiter defaults: Whitespace");
            break;
    }
    // Event listener
    // ComboBox is actually a container
    singleCharDelimiterComboBox.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            // Select the "Single character:" radio button when users click the combo box
            if (!singleCharDelimiterRadioButton.isSelected()) {
                singleCharDelimiterRadioButton.setSelected(true);
            }
        }
    });
    // Add label into this label box to size
    Box valueDelimiterLabelBox = Box.createHorizontalBox();
    valueDelimiterLabelBox.setPreferredSize(labelSize);
    valueDelimiterLabelBox.add(new JLabel("Value delimiter:"));
    // Add info icon next to label to show tooltip on mouseover
    JLabel valueDelimiterLabelInfoIcon = new JLabel(new ImageIcon(ImageUtils.getImage(this, "information_small_white.png")));
    // Add tooltip on mouseover the info icon
    valueDelimiterLabelInfoIcon.setToolTipText("Delimiter used to seperate the variable names and data values");
    valueDelimiterLabelBox.add(valueDelimiterLabelInfoIcon);
    // Option 1
    Box valueDelimiterOption1Box = Box.createHorizontalBox();
    valueDelimiterOption1Box.setPreferredSize(new Dimension(160, 30));
    valueDelimiterOption1Box.add(whitespaceDelimiterRadioButton);
    // Option 2
    Box valueDelimiterOption2Box = Box.createHorizontalBox();
    // Make this box wider so we can see the combo box - Zhou
    valueDelimiterOption2Box.setPreferredSize(new Dimension(300, 30));
    valueDelimiterOption2Box.add(singleCharDelimiterRadioButton);
    valueDelimiterOption2Box.add(singleCharDelimiterComboBox);
    valueDelimiterBox.add(valueDelimiterLabelBox);
    valueDelimiterBox.add(Box.createRigidArea(new Dimension(10, 1)));
    valueDelimiterBox.add(valueDelimiterOption1Box);
    valueDelimiterBox.add(valueDelimiterOption2Box);
    valueDelimiterBox.add(Box.createHorizontalGlue());
    basicSettingsBox.add(valueDelimiterBox);
    // Add seperator line
    JSeparator separator3 = new JSeparator(SwingConstants.HORIZONTAL);
    separator3.setForeground(separatorColor);
    basicSettingsBox.add(separator3);
    // basicSettingsBox.add(Box.createVerticalStrut(5));
    // Var names in first row of data
    Box firstRowVarNamesBox = Box.createHorizontalBox();
    // Yes/No buttons
    firstRowVarNamesYesRadioButton = new JRadioButton("Yes");
    firstRowVarNamesNoRadioButton = new JRadioButton("No");
    // Button group
    ButtonGroup firstRowVarNamesBtnGrp = new ButtonGroup();
    firstRowVarNamesBtnGrp.add(firstRowVarNamesYesRadioButton);
    firstRowVarNamesBtnGrp.add(firstRowVarNamesNoRadioButton);
    // Make Yes button selected by default
    firstRowVarNamesYesRadioButton.setSelected(true);
    // Event listener
    firstRowVarNamesYesRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JRadioButton button = (JRadioButton) actionEvent.getSource();
            if (button.isSelected()) {
                // Enable specifying column labeled option
                if (!idLabeledColRadioButton.isEnabled()) {
                    idLabeledColRadioButton.setEnabled(true);
                }
                if (!idStringField.isEnabled()) {
                    idStringField.setEnabled(true);
                }
            }
        }
    });
    // Event listener
    firstRowVarNamesNoRadioButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            JRadioButton button = (JRadioButton) actionEvent.getSource();
            if (button.isSelected()) {
                // Disable the "Column labeled" option of ignoring case id column
                idLabeledColRadioButton.setEnabled(false);
                idStringField.setEnabled(false);
            }
        }
    });
    // Add label into this label box to size
    Box firstRowVarNamesLabelBox = Box.createHorizontalBox();
    firstRowVarNamesLabelBox.setPreferredSize(labelSize);
    firstRowVarNamesLabelBox.add(new JLabel("First row variable names:"));
    // Add info icon next to label to show tooltip on mouseover
    JLabel firstRowVarNamesLabelInfoIcon = new JLabel(new ImageIcon(ImageUtils.getImage(this, "information_small_white.png")));
    // Add tooltip on mouseover the info icon
    firstRowVarNamesLabelInfoIcon.setToolTipText("Whether the column variable names are presented in the first row of data");
    firstRowVarNamesLabelBox.add(firstRowVarNamesLabelInfoIcon);
    // Option 1
    Box firstRowVarNamesOption1Box = Box.createHorizontalBox();
    firstRowVarNamesOption1Box.setPreferredSize(new Dimension(160, 30));
    firstRowVarNamesOption1Box.add(firstRowVarNamesYesRadioButton);
    // Option 2
    Box firstRowVarNamesOption2Box = Box.createHorizontalBox();
    firstRowVarNamesOption2Box.setPreferredSize(new Dimension(160, 30));
    firstRowVarNamesOption2Box.add(firstRowVarNamesNoRadioButton);
    // Add to firstRowVarNamesBox
    firstRowVarNamesBox.add(firstRowVarNamesLabelBox);
    firstRowVarNamesBox.add(Box.createRigidArea(new Dimension(10, 1)));
    firstRowVarNamesBox.add(firstRowVarNamesOption1Box);
    firstRowVarNamesBox.add(firstRowVarNamesOption2Box);
    firstRowVarNamesBox.add(Box.createHorizontalGlue());
    basicSettingsBox.add(firstRowVarNamesBox);
    // Use a titled border with 5 px inside padding - Zhou
    String borderTitle = "Basic Settings (apply to all files)";
    basicSettingsBox.setBorder(new CompoundBorder(BorderFactory.createTitledBorder(borderTitle), new EmptyBorder(5, 5, 5, 5)));
    return basicSettingsBox;
}
Also used : MouseEvent(java.awt.event.MouseEvent) ActionEvent(java.awt.event.ActionEvent) MouseAdapter(java.awt.event.MouseAdapter) ActionListener(java.awt.event.ActionListener) IntTextField(edu.cmu.tetradapp.util.IntTextField) CompoundBorder(javax.swing.border.CompoundBorder) EmptyBorder(javax.swing.border.EmptyBorder)

Example 8 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class AlgorithmParameterPanel method getIntTextField.

private IntTextField getIntTextField(final String parameter, final Parameters parameters, final int defaultValue, final double lowerBound, final double upperBound) {
    final IntTextField field = new IntTextField(parameters.getInt(parameter, defaultValue), 8);
    field.setFilter((value, oldValue) -> {
        if (value == field.getValue()) {
            return oldValue;
        }
        if (value < lowerBound) {
            return oldValue;
        }
        if (value > upperBound) {
            return oldValue;
        }
        try {
            parameters.set(parameter, value);
        } catch (Exception e) {
        // Ignore.
        }
        return value;
    });
    return field;
}
Also used : IntTextField(edu.cmu.tetradapp.util.IntTextField)

Example 9 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class FciSearchParamEditor method setup.

public void setup() {
    /*
      The variable names from the object being searched over (usually data).
     */
    List varNames = (List<String>) params.get("varNames", null);
    DataModel dataModel1 = null;
    Graph graph = null;
    for (Object parentModel1 : parentModels) {
        if (parentModel1 instanceof DataWrapper) {
            DataWrapper dataWrapper = (DataWrapper) parentModel1;
            dataModel1 = dataWrapper.getSelectedDataModel();
        }
        if (parentModel1 instanceof GraphWrapper) {
            GraphWrapper graphWrapper = (GraphWrapper) parentModel1;
            graph = graphWrapper.getGraph();
        }
        if (parentModel1 instanceof DagWrapper) {
            DagWrapper dagWrapper = (DagWrapper) parentModel1;
            graph = dagWrapper.getDag();
        }
        if (parentModel1 instanceof SemGraphWrapper) {
            SemGraphWrapper semGraphWrapper = (SemGraphWrapper) parentModel1;
            graph = semGraphWrapper.getGraph();
        }
    }
    if (dataModel1 != null) {
        varNames = new ArrayList(dataModel1.getVariableNames());
    } else if (graph != null) {
        Iterator<Node> it = graph.getNodes().iterator();
        varNames = new ArrayList();
        Node temp;
        while (it.hasNext()) {
            temp = it.next();
            if (temp.getNodeType() == NodeType.MEASURED) {
                varNames.add(temp.getName());
            }
        }
    } else {
        throw new NullPointerException("Null model (no graph or data model " + "passed to the search).");
    }
    params.set("varNames", varNames);
    IntTextField depthField = new IntTextField(params.getInt("depth", -1), 4);
    depthField.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                params.set("depth", value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    double alpha = params.getDouble("alpha", 0.001);
    if (!Double.isNaN(alpha)) {
        alphaField = new DoubleTextField(alpha, 4, NumberFormatUtil.getInstance().getNumberFormat());
        alphaField.setFilter(new DoubleTextField.Filter() {

            public double filter(double value, double oldValue) {
                try {
                    params.set("alpha", 0.001);
                    Preferences.userRoot().putDouble("alpha", value);
                    return value;
                } catch (Exception e) {
                    return oldValue;
                }
            }
        });
    }
    setBorder(new MatteBorder(10, 10, 10, 10, super.getBackground()));
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    Box b1 = Box.createHorizontalBox();
    b1.add(new JLabel("Knowledge:"));
    b1.add(Box.createGlue());
    add(b1);
    add(Box.createVerticalStrut(10));
    if (!Double.isNaN(alpha)) {
        Box b2 = Box.createHorizontalBox();
        b2.add(new JLabel("Alpha Value:"));
        b2.add(Box.createGlue());
        b2.add(alphaField);
        add(b2);
        add(Box.createVerticalStrut(10));
    }
    Box b3 = Box.createHorizontalBox();
    b3.add(new JLabel("Search Depth:"));
    b3.add(Box.createGlue());
    b3.add(depthField);
    add(b3);
    add(Box.createVerticalStrut(10));
}
Also used : DoubleTextField(edu.cmu.tetradapp.util.DoubleTextField) Node(edu.cmu.tetrad.graph.Node) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper) GraphWrapper(edu.cmu.tetradapp.model.GraphWrapper) ArrayList(java.util.ArrayList) DataWrapper(edu.cmu.tetradapp.model.DataWrapper) DagWrapper(edu.cmu.tetradapp.model.DagWrapper) MatteBorder(javax.swing.border.MatteBorder) Graph(edu.cmu.tetrad.graph.Graph) DataModel(edu.cmu.tetrad.data.DataModel) IntTextField(edu.cmu.tetradapp.util.IntTextField) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SemGraphWrapper(edu.cmu.tetradapp.model.SemGraphWrapper)

Example 10 with IntTextField

use of edu.cmu.tetradapp.util.IntTextField in project tetrad by cmu-phil.

the class IndependenceFactsAction method actionPerformed.

// ========================PUBLIC METHODS==========================//
/**
 * Performs the action of opening a session from a file.
 */
public void actionPerformed(ActionEvent e) {
    this.independenceTest = getIndTestProducer().getIndependenceTest();
    final List<String> varNames = new ArrayList<>();
    varNames.add("VAR");
    varNames.addAll(getDataVars());
    varNames.add("?");
    varNames.add("+");
    final JComboBox variableBox = new JComboBox();
    DefaultComboBoxModel aModel1 = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
    aModel1.setSelectedItem("VAR");
    variableBox.setModel(aModel1);
    // variableBox.addMouseListener(new MouseAdapter() {
    // public void mouseClicked(MouseEvent e) {
    // System.out.println(e);
    // }
    // });
    variableBox.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            JComboBox box = (JComboBox) e.getSource();
            String var = (String) box.getSelectedItem();
            LinkedList<String> vars = getVars();
            int size = vars.size();
            if ("VAR".equals(var)) {
                return;
            }
            for (int i = 2; i < getVars().size() - 1; i++) {
                if (wildcard(i)) {
                    if (!("?".equals(var) || "+".equals(var))) {
                        JOptionPane.showMessageDialog(centeringComp, "Please specify wildcards after other variables (e.g. X _||_ ? | Y, +)");
                        return;
                    }
                }
            }
            if ("?".equals(var)) {
                if (size >= 0 && !vars.contains("+")) {
                    vars.addLast(var);
                }
            } else if ("+".equals(var)) {
                if (size >= 2) {
                    vars.addLast(var);
                }
            } else if ((vars.indexOf("?") < 2) && !(vars.contains("+")) && !(vars.contains(var))) {
                vars.add(var);
            }
            if (wildcard(0) && vars.size() >= 2 && !wildcard(1)) {
                JOptionPane.showMessageDialog(centeringComp, "Please specify wildcards after other variables (e.g. X _||_ ? | Y, +)");
                return;
            }
            resetText();
            // This is a workaround to an introduced bug in the JDK whereby
            // repeated selections of the same item send out just one
            // action event.
            DefaultComboBoxModel aModel = new DefaultComboBoxModel(varNames.toArray(new String[varNames.size()]));
            aModel.setSelectedItem("VAR");
            variableBox.setModel(aModel);
        }
    });
    final JButton delete = new JButton("Delete");
    delete.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (!getVars().isEmpty()) {
                getVars().removeLast();
                resetText();
            }
        }
    });
    textField.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            if ('?' == e.getKeyChar()) {
                variableBox.setSelectedItem("?");
            } else if ('+' == e.getKeyChar()) {
                variableBox.setSelectedItem("+");
            } else if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
            e.consume();
        }
    });
    delete.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            if ('?' == e.getKeyChar()) {
                variableBox.setSelectedItem("?");
            } else if ('+' == e.getKeyChar()) {
                variableBox.setSelectedItem("+");
            } else if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
        }
    });
    variableBox.addKeyListener(new KeyAdapter() {

        public void keyTyped(KeyEvent e) {
            super.keyTyped(e);
            if ('\b' == e.getKeyChar()) {
                vars.removeLast();
                resetText();
            }
        }
    });
    JButton list = new JButton("LIST");
    list.setFont(new Font("Dialog", Font.BOLD, 14));
    list.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            generateResults();
        }
    });
    Box b1 = Box.createVerticalBox();
    Box b2 = Box.createHorizontalBox();
    b2.add(new JLabel("Test: "));
    b2.add(new JLabel(getIndependenceTest().toString()));
    b2.add(Box.createHorizontalGlue());
    b1.add(b2);
    b1.add(Box.createVerticalStrut(10));
    Box b3 = Box.createHorizontalBox();
    b3.add(getTextField());
    b3.add(variableBox);
    b3.add(delete);
    b1.add(b3);
    b1.add(Box.createVerticalStrut(10));
    tableModel = new AbstractTableModel() {

        public String getColumnName(int column) {
            if (column == 0) {
                return "Index";
            }
            if (column == 1) {
                if (independenceTest instanceof IndTestDSep) {
                    return "D-Separation Relation";
                } else {
                    return "Independence Relation";
                }
            } else if (column == 2) {
                return "Judgment";
            } else if (column == 3) {
                return "P Value";
            }
            return null;
        }

        public int getColumnCount() {
            if (usesDSeparation()) {
                return 3;
            } else {
                return 4;
            }
        }

        public int getRowCount() {
            return getResults().size();
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            Result result = getResults().get(rowIndex);
            if (columnIndex == 0) {
                return result.getIndex() + 1;
            }
            if (columnIndex == 1) {
                return result.getFact();
            } else if (columnIndex == 2) {
                if (independenceTest instanceof IndTestDSep) {
                    if (result.getType() == Result.Type.INDEPENDENT) {
                        return "D-Separated";
                    } else if (result.getType() == Result.Type.DEPENDENT) {
                        return "D-Connected";
                    } else if (result.getType() == Result.Type.UNDETERMINED) {
                        return "*";
                    }
                // return result.getType() ? "D-Separated" : "D-Connected";
                } else {
                    if (result.getType() == Result.Type.INDEPENDENT) {
                        return "Independent";
                    } else if (result.getType() == Result.Type.DEPENDENT) {
                        return "Dependent";
                    } else if (result.getType() == Result.Type.UNDETERMINED) {
                        return "*";
                    }
                // return result.getType() ? "Independent" : "Dependent";
                }
            } else if (columnIndex == 3) {
                return nf.format(result.getpValue());
            }
            return null;
        }

        public Class getColumnClass(int columnIndex) {
            if (columnIndex == 0) {
                return Number.class;
            }
            if (columnIndex == 1) {
                return String.class;
            } else if (columnIndex == 2) {
                return Number.class;
            } else if (columnIndex == 3) {
                return Number.class;
            }
            return null;
        }
    };
    JTable table = new JTable(tableModel);
    table.getColumnModel().getColumn(0).setMinWidth(40);
    table.getColumnModel().getColumn(0).setMaxWidth(40);
    table.getColumnModel().getColumn(1).setMinWidth(200);
    table.getColumnModel().getColumn(2).setMinWidth(100);
    table.getColumnModel().getColumn(2).setMaxWidth(100);
    if (!(usesDSeparation())) {
        table.getColumnModel().getColumn(3).setMinWidth(80);
        table.getColumnModel().getColumn(3).setMaxWidth(80);
    }
    JTableHeader header = table.getTableHeader();
    header.addMouseListener(new MouseAdapter() {

        public void mouseClicked(MouseEvent e) {
            JTableHeader header = (JTableHeader) e.getSource();
            Point point = e.getPoint();
            int col = header.columnAtPoint(point);
            int sortCol = header.getTable().convertColumnIndexToModel(col);
            sortByColumn(sortCol, true);
        }
    });
    JScrollPane scroll = new JScrollPane(table);
    scroll.setPreferredSize(new Dimension(400, 400));
    b1.add(scroll);
    Box b4 = Box.createHorizontalBox();
    b4.add(new JLabel("Limit list to "));
    IntTextField field = new IntTextField(getListLimit(), 7);
    field.setFilter(new IntTextField.Filter() {

        public int filter(int value, int oldValue) {
            try {
                setListLimit(value);
                return value;
            } catch (Exception e) {
                return oldValue;
            }
        }
    });
    b4.add(field);
    b4.add(new JLabel(" items."));
    b4.add(Box.createHorizontalGlue());
    b4.add(list);
    b1.add(b4);
    b1.add(Box.createVerticalStrut(10));
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.add(b1, BorderLayout.CENTER);
    panel.setBorder(new EmptyBorder(10, 10, 10, 10));
    EditorWindow editorWindow = new EditorWindow(panel, "Independence Facts", "Save", false, centeringComp);
    DesktopController.getInstance().addEditorWindow(editorWindow, JLayeredPane.PALETTE_LAYER);
    editorWindow.setVisible(true);
    // Set the ok button so that pressing enter activates it.
    // jdramsey 5/5/02
    JRootPane root = SwingUtilities.getRootPane(editorWindow);
    if (root != null) {
        root.setDefaultButton(list);
    }
}
Also used : JTableHeader(javax.swing.table.JTableHeader) IndTestDSep(edu.cmu.tetrad.search.IndTestDSep) IntTextField(edu.cmu.tetradapp.util.IntTextField) EmptyBorder(javax.swing.border.EmptyBorder) AbstractTableModel(javax.swing.table.AbstractTableModel)

Aggregations

IntTextField (edu.cmu.tetradapp.util.IntTextField)20 ActionEvent (java.awt.event.ActionEvent)11 ActionListener (java.awt.event.ActionListener)11 DoubleTextField (edu.cmu.tetradapp.util.DoubleTextField)6 ArrayList (java.util.ArrayList)5 EmptyBorder (javax.swing.border.EmptyBorder)5 List (java.util.List)4 MatteBorder (javax.swing.border.MatteBorder)4 DataModel (edu.cmu.tetrad.data.DataModel)3 Graph (edu.cmu.tetrad.graph.Graph)3 Node (edu.cmu.tetrad.graph.Node)3 DagWrapper (edu.cmu.tetradapp.model.DagWrapper)3 DataWrapper (edu.cmu.tetradapp.model.DataWrapper)3 GraphWrapper (edu.cmu.tetradapp.model.GraphWrapper)3 SemGraphWrapper (edu.cmu.tetradapp.model.SemGraphWrapper)3 Iterator (java.util.Iterator)3 IndTestDSep (edu.cmu.tetrad.search.IndTestDSep)2 Parameters (edu.cmu.tetrad.util.Parameters)2 DecimalFormat (java.text.DecimalFormat)2 AbstractTableModel (javax.swing.table.AbstractTableModel)2