Search in sources :

Example 36 with DefaultComboBoxModel

use of javax.swing.DefaultComboBoxModel in project knime-core by knime.

the class StringManipulationNodeDialog method createStringManipulationPanel.

/**
 * @return the controls for the string manipulation node
 * @since 3.3
 */
public Component createStringManipulationPanel() {
    m_snippetPanel = new JSnippetPanel(StringManipulatorProvider.getDefault(), createCompletionProvider(), !m_isOnlyVariables);
    m_newNameField = new JTextField(10);
    String radioButtonName;
    String radioButtonToolTip;
    radioButtonName = "Append " + WordUtils.capitalize(m_columnOrVariable) + ": ";
    radioButtonToolTip = "Appends a new " + m_columnOrVariable + " to the input with a given name.";
    m_appendRadio = new JRadioButton(radioButtonName);
    m_appendRadio.setToolTipText(radioButtonToolTip);
    radioButtonName = "Replace " + WordUtils.capitalize(m_columnOrVariable) + ": ";
    if (m_isOnlyVariables) {
        radioButtonToolTip = "Replaces the " + m_columnOrVariable + " if the type stays the same.";
    } else {
        radioButtonToolTip = "Replaces the " + m_columnOrVariable + " and changes the " + m_columnOrVariable + " type accordingly.";
    }
    m_replaceRadio = new JRadioButton(radioButtonName);
    m_replaceRadio.setToolTipText(radioButtonToolTip);
    if (m_isOnlyVariables) {
        // show all variables
        m_replaceVariableCombo = new JComboBox<FlowVariable>(new DefaultComboBoxModel<FlowVariable>());
        m_replaceVariableCombo.setRenderer(new FlowVariableListCellRenderer());
    } else {
        // show all columns
        m_replaceColumnCombo = new ColumnSelectionPanel((Border) null, DataValue.class);
        m_replaceColumnCombo.setRequired(false);
    }
    ButtonGroup buttonGroup = new ButtonGroup();
    buttonGroup.add(m_appendRadio);
    buttonGroup.add(m_replaceRadio);
    ActionListener actionListener = new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            if (m_isOnlyVariables) {
                m_replaceVariableCombo.setEnabled(m_replaceRadio.isSelected());
            } else {
                m_replaceColumnCombo.setEnabled(m_replaceRadio.isSelected());
            }
            m_newNameField.setEnabled(m_appendRadio.isSelected());
        }
    };
    m_appendRadio.addActionListener(actionListener);
    m_replaceRadio.addActionListener(actionListener);
    m_compileOnCloseChecker = new JCheckBox("Syntax check on close");
    m_compileOnCloseChecker.setToolTipText("Checks the syntax of the expression on close.");
    m_insertMissingAsNullChecker = new JCheckBox("Insert Missing As Null");
    m_insertMissingAsNullChecker.setToolTipText("If unselected, missing values in the input will produce a missing cell result");
    return createPanel();
}
Also used : JRadioButton(javax.swing.JRadioButton) DataValue(org.knime.core.data.DataValue) ActionEvent(java.awt.event.ActionEvent) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) JTextField(javax.swing.JTextField) JCheckBox(javax.swing.JCheckBox) FlowVariableListCellRenderer(org.knime.core.node.util.FlowVariableListCellRenderer) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) ColumnSelectionPanel(org.knime.core.node.util.ColumnSelectionPanel) JSnippetPanel(org.knime.base.node.util.JSnippetPanel) Border(javax.swing.border.Border) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 37 with DefaultComboBoxModel

use of javax.swing.DefaultComboBoxModel in project knime-core by knime.

the class StringManipulationNodeDialog method loadSettingsFrom.

/**
 * {@inheritDoc}
 */
@Override
protected void loadSettingsFrom(final NodeSettingsRO settings, final PortObjectSpec[] specs) throws NotConfigurableException {
    DataTableSpec spec;
    if (m_isOnlyVariables) {
        spec = new DataTableSpec();
    } else {
        spec = (DataTableSpec) specs[0];
    }
    StringManipulationSettings s = new StringManipulationSettings();
    s.loadSettingsInDialog(settings, spec);
    String exp = s.getExpression();
    String defaultNewName = "new " + m_columnOrVariable;
    String newName = s.getColName();
    boolean isReplace = s.isReplace();
    boolean isTestCompilation = s.isTestCompilationOnDialogClose();
    boolean isInsertMissingAsNull = s.isInsertMissingAsNull();
    m_newNameField.setText("");
    final Map<String, FlowVariable> availableFlowVariables = getAvailableFlowVariables();
    if (m_isOnlyVariables) {
        DefaultComboBoxModel<FlowVariable> cmbModel = (DefaultComboBoxModel<FlowVariable>) m_replaceVariableCombo.getModel();
        cmbModel.removeAllElements();
        for (FlowVariable v : availableFlowVariables.values()) {
            switch(v.getScope()) {
                case Flow:
                    cmbModel.addElement(v);
                    break;
                default:
            }
        }
        if (availableFlowVariables.containsValue(newName)) {
            m_replaceVariableCombo.setSelectedItem(availableFlowVariables.get(newName));
        }
    } else {
        // will select newColName only if it is in the spec list
        try {
            m_replaceColumnCombo.update(spec, newName);
        } catch (NotConfigurableException e1) {
            NodeLogger.getLogger(getClass()).coding("Combo box throws exception although content is not required", e1);
        }
    }
    m_currentSpec = spec;
    // whether there are variables or columns available
    // -- which of two depends on the customizer
    boolean fieldsAvailable;
    if (m_isOnlyVariables) {
        fieldsAvailable = m_replaceVariableCombo.getItemCount() > 0;
    } else {
        fieldsAvailable = m_replaceColumnCombo.getNrItemsInList() > 0;
    }
    m_replaceRadio.setEnabled(fieldsAvailable);
    if (isReplace && fieldsAvailable) {
        m_replaceRadio.doClick();
    } else {
        m_appendRadio.doClick();
        String newNameString = (newName != null ? newName : defaultNewName);
        m_newNameField.setText(newNameString);
    }
    m_snippetPanel.update(exp, spec, availableFlowVariables);
    m_compileOnCloseChecker.setSelected(isTestCompilation);
    m_insertMissingAsNullChecker.setSelected(isInsertMissingAsNull);
}
Also used : NotConfigurableException(org.knime.core.node.NotConfigurableException) DataTableSpec(org.knime.core.data.DataTableSpec) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 38 with DefaultComboBoxModel

use of javax.swing.DefaultComboBoxModel in project knime-core by knime.

the class ConfigEditTreeNodePanel method setTreeNode.

/**
 * Set a new tree node to display.
 * @param treeNode the new node to represent (may be null).
 */
public void setTreeNode(final ConfigEditTreeNode treeNode) {
    m_treeNode = treeNode;
    boolean isEditable = m_treeNode != null && m_treeNode.isLeaf();
    Type selType;
    String usedVariable;
    m_valueField.setEnabled(isEditable);
    if (m_treeNode != null) {
        AbstractConfigEntry entry = treeNode.getConfigEntry();
        switch(entry.getType()) {
            case xbyte:
            case xlong:
            case xshort:
            case xint:
                selType = Type.INTEGER;
                break;
            case xdouble:
            case xfloat:
                selType = Type.DOUBLE;
                break;
            default:
                selType = Type.STRING;
        }
        Icon icon;
        switch(entry.getType()) {
            case xstring:
                icon = ICON_STRING;
                break;
            case xdouble:
                icon = ICON_DOUBLE;
                break;
            case xint:
                icon = ICON_INT;
                break;
            default:
                icon = ICON_UNKNOWN;
        }
        m_keyIcon = icon;
        m_keyLabel.setText(entry.getKey());
        m_keyLabel.setToolTipText(entry.getKey());
        usedVariable = m_treeNode.getUseVariableName();
        String exposeVariable = m_treeNode.getExposeVariableName();
        m_exposeAsVariableField.setText(exposeVariable);
    } else {
        selType = Type.STRING;
        m_keyLabel.setText("");
        m_keyLabel.setToolTipText(null);
        m_keyIcon = ICON_UNKNOWN;
        m_exposeAsVariableField.setText("");
        usedVariable = null;
    }
    m_keyLabel.setMinimumSize(LABEL_DIMENSION);
    m_keyLabel.setMaximumSize(LABEL_DIMENSION);
    m_keyLabel.setPreferredSize(LABEL_DIMENSION);
    m_keyLabel.setSize(LABEL_DIMENSION);
    DefaultComboBoxModel model = (DefaultComboBoxModel) m_valueField.getModel();
    model.removeAllElements();
    model.addElement(" ");
    @SuppressWarnings("unchecked") Collection<FlowVariable> allVars = getFlowObjectStack() != null ? getFlowObjectStack().getAvailableFlowVariables().values() : (Collection<FlowVariable>) Collections.EMPTY_LIST;
    ComboBoxElement match = null;
    for (FlowVariable v : allVars) {
        boolean isOk = ConfigEditTreeModel.doesTypeAccept(selType, v.getType());
        if (isOk) {
            ComboBoxElement cbe = new ComboBoxElement(v);
            model.addElement(cbe);
            if (v.getName().equals(usedVariable)) {
                match = cbe;
            }
        } else if (v.getName().equals(usedVariable)) {
            String error = "Variable \"" + usedVariable + "\" has wrong type (" + v.getType() + "), expected " + selType;
            ComboBoxElement cbe = new ComboBoxElement(v, error);
            model.addElement(cbe);
            match = cbe;
        }
    }
    if (match != null) {
        m_valueField.setSelectedItem(match);
    } else if (usedVariable != null) {
        // show name in variable in arrows; makes also sure to
        // not violate the namespace of the variable (could be
        // node-local variable, which can't be created outside
        // the workflow package)
        String errorName = "<" + usedVariable + ">";
        String error = "Invalid variable \"" + usedVariable + "\"";
        FlowVariable virtualVar;
        switch(selType) {
            case DOUBLE:
                virtualVar = new FlowVariable(errorName, 0.0);
                break;
            case INTEGER:
                virtualVar = new FlowVariable(errorName, 0);
                break;
            default:
                virtualVar = new FlowVariable(errorName, "");
                break;
        }
        ComboBoxElement cbe = new ComboBoxElement(virtualVar, error);
        model.addElement(cbe);
        m_valueField.setSelectedItem(cbe);
    }
    m_valueField.setEnabled(model.getSize() > 1);
}
Also used : AbstractConfigEntry(org.knime.core.node.config.base.AbstractConfigEntry) Type(org.knime.core.node.workflow.FlowVariable.Type) Icon(javax.swing.Icon) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) FlowVariable(org.knime.core.node.workflow.FlowVariable)

Example 39 with DefaultComboBoxModel

use of javax.swing.DefaultComboBoxModel in project knime-core by knime.

the class ColumnPairsSelectionPanel method addUIControls.

private void addUIControls(final int index, final String leftSelected, final String rightSelected) {
    m_leftComboBoxes.add(index, new JComboBox());
    m_leftComboBoxes.get(index).setModel(new DefaultComboBoxModel());
    ColumnComboBoxRenderer renderer = new ColumnComboBoxRenderer();
    renderer.attachTo(m_leftComboBoxes.get(index));
    initComboBox(m_specs[0], m_leftComboBoxes.get(index), leftSelected);
    m_rightComboBoxes.add(index, new JComboBox());
    m_rightComboBoxes.get(index).setModel(new DefaultComboBoxModel());
    renderer = new ColumnComboBoxRenderer();
    renderer.attachTo(m_rightComboBoxes.get(index));
    initComboBox(m_specs[1], m_rightComboBoxes.get(index), rightSelected);
    JButton addButton = new JButton("+");
    addButton.setToolTipText("Add row preceding this.");
    addButton.addActionListener(m_addButtonListener);
    m_addButtons.add(index, addButton);
    JButton removeButton = new JButton("-");
    removeButton.addActionListener(m_removeButtonListener);
    removeButton.setToolTipText("Remove this row.");
    m_removeButtons.add(index, removeButton);
    // if the first row was added
    if (m_leftComboBoxes.size() == 1) {
        m_persistentAddButton.setText("+");
    }
}
Also used : JComboBox(javax.swing.JComboBox) JButton(javax.swing.JButton) DefaultComboBoxModel(javax.swing.DefaultComboBoxModel)

Example 40 with DefaultComboBoxModel

use of javax.swing.DefaultComboBoxModel in project knime-core by knime.

the class StringHistoryPanel method updateHistory.

/**
 * Updates the elements in the combo box, reads the file history.
 */
public void updateHistory() {
    StringHistory history = StringHistory.getInstance(m_historyID);
    String[] allVals = history.getHistory();
    DefaultComboBoxModel comboModel = (DefaultComboBoxModel) m_textBox.getModel();
    comboModel.removeAllElements();
    for (String s : allVals) {
        comboModel.addElement(s);
    }
    // changing the model will also change the minimum size to be
    // quite big. We have tooltips, we don't need that
    Dimension newMin = new Dimension(0, getPreferredSize().height);
    setMinimumSize(newMin);
}
Also used : DefaultComboBoxModel(javax.swing.DefaultComboBoxModel) Dimension(java.awt.Dimension)

Aggregations

DefaultComboBoxModel (javax.swing.DefaultComboBoxModel)119 JComboBox (javax.swing.JComboBox)22 JPanel (javax.swing.JPanel)21 JLabel (javax.swing.JLabel)17 ActionEvent (java.awt.event.ActionEvent)15 ActionListener (java.awt.event.ActionListener)15 JButton (javax.swing.JButton)15 Insets (java.awt.Insets)14 GridBagConstraints (java.awt.GridBagConstraints)13 GridBagLayout (java.awt.GridBagLayout)12 Dimension (java.awt.Dimension)11 JTextField (javax.swing.JTextField)11 JCheckBox (javax.swing.JCheckBox)10 JScrollPane (javax.swing.JScrollPane)10 ArrayList (java.util.ArrayList)9 Vector (java.util.Vector)9 JList (javax.swing.JList)9 DataColumnSpec (org.knime.core.data.DataColumnSpec)9 BorderLayout (java.awt.BorderLayout)8 ItemEvent (java.awt.event.ItemEvent)8