KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > swing > tabcontrol > ButtonPopupSwitcherTestHid


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.swing.tabcontrol;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Point JavaDoc;
27 import java.awt.event.MouseAdapter JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36 import junit.framework.TestCase;
37 import org.netbeans.swing.popupswitcher.SwitcherTableItem;
38
39 /**
40  * Convenient IDE tester. Just run and push the button. Move the whole frame to
41  * visually check the row per columns computation.
42  *
43  * @author mkrauskopf
44  */

45 public class ButtonPopupSwitcherTestHid extends TestCase {
46     
47     private JFrame JavaDoc frame;
48     
49     private SwitcherTableItem[] items = new SwitcherTableItem[100];
50     
51     public ButtonPopupSwitcherTestHid(String JavaDoc testName) {
52         super(testName);
53         try {
54             UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
55         } catch (Exception JavaDoc ex) {
56             System.err.println("Cannot set L&F: " + ex);
57         }
58     }
59     
60     protected void setUp() {
61         frame = createFrame();
62         frame.setVisible(true);
63         items[0] = new SwitcherTableItem(new DummyActivatable("Something.txt"), "Something.txt", new DummyIcon(Color.BLUE));
64         items[1] = new SwitcherTableItem(new DummyActivatable("Sometime.txt"), "Sometime.txt", new DummyIcon());
65         SwitcherTableItem selectedItem = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "Somewhere.txt", "Somewhere.txt", new DummyIcon(Color.YELLOW), true);
66         items[2] = selectedItem;
67         items[3] = new SwitcherTableItem(new DummyActivatable("AbCd.txt"), "AbCd.txt", new DummyIcon(Color.BLUE));
68         items[4] = new SwitcherTableItem(new DummyActivatable("Sometime.txt"),
69                 "Very Very Very Long" +
70                 " name with a lot of words in its name bla bla bla bla bla bla" +
71                 " which sould be shortened and should ends with three dots [...]." +
72                 " Hmmmmm", new DummyIcon());
73         items[5] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "Somewhere.txt", new DummyIcon(Color.YELLOW));
74         Arrays.fill(items, 6, 70, new SwitcherTableItem(new DummyActivatable("s2.txt"), "s2.txt", new DummyIcon()));
75         items[70] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "null icon", null);
76         Arrays.fill(items, 71, 90, new SwitcherTableItem(new DummyActivatable("s5.txt"), "s5.txt", new DummyIcon()));
77         items[90] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), null, new DummyIcon(Color.BLACK));
78         Arrays.fill(items, 91, 100, new SwitcherTableItem(new DummyActivatable("q1.txt"), "q1.txt", new DummyIcon(Color.GREEN)));
79         Arrays.sort(items);
80         // wait until a developer close the frame
81
sleepForever();
82     }
83     
84     public void testFake() {
85         // needed to "run" this class
86
}
87     
88     private JFrame JavaDoc createFrame() {
89         JFrame JavaDoc frame = new JFrame JavaDoc(getClass().getName());
90         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
91         frame.getContentPane().setLayout(new FlowLayout JavaDoc());
92         JButton JavaDoc pBut = new JButton JavaDoc("Popup");
93         pBut.addMouseListener(new MouseAdapter JavaDoc() {
94             public void mousePressed(MouseEvent JavaDoc e) {
95                 pButAction(e);
96             }
97         });
98         frame.getContentPane().add(pBut);
99         frame.pack();
100         frame.setLocationRelativeTo(null);
101         return frame;
102     }
103     
104     private void pButAction(MouseEvent JavaDoc e) {
105         // create popup with our SwitcherTable
106
JComponent JavaDoc c = (JComponent JavaDoc) e.getSource();
107         Point JavaDoc p = new Point JavaDoc(c.getWidth(), c.getHeight());
108         SwingUtilities.convertPointToScreen(p, c);
109         if (!ButtonPopupSwitcher.isShown()) {
110             ButtonPopupSwitcher.selectItem(c, items, p.x, p.y);
111         }
112     }
113     
114     private static class DummyIcon implements Icon JavaDoc {
115         Color JavaDoc color;
116         private DummyIcon(Color JavaDoc color) {
117             this.color = color;
118         }
119         private DummyIcon() {
120             this.color = Color.RED;
121         }
122         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
123             int left = ((JComponent JavaDoc) c).getInsets().left;
124             int top = ((JComponent JavaDoc) c).getInsets().top;
125             g.setColor(color);
126             g.fillRect(left + 2, top + 2, 12, 12);
127             g.setColor(Color.BLACK);
128             g.fillRect(left + 4, top + 4, 8, 8);
129         }
130         
131         public int getIconWidth() {
132             return 16;
133         }
134         
135         public int getIconHeight() {
136             return 16;
137         }
138     }
139     
140     /**
141      * Activatable tester class.
142      */

143     private static class DummyActivatable implements SwitcherTableItem.Activatable {
144         String JavaDoc dummyName;
145         private DummyActivatable(String JavaDoc name) {
146             this.dummyName = name;
147         }
148         public void activate() {
149             System.out.println("MK> Activating \"" + dummyName + "\"....");
150         }
151     }
152
153     
154     private void sleep() {
155         sleep(12000);
156     }
157     
158     private void sleep(long millis) {
159         try {
160             Thread.sleep(millis);
161         } catch (InterruptedException JavaDoc e) {
162             e.printStackTrace();
163         }
164     }
165     
166     private void sleepForever() {
167         boolean dumb = true;
168         while(dumb) {
169             sleep(60000);
170         }
171     }
172 }
173
Popular Tags