1 19 20 package org.netbeans.core.windows.view.ui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Graphics ; 26 import java.awt.KeyEventDispatcher ; 27 import java.awt.KeyboardFocusManager ; 28 import java.awt.event.InputEvent ; 29 import java.awt.event.KeyEvent ; 30 import java.util.Arrays ; 31 import javax.swing.Icon ; 32 import javax.swing.JComponent ; 33 import javax.swing.JFrame ; 34 import javax.swing.UIManager ; 35 import junit.framework.TestCase; 36 import org.netbeans.swing.popupswitcher.SwitcherTableItem; 37 38 39 45 public class KeyboardPopupSwitcherTestHid extends TestCase 46 implements KeyEventDispatcher { 47 48 private JFrame frame; 49 private SwitcherTableItem[] items = new SwitcherTableItem[100]; 50 51 public KeyboardPopupSwitcherTestHid(String testName) { 52 super(testName); 53 try { 54 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 55 } catch (Exception ex) { 56 System.err.println("Cannot set L&F: " + ex); 57 } 58 } 59 60 protected void setUp() { 61 KeyboardFocusManager 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 sleepForever(); 84 keyboardFocusManager.removeKeyEventDispatcher(this); 85 } 86 87 public void testFake() { 88 } 90 91 private JFrame createFrame() { 92 JFrame frame = new JFrame (getClass().getName()); 93 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 94 frame.setSize(new Dimension (600, 400)); 95 frame.setLocationRelativeTo(null); 96 return frame; 97 } 98 99 public boolean dispatchKeyEvent(java.awt.event.KeyEvent 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 KeyboardPopupSwitcher.selectItem(items, KeyEvent.VK_CONTROL, e.getKeyCode()); 107 return true; 108 } 109 110 return false; 111 } 112 113 116 private static class DummyIcon implements Icon { 117 Color color; 118 private DummyIcon(Color 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 c, Graphics g, int x, int y) { 131 int left = ((JComponent ) c).getInsets().left; 132 int top = ((JComponent ) 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 143 private static class DummyActivatable implements SwitcherTableItem.Activatable { 144 String dummyName; 145 private DummyActivatable(String 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 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 |