1 19 20 package org.openide.awt; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.JComponent ; 28 import javax.swing.JMenu ; 29 import javax.swing.JMenuItem ; 30 import javax.swing.JPopupMenu ; 31 import javax.swing.JSeparator ; 32 import junit.framework.TestCase; 33 import org.openide.util.actions.Presenter; 34 35 39 public class DynaMenuModelTest extends TestCase { 40 41 public DynaMenuModelTest(String testName) { 42 super(testName); 43 } 44 45 48 public void testLoadSubmenu() { 49 System.out.println("loadSubmenu"); 50 51 List cInstances = new ArrayList (); 52 cInstances.add(new Act1()); 53 cInstances.add(new Act2()); 54 JMenu m = new JMenu (); 55 DynaMenuModel instance = new DynaMenuModel(); 56 57 instance.loadSubmenu(cInstances, m); 58 Component [] comps = m.getPopupMenu().getComponents(); 59 assertEquals("0", ((JMenuItem )comps[0]).getText()); 60 assertEquals("1", ((JMenuItem )comps[1]).getText()); 61 assertEquals("2", ((JMenuItem )comps[2]).getText()); 62 63 } 64 65 68 public void testCheckSubmenu() { 69 List cInstances = new ArrayList (); 70 cInstances.add(new Act1()); 71 cInstances.add(new Act2()); 72 JMenu m = new JMenu (); 73 DynaMenuModel instance = new DynaMenuModel(); 74 75 instance.loadSubmenu(cInstances, m); 76 instance.checkSubmenu(m); 77 78 Component [] comps = m.getPopupMenu().getComponents(); 79 assertEquals("0", ((JMenuItem )comps[0]).getText()); 80 assertEquals("1x", ((JMenuItem )comps[1]).getText()); 81 assertEquals("2x", ((JMenuItem )comps[2]).getText()); 82 83 } 84 85 86 87 public void testSeparators() { 88 JPopupMenu menu = new JPopupMenu (); 89 menu.add(new JMenu ()); 90 menu.add(new JSeparator ()); 91 menu.add(new DynaMenuModel.InvisibleMenuItem()); 92 menu.add(new JSeparator ()); 93 menu.add(new JMenuItem ()); 94 DynaMenuModel.checkSeparators(menu.getComponents(), menu); 95 Component [] menus = menu.getComponents(); 96 assertTrue(menus[1].isVisible()); 97 assertFalse(menus[3].isVisible()); 98 99 menu = new JPopupMenu (); 100 menu.add(new JMenu ()); 101 menu.add(new JSeparator ()); 102 menu.add(new DynaMenuModel.InvisibleMenuItem()); 103 menu.add(new JMenuItem ()); 104 menu.add(new DynaMenuModel.InvisibleMenuItem()); 105 menu.add(new JSeparator ()); 106 menu.add(new JSeparator ()); 107 menu.add(new JMenuItem ()); 108 DynaMenuModel.checkSeparators(menu.getComponents(), menu); 109 Component [] menus2 = menu.getComponents(); 110 assertTrue(menus2[1].isVisible()); 111 assertTrue(menus2[5].isVisible()); 112 assertFalse(menus2[6].isVisible()); 113 114 115 116 } 117 118 private class Act1 extends AbstractAction implements Presenter.Menu { 119 public void actionPerformed(ActionEvent actionEvent) { 120 } 121 122 public JMenuItem getMenuPresenter() { 123 return new JMenuItem ("0"); 124 } 125 } 126 127 private class Act2 extends AbstractAction implements Presenter.Menu { 128 public void actionPerformed(ActionEvent actionEvent) { 129 } 130 131 public JMenuItem getMenuPresenter() { 132 return new Dyna(); 133 } 134 } 135 136 private class Dyna extends JMenuItem implements DynamicMenuContent { 137 private JMenuItem itm1; 138 private JMenuItem itm2; 139 public JComponent [] getMenuPresenters() { 140 itm1 = new JMenuItem (); 141 itm1.setText("1"); 142 itm2 = new JMenuItem (); 143 itm2.setText("2"); 144 return new JComponent [] { 145 itm1, 146 itm2 147 }; 148 } 149 150 public JComponent [] synchMenuPresenters(JComponent [] items) { 151 ((JMenuItem )items[0]).setText("1x"); 152 ((JMenuItem )items[1]).setText("2x"); 153 return items; 154 } 155 } 156 157 } 158 | Popular Tags |