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;
031:
032: import java.awt.*;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.lang.ref.*;
036: import java.util.HashSet;
037: import java.util.Set;
038:
039: import javax.swing.*;
040:
041: import org.jvnet.substance.SubstanceLookAndFeel;
042:
043: public class CheckDefaultButton extends JFrame {
044:
045: private static class SimpleDialog extends JDialog {
046: public JButton b1;
047: public JButton b0;
048:
049: public SimpleDialog() {
050: this .setLayout(new FlowLayout());
051: b0 = new JButton("default-0");
052: b1 = new JButton("default-1");
053: JButton b2 = new JButton("disabled");
054: b2.setEnabled(false);
055: JButton b3 = new JButton("regular");
056: this .add(b0);
057: this .add(b1);
058: this .add(b2);
059: this .add(b3);
060:
061: JButton b4 = new JButton("toggle b1.enabled()");
062: b4.addActionListener(new ActionListener() {
063: public void actionPerformed(ActionEvent e) {
064: b1.setEnabled(!b1.isEnabled());
065: }
066: });
067: this .add(b4);
068:
069: JButton b5 = new JButton("toggle b0-b1 default");
070: b5.addActionListener(new ActionListener() {
071: public void actionPerformed(ActionEvent e) {
072: JButton defButton = getRootPane()
073: .getDefaultButton();
074: if (defButton == b0)
075: getRootPane().setDefaultButton(b1);
076: else
077: getRootPane().setDefaultButton(b0);
078: }
079: });
080: this .add(b5);
081:
082: this .getRootPane().setDefaultButton(b1);
083: this .setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
084: }
085: }
086:
087: private Set<SimpleDialog> simpleDialogs = new HashSet<SimpleDialog>();
088:
089: public CheckDefaultButton() {
090: super ("Substance default button test");
091:
092: this .setLayout(new FlowLayout());
093: JButton b1 = new JButton("Default");
094: this .add(b1);
095:
096: JButton bd = new JButton("Open new dialog");
097: bd.addActionListener(new ActionListener() {
098: public void actionPerformed(ActionEvent e) {
099: SimpleDialog sd = new SimpleDialog();
100: sd.setModal(false);
101: sd.pack();
102: Dimension d = Toolkit.getDefaultToolkit()
103: .getScreenSize();
104: // center the frame in the physical screen
105: sd.setLocation((d.width - sd.getWidth()) / 2,
106: (d.height - sd.getHeight()) / 2);
107: sd.setVisible(true);
108: simpleDialogs.add(sd);
109: }
110: });
111: this .add(bd);
112: JButton bcd = new JButton("Close all dialogs");
113: bcd.addActionListener(new ActionListener() {
114: public void actionPerformed(ActionEvent e) {
115: for (SimpleDialog simpleDialog : simpleDialogs) {
116: simpleDialog.removeAll();
117: simpleDialog.dispose();
118: ReferenceQueue<JButton> weakQueue = new ReferenceQueue<JButton>();
119: WeakReference<JButton> weakRef = new WeakReference<JButton>(
120: simpleDialog.b1, weakQueue);
121: weakRef.enqueue();
122: simpleDialog.b1 = null;
123: simpleDialog = null;
124: System.gc();
125: // Wait until the weak reference is on the queue and remove
126: // it
127: System.out.println("Waiting to remove");
128: try {
129: Reference ref = weakQueue.remove();
130: ref.clear();
131: } catch (InterruptedException ie) {
132: ie.printStackTrace();
133: return;
134: }
135: System.out.println("Removed");
136: }
137: simpleDialogs.clear();
138: }
139: });
140: this .add(bcd);
141:
142: this .getRootPane().setDefaultButton(b1);
143: this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
144: }
145:
146: public static void main(String[] args) {
147: try {
148: // UIManager
149: // .setLookAndFeel("com.oyoaha.swing.plaf.oyoaha.OyoahaLookAndFeel");
150: UIManager.setLookAndFeel(new SubstanceLookAndFeel());
151: } catch (Exception ulafe) {
152: ulafe.printStackTrace();
153: }
154: CheckDefaultButton c = new CheckDefaultButton();
155: c.pack();
156: Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
157: // center the frame in the physical screen
158: c.setLocation((d.width - c.getWidth()) / 2, (d.height - c
159: .getHeight()) / 2);
160: c.setVisible(true);
161: }
162:
163: }
|