1 19 20 package org.netbeans.swing.tabcontrol; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.FlowLayout ; 25 import java.awt.Graphics ; 26 import java.awt.Point ; 27 import java.awt.event.MouseAdapter ; 28 import java.awt.event.MouseEvent ; 29 import java.util.Arrays ; 30 import javax.swing.Icon ; 31 import javax.swing.JButton ; 32 import javax.swing.JComponent ; 33 import javax.swing.JFrame ; 34 import javax.swing.SwingUtilities ; 35 import javax.swing.UIManager ; 36 import junit.framework.TestCase; 37 import org.netbeans.swing.popupswitcher.SwitcherTableItem; 38 39 45 public class ButtonPopupSwitcherTestHid extends TestCase { 46 47 private JFrame frame; 48 49 private SwitcherTableItem[] items = new SwitcherTableItem[100]; 50 51 public ButtonPopupSwitcherTestHid(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 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 sleepForever(); 82 } 83 84 public void testFake() { 85 } 87 88 private JFrame createFrame() { 89 JFrame frame = new JFrame (getClass().getName()); 90 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 91 frame.getContentPane().setLayout(new FlowLayout ()); 92 JButton pBut = new JButton ("Popup"); 93 pBut.addMouseListener(new MouseAdapter () { 94 public void mousePressed(MouseEvent 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 e) { 105 JComponent c = (JComponent ) e.getSource(); 107 Point p = new Point (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 { 115 Color color; 116 private DummyIcon(Color color) { 117 this.color = color; 118 } 119 private DummyIcon() { 120 this.color = Color.RED; 121 } 122 public void paintIcon(Component c, Graphics g, int x, int y) { 123 int left = ((JComponent ) c).getInsets().left; 124 int top = ((JComponent ) 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 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 154 private void sleep() { 155 sleep(12000); 156 } 157 158 private void sleep(long millis) { 159 try { 160 Thread.sleep(millis); 161 } catch (InterruptedException 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 |