private RecentColorsPanel(final ColorSelectionModel colorSelectionModel, final UserPreferences preferences) { super(new GridBagLayout()); this.pipetteCursor = SwingTools.createCustomCursor( OperatingSystem.isMacOSX() ? ColorButton.class.getResource("resources/cursors/pipette16x16-macosx.png") : ColorButton.class.getResource("resources/cursors/pipette16x16.png"), ColorButton.class.getResource("resources/cursors/pipette32x32.png"), 0, 1, "Pipette", Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); preferences.addPropertyChangeListener(UserPreferences.Property.RECENT_COLORS, new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent ev) { setRecentColors(colorSelectionModel, preferences); } }); setRecentColors(colorSelectionModel, preferences); setOpaque(false); }
/** * Creates a new instance of ColorButton. Default color is black, default * size of the icon is 16 x 16 pixels. This button is registered with itself * for receiving action performed calls. */ public ColorButton() { this.color = new Color(0, 0, 0); this.iconHeight = 16; this.iconWidth = 16; this.colorChooserTitle = "Choose a Color"; //Set up the dialog that the button brings up. colorChooser = new JColorChooser(); // replace the ugly and useless preview panel by an empty JPanel colorChooser.setPreviewPanel(new JPanel()); // remove the swatch AbstractColorChooserPanel[] choosers = colorChooser.getChooserPanels(); for (AbstractColorChooserPanel chooser : choosers) { String clsName = chooser.getClass().getName(); if (clsName.equals("javax.swing.colorchooser.DefaultSwatchChooserPanel")) { colorChooser.removeChooserPanel(chooser); } } ColorSelectionModel colorSelectionModel = colorChooser.getSelectionModel(); colorSelectionModel.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent evt) { ColorSelectionModel model = (ColorSelectionModel) evt.getSource(); setColor(model.getSelectedColor()); } }); this.updateIcon(); }
@CheckReturnValue @NonNull @SchedulerSupport(SchedulerSupport.NONE) public static Observable<ChangeEvent> change(@NonNull ColorSelectionModel component) { ObjectHelper.requireNonNull(component, "component is null"); return RxSwingPlugins.onAssembly(new ChangeEventColorObservable(component)); }
@Override protected void subscribeActual(Observer<? super ChangeEvent> observer) { ColorSelectionModel w = widget; ChangeEventConsumer aec = new ChangeEventConsumer(observer, w); observer.onSubscribe(aec); w.addChangeListener(aec); if (aec.get() == null) { aec.onDispose(w); } }
/** * Maps {@code JColorChooser.getSelectionModel()} through queue */ public ColorSelectionModel getSelectionModel() { return (runMapping(new MapAction<ColorSelectionModel>("getSelectionModel") { @Override public ColorSelectionModel map() { return ((JColorChooser) getSource()).getSelectionModel(); } })); }
/** * Maps {@code JColorChooser.setSelectionModel(ColorSelectionModel)} * through queue */ public void setSelectionModel(final ColorSelectionModel colorSelectionModel) { runMapping(new MapVoidAction("setSelectionModel") { @Override public void map() { ((JColorChooser) getSource()).setSelectionModel(colorSelectionModel); } }); }
public CColorChooser(final ColorSelectionModel model, final Color[] recentColors) { super(model); // Remove first chooser panel ("Swatches") // TODO(cblichmann): Revisit this for JDK > 1.7 final AbstractColorChooserPanel[] panels = getChooserPanels(); if (panels.length > 0) { removeChooserPanel(panels[0]); } m_recentColors = recentColors; if (m_recentColors != null) { addChooserPanel(new RecentColorsColorChooserPanel()); } }
/** * ChangeListener interface. Called when the color changes */ public void stateChanged(ChangeEvent e) { if (!(e.getSource() instanceof ColorSelectionModel)) return; setPreviewColor(((ColorSelectionModel) e.getSource()).getSelectedColor()); }
public void testJColorChooser() throws Exception { assertSame(DefaultColorSelectionModel.class, ch.getSelectionModel().getClass()); assertNull(ch.accessibleContext); assertEquals(Color.WHITE, ch.getColor()); ch = new JColorChooser(Color.BLACK); assertEquals(Color.BLACK, ch.getColor()); ColorSelectionModel model = new DefaultColorSelectionModel(); ch = new JColorChooser(model); assertFalse(propertyChangeController.isChanged()); assertSame(model, ch.getSelectionModel()); assertSame(BorderLayout.class, ch.getLayout().getClass()); }
public void testSetGetSelectionModel() throws Exception { assertNotNull(ch.getSelectionModel()); ColorSelectionModel oldModel = ch.getSelectionModel(); ColorSelectionModel model = new DefaultColorSelectionModel(); ch.setSelectionModel(model); assertTrue(propertyChangeController.isChanged(JColorChooser.SELECTION_MODEL_PROPERTY)); assertSame(model, ((PropertyChangeEvent) propertyChangeController.getLastEvent()) .getNewValue()); assertSame(oldModel, ((PropertyChangeEvent) propertyChangeController.getLastEvent()) .getOldValue()); ch.setSelectionModel(null); assertNull(ch.getSelectionModel()); }
@Override public void setSelectionModel(ColorSelectionModel newModel) { AlfaColorSelectionModel model = (AlfaColorSelectionModel) newModel; super.setSelectionModel(model); registerListenerToModel(); model.fireStateChanged(); }
private void alphaSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_alphaSliderStateChanged int v=alphaSlider.getValue(); alphaTxt.setText(Integer.toString(v)); ColorSelectionModel csm=getColorSelectionModel(); if(csm!=null){ Color c=csm.getSelectedColor(); Color nc = v < 255 ? new Color(c.getRed(), c.getGreen(), c.getBlue(), v) : new Color(c.getRed(), c.getGreen(), c.getBlue()); csm.setSelectedColor(nc); } }
/** * An implemented <code>ItemListener</code> to change colors when user changes value in the <code>comboBox</code>. * * @param itemEvent the received event * * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent) */ public void itemStateChanged(ItemEvent itemEvent) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { int position = findColorLabel(itemEvent.getItem()); if (position != -1) { ColorSelectionModel selectionModel = getColorSelectionModel(); selectionModel.setSelectedColor(COLORS[position]); } } }
ChangeEventColorObservable(ColorSelectionModel widget) { this.widget = widget; }
ChangeEventConsumer(Observer<? super ChangeEvent> actual, ColorSelectionModel widget) { super(actual, widget); }
@Override protected void onDispose(ColorSelectionModel w) { w.removeChangeListener(this); }
private void setRecentColors(final ColorSelectionModel colorSelectionModel, UserPreferences preferences) { removeAll(); int i = 0; if (UIManager.getLookAndFeel().getID().equals("GTK")) { // Add a label to replace the border that is not active add(new JLabel(UIManager.getString("ColorChooser.previewText")), new GridBagConstraints(i++, 0, 1, 1, 0, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0)); } List<Integer> recentColors = preferences.getRecentColors(); final int colorComponentSize = Math.round(20 * SwingTools.getResolutionScale()); for (int j = 0; j < recentColors.size() && j < MAX_COLORS; j++) { final Integer color = recentColors.get(j); Component colorComponent = new JComponent() { @Override protected void paintComponent(Graphics g) { Insets insets = getInsets(); int drawnWidth = getWidth() - insets.right - insets.left; int drawnHeight = getHeight() - insets.bottom - insets.top; g.setColor(Color.GRAY); g.translate(insets.left, insets.top); g.drawRect(0, 0, drawnWidth - 1, drawnHeight - 1); g.setColor(new Color(color)); g.fillRect(1, 1, drawnWidth - 2, drawnHeight - 2); } @Override public Dimension getPreferredSize() { return new Dimension(colorComponentSize, colorComponentSize); } }; colorComponent.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent ev) { colorSelectionModel.setSelectedColor(new Color(color)); if (ev.getClickCount() == 2) { JRootPane rootPane = (JRootPane) SwingUtilities.getAncestorOfClass(JRootPane.class, RecentColorsPanel.this); if (rootPane != null) { for (JButton button : SwingTools.findChildren(rootPane, JButton.class)) { if ("OK".equals(button.getActionCommand())) { button.doClick(); } } } } } }); colorComponent.setCursor(this.pipetteCursor); add(colorComponent, new GridBagConstraints(i++, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 2), 0, 0)); } }
public CColorChooser(final ColorSelectionModel model) { this(model, null); }
public JColorChooser(ColorSelectionModel model) { colorSelectionModel = model; setLayout(new BorderLayout()); updateUI(); }
public ColorSelectionModel getSelectionModel() { return colorSelectionModel; }
public void setSelectionModel(ColorSelectionModel newModel) { ColorSelectionModel oldModel = colorSelectionModel; colorSelectionModel = newModel; firePropertyChange(SELECTION_MODEL_PROPERTY, oldModel, newModel); }
/** * This method sets the ColorSelectionModel to be used with this * JColorChooser. * * @param model The ColorSelectionModel to be used with this JColorChooser. * * @throws AWTError If the given model is null. */ public void setSelectionModel(ColorSelectionModel model) { if (model == null) throw new AWTError("ColorSelectionModel is not allowed to be null."); selectionModel = model; }
/** * This method returns the ColorSelectionModel used with this JColorChooser. * * @return The ColorSelectionModel. */ public ColorSelectionModel getSelectionModel() { return selectionModel; }