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 AlignAction extends NodeAction { 44 45 private JMenuItem[] items; 46 47 protected boolean enable(Node[] nodes) { 48 List comps = FormUtils.getSelectedLayoutComponents(nodes); 49 return ((comps != null) && (comps.size() > 1)); 50 } 51 52 public String getName() { 53 return NbBundle.getMessage(AlignAction.class, "ACT_Align"); } 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(AlignAction.class, "ACT_Align")); 74 popupMenu.setEnabled(isEnabled()); 75 HelpCtx.setHelpIDString(popupMenu, AlignAction.class.getName()); 76 77 popupMenu.addMenuListener(new MenuListener () { 78 public void menuSelected(MenuEvent e) { 79 JMenu menu = (JMenu) e.getSource(); 80 createAlignSubmenu(menu); 81 } 82 83 public void menuDeselected(MenuEvent e) {} 84 85 public void menuCanceled(MenuEvent e) {} 86 }); 87 return popupMenu; 88 } 89 90 91 private void createAlignSubmenu(JMenu menu) { 92 Node[] nodes = getActivatedNodes(); 93 List components = FormUtils.getSelectedLayoutComponents(nodes); 94 if (!(menu.getMenuComponentCount() > 0)) { 95 ResourceBundle bundle = NbBundle.getBundle(AlignAction.class); 96 97 JMenuItem leftGroupItem = new AlignMenuItem( 98 bundle.getString("CTL_GroupLeft"), components, 100 0); 101 JMenuItem rightGroupItem = new AlignMenuItem( 102 bundle.getString("CTL_GroupRight"), components, 104 1); 105 JMenuItem upGroupItem = new AlignMenuItem( 106 bundle.getString("CTL_GroupUp"), components, 108 2); 109 JMenuItem downGroupItem = new AlignMenuItem( 110 bundle.getString("CTL_GroupDown"), components, 112 3); 113 JMenuItem leftItem = new AlignMenuItem( 114 bundle.getString("CTL_AlignLeft"), components, 116 4); 117 JMenuItem rightItem = new AlignMenuItem( 118 bundle.getString("CTL_AlignRight"), components, 120 5); 121 JMenuItem upItem = new AlignMenuItem( 122 bundle.getString("CTL_AlignUp"), components, 124 6); 125 JMenuItem downItem = new AlignMenuItem( 126 bundle.getString("CTL_AlignDown"), components, 128 7); 129 items = new JMenuItem[] {leftGroupItem, rightGroupItem, upGroupItem, 130 downGroupItem, leftItem, rightItem, upItem, downItem}; 131 for (int i=0; i<8; i++) { 132 items[i].addActionListener(getMenuItemListener()); 133 items[i].setEnabled(false); 134 HelpCtx.setHelpIDString(items[i], AlignAction.class.getName()); 135 menu.add(items[i]); 136 if (i == 3) menu.addSeparator(); 137 } 138 } 139 updateState(components); 140 } 141 142 private void updateState(List components) { 143 if ((components == null) || (components.size()<2)) { 144 return; 145 } 146 RADComponent rc = (RADComponent)components.get(0); 147 FormDesigner formDesigner = FormEditor.getFormDesigner(rc.getFormModel()); 148 for (int i=0; i<4; i++) { 149 Action a = (Action)formDesigner.getDesignerActions(true).toArray()[i]; 150 items[i].setEnabled(a.isEnabled()); 151 items[i+4].setEnabled(a.isEnabled()); 152 } 153 } 154 155 private ActionListener getMenuItemListener() { 156 if (menuItemListener == null) 157 menuItemListener = new AlignMenuItemListener(); 158 return menuItemListener; 159 } 160 161 163 private static class AlignMenuItem extends JMenuItem { 164 private int direction; 165 private List components; 166 167 AlignMenuItem(String text, List components, int direction) { 168 super(text); 169 this.components = components; 170 this.direction = direction; 171 } 172 173 int getDirection() { 174 return direction; 175 } 176 177 List getRADComponents() { 178 return components; 179 } 180 } 181 182 private static class AlignMenuItemListener implements ActionListener { 183 public void actionPerformed(ActionEvent evt) { 184 Object source = evt.getSource(); 185 if (!(source instanceof AlignMenuItem)) { 186 return; 187 } 188 AlignMenuItem mi = (AlignMenuItem) source; 189 if (!mi.isEnabled()) { 190 return; 191 } 192 int index = mi.getDirection(); 193 RADComponent radC = (RADComponent)mi.getRADComponents().get(0); 194 FormModel fm = radC.getFormModel(); 195 FormDesigner fd = FormEditor.getFormDesigner(fm); 196 ((Action)fd.getDesignerActions(false).toArray()[index]).actionPerformed(evt); 197 } 198 } 199 200 private ActionListener menuItemListener; 201 } 202 | Popular Tags |