0001: /*
0002: * Copyright (c) 2005-2008 Substance Kirill Grouchnikov. All Rights Reserved.
0003: *
0004: * Redistribution and use in source and binary forms, with or without
0005: * modification, are permitted provided that the following conditions are met:
0006: *
0007: * o Redistributions of source code must retain the above copyright notice,
0008: * this list of conditions and the following disclaimer.
0009: *
0010: * o Redistributions in binary form must reproduce the above copyright notice,
0011: * this list of conditions and the following disclaimer in the documentation
0012: * and/or other materials provided with the distribution.
0013: *
0014: * o Neither the name of Substance Kirill Grouchnikov nor the names of
0015: * its contributors may be used to endorse or promote products derived
0016: * from this software without specific prior written permission.
0017: *
0018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
0025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
0026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
0027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
0028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029: */
0030: package test.check;
0031:
0032: import java.awt.*;
0033: import java.awt.event.*;
0034: import java.lang.ref.*;
0035: import java.util.Locale;
0036:
0037: import javax.swing.*;
0038: import javax.swing.event.ChangeEvent;
0039: import javax.swing.event.ChangeListener;
0040:
0041: import org.jvnet.lafwidget.LafWidget;
0042: import org.jvnet.lafwidget.LafWidgetUtilities2;
0043: import org.jvnet.lafwidget.animation.FadeConfigurationManager;
0044: import org.jvnet.lafwidget.animation.FadeKind;
0045: import org.jvnet.lafwidget.preview.DefaultPreviewPainter;
0046: import org.jvnet.lafwidget.utils.LafConstants.TabOverviewKind;
0047: import org.jvnet.substance.SubstanceDefaultComboBoxRenderer;
0048: import org.jvnet.substance.SubstanceLookAndFeel;
0049: import org.jvnet.substance.painter.AlphaControlBackgroundComposite;
0050: import org.jvnet.substance.utils.SubstanceCoreUtilities;
0051: import org.jvnet.substance.utils.SubstanceConstants.FocusKind;
0052: import org.jvnet.substance.utils.SubstanceConstants.MenuGutterFillKind;
0053:
0054: import test.Check;
0055: import test.Check.MyMainTabPreviewPainter;
0056:
0057: import com.jgoodies.forms.builder.DefaultFormBuilder;
0058: import com.jgoodies.forms.layout.FormLayout;
0059:
0060: /**
0061: * Factory for creating the global control panels (for global settings and
0062: * testing the dialogs).
0063: *
0064: * @author Kirill Grouchnikov
0065: */
0066: public class ControlPanelFactory {
0067: /**
0068: * Instance of a simple dialog.
0069: */
0070: private static SimpleDialog simpleDialog;
0071:
0072: /**
0073: * Instance of a disposable dialog.
0074: */
0075: private static JDialog disposableDialog;
0076:
0077: /**
0078: * Returns the main control panel.
0079: *
0080: * @param mainFrame
0081: * The main test frame.
0082: * @param mainTabbedPane
0083: * The main tabbed pane.
0084: * @param mainTabPreviewPainter
0085: * The preview painter of the main tabbed pane.
0086: * @param toolbar
0087: * The toolbar of the main test frame.
0088: * @return The main control panel.
0089: */
0090: public static JPanel getMainControlPanel(final JFrame mainFrame,
0091: final JTabbedPane mainTabbedPane,
0092: final MyMainTabPreviewPainter mainTabPreviewPainter,
0093: final JToolBar toolbar) {
0094: FormLayout lm = new FormLayout(
0095: "right:pref, 4dlu, fill:pref:grow", "");
0096: DefaultFormBuilder builder = new DefaultFormBuilder(lm);
0097: // builder.setDefaultDialogBorder();
0098:
0099: builder.appendSeparator("Title pane settings");
0100: final JCheckBox markAsModified = new JCheckBox(
0101: "Marked modified");
0102: markAsModified.setSelected(false);
0103: markAsModified.addActionListener(new ActionListener() {
0104: public void actionPerformed(ActionEvent e) {
0105: mainFrame.getRootPane().putClientProperty(
0106: SubstanceLookAndFeel.WINDOW_MODIFIED,
0107: (markAsModified.isSelected() ? Boolean.TRUE
0108: : false));
0109: }
0110: });
0111: builder.append("Modified", markAsModified);
0112:
0113: final JCheckBox heapPanel = new JCheckBox("Has heap panel");
0114: heapPanel.setSelected(true);
0115: heapPanel.addActionListener(new ActionListener() {
0116: public void actionPerformed(ActionEvent e) {
0117: if (heapPanel.isSelected())
0118: SubstanceLookAndFeel
0119: .permanentlyShowHeapStatusPanel(mainFrame
0120: .getRootPane());
0121: else
0122: SubstanceLookAndFeel
0123: .permanentlyHideHeapStatusPanel(mainFrame
0124: .getRootPane());
0125: }
0126: });
0127: builder.append("Heap panel", heapPanel);
0128:
0129: JButton changeTitleButton = new JButton("Change");
0130: changeTitleButton.addActionListener(new ActionListener() {
0131: public void actionPerformed(ActionEvent e) {
0132: String random = "abcdefghijklmnopqrstuvwxyz ";
0133: int length = 60 + (int) (150 * Math.random());
0134: String title = "";
0135: while (length > 0) {
0136: title += random
0137: .charAt((int) (random.length() * Math
0138: .random()));
0139: length--;
0140: }
0141: mainFrame.setTitle(title);
0142: }
0143: });
0144: builder.append("Title string", changeTitleButton);
0145:
0146: builder.appendSeparator("Miscellaneous");
0147:
0148: final JCheckBox useThemedDefaultIconsCheckBox = new JCheckBox(
0149: "use themed icons");
0150: useThemedDefaultIconsCheckBox
0151: .setSelected(SubstanceCoreUtilities
0152: .useThemedDefaultIcon(null));
0153: useThemedDefaultIconsCheckBox
0154: .addActionListener(new ActionListener() {
0155: public void actionPerformed(ActionEvent e) {
0156: SwingUtilities.invokeLater(new Runnable() {
0157: public void run() {
0158: UIManager
0159: .put(
0160: SubstanceLookAndFeel.USE_THEMED_DEFAULT_ICONS,
0161: useThemedDefaultIconsCheckBox
0162: .isSelected() ? Boolean.TRUE
0163: : null);
0164: mainFrame.repaint();
0165: }
0166: });
0167: }
0168: });
0169: builder.append("Themed icons", useThemedDefaultIconsCheckBox);
0170:
0171: final JCheckBox useConstantThemesOnDialogs = new JCheckBox(
0172: "use constant themes");
0173: useConstantThemesOnDialogs.setSelected(SubstanceLookAndFeel
0174: .isToUseConstantThemesOnDialogs());
0175: useConstantThemesOnDialogs
0176: .addActionListener(new ActionListener() {
0177: public void actionPerformed(ActionEvent e) {
0178: SwingUtilities.invokeLater(new Runnable() {
0179: public void run() {
0180: SubstanceLookAndFeel
0181: .setToUseConstantThemesOnDialogs(useConstantThemesOnDialogs
0182: .isSelected());
0183: try {
0184: UIManager
0185: .setLookAndFeel(new SubstanceLookAndFeel());
0186: for (Frame frame : Frame
0187: .getFrames())
0188: SwingUtilities
0189: .updateComponentTreeUI(frame);
0190: } catch (Throwable t) {
0191: }
0192: }
0193: });
0194: }
0195: });
0196: builder.append("Option pane icons", useConstantThemesOnDialogs);
0197:
0198: final JComboBox placementCombo = new JComboBox(new Object[] {
0199: "top", "bottom", "left", "right" });
0200: placementCombo.addActionListener(new ActionListener() {
0201: public void actionPerformed(ActionEvent e) {
0202: String selected = (String) placementCombo
0203: .getSelectedItem();
0204: if ("top".equals(selected))
0205: mainTabbedPane.setTabPlacement(JTabbedPane.TOP);
0206: if ("bottom".equals(selected))
0207: mainTabbedPane.setTabPlacement(JTabbedPane.BOTTOM);
0208: if ("left".equals(selected))
0209: mainTabbedPane.setTabPlacement(JTabbedPane.LEFT);
0210: if ("right".equals(selected))
0211: mainTabbedPane.setTabPlacement(JTabbedPane.RIGHT);
0212: }
0213: });
0214: builder.append("Placement", placementCombo);
0215:
0216: try {
0217: final JComboBox overviewKindCombo = new JComboBox(
0218: new Object[] { TabOverviewKind.GRID,
0219: TabOverviewKind.MENU_CAROUSEL,
0220: TabOverviewKind.ROUND_CAROUSEL });
0221: overviewKindCombo.setSelectedItem(LafWidgetUtilities2
0222: .getTabPreviewPainter(mainTabbedPane)
0223: .getOverviewKind(mainTabbedPane));
0224: overviewKindCombo.addActionListener(new ActionListener() {
0225: public void actionPerformed(ActionEvent e) {
0226: mainTabPreviewPainter
0227: .setTabOverviewKind((TabOverviewKind) overviewKindCombo
0228: .getSelectedItem());
0229: }
0230: });
0231: overviewKindCombo
0232: .setRenderer(new SubstanceDefaultComboBoxRenderer(
0233: overviewKindCombo) {
0234: @Override
0235: public Component getListCellRendererComponent(
0236: JList list, Object value, int index,
0237: boolean isSelected, boolean cellHasFocus) {
0238: TabOverviewKind kind = (TabOverviewKind) value;
0239: return super .getListCellRendererComponent(
0240: list, kind.getName(), index,
0241: isSelected, cellHasFocus);
0242: }
0243: });
0244: builder.append("Overview kind", overviewKindCombo);
0245: } catch (NoClassDefFoundError ncdfe) {
0246: }
0247:
0248: builder.append("Composite", new BackgroundCompositeCombo(
0249: mainTabbedPane));
0250:
0251: final JComboBox menuGutterFillCombo = new JComboBox(
0252: new Object[] { MenuGutterFillKind.NONE,
0253: MenuGutterFillKind.SOFT,
0254: MenuGutterFillKind.HARD,
0255: MenuGutterFillKind.SOFT_FILL,
0256: MenuGutterFillKind.HARD_FILL });
0257: menuGutterFillCombo
0258: .setRenderer(new SubstanceDefaultComboBoxRenderer(
0259: menuGutterFillCombo) {
0260: @Override
0261: public Component getListCellRendererComponent(
0262: JList list, Object value, int index,
0263: boolean isSelected, boolean cellHasFocus) {
0264: MenuGutterFillKind mgfk = (MenuGutterFillKind) value;
0265: return super .getListCellRendererComponent(list,
0266: mgfk.name().toLowerCase(), index,
0267: isSelected, cellHasFocus);
0268: }
0269: });
0270: menuGutterFillCombo.setSelectedItem(SubstanceCoreUtilities
0271: .getMenuGutterFillKind());
0272: menuGutterFillCombo.addActionListener(new ActionListener() {
0273: public void actionPerformed(ActionEvent e) {
0274: UIManager.put(
0275: SubstanceLookAndFeel.MENU_GUTTER_FILL_KIND,
0276: menuGutterFillCombo.getSelectedItem());
0277: }
0278: });
0279: builder.append("Menu fill", menuGutterFillCombo);
0280:
0281: final JComboBox focusKindCombo = new JComboBox(FocusKind
0282: .values());
0283: focusKindCombo
0284: .setRenderer(new SubstanceDefaultComboBoxRenderer(
0285: focusKindCombo) {
0286: @Override
0287: public Component getListCellRendererComponent(
0288: JList list, Object value, int index,
0289: boolean isSelected, boolean cellHasFocus) {
0290: FocusKind focusKind = (FocusKind) value;
0291: return super .getListCellRendererComponent(list,
0292: focusKind.name().toLowerCase(), index,
0293: isSelected, cellHasFocus);
0294: }
0295: });
0296: focusKindCombo.setSelectedItem(SubstanceCoreUtilities
0297: .getFocusKind(null));
0298: focusKindCombo.addActionListener(new ActionListener() {
0299: public void actionPerformed(ActionEvent e) {
0300: UIManager.put(SubstanceLookAndFeel.FOCUS_KIND,
0301: focusKindCombo.getSelectedItem());
0302: }
0303: });
0304: builder.append("Focus kind", focusKindCombo);
0305:
0306: // final JCheckBox fullScreenMode = new JCheckBox("Full screen mode");
0307: // fullScreenMode.setSelected(false);
0308: // fullScreenMode.addActionListener(new ActionListener() {
0309: // public void actionPerformed(ActionEvent e) {
0310: // boolean isDisp = mainFrame.isDisplayable();
0311: // if (isDisp)
0312: // mainFrame.dispose();
0313: // GraphicsDevice device = GraphicsEnvironment
0314: // .getLocalGraphicsEnvironment().getDefaultScreenDevice();
0315: // if (fullScreenMode.isSelected()) {
0316: // if (device.isFullScreenSupported()) {
0317: // JFrame.setDefaultLookAndFeelDecorated(false);
0318: // mainFrame.setUndecorated(true);
0319: // mainFrame.setResizable(false);
0320: // device.setFullScreenWindow(mainFrame);
0321: // }
0322: // } else {
0323: // JFrame.setDefaultLookAndFeelDecorated(true);
0324: // device.setFullScreenWindow(null);
0325: // mainFrame.setUndecorated(false);
0326: // mainFrame.setResizable(true);
0327: // }
0328: // if (isDisp)
0329: // mainFrame.setVisible(true);
0330: // }
0331: // });
0332: // builder.append("Screen mode", fullScreenMode);
0333:
0334: JButton buttonGlassPane = new JButton("Show");
0335: buttonGlassPane.addActionListener(new ActionListener() {
0336: public void actionPerformed(ActionEvent e) {
0337: final JPanel glassPane = new JPanel() {
0338: @Override
0339: public void paintComponent(Graphics g) {
0340: super .paintComponent(g);
0341: Graphics2D graphics = (Graphics2D) g;
0342: int height = getHeight();
0343: int width = getWidth();
0344: Composite c = AlphaComposite.getInstance(
0345: AlphaComposite.SRC_OVER, (float) 0.4);
0346: graphics.setComposite(c);
0347: for (int i = 0; i < height; i++) {
0348: Color color = (i % 2 == 0) ? new Color(200,
0349: 200, 255)
0350: : new Color(230, 230, 255);
0351: graphics.setColor(color);
0352: graphics.drawLine(0, i, width, i);
0353: }
0354: Composite c2 = AlphaComposite.getInstance(
0355: AlphaComposite.SRC_OVER, (float) 1.0);
0356: graphics.setComposite(c2);
0357: }
0358: };
0359: glassPane.setOpaque(false);
0360: glassPane.addMouseListener(new MouseAdapter() {
0361: });
0362: glassPane.addKeyListener(new KeyAdapter() {
0363: });
0364: mainFrame.setGlassPane(glassPane);
0365: new Thread() {
0366: @Override
0367: public void run() {
0368: glassPane.setVisible(true);
0369: try {
0370: Thread.sleep(5000);
0371: } catch (InterruptedException ie) {
0372: ie.printStackTrace();
0373: }
0374: glassPane.setVisible(false);
0375: }
0376: }.start();
0377: }
0378: });
0379: builder.append("Glass pane", buttonGlassPane);
0380:
0381: builder.appendSeparator("Custom animations");
0382: final JCheckBox allowFocusLoopAnimations = new JCheckBox(
0383: "Allow animation");
0384: allowFocusLoopAnimations.setSelected(FadeConfigurationManager
0385: .getInstance().fadeAllowed(
0386: FadeKind.FOCUS_LOOP_ANIMATION, null));
0387: allowFocusLoopAnimations
0388: .addActionListener(new ActionListener() {
0389: public void actionPerformed(ActionEvent e) {
0390: if (allowFocusLoopAnimations.isSelected()) {
0391: FadeConfigurationManager
0392: .getInstance()
0393: .allowFades(
0394: FadeKind.FOCUS_LOOP_ANIMATION);
0395: } else {
0396: FadeConfigurationManager
0397: .getInstance()
0398: .disallowFades(
0399: FadeKind.FOCUS_LOOP_ANIMATION);
0400: }
0401: }
0402: });
0403: builder.append("Focus loop", allowFocusLoopAnimations);
0404:
0405: final JCheckBox allowGlowIconAnimations = new JCheckBox(
0406: "Allow animation");
0407: allowGlowIconAnimations.addActionListener(new ActionListener() {
0408: public void actionPerformed(ActionEvent e) {
0409: if (allowGlowIconAnimations.isSelected()) {
0410: FadeConfigurationManager.getInstance().allowFades(
0411: FadeKind.ICON_GLOW);
0412: } else {
0413: FadeConfigurationManager.getInstance()
0414: .disallowFades(FadeKind.ICON_GLOW);
0415: }
0416: }
0417: });
0418: builder.append("Icon glow", allowGlowIconAnimations);
0419:
0420: final JCheckBox allowGhostIconAnimations = new JCheckBox(
0421: "Allow animation");
0422: allowGhostIconAnimations
0423: .addActionListener(new ActionListener() {
0424: public void actionPerformed(ActionEvent e) {
0425: if (allowGhostIconAnimations.isSelected()) {
0426: FadeConfigurationManager
0427: .getInstance()
0428: .allowFades(
0429: FadeKind.GHOSTING_ICON_ROLLOVER);
0430: } else {
0431: FadeConfigurationManager
0432: .getInstance()
0433: .disallowFades(
0434: FadeKind.GHOSTING_ICON_ROLLOVER);
0435: }
0436: }
0437: });
0438: builder.append("Icon rollover", allowGhostIconAnimations);
0439:
0440: final JCheckBox allowGhostPressAnimations = new JCheckBox(
0441: "Allow animation");
0442: allowGhostPressAnimations
0443: .addActionListener(new ActionListener() {
0444: public void actionPerformed(ActionEvent e) {
0445: if (allowGhostPressAnimations.isSelected()) {
0446: FadeConfigurationManager
0447: .getInstance()
0448: .allowFades(
0449: FadeKind.GHOSTING_BUTTON_PRESS);
0450: } else {
0451: FadeConfigurationManager
0452: .getInstance()
0453: .disallowFades(
0454: FadeKind.GHOSTING_BUTTON_PRESS);
0455: }
0456: }
0457: });
0458: builder.append("Button press", allowGhostPressAnimations);
0459:
0460: builder.appendSeparator("Toolbar");
0461: final JCheckBox isToolbarFlat = new JCheckBox("Is flat");
0462: final JSlider translucencySlider = new JSlider(0, 100, 100);
0463: translucencySlider.setPreferredSize(new Dimension(50, 24));
0464:
0465: isToolbarFlat.setSelected(true);
0466: translucencySlider.setEnabled(false);
0467: isToolbarFlat.addActionListener(new ActionListener() {
0468: public void actionPerformed(ActionEvent e) {
0469: toolbar.putClientProperty(
0470: SubstanceLookAndFeel.FLAT_PROPERTY, Boolean
0471: .valueOf(isToolbarFlat.isSelected()));
0472: if (isToolbarFlat.isSelected()) {
0473: translucencySlider.setValue(100);
0474: translucencySlider.setEnabled(false);
0475: } else {
0476: translucencySlider.setEnabled(true);
0477: }
0478: toolbar.repaint();
0479: }
0480: });
0481: builder.append("Flat", isToolbarFlat);
0482:
0483: translucencySlider.addChangeListener(new ChangeListener() {
0484: public void stateChanged(ChangeEvent e) {
0485: toolbar.putClientProperty(
0486: SubstanceLookAndFeel.BACKGROUND_COMPOSITE,
0487: new AlphaControlBackgroundComposite(
0488: translucencySlider.getValue() / 100.f));
0489: toolbar.repaint();
0490: }
0491: });
0492: builder.append("Opacity", translucencySlider);
0493:
0494: builder.appendSeparator("Menu bar");
0495: final JCheckBox menuSearch = new JCheckBox("Has menu search");
0496: menuSearch.setSelected(true);
0497: menuSearch.addActionListener(new ActionListener() {
0498: public void actionPerformed(ActionEvent e) {
0499: if (menuSearch.isSelected())
0500: SubstanceLookAndFeel.showMenuSearchPanels();
0501: else
0502: SubstanceLookAndFeel.hideMenuSearchPanels();
0503: }
0504: });
0505: builder.append("Menu search", menuSearch);
0506:
0507: final JCheckBox menuLocale = new JCheckBox("Has custom locale");
0508: menuLocale.setSelected(false);
0509: menuLocale.addActionListener(new ActionListener() {
0510: public void actionPerformed(ActionEvent e) {
0511: if (menuLocale.isSelected()) {
0512: mainFrame.getJMenuBar().setLocale(Locale.FRENCH);
0513: mainFrame.getJMenuBar().putClientProperty(
0514: LafWidget.IGNORE_GLOBAL_LOCALE,
0515: Boolean.TRUE);
0516: } else {
0517: mainFrame.getJMenuBar().putClientProperty(
0518: LafWidget.IGNORE_GLOBAL_LOCALE, null);
0519: }
0520: }
0521: });
0522: builder.append("Menu locale", menuLocale);
0523:
0524: return builder.getPanel();
0525: }
0526:
0527: /**
0528: * Returns the control panel for testing dialogs.
0529: *
0530: * @param mainFrame
0531: * The main test frame.
0532: * @return Control panel for testing dialogs.
0533: */
0534: public static JPanel getDialogControlPanel(final JFrame mainFrame) {
0535: FormLayout lm = new FormLayout(
0536: "right:pref, 4dlu, fill:pref:grow", "");
0537: DefaultFormBuilder builder = new DefaultFormBuilder(lm);
0538:
0539: builder.appendSeparator("Core choosers");
0540: JButton bfo = new JButton("Open dialog", Check
0541: .getIcon("JFileChooserColor16"));
0542: bfo.addActionListener(new ActionListener() {
0543: public void actionPerformed(ActionEvent e) {
0544: SwingUtilities.invokeLater(new Runnable() {
0545: public void run() {
0546: JFileChooser jfc = new JFileChooser();
0547: jfc.showOpenDialog(mainFrame);
0548: }
0549: });
0550: }
0551: });
0552: builder.append("File chooser", bfo);
0553:
0554: JButton bfs = new JButton("Save dialog", Check
0555: .getIcon("JFileChooserColor16"));
0556: bfs.addActionListener(new ActionListener() {
0557: public void actionPerformed(ActionEvent e) {
0558: SwingUtilities.invokeLater(new Runnable() {
0559: public void run() {
0560: JFileChooser jfc = new JFileChooser();
0561: jfc.showSaveDialog(mainFrame);
0562: }
0563: });
0564: }
0565: });
0566: builder.append("", bfs);
0567:
0568: JButton bc = new JButton("Open", Check
0569: .getIcon("JColorChooserColor16"));
0570: bc.addActionListener(new ActionListener() {
0571: public void actionPerformed(ActionEvent e) {
0572: SwingUtilities.invokeLater(new Runnable() {
0573: public void run() {
0574: Color color = JColorChooser.showDialog(
0575: mainFrame, "Color chooser", new Color(
0576: 23, 45, 200));
0577: if (color != null) {
0578: Check.out("Chosen " + color.toString());
0579: }
0580: }
0581: });
0582: }
0583: });
0584: builder.append("Color chooser", bc);
0585:
0586: JButton bcWindow = new JButton("open in window");
0587: bcWindow.addActionListener(new ActionListener() {
0588: public void actionPerformed(ActionEvent e) {
0589: SwingUtilities.invokeLater(new Runnable() {
0590: public void run() {
0591: final Window window = new Window(mainFrame);
0592: window.setLayout(new BorderLayout());
0593: window.add(new JColorChooser());
0594: window.pack();
0595: window.setLocationRelativeTo(null);
0596: window.setVisible(true);
0597: Timer timerDispose = new Timer(5000,
0598: new ActionListener() {
0599: public void actionPerformed(
0600: ActionEvent e) {
0601: window.dispose();
0602: }
0603: });
0604: timerDispose.setRepeats(false);
0605: timerDispose.start();
0606: }
0607: });
0608: }
0609: });
0610: builder.append("", bcWindow);
0611:
0612: builder.appendSeparator("Option panes");
0613:
0614: JButton bop = new JButton("Show");
0615: bop.addActionListener(new ActionListener() {
0616: public void actionPerformed(ActionEvent e) {
0617: SwingUtilities.invokeLater(new Runnable() {
0618: public void run() {
0619: JOptionPane pane = new JOptionPane(
0620: "Sample option pane");
0621: JDialog dialog = pane.createDialog(mainFrame,
0622: "Sample title");
0623: dialog.setVisible(true);
0624: dialog.dispose();
0625: }
0626: });
0627: };
0628: });
0629: builder.append("Plain", bop);
0630:
0631: JButton bopi = new JButton("Show", Check
0632: .getIcon("22/dialog-information"));
0633: bopi.addActionListener(new ActionListener() {
0634: public void actionPerformed(ActionEvent e) {
0635: SwingUtilities.invokeLater(new Runnable() {
0636: public void run() {
0637: JOptionPane.showMessageDialog(mainFrame,
0638: "Sample info message", "Sample title",
0639: JOptionPane.INFORMATION_MESSAGE);
0640: }
0641: });
0642: };
0643: });
0644: builder.append("Info", bopi);
0645:
0646: JButton bope = new JButton("Show", Check
0647: .getIcon("22/dialog-error"));
0648: bope.addActionListener(new ActionListener() {
0649: public void actionPerformed(ActionEvent e) {
0650: SwingUtilities.invokeLater(new Runnable() {
0651: public void run() {
0652: JOptionPane.showMessageDialog(mainFrame,
0653: "Sample error message", "Sample title",
0654: JOptionPane.ERROR_MESSAGE);
0655: }
0656: });
0657: };
0658: });
0659: builder.append("Error", bope);
0660:
0661: JButton bopw = new JButton("Show", Check
0662: .getIcon("22/dialog-warning"));
0663: bopw.addActionListener(new ActionListener() {
0664: public void actionPerformed(ActionEvent e) {
0665: SwingUtilities.invokeLater(new Runnable() {
0666: public void run() {
0667: JOptionPane.showMessageDialog(mainFrame,
0668: "Sample warning message",
0669: "Sample title",
0670: JOptionPane.WARNING_MESSAGE);
0671: }
0672: });
0673: };
0674: });
0675: builder.append("Warning", bopw);
0676:
0677: JButton bopq = new JButton("Show", Check
0678: .getIcon("22/help-browser"));
0679: bopq.addActionListener(new ActionListener() {
0680: public void actionPerformed(ActionEvent e) {
0681: SwingUtilities.invokeLater(new Runnable() {
0682: public void run() {
0683: JOptionPane.showMessageDialog(mainFrame,
0684: "Sample question message",
0685: "Sample title",
0686: JOptionPane.QUESTION_MESSAGE);
0687: }
0688: });
0689: };
0690: });
0691: builder.append("Question", bopq);
0692:
0693: JButton bopc = new JButton("Show");
0694: bopc.addActionListener(new ActionListener() {
0695: public void actionPerformed(ActionEvent e) {
0696: SwingUtilities.invokeLater(new Runnable() {
0697: public void run() {
0698: JOptionPane.showOptionDialog(mainFrame,
0699: new JPanel(), "Option",
0700: JOptionPane.OK_CANCEL_OPTION,
0701: JOptionPane.PLAIN_MESSAGE, null, null,
0702: null);
0703: }
0704: });
0705: };
0706: });
0707: builder.append("Custom", bopc);
0708:
0709: JButton bopii = new JButton("Show");
0710: bopii.addActionListener(new ActionListener() {
0711: public void actionPerformed(ActionEvent e) {
0712: SwingUtilities.invokeLater(new Runnable() {
0713: public void run() {
0714: JDialog dialog = new JDialog(mainFrame,
0715: "Sample dialog", true);
0716: dialog.setSize(400, 300);
0717: dialog.setLocationRelativeTo(mainFrame);
0718: dialog.setLayout(new BorderLayout());
0719: JPanel panel = new JPanel();
0720: dialog.add(panel, BorderLayout.CENTER);
0721: // dialog.setVisible(true);
0722: JOptionPane.showInternalInputDialog(panel,
0723: "Sample info message", "Sample title",
0724: JOptionPane.INFORMATION_MESSAGE);
0725: dialog.dispose();
0726: }
0727: });
0728: }
0729: });
0730: builder.append("Internal input", bopii);
0731:
0732: builder.appendSeparator("Default buttons");
0733:
0734: JButton openDisposable = new JButton("Open");
0735: openDisposable.addActionListener(new ActionListener() {
0736: public void actionPerformed(ActionEvent e) {
0737: SwingUtilities.invokeLater(new Runnable() {
0738: public void run() {
0739: if (disposableDialog != null) {
0740: disposableDialog.setVisible(true);
0741: return;
0742: }
0743: disposableDialog = new JDialog();
0744: disposableDialog.setTitle("Disposable");
0745:
0746: JTree tree = new JTree();
0747: JScrollPane jsp = new JScrollPane(tree,
0748: JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
0749: JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
0750: jsp.putClientProperty(
0751: LafWidget.COMPONENT_PREVIEW_PAINTER,
0752: new DefaultPreviewPainter());
0753:
0754: disposableDialog.setLayout(new BorderLayout());
0755: disposableDialog.add(jsp, BorderLayout.CENTER);
0756:
0757: disposableDialog.setSize(200, 100);
0758: disposableDialog.setLocationRelativeTo(null);
0759: disposableDialog
0760: .setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
0761: disposableDialog.setVisible(true);
0762: }
0763: });
0764: };
0765: });
0766: builder.append("Disposable dialog", openDisposable);
0767:
0768: JButton bd = new JButton("Open");
0769: bd.addActionListener(new ActionListener() {
0770: public void actionPerformed(ActionEvent e) {
0771: SwingUtilities.invokeLater(new Runnable() {
0772: public void run() {
0773: SimpleDialog sd = new SimpleDialog();
0774: sd.setModal(false);
0775: sd.pack();
0776: Dimension d = Toolkit.getDefaultToolkit()
0777: .getScreenSize();
0778: // center the frame in the physical screen
0779: sd.setLocation((d.width - sd.getWidth()) / 2,
0780: (d.height - sd.getHeight()) / 2);
0781: sd.setVisible(true);
0782: simpleDialog = sd;
0783: }
0784: });
0785: };
0786: });
0787: builder.append("Open a dialog", bd);
0788:
0789: JButton bcd = new JButton("Close");
0790: bcd.addActionListener(new ActionListener() {
0791: public void actionPerformed(ActionEvent e) {
0792: SwingUtilities.invokeLater(new Runnable() {
0793: public void run() {
0794: if (simpleDialog != null) {
0795: simpleDialog.removeAll();
0796: simpleDialog.dispose();
0797: ReferenceQueue<JButton> weakQueue = new ReferenceQueue<JButton>();
0798: WeakReference<JButton> weakRef = new WeakReference<JButton>(
0799: simpleDialog.b1, weakQueue);
0800: weakRef.enqueue();
0801: simpleDialog.b1 = null;
0802: simpleDialog = null;
0803: System.gc();
0804: // Wait until the weak reference is on the queue and
0805: // remove
0806: // it
0807: Check.out("Waiting to remove");
0808: try {
0809: Reference<?> ref = weakQueue.remove();
0810: ref.clear();
0811: } catch (InterruptedException ie) {
0812: ie.printStackTrace();
0813: return;
0814: }
0815: Check.out("Removed");
0816: }
0817: }
0818: });
0819: };
0820: });
0821: builder.append("Close the dialog", bcd);
0822:
0823: JButton buttonDialogCloseOnEsc = new JButton("Show");
0824: buttonDialogCloseOnEsc.addActionListener(new ActionListener() {
0825: public void actionPerformed(ActionEvent e) {
0826: SwingUtilities.invokeLater(new Runnable() {
0827: public void run() {
0828: final JDialog dialog = new JDialog(
0829: (Frame) null, "Click ESC to dispose");
0830: dialog.setSize(200, 200);
0831: dialog.setLayout(new BorderLayout());
0832:
0833: JPanel myContentPane = new JPanel();
0834: myContentPane.setLayout(new BorderLayout());
0835: dialog.setContentPane(myContentPane);
0836:
0837: JTabbedPane tabs = new JTabbedPane();
0838: tabs.addTab("Foo", new JButton("Test"));
0839: tabs.addTab("Bar", new JLabel());
0840: dialog.add(tabs, BorderLayout.CENTER);
0841: // add(new JButton("Test"), BorderLayout.CENTER);
0842:
0843: dialog.add(new JLabel(
0844: "Press Esc to close dialog"),
0845: BorderLayout.NORTH);
0846:
0847: // connect "Esc" key with "System.exit(0)"
0848: String actionName = "VK_ESCAPE";
0849: Action action = new AbstractAction(actionName) {
0850: public void actionPerformed(ActionEvent e) {
0851: dialog.dispose();
0852: }
0853: };
0854: myContentPane.getActionMap().put(actionName,
0855: action);
0856: myContentPane
0857: .getInputMap(
0858: JComponent.WHEN_IN_FOCUSED_WINDOW)
0859: .put(
0860: KeyStroke
0861: .getKeyStroke(
0862: java.awt.event.KeyEvent.VK_ESCAPE,
0863: 0), actionName);
0864:
0865: dialog.setLocationRelativeTo(null);
0866: dialog.setVisible(true);
0867: }
0868: });
0869: }
0870: });
0871: builder.append("Dialog with ESC close", buttonDialogCloseOnEsc);
0872:
0873: JButton buttonDialogUndecorated = new JButton("Show");
0874: buttonDialogCloseOnEsc.addActionListener(new ActionListener() {
0875: public void actionPerformed(ActionEvent e) {
0876: SwingUtilities.invokeLater(new Runnable() {
0877: public void run() {
0878: final JDialog dialog = new JDialog(
0879: (Frame) null, "");
0880: dialog.setSize(200, 200);
0881: dialog.setUndecorated(true);
0882: dialog.setLayout(new BorderLayout());
0883:
0884: JPanel myContentPane = new JPanel();
0885: myContentPane.setLayout(new BorderLayout());
0886: dialog.setContentPane(myContentPane);
0887:
0888: dialog.add(new JLabel(
0889: "Press Esc to close dialog"),
0890: BorderLayout.NORTH);
0891:
0892: // connect "Esc" key with "System.exit(0)"
0893: String actionName = "VK_ESCAPE";
0894: Action action = new AbstractAction(actionName) {
0895: public void actionPerformed(ActionEvent e) {
0896: dialog.dispose();
0897: }
0898: };
0899: myContentPane.getActionMap().put(actionName,
0900: action);
0901: myContentPane
0902: .getInputMap(
0903: JComponent.WHEN_IN_FOCUSED_WINDOW)
0904: .put(
0905: KeyStroke
0906: .getKeyStroke(
0907: java.awt.event.KeyEvent.VK_ESCAPE,
0908: 0), actionName);
0909:
0910: dialog.setLocationRelativeTo(null);
0911: dialog.setVisible(true);
0912: }
0913: });
0914: }
0915: });
0916: builder.append("Undecorated dialog", buttonDialogUndecorated);
0917:
0918: builder.appendSeparator("Miscellaneous");
0919:
0920: JButton btf = new JButton("Show");
0921: btf.addActionListener(new ActionListener() {
0922: public void actionPerformed(ActionEvent e) {
0923: SwingUtilities.invokeLater(new Runnable() {
0924: public void run() {
0925: JFrame testFrame = new JFrame("test1");
0926: testFrame.setSize(262, 100);
0927: testFrame.setLocation(400, 400);
0928: testFrame
0929: .setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
0930: testFrame.setVisible(true);
0931: }
0932: });
0933: };
0934: });
0935: builder.append("Regular frame", btf);
0936:
0937: JButton btfU = new JButton("Show");
0938: btfU.addActionListener(new ActionListener() {
0939: public void actionPerformed(ActionEvent e) {
0940: SwingUtilities.invokeLater(new Runnable() {
0941: public void run() {
0942: JFrame.setDefaultLookAndFeelDecorated(false);
0943: JDialog.setDefaultLookAndFeelDecorated(false);
0944:
0945: JFrame testFrame = new JFrame(
0946: "test undecorated");
0947: // testFrame.setUndecorated(true);
0948:
0949: testFrame.setSize(262, 100);
0950: testFrame.setLocation(400, 400);
0951: testFrame
0952: .setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
0953: testFrame.setVisible(true);
0954:
0955: JFrame.setDefaultLookAndFeelDecorated(true);
0956: JDialog.setDefaultLookAndFeelDecorated(true);
0957: }
0958: });
0959: };
0960: });
0961: builder.append("Undecorated frame", btfU);
0962:
0963: JButton bcp = new JButton("Open");
0964: bcp.addActionListener(new ActionListener() {
0965: public void actionPerformed(ActionEvent e) {
0966: SwingUtilities.invokeLater(new Runnable() {
0967: public void run() {
0968:
0969: JFrame colorFrame = new JFrame();
0970: final ColorPanel cp1 = new ColorPanel("default");
0971: final ColorPanel cp2 = new ColorPanel("green");
0972: cp2.setBackground(Color.green);
0973: final ColorPanel cp3 = new ColorPanel("red");
0974: cp3.setBackground(Color.red);
0975: final ColorPanel cp4 = new ColorPanel("black");
0976: cp4.setBackground(Color.black);
0977: // cp2.setBackground(Color.GREEN);
0978: colorFrame.setLayout(new LayoutManager() {
0979: public void addLayoutComponent(String name,
0980: Component comp) {
0981: }
0982:
0983: public void layoutContainer(Container parent) {
0984: int h = parent.getHeight() / 2;
0985: int w = parent.getWidth() / 2;
0986: cp1.setBounds(0, 0, w, h);
0987: cp2.setBounds(0, h, w, parent
0988: .getHeight()
0989: - h);
0990: cp3.setBounds(w, 0, w, h + 1);
0991: cp4.setBounds(w, h + 1, w, parent
0992: .getHeight()
0993: - h);
0994: }
0995:
0996: public Dimension minimumLayoutSize(
0997: Container parent) {
0998: return preferredLayoutSize(parent);
0999: }
1000:
1001: public Dimension preferredLayoutSize(
1002: Container parent) {
1003: return new Dimension(100, 100);
1004: }
1005:
1006: public void removeLayoutComponent(
1007: Component comp) {
1008: }
1009:
1010: });
1011: colorFrame.add(cp1);
1012: colorFrame.add(cp2);
1013: colorFrame.add(cp3);
1014: colorFrame.add(cp4);
1015:
1016: colorFrame.setSize(400, 399);
1017: colorFrame.setLocation(300, 300);
1018: colorFrame
1019: .setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
1020: colorFrame.setVisible(true);
1021: }
1022: });
1023: };
1024: });
1025: builder.append("Color panels", bcp);
1026:
1027: JButton paneDialog = new JButton("Open");
1028: paneDialog.addActionListener(new ActionListener() {
1029: public void actionPerformed(ActionEvent e) {
1030: SwingUtilities.invokeLater(new Runnable() {
1031: public void run() {
1032: JDialog dialog = new JDialog(mainFrame, true);
1033: dialog
1034: .setTitle("Test text pane in scroll pane");
1035: JTextPane textPane = new JTextPane();
1036: String contents = "";
1037: for (int i = 0; i < 100; i++)
1038: contents += "This is sample line "
1039: + i
1040: + " and a lot of other irrelevant text\n";
1041: textPane.replaceSelection(contents);
1042:
1043: JScrollPane scroll = new JScrollPane(textPane);
1044: JPanel panel = new JPanel();
1045: panel.setLayout(new BorderLayout());
1046: panel.add(scroll, BorderLayout.CENTER);
1047:
1048: dialog.setLayout(new BorderLayout());
1049: dialog.add(panel, BorderLayout.CENTER);
1050: dialog.setSize(400, 400);
1051: dialog.setLocation(400, 200);
1052: dialog.setVisible(true);
1053: }
1054: });
1055: };
1056: });
1057: builder.append("Text pane dialog", paneDialog);
1058:
1059: return builder.getPanel();
1060: }
1061: }
|