001: /*
002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package test.check;
031:
032: import java.awt.*;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.util.*;
036: import java.util.List;
037:
038: import javax.swing.*;
039: import javax.swing.border.TitledBorder;
040:
041: import org.jvnet.substance.SubstanceLookAndFeel;
042: import org.jvnet.substance.painter.SpecularGradientPainter;
043: import org.jvnet.substance.theme.*;
044: import org.jvnet.substance.theme.SubstanceTheme.ThemeKind;
045:
046: import test.Check;
047:
048: /**
049: * Test application panel for testing {@link JButton} component under various
050: * {@link SubstanceMixTheme} combinations.
051: *
052: * @author Kirill Grouchnikov
053: */
054: public class MixedButtonsPanel extends JPanel {
055: /**
056: * Contains all buttons that use bright themes.
057: */
058: protected List<JButton> brightMixed;
059:
060: /**
061: * Contains all buttons that use cold themes.
062: */
063: protected List<JButton> coldMixed;
064:
065: /**
066: * Maps the buttons to the color distances between the component mix themes.
067: */
068: protected Map<JButton, Integer> colorDistances;
069:
070: /**
071: * Current page number.
072: */
073: protected int pageNumber;
074:
075: /**
076: * Number of buttons per page.
077: */
078: protected int pageSize = 40;
079:
080: /**
081: * Panel for bright buttons.
082: */
083: protected JPanel brightButtons;
084:
085: /**
086: * Panel for cold buttons.
087: */
088: protected JPanel coldButtons;
089:
090: /**
091: * Creates a new panel with buttons using mixed themes.
092: */
093: public MixedButtonsPanel() {
094: this .setLayout(new BorderLayout());
095:
096: this .brightMixed = new ArrayList<JButton>();
097: this .coldMixed = new ArrayList<JButton>();
098: for (ThemeInfo theme : SubstanceLookAndFeel.getAllThemes()
099: .values()) {
100: if (theme.getThemeKind() == ThemeKind.MIXED)
101: continue;
102: if (theme.getThemeKind() == ThemeKind.DARK)
103: continue;
104: if (theme.getThemeKind() == ThemeKind.INVERTED)
105: continue;
106: if (theme.getThemeKind() == ThemeKind.NEGATED)
107: continue;
108: for (ThemeInfo theme2 : SubstanceLookAndFeel.getAllThemes()
109: .values()) {
110: if (theme.getThemeKind() != theme2.getThemeKind())
111: continue;
112: if (theme.getDisplayName().equals(
113: theme2.getDisplayName()))
114: continue;
115:
116: try {
117: JButton mixedButton = new JButton("Test");
118: SubstanceMixTheme mixedTheme = new SubstanceMixTheme(
119: SubstanceTheme.createInstance(theme),
120: SubstanceTheme.createInstance(theme2));
121: mixedButton.putClientProperty(
122: SubstanceLookAndFeel.THEME_PROPERTY,
123: mixedTheme);
124: mixedButton.putClientProperty(
125: SubstanceLookAndFeel.PAINT_ACTIVE_PROPERTY,
126: Boolean.TRUE);
127: mixedButton
128: .putClientProperty(
129: SubstanceLookAndFeel.GRADIENT_PAINTER_PROPERTY,
130: SpecularGradientPainter.class
131: .getName());
132: mixedButton.setToolTipText(mixedTheme
133: .getDisplayName());
134: // mixedButton.putClientProperty(
135: // SubstanceLookAndFeel.BUTTON_NO_MIN_SIZE_PROPERTY,
136: // Boolean.TRUE);
137: // mixedButton.setPreferredSize(new Dimension(45, 16));
138: if (theme.getThemeKind() == ThemeKind.BRIGHT)
139: brightMixed.add(mixedButton);
140: if (theme.getThemeKind() == ThemeKind.COLD)
141: coldMixed.add(mixedButton);
142: } catch (Exception exc) {
143: }
144: }
145: }
146:
147: this .colorDistances = new HashMap<JButton, Integer>();
148: for (JButton bright : brightMixed) {
149: SubstanceMixTheme mix = (SubstanceMixTheme) bright
150: .getClientProperty(SubstanceLookAndFeel.THEME_PROPERTY);
151: SubstanceTheme[] origThemes = ((SubstanceMixTheme) mix)
152: .getOriginalThemes();
153: SubstanceTheme theme1 = origThemes[0];
154: SubstanceTheme theme2 = origThemes[origThemes.length - 1];
155: Color c1 = theme1.getColorScheme().getMidColor();
156: Color c2 = theme2.getColorScheme().getMidColor();
157: int dr = c1.getRed() - c2.getRed();
158: int dg = c1.getGreen() - c2.getGreen();
159: int db = c1.getBlue() - c2.getBlue();
160:
161: double distance = Math.sqrt(dr * dr + dg * dg + db * db);
162: colorDistances.put(bright, (int) distance);
163: }
164: for (JButton cold : coldMixed) {
165: SubstanceMixTheme mix = (SubstanceMixTheme) cold
166: .getClientProperty(SubstanceLookAndFeel.THEME_PROPERTY);
167: SubstanceTheme[] origThemes = ((SubstanceMixTheme) mix)
168: .getOriginalThemes();
169: SubstanceTheme theme1 = origThemes[0];
170: SubstanceTheme theme2 = origThemes[origThemes.length - 1];
171: Color c1 = theme1.getColorScheme().getMidColor();
172: Color c2 = theme2.getColorScheme().getMidColor();
173: int dr = c1.getRed() - c2.getRed();
174: int dg = c1.getGreen() - c2.getGreen();
175: int db = c1.getBlue() - c2.getBlue();
176:
177: double distance = Math.sqrt(dr * dr + dg * dg + db * db);
178: colorDistances.put(cold, (int) distance);
179: }
180:
181: Comparator<JButton> mixComparator = new Comparator<JButton>() {
182: public int compare(JButton o1, JButton o2) {
183: int dist1 = colorDistances.get(o1);
184: int dist2 = colorDistances.get(o2);
185: if (dist1 == dist2) {
186: SubstanceMixTheme mix1 = (SubstanceMixTheme) o1
187: .getClientProperty(SubstanceLookAndFeel.THEME_PROPERTY);
188: SubstanceMixTheme mix2 = (SubstanceMixTheme) o2
189: .getClientProperty(SubstanceLookAndFeel.THEME_PROPERTY);
190: return mix1.getDisplayName().compareTo(
191: mix2.getDisplayName());
192: } else
193: return dist1 - dist2;
194: }
195: };
196:
197: Collections.sort(brightMixed, mixComparator);
198: // for (JButton bright : brightMixed) {
199: // bright.setText("" + colorDistances.get(bright));
200: // this.add(bright);
201: // }
202: Collections.sort(coldMixed, mixComparator);
203: // for (JButton cold : coldMixed) {
204: // cold.setText("" + colorDistances.get(cold));
205: // this.add(cold);
206: // }
207:
208: this .brightButtons = new JPanel(new FlowLayout());
209: this .coldButtons = new JPanel(new FlowLayout());
210:
211: JPanel controlPanel = new JPanel(new FlowLayout(
212: FlowLayout.RIGHT));
213: JButton nextPage = new JButton("Next " + this .pageSize);
214: JButton prevPage = new JButton("Prev " + this .pageSize);
215:
216: nextPage.addActionListener(new ActionListener() {
217: public void actionPerformed(ActionEvent e) {
218: pageNumber++;
219: syncPage();
220: }
221: });
222:
223: prevPage.addActionListener(new ActionListener() {
224: public void actionPerformed(ActionEvent e) {
225: pageNumber--;
226: syncPage();
227: }
228: });
229:
230: this .pageNumber = 0;
231: syncPage();
232:
233: controlPanel.add(prevPage);
234: controlPanel.add(nextPage);
235:
236: this .add(controlPanel, BorderLayout.SOUTH);
237:
238: JPanel mainPanel = new JPanel(new GridLayout(1, 2));
239: mainPanel.add(this .brightButtons);
240: mainPanel.add(this .coldButtons);
241:
242: this .add(mainPanel, BorderLayout.CENTER);
243:
244: JPanel pillButtonPanel = new JPanel();
245: JPanel pillButtonPanelTop = new JPanel(new FlowLayout(
246: FlowLayout.LEFT));
247: JPanel pillButtonPanelBottom = new JPanel(new FlowLayout(
248: FlowLayout.LEFT));
249: // pillButtonPanel.setLayout(new BoxLayout(pillButtonPanel,
250: // BoxLayout.Y_AXIS));
251: pillButtonPanel.setLayout(new GridLayout(2, 1));
252: pillButtonPanel.add(pillButtonPanelTop);
253: pillButtonPanel.add(pillButtonPanelBottom);
254:
255: pillButtonPanel.setBorder(new TitledBorder("Presets"));
256:
257: // pillButtonPanel.add(Box.createVerticalStrut(3));
258: pillButtonPanelTop.add(Check.getPill(new SubstanceAquaTheme(),
259: new SubstanceLightAquaTheme()));
260: // pillButtonPanel.add(Box.createVerticalStrut(2));
261: pillButtonPanelTop.add(Check.getPill(new SubstanceAquaTheme(),
262: new SubstanceBottleGreenTheme()));
263: // pillButtonPanel.add(Box.createVerticalStrut(2));
264: pillButtonPanelTop.add(Check.getPill(
265: new SubstanceBarbyPinkTheme(),
266: new SubstanceRaspberryTheme()));
267: // pillButtonPanel.add(Box.createVerticalStrut(2));
268: pillButtonPanelTop.add(Check.getPill(
269: new SubstanceBottleGreenTheme(),
270: new SubstanceLimeGreenTheme()));
271: // pillButtonPanel.add(Box.createVerticalStrut(2));
272: pillButtonPanelTop.add(Check.getPill(new SubstanceBrownTheme(),
273: new SubstanceSunGlareTheme()));
274: // pillButtonPanel.add(Box.createVerticalStrut(2));
275: pillButtonPanelTop
276: .add(Check.getPill(new SubstanceSunsetTheme(),
277: new SubstanceOrangeTheme()));
278: // pillButtonPanel.add(Box.createVerticalStrut(2));
279: pillButtonPanelTop.add(Check.getPill(
280: new SubstanceLightAquaTheme(),
281: new SubstancePurpleTheme()));
282: // pillButtonPanel.add(Box.createVerticalStrut(2));
283: pillButtonPanelTop.add(Check.getPill(new SubstanceBrownTheme(),
284: new SubstanceTerracottaTheme()));
285: // pillButtonPanel.add(Box.createVerticalStrut(2));
286:
287: pillButtonPanelBottom.add(Check.getMultiPill(
288: new SubstanceAquaTheme(),
289: new SubstanceLimeGreenTheme(),
290: new SubstanceBottleGreenTheme()));
291: // pillButtonPanel.add(Box.createVerticalStrut(2));
292: pillButtonPanelBottom.add(Check.getMultiPill(
293: new SubstanceBarbyPinkTheme(),
294: new SubstancePurpleTheme(),
295: new SubstanceRaspberryTheme()));
296: // pillButtonPanel.add(Box.createVerticalStrut(2));
297: pillButtonPanelBottom
298: .add(Check.getMultiPill(new SubstanceRaspberryTheme(),
299: new SubstanceSunsetTheme(),
300: new SubstanceOrangeTheme()));
301: // pillButtonPanel.add(Box.createVerticalStrut(2));
302: pillButtonPanelBottom.add(Check.getMultiPill(
303: new SubstanceBrownTheme(),
304: new SubstanceSunGlareTheme(),
305: new SubstanceOrangeTheme()));
306: // pillButtonPanel.add(Box.createVerticalStrut(2));
307: pillButtonPanelBottom.add(Check.getMultiPill(
308: new SubstanceAquaTheme(),
309: new SubstanceLightAquaTheme(),
310: new SubstancePurpleTheme(),
311: new SubstanceBarbyPinkTheme()));
312: // pillButtonPanel.add(Box.createVerticalStrut(2));
313: pillButtonPanelBottom
314: .add(Check.getMultiPill(new SubstanceBrownTheme(),
315: new SubstanceTerracottaTheme(),
316: new SubstanceSunGlareTheme(),
317: new SubstanceOrangeTheme(),
318: new SubstanceSunsetTheme()));
319:
320: this .add(pillButtonPanel, BorderLayout.NORTH);
321: }
322:
323: /**
324: * Synchronizes panel contents.
325: */
326: protected void syncPage() {
327: int startIndex = this .pageNumber * this .pageSize;
328: int endIndex = startIndex + this .pageSize;
329:
330: this .brightButtons.removeAll();
331: this .coldButtons.removeAll();
332:
333: for (int i = startIndex; i < endIndex; i++) {
334: if ((i >= 0) && (i < this .brightMixed.size())) {
335: JButton bright = brightMixed.get(i);
336: bright.setText("" + colorDistances.get(bright));
337: this .brightButtons.add(bright);
338: }
339: if ((i >= 0) && (i < this .coldMixed.size())) {
340: JButton cold = coldMixed.get(i);
341: cold.setText("" + colorDistances.get(cold));
342: this .coldButtons.add(cold);
343: }
344: }
345: this .revalidate();
346: this .repaint();
347: }
348:
349: /*
350: * (non-Javadoc)
351: *
352: * @see javax.swing.JComponent#isOpaque()
353: */
354: @Override
355: public boolean isOpaque() {
356: return false;
357: }
358:
359: /*
360: * (non-Javadoc)
361: *
362: * @see javax.swing.JComponent#paint(java.awt.Graphics)
363: */
364: @Override
365: public void paint(Graphics g) {
366: long time0 = System.currentTimeMillis();
367: super .paint(g);
368: long time1 = System.currentTimeMillis();
369: Check.out("Time to paint mixed is " + (time1 - time0));
370: }
371: }
|