1 19 package org.openide.awt; 20 21 import java.awt.Component ; 22 import javax.swing.AbstractAction ; 23 import javax.swing.Action ; 24 import javax.swing.JComponent ; 25 import javax.swing.JMenuItem ; 26 import javax.swing.JPopupMenu ; 27 28 import junit.framework.*; 29 30 import org.netbeans.junit.*; 31 import org.openide.util.Lookup; 32 import org.openide.util.Utilities; 33 import org.openide.util.actions.Presenter; 34 35 37 public class UtilitiesActionsTest extends NbTestCase { 38 39 public UtilitiesActionsTest(java.lang.String testName) { 40 super(testName); 41 } 42 43 public static void main(java.lang.String [] args) { 44 junit.textui.TestRunner.run(new NbTestSuite(UtilitiesActionsTest.class)); 45 } 46 47 public void testDynamicMenuContent() { 48 Action [] acts = new Action [] { 49 new Act1(), 50 new Act2() 51 52 }; 53 JPopupMenu popup = Utilities.actionsToPopup(acts, Lookup.EMPTY); 54 Component [] comp = popup.getComponents(); 55 boolean onepresent = false; 56 boolean twopresent = false; 57 boolean threepresent = false; 58 boolean fourpresent = false; 59 boolean zeropresent = false; 60 for (int i = 0; i < comp.length; i++) { 61 if ("0".equals(((JMenuItem )comp[i]).getText())) { 62 zeropresent = true; 63 } 64 if ("2".equals(((JMenuItem )comp[i]).getText())) { 65 twopresent = true; 66 } 67 if ("4".equals(((JMenuItem )comp[i]).getText())) { 68 fourpresent = true; 69 } 70 if ("3".equals(((JMenuItem )comp[i]).getText())) { 71 threepresent = true; 72 } 73 if ("1".equals(((JMenuItem )comp[i]).getText())) { 74 onepresent = true; 75 } 76 } 77 assertTrue(threepresent); 78 assertTrue(fourpresent); 79 assertTrue(onepresent); 80 assertTrue(twopresent); 81 assertTrue(zeropresent); 82 } 83 84 private class Act1 extends AbstractAction implements Presenter.Popup, Presenter.Menu { 85 public void actionPerformed(java.awt.event.ActionEvent actionEvent) { 86 } 87 88 public JMenuItem getPopupPresenter() { 89 return new JMenuItem ("0"); 90 } 91 92 public JMenuItem getMenuPresenter() { 93 return new JMenuItem ("0"); 94 } 95 } 96 97 private class Act2 extends AbstractAction implements Presenter.Menu, Presenter.Popup { 98 public void actionPerformed(java.awt.event.ActionEvent actionEvent) { 99 } 100 public JMenuItem getPopupPresenter() { 101 return new Dyna(); 102 } 103 104 public JMenuItem getMenuPresenter() { 105 return new Dyna(); 106 } 107 } 108 109 private class Dyna extends JMenuItem implements DynamicMenuContent { 110 private JMenuItem itm1; 111 private JMenuItem itm2; 112 public JComponent [] getMenuPresenters() { 113 itm1 = new JMenuItem (); 114 itm1.setText("1"); 115 itm2 = new Dyna2(); 116 itm2.setText("2"); 117 return new JComponent [] { 118 itm1, 119 itm2 120 }; 121 } 122 123 public JComponent [] synchMenuPresenters(JComponent [] items) { 124 ((JMenuItem )items[0]).setText("1x"); 125 ((JMenuItem )items[1]).setText("2x"); 126 return items; 127 } 128 } 129 130 private class Dyna2 extends JMenuItem implements DynamicMenuContent { 131 private JMenuItem itm1; 132 private JMenuItem itm2; 133 public JComponent [] getMenuPresenters() { 134 itm1 = new JMenuItem (); 135 itm1.setText("3"); 136 itm2 = new JMenuItem (); 137 itm2.setText("4"); 138 return new JComponent [] { 139 itm1, 140 itm2, 141 this 142 }; 143 } 144 145 public JComponent [] synchMenuPresenters(JComponent [] items) { 146 ((JMenuItem )items[0]).setText("3x"); 147 ((JMenuItem )items[1]).setText("4x"); 148 return items; 149 } 150 } 151 152 } 153 | Popular Tags |