KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > AbstractSystemAction


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.versioning.system.cvss.ui.actions;
21
22 import org.openide.util.actions.NodeAction;
23 import org.openide.util.HelpCtx;
24 import org.openide.util.NbBundle;
25 import org.openide.nodes.Node;
26 import org.openide.loaders.DataObject;
27 import org.openide.loaders.DataShadow;
28 import org.openide.filesystems.FileObject;
29 import org.openide.LifecycleManager;
30 import org.openide.windows.TopComponent;
31 import org.netbeans.modules.versioning.system.cvss.FileInformation;
32 import org.netbeans.modules.versioning.system.cvss.util.Utils;
33 import org.netbeans.modules.versioning.system.cvss.util.Context;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectUtils;
36
37 import java.text.MessageFormat JavaDoc;
38 import java.io.File JavaDoc;
39 import java.util.MissingResourceException JavaDoc;
40 import java.awt.event.ActionEvent JavaDoc;
41
42 /**
43  * Base for all context-sensitive CVS actions.
44  *
45  * @author Maros Sandor
46  */

47 public abstract class AbstractSystemAction extends NodeAction {
48
49     /**
50      * @return bundle key base name
51      * @see #getName
52      */

53     protected abstract String JavaDoc getBaseName(Node [] activatedNodes);
54
55     protected AbstractSystemAction() {
56         setIcon(null);
57         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
58
}
59
60     /**
61      * Synchronizes memory modificatios with disk and calls
62      * {@link #performCvsAction}.
63      */

64     protected void performAction(Node[] nodes) {
65         LifecycleManager.getDefault().saveAll();
66         performCvsAction(nodes);
67     }
68
69     protected boolean enable(Node[] nodes) {
70         return getContext(nodes).getRootFiles().length > 0;
71     }
72
73     protected abstract void performCvsAction(Node[] nodes);
74
75     /** Be sure nobody overwrites */
76     public final boolean isEnabled() {
77         return super.isEnabled();
78     }
79
80     /** Be sure nobody overwrites */
81     public final void setEnabled(boolean enabled) {
82         super.setEnabled(enabled);
83     }
84
85     /** Be sure nobody overwrites */
86     public final void actionPerformed(ActionEvent JavaDoc event) {
87         super.actionPerformed(event);
88     }
89
90     /** Be sure nobody overwrites */
91     public final void performAction() {
92         super.performAction();
93     }
94
95     /**
96      * Display name, it seeks action class bundle for:
97      * <ul>
98      * <li><code>getBaseName()</code> key
99      * <li><code>getBaseName() + "_Context"</code> key for one selected file
100      * <li><code>getBaseName() + "_Context_Multiple"</code> key for multiple selected files
101      * <li><code>getBaseName() + "_Project"</code> key for one selected project
102      * <li><code>getBaseName() + "_Projects"</code> key for multiple selected projects
103      * </ul>
104      */

105     public String JavaDoc getName() {
106         return getName("", TopComponent.getRegistry().getActivatedNodes()); // NOI18N
107
}
108     
109
110     /**
111      * Running action display name, it seeks action class bundle for:
112      * <ul>
113      * <li><code>getBaseName() + "Running"</code> key
114      * <li><code>getBaseName() + "Running_Context"</code> key for one selected file
115      * <li><code>getBaseName() + "Running_Context_Multiple"</code> key for multiple selected files
116      * <li><code>getBaseName() + "Running_Project"</code> key for one selected project
117      * <li><code>getBaseName() + "Running_Projects"</code> key for multiple selected projects
118      * </ul>
119      */

120     public String JavaDoc getRunningName(Node [] activatedNodes) {
121         return getName("Running", activatedNodes); // NOI18N
122
}
123         
124     private String JavaDoc getName(String JavaDoc role, Node [] activatedNodes) {
125         String JavaDoc baseName = getBaseName(activatedNodes) + role;
126         if (!isEnabled()) {
127             return NbBundle.getBundle(this.getClass()).getString(baseName);
128         }
129
130         File JavaDoc [] nodes = Utils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles();
131         int objectCount = nodes.length;
132         // if all nodes represent project node the use plain name
133
// It avoids "Show changes 2 files" on project node
134
// caused by fact that project contains two source groups.
135

136         boolean projectsOnly = true;
137         for (int i = 0; i < activatedNodes.length; i++) {
138             Node activatedNode = activatedNodes[i];
139             Project project = (Project) activatedNode.getLookup().lookup(Project.class);
140             if (project == null) {
141                 projectsOnly = false;
142                 break;
143             }
144         }
145         if (projectsOnly) objectCount = activatedNodes.length;
146
147         if (objectCount == 0) {
148             return NbBundle.getBundle(this.getClass()).getString(baseName);
149         } else if (objectCount == 1) {
150             if (projectsOnly) {
151                 String JavaDoc dispName = ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName();
152                 return NbBundle.getMessage(this.getClass(), baseName + "_Context", // NOI18N
153
dispName);
154             }
155             String JavaDoc name;
156             FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class);
157             if (fo != null) {
158                 name = fo.getNameExt();
159             } else {
160                 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
161                 if (dao instanceof DataShadow) {
162                     dao = ((DataShadow) dao).getOriginal();
163                 }
164                 if (dao != null) {
165                     name = dao.getPrimaryFile().getNameExt();
166                 } else {
167                     name = activatedNodes[0].getDisplayName();
168                 }
169             }
170             return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context"), // NOI18N
171
new Object JavaDoc [] { name });
172         } else {
173             if (projectsOnly) {
174                 try {
175                     return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Projects"), // NOI18N
176
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
177                 } catch (MissingResourceException JavaDoc ex) {
178                     // ignore use files alternative bellow
179
}
180             }
181             return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context_Multiple"), // NOI18N
182
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
183         }
184     }
185     
186     /**
187      * Computes display name of the context this action will operate.
188      *
189      * @return String name of this action's context, e.g. "3 files", "MyProject", "2 projects", "Foo.java". Returns
190      * null if the context is empty
191      */

192     public String JavaDoc getContextDisplayName(Node [] activatedNodes) {
193         // TODO: reuse this code in getName()
194
File JavaDoc [] nodes = Utils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles();
195         int objectCount = nodes.length;
196         // if all nodes represent project node the use plain name
197
// It avoids "Show changes 2 files" on project node
198
// caused by fact that project contains two source groups.
199

200         boolean projectsOnly = true;
201         for (int i = 0; i < activatedNodes.length; i++) {
202             Node activatedNode = activatedNodes[i];
203             Project project = (Project) activatedNode.getLookup().lookup(Project.class);
204             if (project == null) {
205                 projectsOnly = false;
206                 break;
207             }
208         }
209         if (projectsOnly) objectCount = activatedNodes.length;
210
211         if (objectCount == 0) {
212             return null;
213         } else if (objectCount == 1) {
214             if (projectsOnly) {
215                 return ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName();
216             }
217             FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class);
218             if (fo != null) {
219                 return fo.getNameExt();
220             } else {
221                 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
222                 if (dao instanceof DataShadow) {
223                     dao = ((DataShadow) dao).getOriginal();
224                 }
225                 if (dao != null) {
226                     return dao.getPrimaryFile().getNameExt();
227                 } else {
228                     return activatedNodes[0].getDisplayName();
229                 }
230             }
231         } else {
232             if (projectsOnly) {
233                 try {
234                     return MessageFormat.format(NbBundle.getBundle(AbstractSystemAction.class).getString("MSG_ActionContext_MultipleProjects"), // NOI18N
235
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
236                 } catch (MissingResourceException JavaDoc ex) {
237                     // ignore use files alternative bellow
238
}
239             }
240             return MessageFormat.format(NbBundle.getBundle(AbstractSystemAction.class).getString("MSG_ActionContext_MultipleFiles"), // NOI18N
241
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
242         }
243     }
244     
245     public HelpCtx getHelpCtx() {
246         return new HelpCtx(this.getClass());
247     }
248
249     protected Context getContext(Node[] nodes) {
250         return Utils.getCurrentContext(nodes, getFileEnabledStatus(), getDirectoryEnabledStatus());
251     }
252     
253     protected int getFileEnabledStatus() {
254         return ~0;
255     }
256
257     protected int getDirectoryEnabledStatus() {
258         return FileInformation.STATUS_MANAGED & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
259     }
260 }
261
Popular Tags