edu.cmu.tetradapp.util.ParameterComponents Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2019 University of Pittsburgh.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package edu.cmu.tetradapp.util;
import edu.cmu.tetrad.util.ParamDescription;
import edu.cmu.tetrad.util.ParamDescriptions;
import edu.cmu.tetrad.util.Parameters;
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* A utility for creating parameter components for GUI.
*
* May 24, 2019 11:37:33 AM
*
* @author Kevin V. Bui ([email protected])
* @version $Id: $Id
*/
public final class ParameterComponents {
private ParameterComponents() {
}
/**
*
toArray.
*
* @param parameterComponents a {@link java.util.Map} object
* @return an array of {@link javax.swing.Box} objects
*/
public static Box[] toArray(Map parameterComponents) {
ParamDescriptions paramDescs = ParamDescriptions.getInstance();
List boolComps = new LinkedList<>();
List otherComps = new LinkedList<>();
parameterComponents.forEach((k, v) -> {
if (paramDescs.get(k).getDefaultValue() instanceof Boolean) {
boolComps.add(v);
} else {
otherComps.add(v);
}
});
return Stream.concat(otherComps.stream(), boolComps.stream())
.toArray(Box[]::new);
}
/**
* createParameterComponents.
*
* @param params a {@link java.util.Set} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @return a {@link java.util.Map} object
*/
public static Map createParameterComponents(Set params, Parameters parameters) {
ParamDescriptions paramDescs = ParamDescriptions.getInstance();
return params.stream()
.collect(Collectors.toMap(
Function.identity(),
e -> ParameterComponents.createParameterComponent(e, parameters, paramDescs.get(e)),
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s.", u));
},
TreeMap::new));
}
/**
* getDoubleField.
*
* @param parameter a {@link java.lang.String} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @param defaultValue a double
* @param lowerBound a double
* @param upperBound a double
* @return a {@link edu.cmu.tetradapp.util.DoubleTextField} object
*/
public static DoubleTextField getDoubleField(String parameter, Parameters parameters,
double defaultValue, double lowerBound, double upperBound) {
DoubleTextField field = new DoubleTextField(parameters.getDouble(parameter, defaultValue),
8, new DecimalFormat("0.####"), new DecimalFormat("0.0#E0"), 0.001);
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;
}
/**
* getIntTextField.
*
* @param parameter a {@link java.lang.String} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @param defaultValue a int
* @param lowerBound a double
* @param upperBound a double
* @return a {@link edu.cmu.tetradapp.util.IntTextField} object
*/
public static IntTextField getIntTextField(String parameter, Parameters parameters,
int defaultValue, double lowerBound, double upperBound) {
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;
}
/**
* getLongTextField.
*
* @param parameter a {@link java.lang.String} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @param defaultValue a long
* @param lowerBound a double
* @param upperBound a double
* @return a {@link edu.cmu.tetradapp.util.LongTextField} object
*/
public static LongTextField getLongTextField(String parameter, Parameters parameters,
long defaultValue, double lowerBound, double upperBound) {
LongTextField field = new LongTextField(parameters.getLong(parameter, defaultValue), 10);
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;
}
/**
* getBooleanSelectionBox.
*
* @param parameter a {@link java.lang.String} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @param defaultValue a boolean
* @return a {@link javax.swing.Box} object
*/
public static Box getBooleanSelectionBox(String parameter, Parameters parameters, boolean defaultValue) {
Box selectionBox = Box.createHorizontalBox();
JRadioButton yesButton = new JRadioButton("Yes");
JRadioButton noButton = new JRadioButton("No");
// Button group to ensure only only one option can be selected
ButtonGroup selectionBtnGrp = new ButtonGroup();
selectionBtnGrp.add(yesButton);
selectionBtnGrp.add(noButton);
boolean aBoolean = parameters.getBoolean(parameter, defaultValue);
// Set default selection
if (aBoolean) {
yesButton.setSelected(true);
} else {
noButton.setSelected(true);
}
// Add to containing box
selectionBox.add(yesButton);
selectionBox.add(noButton);
// Event listener
yesButton.addActionListener((e) -> {
JRadioButton button = (JRadioButton) e.getSource();
if (button.isSelected()) {
parameters.set(parameter, true);
}
});
// Event listener
noButton.addActionListener((e) -> {
JRadioButton button = (JRadioButton) e.getSource();
if (button.isSelected()) {
parameters.set(parameter, false);
}
});
return selectionBox;
}
/**
* getStringField.
*
* @param parameter a {@link java.lang.String} object
* @param parameters a {@link edu.cmu.tetrad.util.Parameters} object
* @param defaultValue a {@link java.lang.String} object
* @return a {@link edu.cmu.tetradapp.util.StringTextField} object
*/
public static StringTextField getStringField(String parameter, Parameters parameters, String defaultValue) {
StringTextField field = new StringTextField(parameters.getString(parameter, defaultValue), 20);
field.setFilter((value, oldValue) -> {
if (value.equals(field.getValue().trim())) {
return oldValue;
}
try {
parameters.set(parameter, value);
} catch (Exception e) {
// Ignore.
}
return value;
});
return field;
}
private static Box createParameterComponent(String parameter, Parameters parameters, ParamDescription paramDesc) {
JComponent component;
Object defaultValue = paramDesc.getDefaultValue();
if (defaultValue instanceof Double) {
double lowerBoundDouble = paramDesc.getLowerBoundDouble();
double upperBoundDouble = paramDesc.getUpperBoundDouble();
component = ParameterComponents.getDoubleField(parameter, parameters, (Double) defaultValue, lowerBoundDouble, upperBoundDouble);
} else if (defaultValue instanceof Integer) {
int lowerBoundInt = paramDesc.getLowerBoundInt();
int upperBoundInt = paramDesc.getUpperBoundInt();
component = ParameterComponents.getIntTextField(parameter, parameters, (Integer) defaultValue, lowerBoundInt, upperBoundInt);
} else if (defaultValue instanceof Long) {
long lowerBoundInt = paramDesc.getLowerBoundLong();
long upperBoundInt = paramDesc.getUpperBoundLong();
component = ParameterComponents.getLongTextField(parameter, parameters, (Long) defaultValue, lowerBoundInt, upperBoundInt);
} else if (defaultValue instanceof Boolean) {
component = ParameterComponents.getBooleanSelectionBox(parameter, parameters, (Boolean) defaultValue);
} else if (defaultValue instanceof String) {
component = ParameterComponents.getStringField(parameter, parameters, (String) defaultValue);
} else {
throw new IllegalArgumentException("Unexpected type: " + defaultValue.getClass());
}
Box paramRow = Box.createHorizontalBox();
JLabel paramLabel = new JLabel(paramDesc.getShortDescription());
String longDescription = paramDesc.getLongDescription();
if (longDescription != null) {
paramLabel.setToolTipText(longDescription);
}
paramRow.add(paramLabel);
paramRow.add(Box.createHorizontalGlue());
paramRow.add(component);
return paramRow;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy