KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > KeyboardPopupSwitcherTestHid


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.core.windows.view.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.KeyEventDispatcher JavaDoc;
27 import java.awt.KeyboardFocusManager JavaDoc;
28 import java.awt.event.InputEvent JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.util.Arrays JavaDoc;
31 import javax.swing.Icon JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JFrame JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import junit.framework.TestCase;
36 import org.netbeans.swing.popupswitcher.SwitcherTableItem;
37
38
39 /**
40  * Convenient IDE tester. Tests DocumentSwitcherTable. Just run and play with
41  * Ctrl+Tab and Ctrl+Shift+Tab keys.
42  *
43  * @author mkrauskopf
44  */

45 public class KeyboardPopupSwitcherTestHid extends TestCase
46         implements KeyEventDispatcher JavaDoc {
47     
48     private JFrame JavaDoc frame;
49     private SwitcherTableItem[] items = new SwitcherTableItem[100];
50     
51     public KeyboardPopupSwitcherTestHid(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         KeyboardFocusManager JavaDoc keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
62         keyboardFocusManager.addKeyEventDispatcher(this);
63         frame = createFrame();
64         frame.setVisible(true);
65         
66         items[0] = new SwitcherTableItem(new DummyActivatable("Something.txt"), "Something.txt", new DummyIcon(Color.BLUE));
67         items[1] = new SwitcherTableItem(new DummyActivatable("Sometime.txt"), "Sometime.txt", new DummyIcon());
68         items[2] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "Somewhere.txt", new DummyIcon(Color.YELLOW));
69         items[3] = new SwitcherTableItem(new DummyActivatable("Something.txt"), "Something.txt", new DummyIcon(Color.BLUE));
70         items[4] = new SwitcherTableItem(new DummyActivatable("Sometime.txt"),
71                 "Very Very Very Long" +
72                 " name with a lot of words in its name bla bla bla bla bla bla" +
73                 " which sould be shortened and should ends with three dots [...]." +
74                 " Hmmmmm", new DummyIcon());
75         items[5] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "Somewhere.txt", new DummyIcon(Color.YELLOW));
76         Arrays.fill(items, 6, 70, new SwitcherTableItem(new DummyActivatable("s1.txt"), "s1.txt", new DummyIcon()));
77         items[70] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), "null icon", null);
78         Arrays.fill(items, 71, 90, new SwitcherTableItem(new DummyActivatable("s1.txt"), "s1.txt", new DummyIcon()));
79         items[90] = new SwitcherTableItem(new DummyActivatable("Somewhere.txt"), null, new DummyIcon(Color.BLACK));
80         Arrays.fill(items, 91, 100, new SwitcherTableItem(new DummyActivatable("s1.txt"), "s1.txt", new DummyIcon(Color.GREEN)));
81         
82         // wait until a developer close the frame
83
sleepForever();
84         keyboardFocusManager.removeKeyEventDispatcher(this);
85     }
86     
87     public void testFake() {
88         // needed to "run" this class
89
}
90     
91     private JFrame JavaDoc createFrame() {
92         JFrame JavaDoc frame = new JFrame JavaDoc(getClass().getName());
93         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
94         frame.setSize(new Dimension JavaDoc(600, 400));
95         frame.setLocationRelativeTo(null);
96         return frame;
97     }
98     
99     public boolean dispatchKeyEvent(java.awt.event.KeyEvent JavaDoc e) {
100         boolean isCtrl = e.getModifiers() == InputEvent.CTRL_MASK;
101         boolean isCtrlShift = e.getModifiers() == (InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK);
102         boolean doPopup = (e.getKeyCode() == KeyEvent.VK_TAB) &&
103                 (isCtrl || isCtrlShift);
104         if (doPopup && !KeyboardPopupSwitcher.isShown()) {
105             // create popup with our SwitcherTable
106
KeyboardPopupSwitcher.selectItem(items, KeyEvent.VK_CONTROL, e.getKeyCode());
107             return true;
108         }
109         
110         return false;
111     }
112     
113     /**
114      * Dummy icon meant for testing prupose.
115      */

116     private static class DummyIcon implements Icon JavaDoc {
117         Color JavaDoc color;
118         private DummyIcon(Color JavaDoc color) {
119             this.color = color;
120         }
121         private DummyIcon() {
122             this(Color.RED);
123         }
124         public int getIconWidth() {
125             return 16;
126         }
127         public int getIconHeight() {
128             return 16;
129         }
130         public void paintIcon(Component JavaDoc c, Graphics JavaDoc g, int x, int y) {
131             int left = ((JComponent JavaDoc) c).getInsets().left;
132             int top = ((JComponent JavaDoc) c).getInsets().top;
133             g.setColor(color);
134             g.fillRect(left + 2, top + 2, 12, 12);
135             g.setColor(Color.BLACK);
136             g.fillRect(left + 4, top + 4, 8, 8);
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     private void sleep() {
154         sleep(500);
155     }
156     
157     private void sleep(long millis) {
158         try {
159             Thread.sleep(millis);
160         } catch (InterruptedException JavaDoc e) {
161             e.printStackTrace();
162         }
163     }
164     
165     private void sleepForever() {
166         boolean dumb = true;
167         while(dumb) {
168             sleep();
169         }
170     }
171 }
172
Popular Tags