1 19 20 package org.netbeans.modules.form.actions; 21 22 import java.awt.event.ActionListener ; 23 import java.awt.event.ActionEvent ; 24 import java.util.List ; 25 import java.util.ResourceBundle ; 26 import javax.swing.*; 27 import javax.swing.event.MenuListener ; 28 import javax.swing.event.MenuEvent ; 29 30 import org.openide.util.HelpCtx; 31 import org.openide.util.actions.*; 32 import org.openide.nodes.Node; 33 import org.openide.util.NbBundle; 34 35 import org.netbeans.modules.form.*; 36 37 42 43 public class SetResizabilityAction extends NodeAction { 44 45 private JCheckBoxMenuItem[] items; 46 47 protected boolean enable(Node[] nodes) { 48 List comps = FormUtils.getSelectedLayoutComponents(nodes); 49 return ((comps != null) && (comps.size() > 0)); 50 } 51 52 public String getName() { 53 return NbBundle.getMessage(SetResizabilityAction.class, "ACT_SetResizability"); } 55 56 public HelpCtx getHelpCtx() { 57 return HelpCtx.DEFAULT_HELP; 58 } 59 60 protected void performAction(Node[] activatedNodes) { } 61 62 public JMenuItem getMenuPresenter() { 63 return getPopupPresenter(); 64 } 65 66 70 public JMenuItem getPopupPresenter() { 71 JMenu popupMenu = new JMenu( 72 NbBundle.getMessage(SetResizabilityAction.class, "ACT_SetResizability")); 74 popupMenu.setEnabled(isEnabled()); 75 HelpCtx.setHelpIDString(popupMenu, SetResizabilityAction.class.getName()); 76 77 popupMenu.addMenuListener(new MenuListener () { 78 public void menuSelected(MenuEvent e) { 79 JMenu menu = (JMenu) e.getSource(); 80 createResizabilitySubmenu(menu); 81 } 82 83 public void menuDeselected(MenuEvent e) {} 84 85 public void menuCanceled(MenuEvent e) {} 86 }); 87 return popupMenu; 88 } 89 90 private void createResizabilitySubmenu(JMenu menu) { 91 Node[] nodes = getActivatedNodes(); 92 List components = FormUtils.getSelectedLayoutComponents(nodes); 93 if ((components == null) || (components.size() < 1)) { 94 return; 95 } 96 if (!(menu.getMenuComponentCount() > 0)) { 97 ResourceBundle bundle = NbBundle.getBundle(SetResizabilityAction.class); 98 99 JCheckBoxMenuItem hItem = new ResizabilityMenuItem( 100 bundle.getString("CTL_ResizabilityH"), components, 102 0); 103 JCheckBoxMenuItem vItem = new ResizabilityMenuItem( 104 bundle.getString("CTL_ResizabilityV"), components, 106 1); 107 items = new JCheckBoxMenuItem[] {hItem, vItem}; 108 109 for (int i=0; i<2; i++) { 110 items[i].addActionListener(getMenuItemListener()); 111 HelpCtx.setHelpIDString(items[i], SetResizabilityAction.class.getName()); 112 menu.add(items[i]); 113 } 114 } 115 updateState(components); 116 } 117 118 private void updateState(List components) { 119 if ((components == null) || (components.size()<1)) { 120 return; 121 } 122 RADComponent rc = (RADComponent)components.get(0); 123 FormDesigner formDesigner = FormEditor.getFormDesigner(rc.getFormModel()); 124 formDesigner.updateResizabilityActions(); 125 for (int i=0; i<2; i++) { 126 items[i].setEnabled(formDesigner.getResizabilityButtons()[i].isEnabled()); 127 items[i].setSelected(formDesigner.getResizabilityButtons()[i].isSelected()); 128 } 129 } 130 131 private ActionListener getMenuItemListener() { 132 if (menuItemListener == null) 133 menuItemListener = new ResizabilityMenuItemListener(); 134 return menuItemListener; 135 } 136 137 139 private static class ResizabilityMenuItem extends JCheckBoxMenuItem { 140 private int direction; 141 private List components; 142 143 ResizabilityMenuItem(String text, List components, int direction) { 144 super(text); 145 this.components = components; 146 this.direction = direction; 147 } 148 149 int getDirection() { 150 return direction; 151 } 152 153 List getRADComponents() { 154 return components; 155 } 156 } 157 158 private static class ResizabilityMenuItemListener implements ActionListener { 159 public void actionPerformed(ActionEvent evt) { 160 Object source = evt.getSource(); 161 if (!(source instanceof ResizabilityMenuItem)) { 162 return; 163 } 164 ResizabilityMenuItem mi = (ResizabilityMenuItem) source; 165 if (!mi.isEnabled()) { 166 return; 167 } 168 int index = mi.getDirection(); 169 RADComponent radC = (RADComponent)mi.getRADComponents().get(0); 170 FormModel fm = radC.getFormModel(); 171 FormDesigner fd = FormEditor.getFormDesigner(fm); 172 fd.getResizabilityButtons()[index].setSelected(!fd.getResizabilityButtons()[index].isSelected()); 173 ((Action)fd.getResizabilityActions().toArray()[index]).actionPerformed(evt); 174 } 175 } 176 177 private ActionListener menuItemListener; 178 } 179 | Popular Tags |