Skip to content

Commit c1d5213

Browse files
committed
SwingColorAlphaWidget: Simplify code
* remove duplicate static methods which got copied from SwingColorWidget to SwingColorAlphaWidget * make them package private in SwingColorWidget and reference to it
1 parent c86439d commit c1d5213

File tree

2 files changed

+5
-105
lines changed

2 files changed

+5
-105
lines changed

src/main/java/org/scijava/ui/swing/widget/SwingColorAlphaWidget.java

Lines changed: 2 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,13 @@
3939
import javax.swing.BoxLayout;
4040
import javax.swing.ImageIcon;
4141
import javax.swing.JButton;
42-
import javax.swing.JColorChooser;
43-
import javax.swing.JDialog;
4442
import javax.swing.JPanel;
45-
import javax.swing.colorchooser.AbstractColorChooserPanel;
4643
import java.awt.Color;
47-
import java.awt.Component;
4844
import java.awt.Dimension;
4945
import java.awt.Graphics;
50-
import java.awt.HeadlessException;
5146
import java.awt.event.ActionEvent;
5247
import java.awt.event.ActionListener;
5348
import java.awt.image.BufferedImage;
54-
import java.util.Arrays;
55-
import java.util.Comparator;
5649

5750
/**
5851
* Swing implementation of color chooser widget including alpha.
@@ -64,26 +57,14 @@
6457
public class SwingColorAlphaWidget extends SwingInputWidget<ColorRGBA> implements
6558
ActionListener, InputWidget<ColorRGBA, JPanel>
6659
{
67-
68-
private static final int SWATCH_WIDTH = 64, SWATCH_HEIGHT = 16;
69-
70-
private static final String HSB_CLASS_NAME =
71-
"javax.swing.colorchooser.DefaultHSBChooserPanel";
72-
73-
protected static final String RGB_CLASS_NAME =
74-
"javax.swing.colorchooser.DefaultRGBChooserPanel";
75-
76-
protected static final String SWATCHES_CLASS_NAME =
77-
"javax.swing.colorchooser.DefaultSwatchChooserPanel";
78-
7960
private JButton choose;
8061
private Color color;
8162

8263
// -- ActionListener methods --
8364

8465
@Override
8566
public void actionPerformed(final ActionEvent e) {
86-
final Color choice = showColorDialog(choose, "Select a color", color);
67+
final Color choice = SwingColorWidget.showColorDialog(choose, "Select a color", color);
8768
if (choice == null) return;
8869
color = choice;
8970
updateModel();
@@ -126,88 +107,6 @@ public boolean supports(final WidgetModel model) {
126107
return super.supports(model) && model.isType(ColorRGBA.class);
127108
}
128109

129-
// -- Utility methods --
130-
131-
/**
132-
* This method is identical to
133-
* {@link JColorChooser#showDialog(Component, String, Color)} except that it
134-
* reorders the panels of the color chooser to be more desirable. It uses
135-
* (HSB, RGB, Swatches) rather than the default of (Swatches, HSB, RGB).
136-
*/
137-
public static Color showColorDialog(final Component component,
138-
final String title, final Color initialColor) throws HeadlessException
139-
{
140-
final JColorChooser pane = createColorChooser(initialColor);
141-
142-
class ColorTracker implements ActionListener {
143-
144-
private final JColorChooser chooser;
145-
private Color color;
146-
147-
public ColorTracker(final JColorChooser c) {
148-
chooser = c;
149-
}
150-
151-
@Override
152-
public void actionPerformed(final ActionEvent e) {
153-
color = chooser.getColor();
154-
}
155-
156-
public Color getColor() {
157-
return color;
158-
}
159-
}
160-
final ColorTracker ok = new ColorTracker(pane);
161-
162-
final JDialog dialog =
163-
JColorChooser.createDialog(component, title, true, pane, ok, null);
164-
165-
dialog.setVisible(true);
166-
167-
return ok.getColor();
168-
}
169-
170-
// -- Helper methods --
171-
172-
/**
173-
* Creates a new {@link JColorChooser} with panels in a desirable order.
174-
* <p>
175-
* All of this code exists solely to reorder the panels from (Swatches, HSB,
176-
* RGB) to (HSB, RGB, Swatches) since we believe the HSB tab is the most
177-
* commonly useful.
178-
* </p>
179-
*/
180-
private static JColorChooser createColorChooser(final Color initialColor) {
181-
final JColorChooser chooser =
182-
new JColorChooser(initialColor != null ? initialColor : Color.white);
183-
184-
// get the list of panels
185-
final AbstractColorChooserPanel[] panels =
186-
chooser.getChooserPanels().clone();
187-
188-
// sort panels into the desired order
189-
Arrays.sort(panels, new Comparator<Object>() {
190-
191-
@Override
192-
public int compare(final Object o1, final Object o2) {
193-
return value(o1) - value(o2);
194-
}
195-
196-
private int value(final Object o) {
197-
final String className = o.getClass().getName();
198-
if (className.equals(HSB_CLASS_NAME)) return 1;
199-
if (className.equals(RGB_CLASS_NAME)) return 2;
200-
if (className.equals(SWATCHES_CLASS_NAME)) return 3;
201-
return 4;
202-
}
203-
});
204-
205-
// reset the panels to match the sorted list
206-
chooser.setChooserPanels(panels);
207-
208-
return chooser;
209-
}
210-
211110
// -- AbstractUIInputWidget methods ---
212111

213112
@Override
@@ -216,7 +115,7 @@ public void doRefresh() {
216115
color = AWTColors.getColor(value);
217116

218117
final BufferedImage image =
219-
new BufferedImage(SWATCH_WIDTH, SWATCH_HEIGHT, BufferedImage.TYPE_INT_ARGB);
118+
new BufferedImage(SwingColorWidget.SWATCH_WIDTH, SwingColorWidget.SWATCH_HEIGHT, BufferedImage.TYPE_INT_ARGB);
220119
final Graphics g = image.getGraphics();
221120
g.setColor(color);
222121
g.fillRect(0, 0, image.getWidth(), image.getHeight());

src/main/java/org/scijava/ui/swing/widget/SwingColorWidget.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public class SwingColorWidget extends SwingInputWidget<ColorRGB> implements
6565
ActionListener, ColorWidget<JPanel>
6666
{
6767

68-
private static final int SWATCH_WIDTH = 64, SWATCH_HEIGHT = 16;
68+
static final int SWATCH_WIDTH = 64;
69+
static final int SWATCH_HEIGHT = 16;
6970

7071
private static final String HSB_CLASS_NAME =
7172
"javax.swing.colorchooser.DefaultHSBChooserPanel";
@@ -177,7 +178,7 @@ public Color getColor() {
177178
* commonly useful.
178179
* </p>
179180
*/
180-
private static JColorChooser createColorChooser(final Color initialColor) {
181+
static JColorChooser createColorChooser(final Color initialColor) {
181182
final JColorChooser chooser =
182183
new JColorChooser(initialColor != null ? initialColor : Color.white);
183184

0 commit comments

Comments
 (0)