Search in sources :

Example 1 with Algorithm

use of edu.cmu.tetrad.annotation.Algorithm in project tetrad by cmu-phil.

the class AlgorithmParameterPanel method addToPanel.

public void addToPanel(GeneralAlgorithmRunner runner) {
    List<String> parametersToEdit = runner.getAlgorithm().getParameters();
    Parameters parameters = runner.getParameters();
    Algorithm algoAnno = runner.getAlgorithm().getClass().getAnnotation(Algorithm.class);
    String title = (algoAnno == null) ? DEFAULT_TITLE_BORDER : String.format("%s Parameters", algoAnno.name());
    setBorder(BorderFactory.createTitledBorder(title));
    removeAll();
    Box paramsBox = Box.createVerticalBox();
    // But we added bootstrap parameters anyway  - Zhou
    if (parametersToEdit.isEmpty()) {
        Box row = Box.createHorizontalBox();
        row.add(new JLabel("No parameters to edit"));
        paramsBox.add(row);
    } else {
        List<String> uniqueParams = parametersToEdit.stream().distinct().collect(Collectors.toList());
        uniqueParams.forEach(parameter -> {
            Object defaultValue = ParamDescriptions.getInstance().get(parameter).getDefaultValue();
            JComponent parameterSelection;
            if (defaultValue instanceof Double) {
                double lowerBoundDouble = ParamDescriptions.getInstance().get(parameter).getLowerBoundDouble();
                double upperBoundDouble = ParamDescriptions.getInstance().get(parameter).getUpperBoundDouble();
                parameterSelection = getDoubleField(parameter, parameters, (Double) defaultValue, lowerBoundDouble, upperBoundDouble);
            } else if (defaultValue instanceof Integer) {
                int lowerBoundInt = ParamDescriptions.getInstance().get(parameter).getLowerBoundInt();
                int upperBoundInt = ParamDescriptions.getInstance().get(parameter).getUpperBoundInt();
                parameterSelection = getIntTextField(parameter, parameters, (Integer) defaultValue, lowerBoundInt, upperBoundInt);
            } else if (defaultValue instanceof Boolean) {
                // Joe's old implementation with dropdown yes or no
                // parameterSelection = getBooleanBox(parameter, parameters, (Boolean) defaultValue);
                // Zhou's new implementation with yes/no radio buttons
                parameterSelection = getBooleanSelectionBox(parameter, parameters, (Boolean) defaultValue);
            } else if (defaultValue instanceof String) {
                parameterSelection = getStringField(parameter, parameters, (String) defaultValue);
            } else {
                throw new IllegalArgumentException("Unexpected type: " + defaultValue.getClass());
            }
            // Each parameter row contains parameter label and selection/input field
            Box paramRow = Box.createHorizontalBox();
            JLabel paramLabel = new JLabel(ParamDescriptions.getInstance().get(parameter).getDescription());
            paramRow.add(paramLabel);
            paramRow.add(Box.createHorizontalGlue());
            paramRow.add(parameterSelection);
            // Add each paramRow to paramsBox
            paramsBox.add(paramRow);
            // Also add some gap between rows
            paramsBox.add(Box.createVerticalStrut(10));
        });
    }
    add(new PaddingPanel(paramsBox), BorderLayout.CENTER);
}
Also used : Parameters(edu.cmu.tetrad.util.Parameters) JComponent(javax.swing.JComponent) JLabel(javax.swing.JLabel) Box(javax.swing.Box) JComboBox(javax.swing.JComboBox) PaddingPanel(edu.cmu.tetradapp.ui.PaddingPanel) Algorithm(edu.cmu.tetrad.annotation.Algorithm)

Aggregations

Algorithm (edu.cmu.tetrad.annotation.Algorithm)1 Parameters (edu.cmu.tetrad.util.Parameters)1 PaddingPanel (edu.cmu.tetradapp.ui.PaddingPanel)1 Box (javax.swing.Box)1 JComboBox (javax.swing.JComboBox)1 JComponent (javax.swing.JComponent)1 JLabel (javax.swing.JLabel)1