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.beans.PropertyVetoException;
036: import java.util.*;
037:
038: import javax.swing.*;
039: import javax.swing.JInternalFrame.JDesktopIcon;
040: import javax.swing.plaf.basic.BasicInternalFrameUI;
041:
042: import org.jvnet.substance.SubstanceImageCreator;
043: import org.jvnet.substance.SubstanceLookAndFeel;
044: import org.jvnet.substance.theme.*;
045: import org.jvnet.substance.theme.SubstanceTheme.ThemeKind;
046: import org.jvnet.substance.watermark.WatermarkInfo;
047:
048: import com.jgoodies.forms.builder.DefaultFormBuilder;
049: import com.jgoodies.forms.layout.FormLayout;
050:
051: /**
052: * Test application panel for testing {@link JDesktopPane},
053: * {@link JInternalFrame} and {@link JDesktopIcon} components.
054: *
055: * @author Kirill Grouchnikov
056: */
057: public class DesktopPanel extends ControllablePanel {
058: /**
059: * The desktop pane.
060: */
061: private JDesktopPane jdp;
062:
063: /**
064: * Counter for creating the internal frames.
065: */
066: private int count = 0;
067:
068: /**
069: * A set of disposed internal frames.
070: */
071: private Set<JInternalFrame> disposed = new HashSet<JInternalFrame>();
072:
073: /**
074: * Creates the desktop panel.
075: */
076: public DesktopPanel() {
077: this .setLayout(new BorderLayout());
078: jdp = new JDesktopPane();
079: this .add(jdp, BorderLayout.CENTER);
080:
081: FormLayout lm = new FormLayout(
082: "right:pref, 4dlu, fill:pref:grow", "");
083: DefaultFormBuilder builder = new DefaultFormBuilder(lm,
084: new ScrollablePanel());
085: builder.appendSeparator("New sample frame");
086:
087: JButton sample = new JButton("Add");
088: sample.addActionListener(new ActionListener() {
089: public void actionPerformed(ActionEvent e) {
090: SwingUtilities.invokeLater(new Runnable() {
091: public void run() {
092: JInternalFrame jif = new SampleInternalFrame() {
093: // @Override
094: // public void paint(Graphics g) {
095: // long start = System.nanoTime();
096: // super.paint(g);
097: // long end = System.nanoTime();
098: // System.err.println("paint done in " + (end -
099: // start));
100: // }
101: };
102: jif.setBounds(0, 0, 300, 200);
103: jdp.add(jif);
104: jif.setVisible(true);
105: jif.setComponentOrientation(jdp
106: .getComponentOrientation());
107: }
108: });
109: }
110: });
111: builder.append("Add sample", sample);
112:
113: builder.appendSeparator("New custom frame");
114: final JCheckBox makeZero = new JCheckBox("Has zero bounds");
115: builder.append("Custom settings", makeZero);
116:
117: final JCheckBox isClosable = new JCheckBox("Is closable");
118: isClosable.setSelected(true);
119: builder.append("", isClosable);
120:
121: final JCheckBox isMaximizable = new JCheckBox("Is maximizable");
122: isMaximizable.setSelected(true);
123: builder.append("", isMaximizable);
124:
125: final JCheckBox isIconifiable = new JCheckBox("Is iconifiable");
126: isIconifiable.setSelected(true);
127: builder.append("", isIconifiable);
128:
129: final JCheckBox isResizable = new JCheckBox("Is resizable");
130: isResizable.setSelected(true);
131: builder.append("", isResizable);
132:
133: JButton bt = new JButton("Add");
134: bt.addActionListener(new ActionListener() {
135: public void actionPerformed(ActionEvent e) {
136: String title = "[" + count + "]Internal title ";
137: int c = (int) (20 * Math.random());
138: for (int i = 0; i < c; i++) {
139: title += "0";
140: }
141: final JInternalFrame jif = new JInternalFrame(title);
142: // {
143: // @Override
144: // public void paint(Graphics g) {
145: // long start = System.nanoTime();
146: // super.paint(g);
147: // long end = System.nanoTime();
148: // System.err.println("paint done in " + (end - start));
149: // }
150: //
151: // @Override
152: // public void setLocation(int x, int y) {
153: // super.setLocation(x, y);
154: // }
155: //
156: // @Override
157: // public void setSize(int width, int height) {
158: // super.setSize(width, height);
159: // }
160: //
161: // @Override
162: // public void setBounds(int x, int y, int width, int height) {
163: // super.setBounds(x, y, width, height);
164: // }
165: //
166: // };
167: jif.setFrameIcon(new ImageIcon(SubstanceImageCreator
168: .getBigHexaMarker(count, SubstanceLookAndFeel
169: .getTheme())));
170: SubstanceLookAndFeel
171: .registerThemeChangeListener(new ThemeChangeListener() {
172: public void themeChanged() {
173: jif
174: .setFrameIcon(new ImageIcon(
175: SubstanceImageCreator
176: .getBigHexaMarker(
177: count,
178: SubstanceLookAndFeel
179: .getTheme())));
180: }
181: });
182: jif.setLayout(new BorderLayout());
183: JPanel controls = new JPanel(new FlowLayout());
184: int comps = 5 + (int) (10 * Math.random());
185: for (int i = 0; i < comps; i++) {
186: double r = Math.random();
187: if (r < 0.1) {
188: controls.add(new JButton("button" + i));
189: } else {
190: if (r < 0.2) {
191: controls.add(new JLabel("label" + i));
192: } else {
193: if (r < 0.3) {
194: controls.add(new JRadioButton("radio"
195: + i));
196: } else {
197: if (r < 0.4) {
198: controls.add(new JCheckBox("check"
199: + i));
200: } else {
201: if (r < 0.5) {
202: controls.add(new JToggleButton(
203: "toggle" + i));
204: } else {
205: if (r < 0.6) {
206: controls
207: .add(new JComboBox(
208: new Object[] { "combo"
209: + i }));
210: } else {
211: if (r < 0.7) {
212: controls
213: .add(new JTextField(
214: "text field"
215: + i));
216: } else {
217: if (r < 0.8) {
218: controls
219: .add(new JPasswordField(
220: "password"
221: + i));
222: } else {
223: if (r < 0.9) {
224: controls
225: .add(new JSpinner());
226: } else {
227: controls
228: .add(new JList(
229: new Object[] { "list"
230: + i }));
231: }
232: }
233: }
234: }
235: }
236: }
237: }
238: }
239: }
240: }
241:
242: jif.add(controls, BorderLayout.CENTER);
243: jif.setClosable(isClosable.isSelected());
244: jif.setMaximizable(isMaximizable.isSelected());
245: jif.setIconifiable(isIconifiable.isSelected());
246: jif.setResizable(isResizable.isSelected());
247:
248: JPanel buttons = new JPanel(new FlowLayout(
249: FlowLayout.RIGHT));
250: final JCheckBox isModified = new JCheckBox("modified");
251: isModified.addActionListener(new ActionListener() {
252: public void actionPerformed(ActionEvent e) {
253: jif.putClientProperty(
254: SubstanceLookAndFeel.WINDOW_MODIFIED,
255: Boolean
256: .valueOf(isModified
257: .isSelected()));
258: }
259: });
260: buttons.add(isModified);
261:
262: JButton changeTitleButton = new JButton("Change title");
263: changeTitleButton
264: .addActionListener(new ActionListener() {
265: public void actionPerformed(ActionEvent e) {
266: String random = "abcdefghijklmnopqrstuvwxyz ";
267: int length = (int) (50 * Math.random());
268: String title = "";
269: while (length > 0) {
270: title += random
271: .charAt((int) (random
272: .length() * Math
273: .random()));
274: length--;
275: }
276: jif.setTitle(title);
277: }
278: });
279: buttons.add(changeTitleButton);
280:
281: JButton setNullTitlePane = new JButton(
282: "Remove title pane");
283: setNullTitlePane
284: .addActionListener(new ActionListener() {
285: public void actionPerformed(ActionEvent e) {
286: SwingUtilities
287: .invokeLater(new Runnable() {
288: public void run() {
289: ((BasicInternalFrameUI) jif
290: .getUI())
291: .setNorthPane(null);
292: jif.revalidate();
293: }
294: });
295: }
296: });
297: buttons.add(setNullTitlePane);
298:
299: jif.add(buttons, BorderLayout.SOUTH);
300:
301: count++;
302: int width = 50 + (int) (400 * Math.random());
303: int height = 50 + (int) (200 * Math.random());
304: jif.setBounds(20 * (count % 10), 20 * (count % 10),
305: width, height);
306: if (makeZero.isSelected())
307: jif.setBounds(0, 0, 0, 0);
308: jif.setBackground(new Color(128 + (int) (128 * Math
309: .random()), 128 + (int) (128 * Math.random()),
310: 128 + (int) (128 * Math.random())));
311: // jif.setClosable(true);
312: // jif.setMaximizable(true);
313: // jif.setResizable(true);
314: // jif.setIconifiable(true);
315: jif.setComponentOrientation(jdp
316: .getComponentOrientation());
317: jdp.add(jif, 1);
318:
319: JMenuBar jmb = new JMenuBar();
320:
321: if (UIManager.getLookAndFeel() instanceof SubstanceLookAndFeel) {
322: Map<String, ThemeInfo> allThemes = SubstanceLookAndFeel
323: .getAllThemes();
324:
325: JMenu jmTheme = new JMenu("Theme");
326: JMenu jmInvertedTheme = SubstanceLookAndFeel
327: .toEnableInvertedThemes() ? new JMenu(
328: "Inverted theme") : null;
329:
330: ButtonGroup bgTheme = new ButtonGroup();
331: for (Map.Entry<String, ThemeInfo> themeEntry : allThemes
332: .entrySet()) {
333: String themeName = themeEntry.getKey();
334: final ThemeInfo themeInfo = themeEntry
335: .getValue();
336: final String themeClassName = themeInfo
337: .getClassName();
338:
339: JRadioButtonMenuItem jmiTheme = new JRadioButtonMenuItem(
340: themeName);
341: try {
342: Class<?> themeClass = Class
343: .forName(themeClassName);
344: SubstanceTheme theme = (SubstanceTheme) themeClass
345: .newInstance();
346: if (themeInfo.getThemeKind() == ThemeKind.INVERTED)
347: theme = new SubstanceInvertedTheme(
348: theme);
349: jmiTheme.setIcon(SubstanceImageCreator
350: .getThemeIcon(theme));
351: } catch (Exception exc) {
352: continue;
353: }
354:
355: jmiTheme
356: .addActionListener(new ActionListener() {
357: public void actionPerformed(
358: ActionEvent e) {
359: try {
360: if ((themeInfo
361: .getThemeKind() == ThemeKind.INVERTED)
362: || (themeInfo
363: .getThemeKind() == ThemeKind.NEGATED)) {
364: SubstanceLookAndFeel
365: .setCurrentTheme(themeInfo);
366: } else {
367: SubstanceLookAndFeel
368: .setCurrentTheme(themeClassName);
369: }
370: // UIManager
371: // .setLookAndFeel(new
372: // SubstanceLookAndFeel());
373: } catch (Exception exc) {
374: exc.printStackTrace();
375: }
376: for (Frame frame : Frame
377: .getFrames())
378: SwingUtilities
379: .updateComponentTreeUI(frame);
380: }
381: });
382: if (themeName.equals(SubstanceLookAndFeel
383: .getCurrentThemeName())) {
384: jmiTheme.setSelected(true);
385: }
386: bgTheme.add(jmiTheme);
387:
388: if (themeEntry.getValue().getThemeKind() == ThemeKind.INVERTED)
389: jmInvertedTheme.add(jmiTheme);
390: else
391: jmTheme.add(jmiTheme);
392: }
393:
394: jmb.add(jmTheme);
395: if (jmInvertedTheme != null)
396: jmb.add(jmInvertedTheme);
397:
398: JMenu jmWatermark = new JMenu("Watermark");
399:
400: ButtonGroup bgWatermark = new ButtonGroup();
401: Map<String, WatermarkInfo> allWatermarks = SubstanceLookAndFeel
402: .getAllWatermarks();
403: for (Map.Entry<String, WatermarkInfo> watermarkEntry : allWatermarks
404: .entrySet()) {
405: String watermarkName = watermarkEntry.getKey();
406: final String watermarkClassName = watermarkEntry
407: .getValue().getClassName();
408:
409: JRadioButtonMenuItem jmiWatermark = new JRadioButtonMenuItem(
410: watermarkName);
411: jmiWatermark
412: .addActionListener(new ActionListener() {
413: public void actionPerformed(
414: ActionEvent e) {
415: SubstanceLookAndFeel
416: .setCurrentWatermark(watermarkClassName);
417: for (Frame frame : Frame
418: .getFrames())
419: SwingUtilities
420: .updateComponentTreeUI(frame);
421: }
422: });
423: if (watermarkName.equals(SubstanceLookAndFeel
424: .getCurrentWatermarkName())) {
425: jmiWatermark.setSelected(true);
426: }
427: bgWatermark.add(jmiWatermark);
428: jmWatermark.add(jmiWatermark);
429: }
430:
431: jmWatermark.addSeparator();
432: }
433:
434: JMenu jm1 = new JMenu("Menu1");
435: jm1.setMnemonic('1');
436: int mcount = 0;
437: for (LinkedList<JMenuItem> miList : SampleMenuFactory
438: .getTestMenuItems()) {
439: if (mcount > 0) {
440: if (mcount % 2 == 0)
441: jm1.addSeparator();
442: else
443: jm1.add(new JSeparator());
444: }
445: for (JMenuItem menuItem : miList) {
446: jm1.add(menuItem);
447: }
448: mcount++;
449: }
450: jmb.add(jm1);
451: jif.setJMenuBar(jmb);
452:
453: jif.show();
454: }
455: });
456:
457: builder.append("Add custom", bt);
458:
459: builder.appendSeparator("Operations");
460:
461: JButton minAll = new JButton("Execute");
462: minAll.addActionListener(new ActionListener() {
463: public void actionPerformed(ActionEvent e) {
464: for (JInternalFrame jif : jdp.getAllFrames()) {
465: try {
466: jif.setIcon(true);
467: } catch (PropertyVetoException pve) {
468: }
469: }
470: }
471: });
472: builder.append("Minimize all", minAll);
473:
474: JButton closeAll = new JButton("Execute");
475: closeAll.addActionListener(new ActionListener() {
476: public void actionPerformed(ActionEvent e) {
477: for (JInternalFrame jif : jdp.getAllFrames()) {
478: try {
479: jif.setClosed(true);
480: } catch (PropertyVetoException pve) {
481: }
482: }
483: }
484: });
485: builder.append("Close all", closeAll);
486:
487: JButton disposeAll = new JButton("Execute");
488: disposeAll.addActionListener(new ActionListener() {
489: public void actionPerformed(ActionEvent e) {
490: for (JInternalFrame jif : jdp.getAllFrames()) {
491: disposed.add(jif);
492: jif.dispose();
493: }
494: }
495: });
496: builder.append("Dispose all", disposeAll);
497:
498: JButton reshowAll = new JButton("Execute");
499: reshowAll.addActionListener(new ActionListener() {
500: public void actionPerformed(ActionEvent e) {
501: for (JInternalFrame jif : disposed) {
502: jif.setVisible(true);
503: }
504: }
505: });
506: builder.append("Reshow all", reshowAll);
507:
508: this .controlPanel = builder.getPanel();
509:
510: this .setPreferredSize(new Dimension(400, 400));
511: this.setSize(this.getPreferredSize());
512: this.setMinimumSize(this.getPreferredSize());
513: }
514: }
|