KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > actions > ChooseSameSizeAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.form.actions;
21
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import javax.swing.*;
29 import javax.swing.event.MenuListener JavaDoc;
30 import javax.swing.event.MenuEvent JavaDoc;
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 /**
43  * Action class providing popup menu presenter for setthesamesize submenu.
44  *
45  * @author Martin Grebac
46  */

47
48 public class ChooseSameSizeAction extends NodeAction {
49
50     protected boolean enable(Node[] nodes) {
51         List JavaDoc comps = FormUtils.getSelectedLayoutComponents(nodes);
52         return ((comps != null) && (comps.size() > 1));
53     }
54     
55     public String JavaDoc getName() {
56         return NbBundle.getMessage(ChooseSameSizeAction.class, "ACT_ChooseSameSize"); // NOI18N
57
}
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     /**
70      * Returns a JMenuItem that presents this action in a Popup Menu.
71      * @return the JMenuItem representation for the action
72      */

73     public JMenuItem getPopupPresenter() {
74         JMenu popupMenu = new JMenu(
75             NbBundle.getMessage(ChooseSameSizeAction.class, "ACT_ChooseSameSize")); // NOI18N
76

77         popupMenu.setEnabled(isEnabled());
78         HelpCtx.setHelpIDString(popupMenu, ChooseSameSizeAction.class.getName());
79         
80         popupMenu.addMenuListener(new MenuListener JavaDoc() {
81             public void menuSelected(MenuEvent JavaDoc e) {
82                 JMenu menu = (JMenu) e.getSource();
83                 createSameSizeSubmenu(menu);
84             }
85             
86             public void menuDeselected(MenuEvent JavaDoc e) {}
87             
88             public void menuCanceled(MenuEvent JavaDoc 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 JavaDoc components = FormUtils.getSelectedLayoutComponents(nodes);
101         if ((components == null) || (components.size() < 1)) { //FFF
102
return;
103         }
104
105         ResourceBundle JavaDoc bundle = NbBundle.getBundle(ChooseSameSizeAction.class);
106
107         JCheckBoxMenuItem sameSizeItemH = new SameSizeMenuItem(
108                 bundle.getString("CTL_SameSizeHorizontal"), // NOI18N
109
components,
110                 LayoutConstants.HORIZONTAL);
111         JCheckBoxMenuItem sameSizeItemV = new SameSizeMenuItem(
112                 bundle.getString("CTL_SameSizeVertical"), // NOI18N
113
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 JavaDoc getMenuItemListener() {
156         if (menuItemListener == null)
157             menuItemListener = new SameSizeMenuItemListener();
158         return menuItemListener;
159     }
160
161     // --------
162

163     private static class SameSizeMenuItem extends JCheckBoxMenuItem {
164         private int dimension;
165         private List JavaDoc components;
166
167         SameSizeMenuItem(String JavaDoc text, List JavaDoc 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 JavaDoc getRADComponents() {
178             return components;
179         }
180     }
181
182     private static class SameSizeMenuItemListener implements ActionListener JavaDoc {
183         public void actionPerformed(ActionEvent JavaDoc evt) {
184             Object JavaDoc 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 JavaDoc 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") //NOI18N
202
)
203                     );
204                     return;
205                 }
206             }
207                         
208             FormModel formModel = rc.getFormModel();
209             LayoutModel layoutModel = formModel.getLayoutModel();
210             Object JavaDoc layoutUndoMark = layoutModel.getChangeMark();
211             javax.swing.undo.UndoableEdit JavaDoc ue = layoutModel.getUndoableEdit();
212             boolean autoUndo = true;
213
214             try {
215                 List JavaDoc 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 JavaDoc iter = compIds.iterator();
221                     while (iter.hasNext()) {
222                         String JavaDoc compId = (String JavaDoc)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 JavaDoc getComponentIds(List JavaDoc/*<RADComponent>*/ components) {
246         List JavaDoc ids = new ArrayList JavaDoc();
247         Iterator JavaDoc 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 JavaDoc menuItemListener;
258 }
259
Popular Tags