KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ResourceBundle JavaDoc;
28 import java.util.Set JavaDoc;
29 import javax.swing.*;
30 import javax.swing.event.MenuListener JavaDoc;
31 import javax.swing.event.MenuEvent JavaDoc;
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 /**
45  * Action class providing popup menu presenter for setanchoring submenu.
46  *
47  * @author Martin Grebac
48  */

49
50 public class SetAnchoringAction extends NodeAction {
51
52     private JCheckBoxMenuItem[] items;
53     
54     protected boolean enable(Node[] nodes) {
55         List JavaDoc comps = FormUtils.getSelectedLayoutComponents(nodes);
56         return ((comps != null) && (comps.size() > 0));
57     }
58     
59     public String JavaDoc getName() {
60         return NbBundle.getMessage(SetAnchoringAction.class, "ACT_SetAnchoring"); // NOI18N
61
}
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     /**
74      * Returns a JMenuItem that presents this action in a Popup Menu.
75      * @return the JMenuItem representation for the action
76      */

77     public JMenuItem getPopupPresenter() {
78         JMenu popupMenu = new JMenu(
79             NbBundle.getMessage(SetAnchoringAction.class, "ACT_SetAnchoring")); // NOI18N
80

81         popupMenu.setEnabled(isEnabled());
82         HelpCtx.setHelpIDString(popupMenu, SetAnchoringAction.class.getName());
83         
84         popupMenu.addMenuListener(new MenuListener JavaDoc() {
85             public void menuSelected(MenuEvent JavaDoc e) {
86                 JMenu menu = (JMenu) e.getSource();
87                 createAnchoringSubmenu(menu);
88             }
89             
90             public void menuDeselected(MenuEvent JavaDoc e) {}
91             
92             public void menuCanceled(MenuEvent JavaDoc e) {}
93         });
94         return popupMenu;
95     }
96
97     private void createAnchoringSubmenu(JMenu menu) {
98         Node[] nodes = getActivatedNodes();
99         List JavaDoc components = FormUtils.getSelectedLayoutComponents(nodes);
100         if ((components == null) || (components.size() < 1)) {
101             return;
102         }
103         if (!(menu.getMenuComponentCount() > 0)) {
104             ResourceBundle JavaDoc bundle = NbBundle.getBundle(SetAnchoringAction.class);
105
106             JCheckBoxMenuItem leftItem = new AnchoringMenuItem(
107                     bundle.getString("CTL_AnchorLeft"), // NOI18N
108
components,
109                     0);
110             JCheckBoxMenuItem rightItem = new AnchoringMenuItem(
111                     bundle.getString("CTL_AnchorRight"), // NOI18N
112
components,
113                     1);
114             JCheckBoxMenuItem topItem = new AnchoringMenuItem(
115                     bundle.getString("CTL_AnchorTop"), // NOI18N
116
components,
117                     2);
118             JCheckBoxMenuItem bottomItem = new AnchoringMenuItem(
119                     bundle.getString("CTL_AnchorBottom"), // NOI18N
120
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 JavaDoc 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 JavaDoc 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 JavaDoc 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) { // the alignment cannot be changed
152
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 JavaDoc getMenuItemListener() {
170         if (menuItemListener == null)
171             menuItemListener = new AnchoringMenuItemListener();
172         return menuItemListener;
173     }
174
175     // --------
176

177     private static class AnchoringMenuItem extends JCheckBoxMenuItem {
178         private int direction;
179         private List JavaDoc components;
180
181         AnchoringMenuItem(String JavaDoc text, List JavaDoc 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 JavaDoc getRADComponents() {
192             return components;
193         }
194     }
195
196     private static class AnchoringMenuItemListener implements ActionListener JavaDoc {
197         public void actionPerformed(ActionEvent JavaDoc evt) {
198             Object JavaDoc 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 JavaDoc layoutUndoMark = layoutModel.getChangeMark();
210             javax.swing.undo.UndoableEdit JavaDoc ue = layoutModel.getUndoableEdit();
211             FormDesigner formDesigner = FormEditor.getFormDesigner(formModel);
212             LayoutDesigner layoutDesigner = formDesigner.getLayoutDesigner();
213             Set JavaDoc containers = new HashSet JavaDoc();
214             boolean autoUndo = true;
215             try {
216                 Iterator JavaDoc iter = mi.getRADComponents().iterator();
217                 while (iter.hasNext()) {
218                     RADComponent radC = (RADComponent)iter.next();
219                     String JavaDoc 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 JavaDoc 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 JavaDoc menuItemListener;
249 }
250
Popular Tags