001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.actions;
043:
044: import java.awt.event.ActionListener;
045: import java.awt.event.ActionEvent;
046: import java.util.List;
047: import java.util.ResourceBundle;
048: import javax.swing.*;
049: import javax.swing.event.MenuListener;
050: import javax.swing.event.MenuEvent;
051:
052: import org.openide.util.HelpCtx;
053: import org.openide.util.actions.*;
054: import org.openide.nodes.Node;
055: import org.openide.util.NbBundle;
056:
057: import org.netbeans.modules.form.*;
058:
059: /**
060: * Action class providing popup menu presenter for align submenu.
061: *
062: * @author Martin Grebac
063: */
064:
065: public class AlignAction extends NodeAction {
066:
067: private JMenuItem[] items;
068:
069: protected boolean enable(Node[] nodes) {
070: List comps = FormUtils.getSelectedLayoutComponents(nodes);
071: return ((comps != null) && (comps.size() > 1));
072: }
073:
074: public String getName() {
075: return NbBundle.getMessage(AlignAction.class, "ACT_Align"); // NOI18N
076: }
077:
078: public HelpCtx getHelpCtx() {
079: return HelpCtx.DEFAULT_HELP;
080: }
081:
082: protected void performAction(Node[] activatedNodes) {
083: }
084:
085: @Override
086: public JMenuItem getMenuPresenter() {
087: return getPopupPresenter();
088: }
089:
090: /**
091: * Returns a JMenuItem that presents this action in a Popup Menu.
092: * @return the JMenuItem representation for the action
093: */
094: @Override
095: public JMenuItem getPopupPresenter() {
096: JMenu popupMenu = new JMenu(NbBundle.getMessage(
097: AlignAction.class, "ACT_Align")); // NOI18N
098:
099: popupMenu.setEnabled(isEnabled());
100: HelpCtx.setHelpIDString(popupMenu, AlignAction.class.getName());
101:
102: popupMenu.addMenuListener(new MenuListener() {
103: public void menuSelected(MenuEvent e) {
104: JMenu menu = (JMenu) e.getSource();
105: createAlignSubmenu(menu);
106: }
107:
108: public void menuDeselected(MenuEvent e) {
109: }
110:
111: public void menuCanceled(MenuEvent e) {
112: }
113: });
114: return popupMenu;
115: }
116:
117: private void createAlignSubmenu(JMenu menu) {
118: Node[] nodes = getActivatedNodes();
119: List components = FormUtils.getSelectedLayoutComponents(nodes);
120: if (!(menu.getMenuComponentCount() > 0)) {
121: ResourceBundle bundle = NbBundle
122: .getBundle(AlignAction.class);
123:
124: JMenuItem leftGroupItem = new AlignMenuItem(bundle
125: .getString("CTL_GroupLeft"), // NOI18N
126: components, 0);
127: JMenuItem rightGroupItem = new AlignMenuItem(bundle
128: .getString("CTL_GroupRight"), // NOI18N
129: components, 1);
130: JMenuItem centerHGroupItem = new AlignMenuItem(bundle
131: .getString("CTL_GroupHCenter"), // NOI18N
132: components, 2);
133: JMenuItem upGroupItem = new AlignMenuItem(bundle
134: .getString("CTL_GroupUp"), // NOI18N
135: components, 3);
136: JMenuItem downGroupItem = new AlignMenuItem(bundle
137: .getString("CTL_GroupDown"), // NOI18N
138: components, 4);
139: JMenuItem centerVGroupItem = new AlignMenuItem(bundle
140: .getString("CTL_GroupVCenter"), // NOI18N
141: components, 5);
142: JMenuItem leftItem = new AlignMenuItem(bundle
143: .getString("CTL_AlignLeft"), // NOI18N
144: components, 6);
145: JMenuItem rightItem = new AlignMenuItem(bundle
146: .getString("CTL_AlignRight"), // NOI18N
147: components, 7);
148: JMenuItem upItem = new AlignMenuItem(bundle
149: .getString("CTL_AlignUp"), // NOI18N
150: components, 8);
151: JMenuItem downItem = new AlignMenuItem(bundle
152: .getString("CTL_AlignDown"), // NOI18N
153: components, 9);
154: items = new JMenuItem[] { leftGroupItem, rightGroupItem,
155: centerHGroupItem, upGroupItem, downGroupItem,
156: centerVGroupItem, leftItem, rightItem, upItem,
157: downItem };
158: for (int i = 0; i < items.length; i++) {
159: items[i].addActionListener(getMenuItemListener());
160: items[i].setEnabled(false);
161: HelpCtx.setHelpIDString(items[i], AlignAction.class
162: .getName());
163: menu.add(items[i]);
164: if (i == 5) {
165: menu.addSeparator();
166: }
167: }
168: }
169: updateState(components);
170: }
171:
172: private void updateState(List components) {
173: if ((components == null) || (components.size() < 2)) {
174: return;
175: }
176: RADComponent rc = (RADComponent) components.get(0);
177: FormDesigner formDesigner = FormEditor.getFormDesigner(rc
178: .getFormModel());
179: java.util.Collection<Action> col = formDesigner
180: .getDesignerActions(true);
181: assert (items.length == 10);
182: int n = col.size();
183: Action[] actions = col.toArray(new Action[n]);
184: for (int i = 0; i < n; i++) {
185: items[i].setEnabled(actions[i].isEnabled());
186: }
187: items[n].setEnabled(actions[0].isEnabled());
188: items[n + 1].setEnabled(actions[1].isEnabled());
189: items[n + 2].setEnabled(actions[3].isEnabled());
190: items[n + 3].setEnabled(actions[4].isEnabled());
191: }
192:
193: private ActionListener getMenuItemListener() {
194: if (menuItemListener == null)
195: menuItemListener = new AlignMenuItemListener();
196: return menuItemListener;
197: }
198:
199: // --------
200:
201: private static class AlignMenuItem extends JMenuItem {
202: private int direction;
203: private List components;
204:
205: AlignMenuItem(String text, List components, int direction) {
206: super (text);
207: this .components = components;
208: this .direction = direction;
209: }
210:
211: int getDirection() {
212: return direction;
213: }
214:
215: List getRADComponents() {
216: return components;
217: }
218: }
219:
220: private static class AlignMenuItemListener implements
221: ActionListener {
222: public void actionPerformed(ActionEvent evt) {
223: Object source = evt.getSource();
224: if (!(source instanceof AlignMenuItem)) {
225: return;
226: }
227: AlignMenuItem mi = (AlignMenuItem) source;
228: if (!mi.isEnabled()) {
229: return;
230: }
231: int index = mi.getDirection();
232: RADComponent radC = (RADComponent) mi.getRADComponents()
233: .get(0);
234: FormModel fm = radC.getFormModel();
235: FormDesigner fd = FormEditor.getFormDesigner(fm);
236: ((Action) fd.getDesignerActions(false).toArray()[index])
237: .actionPerformed(evt);
238: }
239: }
240:
241: private ActionListener menuItemListener;
242: }
|