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 javax.swing.*;
045:
046: import org.openide.util.HelpCtx;
047: import org.openide.util.actions.CallableSystemAction;
048: import org.openide.nodes.Node;
049: import org.openide.nodes.NodeAcceptor;
050:
051: import org.netbeans.modules.form.palette.PaletteItem;
052: import org.netbeans.modules.form.palette.PaletteMenuView;
053: import org.netbeans.modules.form.*;
054: import org.openide.DialogDisplayer;
055: import org.openide.NotifyDescriptor;
056:
057: /**
058: * Action allowing to choose a component from palette content and add it to
059: * the selected containers in current form. Presented only in contextual menus
060: * within the Form Editor.
061: *
062: * @author Tomas Pavek
063: */
064:
065: public class AddAction extends CallableSystemAction {
066:
067: private static String name;
068:
069: public String getName() {
070: if (name == null)
071: name = org.openide.util.NbBundle.getBundle(AddAction.class)
072: .getString("ACT_Add"); // NOI18N
073: return name;
074: }
075:
076: public HelpCtx getHelpCtx() {
077: return new HelpCtx(AddAction.class);
078: }
079:
080: @Override
081: public boolean isEnabled() {
082: Node[] nodes = getNodes();
083: for (int i = 0; i < nodes.length; i++) {
084: FormCookie formCookie = nodes[i]
085: .getCookie(FormCookie.class);
086: if (formCookie == null)
087: return false;
088:
089: RADComponentCookie radCookie = nodes[i]
090: .getCookie(RADComponentCookie.class);
091: if (radCookie != null
092: && !(radCookie.getRADComponent() instanceof ComponentContainer))
093: return false;
094: }
095: return true;
096: }
097:
098: @Override
099: public JMenuItem getMenuPresenter() {
100: return getPopupPresenter();
101: }
102:
103: @Override
104: public JMenuItem getPopupPresenter() {
105: JMenuItem menu = new PaletteMenuView(new NodeAcceptor() {
106: public boolean acceptNodes(Node[] nodes) {
107: if (nodes.length != 1)
108: return false;
109:
110: PaletteItem paletteItem = nodes[0]
111: .getCookie(PaletteItem.class);
112: if (paletteItem == null)
113: return false;
114: String chooseBeanType = null;
115: if (PaletteItem.TYPE_CHOOSE_BEAN.equals(paletteItem
116: .getExplicitComponentType())) {
117: NotifyDescriptor.InputLine desc = new NotifyDescriptor.InputLine(
118: FormUtils
119: .getBundleString("MSG_Choose_Bean"), // NOI18N
120: FormUtils
121: .getBundleString("TITLE_Choose_Bean")); // NOI18N
122: DialogDisplayer.getDefault().notify(desc);
123: if (NotifyDescriptor.OK_OPTION.equals(desc
124: .getValue())) {
125: chooseBeanType = desc.getInputText();
126: } else {
127: return false;
128: }
129: }
130:
131: nodes = getNodes();
132: if (nodes.length == 0)
133: return false;
134:
135: boolean added = false;
136:
137: for (int i = 0; i < nodes.length; i++) {
138: FormCookie formCookie = nodes[i]
139: .getCookie(FormCookie.class);
140: if (formCookie == null)
141: continue;
142:
143: RADComponentCookie radCookie = nodes[i]
144: .getCookie(RADComponentCookie.class);
145: RADComponent targetComponent;
146: if (radCookie != null) {
147: targetComponent = radCookie.getRADComponent();
148: if (!(targetComponent instanceof ComponentContainer))
149: continue;
150: } else
151: targetComponent = null;
152:
153: FormModel formModel = formCookie.getFormModel();
154: if (chooseBeanType != null) {
155: paletteItem.setClassFromCurrentProject(
156: chooseBeanType, FormEditor
157: .getFormDataObject(formModel)
158: .getPrimaryFile());
159: }
160: if (formModel.getComponentCreator()
161: .createComponent(
162: paletteItem
163: .getComponentClassSource(),
164: targetComponent, null) != null) {
165: added = true;
166: }
167: }
168:
169: return added;
170: }
171: });
172:
173: menu.setText(getName());
174: menu.setEnabled(isEnabled());
175: HelpCtx.setHelpIDString(menu, AddAction.class.getName());
176: return menu;
177: }
178:
179: @Override
180: protected boolean asynchronous() {
181: return false;
182: }
183:
184: public void performAction() {
185: }
186:
187: // -------
188:
189: private static Node[] getNodes() {
190: // using NodeAction and global activated nodes is not reliable
191: // (activated nodes are set with a delay after selection in
192: // ComponentInspector)
193: return ComponentInspector.getInstance().getExplorerManager()
194: .getSelectedNodes();
195: }
196: }
|