KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.*;
23
24 import org.openide.util.HelpCtx;
25 import org.openide.util.actions.CallableSystemAction;
26 import org.openide.nodes.Node;
27 import org.openide.nodes.NodeAcceptor;
28
29 import org.netbeans.modules.form.palette.PaletteItem;
30 import org.netbeans.modules.form.palette.PaletteMenuView;
31 import org.netbeans.modules.form.*;
32
33 /**
34  * Action allowing to choose a component from palette content and add it to
35  * the selected containers in current form. Presented only in contextual menus
36  * within the Form Editor.
37  *
38  * @author Tomas Pavek
39  */

40
41 public class AddAction extends CallableSystemAction {
42
43     private static String JavaDoc name;
44
45     public String JavaDoc getName() {
46         if (name == null)
47             name = org.openide.util.NbBundle.getBundle(AddAction.class)
48                      .getString("ACT_Add"); // NOI18N
49
return name;
50     }
51
52     public HelpCtx getHelpCtx() {
53         return new HelpCtx(AddAction.class);
54     }
55
56     public boolean isEnabled() {
57         Node[] nodes = getNodes();
58         for (int i=0; i < nodes.length; i++) {
59             FormCookie formCookie =
60                 (FormCookie) nodes[i].getCookie(FormCookie.class);
61             if (formCookie == null)
62                 return false;
63
64             RADComponentCookie radCookie =
65                 (RADComponentCookie) nodes[i].getCookie(RADComponentCookie.class);
66             if (radCookie != null
67                   && !(radCookie.getRADComponent() instanceof ComponentContainer))
68                 return false;
69         }
70         return true;
71     }
72
73     public JMenuItem getMenuPresenter() {
74         return getPopupPresenter();
75     }
76
77     public JMenuItem getPopupPresenter() {
78         JMenuItem menu = new PaletteMenuView(
79             new NodeAcceptor() {
80                 public boolean acceptNodes(Node[] nodes) {
81                     if (nodes.length != 1)
82                         return false;
83
84                     PaletteItem paletteItem =
85                         (PaletteItem) nodes[0].getCookie(PaletteItem.class);
86                     if (paletteItem == null)
87                         return false;
88
89                     nodes = getNodes();
90                     if (nodes.length == 0)
91                         return false;
92
93                     boolean added = false;
94
95                     for (int i=0; i < nodes.length; i++) {
96                         FormCookie formCookie =
97                             (FormCookie) nodes[i].getCookie(FormCookie.class);
98                         if (formCookie == null)
99                             continue;
100
101                         RADComponentCookie radCookie = (RADComponentCookie)
102                             nodes[i].getCookie(RADComponentCookie.class);
103                         RADComponent targetComponent;
104                         if (radCookie != null) {
105                             targetComponent = radCookie.getRADComponent();
106                             if (!(targetComponent instanceof ComponentContainer))
107                                 continue;
108                         }
109                         else targetComponent = null;
110
111                         if (formCookie.getFormModel().getComponentCreator()
112                               .createComponent(paletteItem.getComponentClassSource(),
113                                                targetComponent,
114                                                null) != null)
115                             added = true;
116                     }
117
118                     return added;
119                 }
120             }
121         );
122
123         menu.setText(getName());
124         menu.setEnabled(isEnabled());
125         HelpCtx.setHelpIDString(menu, AddAction.class.getName());
126         return menu;
127     }
128
129     protected boolean asynchronous() {
130         return false;
131     }
132
133     public void performAction() {
134     }
135
136     // -------
137

138     private static Node[] getNodes() {
139         // using NodeAction and global activated nodes is not reliable
140
// (activated nodes are set with a delay after selection in
141
// ComponentInspector)
142
return ComponentInspector.getInstance().getExplorerManager().getSelectedNodes();
143     }
144 }
145
Popular Tags