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.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ResourceBundle ; 28 import java.util.Set ; 29 import javax.swing.*; 30 import javax.swing.event.MenuListener ; 31 import javax.swing.event.MenuEvent ; 32 import org.netbeans.modules.form.layoutdesign.LayoutComponent; 33 import org.netbeans.modules.form.layoutdesign.LayoutConstants; 34 import org.netbeans.modules.form.layoutdesign.LayoutDesigner; 35 import org.netbeans.modules.form.layoutdesign.LayoutModel; 36 37 import org.openide.util.HelpCtx; 38 import org.openide.util.actions.*; 39 import org.openide.nodes.Node; 40 import org.openide.util.NbBundle; 41 42 import org.netbeans.modules.form.*; 43 44 49 50 public class SetAnchoringAction extends NodeAction { 51 52 private JCheckBoxMenuItem[] items; 53 54 protected boolean enable(Node[] nodes) { 55 List comps = FormUtils.getSelectedLayoutComponents(nodes); 56 return ((comps != null) && (comps.size() > 0)); 57 } 58 59 public String getName() { 60 return NbBundle.getMessage(SetAnchoringAction.class, "ACT_SetAnchoring"); } 62 63 public HelpCtx getHelpCtx() { 64 return HelpCtx.DEFAULT_HELP; 65 } 66 67 protected void performAction(Node[] activatedNodes) { } 68 69 public JMenuItem getMenuPresenter() { 70 return getPopupPresenter(); 71 } 72 73 77 public JMenuItem getPopupPresenter() { 78 JMenu popupMenu = new JMenu( 79 NbBundle.getMessage(SetAnchoringAction.class, "ACT_SetAnchoring")); 81 popupMenu.setEnabled(isEnabled()); 82 HelpCtx.setHelpIDString(popupMenu, SetAnchoringAction.class.getName()); 83 84 popupMenu.addMenuListener(new MenuListener () { 85 public void menuSelected(MenuEvent e) { 86 JMenu menu = (JMenu) e.getSource(); 87 createAnchoringSubmenu(menu); 88 } 89 90 public void menuDeselected(MenuEvent e) {} 91 92 public void menuCanceled(MenuEvent e) {} 93 }); 94 return popupMenu; 95 } 96 97 private void createAnchoringSubmenu(JMenu menu) { 98 Node[] nodes = getActivatedNodes(); 99 List components = FormUtils.getSelectedLayoutComponents(nodes); 100 if ((components == null) || (components.size() < 1)) { 101 return; 102 } 103 if (!(menu.getMenuComponentCount() > 0)) { 104 ResourceBundle bundle = NbBundle.getBundle(SetAnchoringAction.class); 105 106 JCheckBoxMenuItem leftItem = new AnchoringMenuItem( 107 bundle.getString("CTL_AnchorLeft"), components, 109 0); 110 JCheckBoxMenuItem rightItem = new AnchoringMenuItem( 111 bundle.getString("CTL_AnchorRight"), components, 113 1); 114 JCheckBoxMenuItem topItem = new AnchoringMenuItem( 115 bundle.getString("CTL_AnchorTop"), components, 117 2); 118 JCheckBoxMenuItem bottomItem = new AnchoringMenuItem( 119 bundle.getString("CTL_AnchorBottom"), components, 121 3); 122 items = new JCheckBoxMenuItem[] {leftItem, rightItem, topItem, bottomItem}; 123 for (int i=0; i<4; i++) { 124 items[i].addActionListener(getMenuItemListener()); 125 HelpCtx.setHelpIDString(items[i], SetAnchoringAction.class.getName()); 126 menu.add(items[i]); 127 } 128 } 129 updateState(components); 130 } 131 132 private void updateState(List components) { 133 if ((components == null) || (components.size() < 1)) { 134 return; 135 } 136 FormModel formModel = ((RADComponent)components.get(0)).getFormModel(); 137 LayoutModel layoutModel = formModel.getLayoutModel(); 138 FormDesigner formDesigner = FormEditor.getFormDesigner(formModel); 139 LayoutDesigner layoutDesigner = formDesigner.getLayoutDesigner(); 140 Iterator iter = components.iterator(); 141 boolean[] matchAlignment = new boolean[4]; 142 boolean[] cannotChangeTo = new boolean[4]; 143 while (iter.hasNext()) { 144 RADComponent radC = (RADComponent)iter.next(); 145 String id = radC.getId(); 146 LayoutComponent comp = layoutModel.getLayoutComponent(id); 147 int[][] alignment = new int[][] { 148 layoutDesigner.getAdjustableComponentAlignment(comp, LayoutConstants.HORIZONTAL), 149 layoutDesigner.getAdjustableComponentAlignment(comp, LayoutConstants.VERTICAL)}; 150 for (int i=0; i<4; i++) { 151 if ((alignment[i/2][1] & (1 << i%2)) == 0) { cannotChangeTo[i] = true; 153 } 154 if (alignment[i/2][0] != -1) { 155 matchAlignment[i] = matchAlignment[i] || (alignment[i/2][0] == i%2); 156 } 157 } 158 } 159 for (int i=0; i<4; i++) { 160 boolean match; 161 boolean miss; 162 match = matchAlignment[i]; 163 miss = matchAlignment[2*(i/2) + 1 - i%2]; 164 items[i].setEnabled((match || miss) && (!cannotChangeTo[i])); 165 items[i].setSelected(!miss && match); 166 } 167 } 168 169 private ActionListener getMenuItemListener() { 170 if (menuItemListener == null) 171 menuItemListener = new AnchoringMenuItemListener(); 172 return menuItemListener; 173 } 174 175 177 private static class AnchoringMenuItem extends JCheckBoxMenuItem { 178 private int direction; 179 private List components; 180 181 AnchoringMenuItem(String text, List components, int direction) { 182 super(text); 183 this.components = components; 184 this.direction = direction; 185 } 186 187 int getDirection() { 188 return direction; 189 } 190 191 List getRADComponents() { 192 return components; 193 } 194 } 195 196 private static class AnchoringMenuItemListener implements ActionListener { 197 public void actionPerformed(ActionEvent evt) { 198 Object source = evt.getSource(); 199 if (!(source instanceof AnchoringMenuItem)) { 200 return; 201 } 202 AnchoringMenuItem mi = (AnchoringMenuItem) source; 203 if (!mi.isEnabled()) { 204 return; 205 } 206 int index = mi.getDirection(); 207 FormModel formModel = ((RADComponent)mi.getRADComponents().get(0)).getFormModel(); 208 LayoutModel layoutModel = formModel.getLayoutModel(); 209 Object layoutUndoMark = layoutModel.getChangeMark(); 210 javax.swing.undo.UndoableEdit ue = layoutModel.getUndoableEdit(); 211 FormDesigner formDesigner = FormEditor.getFormDesigner(formModel); 212 LayoutDesigner layoutDesigner = formDesigner.getLayoutDesigner(); 213 Set containers = new HashSet (); 214 boolean autoUndo = true; 215 try { 216 Iterator iter = mi.getRADComponents().iterator(); 217 while (iter.hasNext()) { 218 RADComponent radC = (RADComponent)iter.next(); 219 String compId = radC.getId(); 220 LayoutComponent layoutComp = layoutModel.getLayoutComponent(compId); 221 boolean changed = false; 222 int[] alignment = layoutDesigner.getAdjustableComponentAlignment(layoutComp, index/2); 223 if (((alignment[1] & (1 << index%2)) != 0) && (alignment[0] != index%2)) { 224 layoutDesigner.adjustComponentAlignment(layoutComp, index/2, index%2); 225 changed = true; 226 } 227 if (changed) { 228 RADVisualComponent comp = (RADVisualComponent)formModel.getMetaComponent(compId); 229 containers.add(comp.getParentContainer()); 230 } 231 } 232 autoUndo = false; 233 } finally { 234 Iterator iter = containers.iterator(); 235 while (iter.hasNext()) { 236 formModel.fireContainerLayoutChanged((RADVisualContainer)iter.next(), null, null, null); 237 } 238 if (!layoutUndoMark.equals(layoutModel.getChangeMark())) { 239 formModel.addUndoableEdit(ue); 240 } 241 if (autoUndo) { 242 formModel.forceUndoOfCompoundEdit(); 243 } 244 } 245 } 246 } 247 248 private ActionListener menuItemListener; 249 } 250 | Popular Tags |