Search in sources :

Example 1 with ValueCheckListener

use of maspack.widgets.ValueCheckListener in project artisynth_core by artisynth.

the class MenuBarHandler method createStepDisplay.

private DoubleField createStepDisplay() {
    DoubleField display = new DoubleField("step:", 0, "%7.5f");
    display.setToolTipText("maximum step size");
    display.getTextField().setToolTipText("maximum step size");
    // display.setEnabledAll (false);
    display.setColumns(5);
    display.addValueChangeListener(this);
    display.addValueCheckListener(new ValueCheckListener() {

        public Object validateValue(ValueChangeEvent e, StringHolder errMsg) {
            double dval = ((Double) e.getValue()).doubleValue();
            if (dval <= 0) {
                return PropertyUtils.illegalValue("value must be positive", errMsg);
            } else {
                return PropertyUtils.validValue(e.getValue(), errMsg);
            }
        }
    });
    return display;
}
Also used : ValueCheckListener(maspack.widgets.ValueCheckListener) ValueChangeEvent(maspack.widgets.ValueChangeEvent) StringHolder(maspack.util.StringHolder) DoubleField(maspack.widgets.DoubleField)

Example 2 with ValueCheckListener

use of maspack.widgets.ValueCheckListener in project artisynth_core by artisynth.

the class ControlPanel method actionPerformed.

public void actionPerformed(ActionEvent e) {
    String actionCmd = e.getActionCommand();
    if (actionCmd.equals("add widget")) {
        myPanel.actionPerformed(e);
    } else if (actionCmd.equals("set name")) {
        WidgetDialog dialog = WidgetDialog.createDialog(myFrame, "set name", "Set");
        StringField widget = new StringField("name:", getName(), 20);
        widget.addValueCheckListener(new ValueCheckListener() {

            public Object validateValue(ValueChangeEvent e, StringHolder errMsg) {
                String name = (String) e.getValue();
                if (name != null && name.length() == 0) {
                    return null;
                }
                errMsg.value = ModelComponentBase.checkName(name, null);
                if (errMsg.value != null) {
                    return Property.IllegalValue;
                } else {
                    return name;
                }
            }
        });
        dialog.addWidget(widget);
        GuiUtils.locateVertically(dialog, myFrame, GuiUtils.BELOW);
        GuiUtils.locateHorizontally(dialog, myFrame, GuiUtils.CENTER);
        dialog.setVisible(true);
        if (dialog.getReturnValue() == OptionPanel.OK_OPTION) {
            String name = (String) widget.getValue();
            setName(name);
        }
    } else if (actionCmd.equals("save as ...")) {
        Main main = Main.getMain();
        RootModel root = main.getRootModel();
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(main.getModelDirectory());
        int retVal = chooser.showSaveDialog(myFrame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File file = chooser.getSelectedFile();
            try {
                ComponentUtils.saveComponent(file, this, new NumberFormat("%.6g"), root);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(myFrame, "Error saving file: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
            main.setModelDirectory(chooser.getCurrentDirectory());
        }
    } else if (actionCmd.equals("merge panel")) {
        Main main = Main.getMain();
        main.getRootModel().mergeControlPanel(true, this);
    } else if (actionCmd.equals("separate panel")) {
        Main main = Main.getMain();
        main.getRootModel().mergeControlPanel(false, this);
    } else {
        throw new InternalErrorException("Unknown action: " + actionCmd);
    }
}
Also used : WidgetDialog(maspack.widgets.WidgetDialog) RootModel(artisynth.core.workspace.RootModel) Point(java.awt.Point) IOException(java.io.IOException) ValueCheckListener(maspack.widgets.ValueCheckListener) ValueChangeEvent(maspack.widgets.ValueChangeEvent) JFileChooser(javax.swing.JFileChooser) StringField(maspack.widgets.StringField) Main(artisynth.core.driver.Main) File(java.io.File)

Example 3 with ValueCheckListener

use of maspack.widgets.ValueCheckListener in project artisynth_core by artisynth.

the class PropertyWidgetDialog method initialize.

private void initialize() {
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    Container pane = getContentPane();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    LabeledComponentPanel propPanel = new LabeledComponentPanel();
    propPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    // myChooser = new PropertyChooser ("component/property", 30, myMain);
    // myChooser.setAlignmentX (Component.CENTER_ALIGNMENT);
    // myChooser.addValueChangeListener (this);
    // pane.add (Box.createRigidArea (new Dimension (0, 4)));
    // propPanel.addWidget (myChooser);
    myPropField = new PropertyField("component/property", 30, myMain);
    myPropField.setAlignmentX(Component.CENTER_ALIGNMENT);
    myPropField.addValueChangeListener(this);
    propPanel.addWidget(myPropField);
    propPanel.addWidget(new JSeparator());
    mySliderSelector = new BooleanSelector("slider");
    mySliderSelector.addValueChangeListener(this);
    propPanel.addWidget(mySliderSelector);
    myRangeField = new DoubleIntervalField("range", new DoubleInterval(0, 1), "%.6g");
    // new VectorMultiField (
    // "range", new String[] { "min:", "max:" }, new Vector2d (0, 1),
    // "%.6f");
    // myRangeField.setAlignmentX (Component.CENTER_ALIGNMENT);
    propPanel.addWidget(myRangeField);
    myLabelTextField = new StringField("labelText", 20);
    myLabelTextField.setEnabledAll(false);
    myLabelTextField.addValueCheckListener(new ValueCheckListener() {

        public Object validateValue(ValueChangeEvent e, StringHolder errMsg) {
            String newName = (String) e.getValue();
            String err = ModelComponentBase.checkName(newName, null);
            if (err != null) {
                err = "Invalid name '" + newName + "': " + err;
                newName = null;
            }
            if (errMsg != null) {
                errMsg.value = err;
            }
            return newName;
        }
    });
    propPanel.addWidget(myLabelTextField);
    myLabelFontColorSelector = new ColorSelector("labelFontColor");
    myLabelFontColorSelector.enableNullColors();
    myLabelFontColorSelector.setValue(null);
    propPanel.addWidget(myLabelFontColorSelector);
    myBackgroundColorSelector = new ColorSelector("backgroundColor");
    myBackgroundColorSelector.enableNullColors();
    myBackgroundColorSelector.setValue(null);
    propPanel.addWidget(myBackgroundColorSelector);
    // sliderPanel.add (myRangeField);
    // pane.add (Box.createRigidArea (new Dimension (0, 4)));
    // pane.add (sliderPanel);
    pane.add(propPanel);
    pane.add(new JSeparator());
    // set slider value here because valueChanged needs some
    // other widgets instantiated.
    mySliderSelector.setValue(false);
    mySliderSelector.setEnabledAll(false);
    OptionPanel options = new OptionPanel("OK Cancel", this);
    myOKButton = options.getButton("OK");
    myOKButton.setEnabled(false);
    options.setAlignmentX(Component.CENTER_ALIGNMENT);
    pane.add(options);
    pack();
}
Also used : BoxLayout(javax.swing.BoxLayout) JSeparator(javax.swing.JSeparator) Container(java.awt.Container) PropertyField(artisynth.core.gui.widgets.PropertyField) ValueCheckListener(maspack.widgets.ValueCheckListener) ValueChangeEvent(maspack.widgets.ValueChangeEvent) StringHolder(maspack.util.StringHolder) BooleanSelector(maspack.widgets.BooleanSelector) DoubleIntervalField(maspack.widgets.DoubleIntervalField) StringField(maspack.widgets.StringField) DoubleInterval(maspack.util.DoubleInterval) LabeledComponentPanel(maspack.widgets.LabeledComponentPanel) ColorSelector(maspack.widgets.ColorSelector) OptionPanel(maspack.widgets.OptionPanel)

Example 4 with ValueCheckListener

use of maspack.widgets.ValueCheckListener in project artisynth_core by artisynth.

the class TimelineController method getWayPointFromUser.

/**
 * Gets waypoint information from the user and adds the waypoint to
 * the waypoint probe.
 */
public void getWayPointFromUser() {
    DoubleField myTimeField = new DoubleField("Time");
    myTimeField.addValueCheckListener(new ValueCheckListener() {

        public Object validateValue(ValueChangeEvent e, StringHolder errMsg) {
            Object val = e.getValue();
            if (val instanceof Double && ((Double) val).doubleValue() <= 0) {
                if (errMsg != null) {
                    errMsg.value = "Time value must be positive";
                }
                return Property.IllegalValue;
            } else {
                if (errMsg != null) {
                    errMsg.value = null;
                }
                return val;
            }
        }
    });
    myTimeField.setVoidValueEnabled(true);
    myTimeField.setValue(Property.VoidValue);
    IntegerField myRepeatField = new IntegerField("Repeat", 1);
    myRepeatField.setRange(1, Integer.MAX_VALUE);
    BooleanSelector myBreakpointSelector = new BooleanSelector("Breakpoint", false);
    PropertyPanel addPanel = new PropertyPanel();
    addPanel.addWidget(myTimeField);
    addPanel.addWidget(myRepeatField);
    addPanel.addWidget(myBreakpointSelector);
    PropertyDialog addDialog = new PropertyDialog(this, "Add Waypoints", addPanel, "OK Cancel");
    addDialog.setModal(true);
    GuiUtils.locateCenter(addDialog, this);
    addDialog.setVisible(true);
    if (addDialog.getReturnValue() == OptionPanel.OK_OPTION && !myTimeField.valueIsVoid()) {
        double t = myTimeField.getDoubleValue();
        for (int i = 1; i <= myRepeatField.getIntValue(); i++) {
            addWayPoint(t * i, myBreakpointSelector.getBooleanValue());
        }
        myToolBar.validateFastForward(myMain.getRootModel());
    }
}
Also used : ValueCheckListener(maspack.widgets.ValueCheckListener) ValueChangeEvent(maspack.widgets.ValueChangeEvent) StringHolder(maspack.util.StringHolder) BooleanSelector(maspack.widgets.BooleanSelector) PropertyDialog(maspack.widgets.PropertyDialog) IntegerField(maspack.widgets.IntegerField) DoubleField(maspack.widgets.DoubleField) WayPoint(artisynth.core.probes.WayPoint) PropertyPanel(maspack.widgets.PropertyPanel)

Aggregations

ValueChangeEvent (maspack.widgets.ValueChangeEvent)4 ValueCheckListener (maspack.widgets.ValueCheckListener)4 StringHolder (maspack.util.StringHolder)3 BooleanSelector (maspack.widgets.BooleanSelector)2 DoubleField (maspack.widgets.DoubleField)2 StringField (maspack.widgets.StringField)2 Main (artisynth.core.driver.Main)1 PropertyField (artisynth.core.gui.widgets.PropertyField)1 WayPoint (artisynth.core.probes.WayPoint)1 RootModel (artisynth.core.workspace.RootModel)1 Container (java.awt.Container)1 Point (java.awt.Point)1 File (java.io.File)1 IOException (java.io.IOException)1 BoxLayout (javax.swing.BoxLayout)1 JFileChooser (javax.swing.JFileChooser)1 JSeparator (javax.swing.JSeparator)1 DoubleInterval (maspack.util.DoubleInterval)1 ColorSelector (maspack.widgets.ColorSelector)1 DoubleIntervalField (maspack.widgets.DoubleIntervalField)1