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.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.ResourceBundle ; 28 import javax.swing.*; 29 import javax.swing.event.MenuListener ; 30 import javax.swing.event.MenuEvent ; 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 34 import org.openide.util.HelpCtx; 35 import org.openide.util.actions.*; 36 import org.openide.nodes.Node; 37 import org.openide.util.NbBundle; 38 39 import org.netbeans.modules.form.*; 40 import org.netbeans.modules.form.layoutdesign.*; 41 42 47 48 public class ChooseSameSizeAction extends NodeAction { 49 50 protected boolean enable(Node[] nodes) { 51 List comps = FormUtils.getSelectedLayoutComponents(nodes); 52 return ((comps != null) && (comps.size() > 1)); 53 } 54 55 public String getName() { 56 return NbBundle.getMessage(ChooseSameSizeAction.class, "ACT_ChooseSameSize"); } 58 59 public HelpCtx getHelpCtx() { 60 return HelpCtx.DEFAULT_HELP; 61 } 62 63 protected void performAction(Node[] activatedNodes) { } 64 65 public JMenuItem getMenuPresenter() { 66 return getPopupPresenter(); 67 } 68 69 73 public JMenuItem getPopupPresenter() { 74 JMenu popupMenu = new JMenu( 75 NbBundle.getMessage(ChooseSameSizeAction.class, "ACT_ChooseSameSize")); 77 popupMenu.setEnabled(isEnabled()); 78 HelpCtx.setHelpIDString(popupMenu, ChooseSameSizeAction.class.getName()); 79 80 popupMenu.addMenuListener(new MenuListener () { 81 public void menuSelected(MenuEvent e) { 82 JMenu menu = (JMenu) e.getSource(); 83 createSameSizeSubmenu(menu); 84 } 85 86 public void menuDeselected(MenuEvent e) {} 87 88 public void menuCanceled(MenuEvent e) {} 89 }); 90 return popupMenu; 91 } 92 93 private void createSameSizeSubmenu(JMenu menu) { 94 if (menu.getMenuComponentCount() > 0) { 95 menu.removeAll(); 96 } 97 98 Node[] nodes = getActivatedNodes(); 99 100 List components = FormUtils.getSelectedLayoutComponents(nodes); 101 if ((components == null) || (components.size() < 1)) { return; 103 } 104 105 ResourceBundle bundle = NbBundle.getBundle(ChooseSameSizeAction.class); 106 107 JCheckBoxMenuItem sameSizeItemH = new SameSizeMenuItem( 108 bundle.getString("CTL_SameSizeHorizontal"), components, 110 LayoutConstants.HORIZONTAL); 111 JCheckBoxMenuItem sameSizeItemV = new SameSizeMenuItem( 112 bundle.getString("CTL_SameSizeVertical"), components, 114 LayoutConstants.VERTICAL); 115 116 RADComponent c = (RADComponent)components.get(0); 117 LayoutModel lModel = c.getFormModel().getLayoutModel(); 118 119 int hLinked = lModel.areComponentsLinkSized(getComponentIds(components), LayoutConstants.HORIZONTAL); 120 int vLinked = lModel.areComponentsLinkSized(getComponentIds(components), LayoutConstants.VERTICAL); 121 122 if (components.size() > 1) { 123 if (hLinked != LayoutConstants.INVALID) { 124 sameSizeItemH.setSelected((hLinked == LayoutConstants.TRUE) ? true : false); 125 } else { 126 sameSizeItemH.setEnabled(false); 127 } 128 129 if (vLinked != LayoutConstants.INVALID) { 130 sameSizeItemV.setSelected((vLinked == LayoutConstants.TRUE) ? true : false); 131 } else { 132 sameSizeItemV.setEnabled(false); 133 } 134 } else { 135 sameSizeItemH.setSelected((hLinked == LayoutConstants.TRUE) ? true : false); 136 if (hLinked != LayoutConstants.TRUE) { 137 sameSizeItemH.setEnabled(false); 138 } 139 sameSizeItemV.setSelected((vLinked == LayoutConstants.TRUE) ? true : false); 140 if (vLinked != LayoutConstants.TRUE) { 141 sameSizeItemV.setEnabled(false); 142 } 143 } 144 145 sameSizeItemH.addActionListener(getMenuItemListener()); 146 sameSizeItemV.addActionListener(getMenuItemListener()); 147 148 HelpCtx.setHelpIDString(sameSizeItemH, ChooseSameSizeAction.class.getName()); 149 HelpCtx.setHelpIDString(sameSizeItemV, ChooseSameSizeAction.class.getName()); 150 151 menu.add(sameSizeItemH); 152 menu.add(sameSizeItemV); 153 } 154 155 private ActionListener getMenuItemListener() { 156 if (menuItemListener == null) 157 menuItemListener = new SameSizeMenuItemListener(); 158 return menuItemListener; 159 } 160 161 163 private static class SameSizeMenuItem extends JCheckBoxMenuItem { 164 private int dimension; 165 private List components; 166 167 SameSizeMenuItem(String text, List components, int direction) { 168 super(text); 169 this.components = components; 170 this.dimension = direction; 171 } 172 173 int getDimension() { 174 return dimension; 175 } 176 177 List getRADComponents() { 178 return components; 179 } 180 } 181 182 private static class SameSizeMenuItemListener implements ActionListener { 183 public void actionPerformed(ActionEvent evt) { 184 Object source = evt.getSource(); 185 if (!(source instanceof SameSizeMenuItem)) { 186 return; 187 } 188 SameSizeMenuItem mi = (SameSizeMenuItem) source; 189 if (!mi.isEnabled()) { 190 return; 191 } 192 List radComponents = mi.getRADComponents(); 193 RADComponent rc = (RADComponent)radComponents.get(0); 194 RADVisualContainer visCont = ((RADVisualComponent)rc).getParentContainer(); 195 196 for (int i = 0; i < radComponents.size(); i++) { 197 RADComponent rcomp = (RADComponent)radComponents.get(i); 198 if (!((RADVisualComponent)rcomp).getParentContainer().equals(visCont)) { 199 DialogDisplayer.getDefault().notify( 200 new NotifyDescriptor.Message( 201 NbBundle.getMessage(ChooseSameSizeAction.class, "TXT_ComponentsNotInOneContainer") ) 203 ); 204 return; 205 } 206 } 207 208 FormModel formModel = rc.getFormModel(); 209 LayoutModel layoutModel = formModel.getLayoutModel(); 210 Object layoutUndoMark = layoutModel.getChangeMark(); 211 javax.swing.undo.UndoableEdit ue = layoutModel.getUndoableEdit(); 212 boolean autoUndo = true; 213 214 try { 215 List compIds = getComponentIds(mi.getRADComponents()); 216 int dimension = mi.getDimension(); 217 if (mi.isSelected()) { 218 FormDesigner designer = FormEditor.getFormDesigner(formModel); 219 LayoutDesigner lDesigner = designer.getLayoutDesigner(); 220 Iterator iter = compIds.iterator(); 221 while (iter.hasNext()) { 222 String compId = (String )iter.next(); 223 LayoutComponent lc = layoutModel.getLayoutComponent(compId); 224 if (lDesigner.isComponentResizing(lc, dimension)) { 225 lDesigner.setComponentResizing(lc, dimension, false); 226 } 227 } 228 layoutModel.setSameSize(compIds, dimension); 229 } else { 230 layoutModel.unsetSameSize(compIds, dimension); 231 } 232 autoUndo = false; 233 } finally { 234 formModel.fireContainerLayoutChanged(visCont, null, null, null); 235 if (!layoutUndoMark.equals(layoutModel.getChangeMark())) { 236 formModel.addUndoableEdit(ue); 237 } 238 if (autoUndo) { 239 formModel.forceUndoOfCompoundEdit(); 240 } 241 } 242 } 243 } 244 245 private static List getComponentIds(List components) { 246 List ids = new ArrayList (); 247 Iterator i = components.iterator(); 248 while (i.hasNext()) { 249 RADComponent rc = (RADComponent)i.next(); 250 if (rc != null) { 251 ids.add(rc.getId()); 252 } 253 } 254 return ids; 255 } 256 257 private ActionListener menuItemListener; 258 } 259 | Popular Tags |