private void resetColorChooser(JCheckBox checkBox, ColorPanel colorPanel, @Nullable Color color) { checkBox.setEnabled(true); if (color == null) { checkBox.setSelected(false); colorPanel.setEnabled(false); colorPanel.setSelectedColor(null); } else { checkBox.setSelected(true); colorPanel.setEnabled(true); colorPanel.setSelectedColor(color); } }
/** * Retrieve a value from the given JComponent */ @Nullable protected Object getComponentValue(Parameter param, JComponent component) { Object newValue = null; if (component instanceof JCheckBox) { newValue = ((JCheckBox)component).isSelected(); } else if (component instanceof JComboBox) { ComboBoxItem selectedItem = (ComboBoxItem)((JComboBox)component).getSelectedItem(); myComboBoxValues.put(param, selectedItem); if (selectedItem != null) { newValue = selectedItem.id; } } else if (component instanceof JTextField) { newValue = ((JTextField)component).getText(); } else if (component instanceof TextFieldWithBrowseButton) { newValue = ((TextFieldWithBrowseButton)component).getText(); } else if (component instanceof JSlider) { newValue = ((JSlider)component).getValue(); } else if (component instanceof JSpinner) { newValue = ((JSpinner)component).getValue(); } else if (component instanceof ColorPanel) { newValue = ((ColorPanel)component).getSelectedColor(); } return newValue; }
protected void register(@NotNull String paramName, @NotNull ColorPanel colorPanel) { Color value = (Color)myTemplateState.get(paramName); if (value != null) { colorPanel.setSelectedColor(value); } else { myTemplateState.put(paramName, colorPanel.getSelectedColor()); } myParamFields.put(paramName, (JComponent)colorPanel); colorPanel.addFocusListener(this); colorPanel.addActionListener(this); }
/** * Retrieve a value from the given JComponent */ @Nullable protected Object getComponentValue(@NotNull JComponent component) { Object newValue = null; if (component instanceof JCheckBox) { newValue = ((JCheckBox)component).isSelected(); } else if (component instanceof JComboBox) { Object selectedObject = ((JComboBox)component).getSelectedItem(); if (selectedObject instanceof ComboBoxItem) { ComboBoxItem selectedItem = (ComboBoxItem)selectedObject; newValue = selectedItem.id; } else { newValue = selectedObject; } } else if (component instanceof JSlider) { newValue = ((JSlider)component).getValue(); } else if (component instanceof JSpinner) { newValue = ((JSpinner)component).getValue(); } else if (component instanceof ColorPanel) { newValue = ((ColorPanel)component).getSelectedColor(); } else { TextAccessor accessor = TextAccessors.getTextAccessor(component); if (accessor != null) { newValue = accessor.getText(); } } return newValue; }
/** * Connects the given {@link ColorPanel} to the given key and sets a listener to pick up changes that need to trigger validation * and UI updates. */ protected void register(@NotNull Key<Color> key, @NotNull ColorPanel colorPanel) { Color value = bindAndGet(key, colorPanel, null); if (value != null) { colorPanel.setSelectedColor(value); } else { myState.put(key, colorPanel.getSelectedColor()); } colorPanel.addFocusListener(this); colorPanel.addActionListener(this); }
/** * Removes all component bindings and listeners. */ protected void deregister(JComponent component) { if (myComponentBindings.rowMap().remove(component) != null) { component.removeFocusListener(this); if (component instanceof JCheckBox) { ((JCheckBox)component).removeItemListener(this); } else if (component instanceof JComboBox) { ((JComboBox)component).removeActionListener(this); } else if (component instanceof JTextField) { ((JTextField)component).getDocument().removeDocumentListener(this); } else if (component instanceof JRadioButton) { ((JRadioButton)component).removeActionListener(this); } else if (component instanceof JSlider) { ((JSlider)component).removeChangeListener(this); } else if (component instanceof JSpinner) { ((JSpinner)component).removeChangeListener(this); } else if (component instanceof TextFieldWithBrowseButton) { ((TextFieldWithBrowseButton)component).getTextField().getDocument().removeDocumentListener(this); ((TextFieldWithBrowseButton)component).getTextField().removeFocusListener(this); } else if (component instanceof ColorPanel) { ((ColorPanel)component).removeActionListener(this); } } }
private static void updateColorChooser(JCheckBox checkBox, ColorPanel colorPanel, boolean isEnabled, boolean isChecked, @Nullable Color color) { checkBox.setEnabled(isEnabled); checkBox.setSelected(isChecked); if (color != null) { colorPanel.setSelectedColor(color); } else { colorPanel.setSelectedColor(Color.white); } colorPanel.setEnabled(isChecked); }
public void actionPerformed(ActionEvent e) { ColorPanel source = (ColorPanel)e.getSource(); options.setOption(name, source.getSelectedColor()); }
public void refreshUiFromParameters() { if (myTemplateState.myTemplate == null) { return; } for (Parameter param : myTemplateState.myTemplate.getMetadata().getParameters()) { if (param.initial != null && !myTemplateState.myModified.contains(param.id)) { myTemplateState.myParameters.remove(param.id); } } myTemplateState.setParameterDefaults(); Template.convertApisToInt(myTemplateState.getParameters()); boolean oldIgnoreUpdates = myIgnoreUpdates; try { myIgnoreUpdates = true; for (String paramName : myParamFields.keySet()) { if (myTemplateState.myHidden.contains(paramName)) { continue; } JComponent component = myParamFields.get(paramName); Object value = myTemplateState.get(paramName); if (value == null) { continue; } if (component instanceof JCheckBox) { ((JCheckBox)component).setSelected(Boolean.parseBoolean(value.toString())); } else if (component instanceof JComboBox) { for (int i = 0; i < ((JComboBox)component).getItemCount(); i++) { if (((ComboBoxItem)((JComboBox)component).getItemAt(i)).id.equals(value)) { ((JComboBox)component).setSelectedIndex(i); break; } } } else if (component instanceof JTextField) { ((JTextField)component).setText(value.toString()); } else if (component instanceof TextFieldWithBrowseButton) { ((TextFieldWithBrowseButton)component).setText(value.toString()); } else if (component instanceof JSlider) { ((JSlider)component).setValue(Integer.parseInt(value.toString())); } else if (component instanceof JSpinner) { ((JSpinner)component).setValue(Integer.parseInt(value.toString())); } else if (component instanceof ColorPanel) { ((ColorPanel)component).setSelectedColor((Color)value); } } } finally { myIgnoreUpdates = oldIgnoreUpdates; } }
public void testRegisterColorPanel() throws Exception { Key<Color> colorKey = myState.createKey("colorPanel", Color.class); Key<Color> colorKey2 = myState.createKey("boundSecond", Color.class); final Key<String> triggerKey = myState.createKey("triggerKey", String.class); ColorPanel colorPanel = new ColorPanel(); myScopedDataBinder.register(colorKey, colorPanel); myScopedDataBinder.register(colorKey2, colorPanel); // Test binding UI -> Store colorPanel.setSelectedColor(Color.BLUE); // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here myScopedDataBinder.saveState(colorPanel); assertEquals(Color.BLUE, myState.get(colorKey)); assertEquals(Color.BLUE, myState.get(colorKey2)); // Test binding Store -> UI myState.put(colorKey, Color.RED); // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here myScopedDataBinder.saveState(colorPanel); assertEquals(Color.RED, colorPanel.getSelectedColor()); assertEquals(Color.RED, myState.get(colorKey2)); myState.put(colorKey2, Color.GREEN); // ColorPanel doesn't call listeners on setSelectedColor, so we manually invoke here myScopedDataBinder.saveState(colorPanel); assertEquals(Color.GREEN, colorPanel.getSelectedColor()); assertEquals(Color.GREEN, myState.get(colorKey)); final AtomicBoolean respectsUserEdits = new AtomicBoolean(true); // Test value derivation myScopedDataBinder.registerValueDeriver(colorKey, new ValueDeriver<Color>() { @Nullable @Override public Set<Key<?>> getTriggerKeys() { return makeSetOf(triggerKey); } @Override public boolean respectUserEdits() { return respectsUserEdits.get(); } @Override public Color deriveValue(ScopedStateStore state, Key changedKey, @Nullable Color currentValue) { String trigger = state.get(triggerKey); if (trigger == null) { return null; } else { return Color.decode(trigger); } } }); myState.put(triggerKey, "Not A Value"); // The deriver does not fire because user edits are respected assertEquals(Color.GREEN, colorPanel.getSelectedColor()); respectsUserEdits.set(false); myState.put(triggerKey, "#FFFFFF"); // The deriver fires because user edits are not respected assertEquals(Color.WHITE, colorPanel.getSelectedColor()); }
private void init() { setLayout(new BorderLayout()); JPanel c = this; scrollToFirst = new JCheckBox("Scroll first hit into visible area"); scrollToFirst.setMnemonic('S'); useContextAtCursor = new JCheckBox("Use node at cursor as context node"); useContextAtCursor.setMnemonic('N'); useContextAtCursor.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stateChanged(); } }); highlightStartTagOnly = new JCheckBox("Highlight only start tag instead of whole tag content"); highlightStartTagOnly.setMnemonic('H'); addErrorStripe = new JCheckBox("Add error stripe markers for each result"); addErrorStripe.setMnemonic('A'); showInToolbar = new JCheckBox("Show actions in Toolbar"); showInToolbar.setMnemonic('T'); showInToolbar.setToolTipText("Uncheck to remove XPath-related actions from the toolbar"); showInMainMenu = new JCheckBox("Show actions in Main Menu"); showInMainMenu.setMnemonic('M'); showInMainMenu.setToolTipText("Uncheck to remove XPath-related actions from the Main-Menubar"); JPanel settings = new JPanel(new BorderLayout()); settings.setBorder(IdeBorderFactory.createTitledBorder("Settings", true)); c.add(c = new JPanel(new BorderLayout()), BorderLayout.NORTH); c.add(settings, BorderLayout.NORTH); settings.add(scrollToFirst, BorderLayout.NORTH); settings.add(settings = new JPanel(new BorderLayout()), BorderLayout.SOUTH); settings.add(useContextAtCursor, BorderLayout.NORTH); settings.add(settings = new JPanel(new BorderLayout()), BorderLayout.SOUTH); settings.add(highlightStartTagOnly, BorderLayout.NORTH); settings.add(settings = new JPanel(new BorderLayout()), BorderLayout.SOUTH); settings.add(addErrorStripe, BorderLayout.NORTH); settings.add(settings = new JPanel(new BorderLayout()), BorderLayout.SOUTH); settings.add(showInToolbar, BorderLayout.NORTH); settings.add(settings = new JPanel(new BorderLayout()), BorderLayout.SOUTH); settings.add(showInMainMenu, BorderLayout.NORTH); settings.add(/*settings = */new JPanel(new BorderLayout()), BorderLayout.SOUTH); JPanel colors = new JPanel(new GridBagLayout()); colors.setBorder(IdeBorderFactory.createTitledBorder("Colors", true)); c.add(c = new JPanel(new BorderLayout()), BorderLayout.SOUTH); c.add(colors, BorderLayout.NORTH); final GridBagConstraints constraints = new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0); colors.add(new JLabel("Highlight color:"), constraints); constraints.gridx = 1; constraints.weightx = 1; chooseHighlight = new ColorPanel(); colors.add(chooseHighlight, constraints); constraints.gridx = 0; constraints.gridy = 1; constraints.weightx = 0; colors.add(new JLabel("Context node color:"), constraints); constraints.gridx = 1; constraints.gridy = 1; constraints.weightx = 1; chooseContext = new ColorPanel(); colors.add(chooseContext, constraints); }