KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
25 import java.util.ResourceBundle JavaDoc;
26 import javax.swing.*;
27 import javax.swing.event.MenuListener JavaDoc;
28 import javax.swing.event.MenuEvent JavaDoc;
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 /**
38  * Action class providing popup menu presenter for align submenu.
39  *
40  * @author Martin Grebac
41  */

42
43 public class AlignAction extends NodeAction {
44
45     private JMenuItem[] items;
46     
47     protected boolean enable(Node[] nodes) {
48         List JavaDoc comps = FormUtils.getSelectedLayoutComponents(nodes);
49         return ((comps != null) && (comps.size() > 1));
50     }
51     
52     public String JavaDoc getName() {
53         return NbBundle.getMessage(AlignAction.class, "ACT_Align"); // NOI18N
54
}
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     /**
67      * Returns a JMenuItem that presents this action in a Popup Menu.
68      * @return the JMenuItem representation for the action
69      */

70     public JMenuItem getPopupPresenter() {
71         JMenu popupMenu = new JMenu(
72             NbBundle.getMessage(AlignAction.class, "ACT_Align")); // NOI18N
73

74         popupMenu.setEnabled(isEnabled());
75         HelpCtx.setHelpIDString(popupMenu, AlignAction.class.getName());
76         
77         popupMenu.addMenuListener(new MenuListener JavaDoc() {
78             public void menuSelected(MenuEvent JavaDoc e) {
79                 JMenu menu = (JMenu) e.getSource();
80                 createAlignSubmenu(menu);
81             }
82             
83             public void menuDeselected(MenuEvent JavaDoc e) {}
84             
85             public void menuCanceled(MenuEvent JavaDoc e) {}
86         });
87         return popupMenu;
88     }
89
90
91     private void createAlignSubmenu(JMenu menu) {
92         Node[] nodes = getActivatedNodes();
93         List JavaDoc components = FormUtils.getSelectedLayoutComponents(nodes);
94         if (!(menu.getMenuComponentCount() > 0)) {
95             ResourceBundle JavaDoc bundle = NbBundle.getBundle(AlignAction.class);
96
97             JMenuItem leftGroupItem = new AlignMenuItem(
98                     bundle.getString("CTL_GroupLeft"), // NOI18N
99
components,
100                     0);
101             JMenuItem rightGroupItem = new AlignMenuItem(
102                     bundle.getString("CTL_GroupRight"), // NOI18N
103
components,
104                     1);
105             JMenuItem upGroupItem = new AlignMenuItem(
106                     bundle.getString("CTL_GroupUp"), // NOI18N
107
components,
108                     2);
109             JMenuItem downGroupItem = new AlignMenuItem(
110                     bundle.getString("CTL_GroupDown"), // NOI18N
111
components,
112                     3);
113             JMenuItem leftItem = new AlignMenuItem(
114                     bundle.getString("CTL_AlignLeft"), // NOI18N
115
components,
116                     4);
117             JMenuItem rightItem = new AlignMenuItem(
118                     bundle.getString("CTL_AlignRight"), // NOI18N
119
components,
120                     5);
121             JMenuItem upItem = new AlignMenuItem(
122                     bundle.getString("CTL_AlignUp"), // NOI18N
123
components,
124                     6);
125             JMenuItem downItem = new AlignMenuItem(
126                     bundle.getString("CTL_AlignDown"), // NOI18N
127
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 JavaDoc 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 JavaDoc getMenuItemListener() {
156         if (menuItemListener == null)
157             menuItemListener = new AlignMenuItemListener();
158         return menuItemListener;
159     }
160
161     // --------
162

163     private static class AlignMenuItem extends JMenuItem {
164         private int direction;
165         private List JavaDoc components;
166
167         AlignMenuItem(String JavaDoc text, List JavaDoc 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 JavaDoc getRADComponents() {
178             return components;
179         }
180     }
181
182     private static class AlignMenuItemListener implements ActionListener JavaDoc {
183         public void actionPerformed(ActionEvent JavaDoc evt) {
184             Object JavaDoc 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 JavaDoc menuItemListener;
201 }
202
Popular Tags