KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > actions > ContextAction


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.subversion.ui.actions;
21
22 import org.netbeans.api.project.ProjectUtils;
23 import org.netbeans.modules.subversion.client.SvnProgressSupport;
24 import org.openide.util.actions.*;
25 import org.openide.util.HelpCtx;
26 import org.openide.util.NbBundle;
27 import org.openide.util.RequestProcessor;
28 import org.openide.nodes.Node;
29 import org.openide.windows.TopComponent;
30 import org.openide.loaders.DataObject;
31 import org.openide.loaders.DataShadow;
32 import org.openide.filesystems.FileObject;
33 import org.openide.LifecycleManager;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.progress.ProgressHandle;
36 import org.netbeans.modules.subversion.util.SvnUtils;
37 import org.netbeans.modules.subversion.util.Context;
38 import org.netbeans.modules.subversion.FileInformation;
39 import java.text.MessageFormat JavaDoc;
40 import java.text.DateFormat JavaDoc;
41 import java.io.File JavaDoc;
42 import java.util.MissingResourceException JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.awt.event.ActionEvent JavaDoc;
45 import org.netbeans.modules.subversion.Subversion;
46 import org.tigris.subversion.svnclientadapter.SVNUrl;
47
48 /**
49  * Base for all context-sensitive SVN actions.
50  *
51  * @author Maros Sandor
52  */

53 public abstract class ContextAction extends NodeAction {
54
55     // it's singleton
56
// do not declare any instance data
57

58     protected ContextAction() {
59         setIcon(null);
60         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
61
}
62     
63     /**
64      * @return bundle key base name
65      * @see #getName
66      */

67     protected abstract String JavaDoc getBaseName(Node[] activatedNodes);
68
69     protected boolean enable(Node[] nodes) {
70         return getContext(nodes).getRootFiles().length > 0;
71     }
72     
73     /**
74      * Synchronizes memory modificatios with disk and calls
75      * {@link #performContextAction}.
76      */

77     protected void performAction(final Node[] nodes) {
78         // TODO try to save files in invocation context only
79
// list somehow modified file in the context and save
80
// just them.
81
// The same (global save) logic is in CVS, no complaint
82
LifecycleManager.getDefault().saveAll();
83         performContextAction(nodes);
84     }
85
86     protected SVNUrl getSvnUrl(Node[] nodes) {
87         return getSvnUrl(getContext(nodes));
88     }
89
90     public static SVNUrl getSvnUrl(Context ctx) {
91         File JavaDoc[] roots = ctx.getRootFiles();
92         return SvnUtils.getRepositoryRootUrl(roots[0]);
93     }
94
95     protected abstract void performContextAction(Node[] nodes);
96
97     /** Be sure nobody overwrites */
98     public final boolean isEnabled() {
99         return super.isEnabled();
100     }
101
102     /** Be sure nobody overwrites */
103     public final void setEnabled(boolean enabled) {
104         super.setEnabled(enabled);
105     }
106
107     /** Be sure nobody overwrites */
108     public final void actionPerformed(ActionEvent JavaDoc event) {
109         super.actionPerformed(event);
110     }
111
112     /** Be sure nobody overwrites */
113     public final void performAction() {
114         super.performAction();
115     }
116
117     /**
118      * Running action display name, it seeks action class bundle for:
119      * <ul>
120      * <li><code>getBaseName() + "Running"</code> key
121      * <li><code>getBaseName() + "Running_Context"</code> key for one selected file
122      * <li><code>getBaseName() + "Running_Context_Multiple"</code> key for multiple selected files
123      * <li><code>getBaseName() + "Running_Project"</code> key for one selected project
124      * <li><code>getBaseName() + "Running_Projects"</code> key for multiple selected projects
125      * </ul>
126      */

127     public String JavaDoc getRunningName(Node [] activatedNodes) {
128         return getName("Running", activatedNodes); // NOI18N
129
}
130
131     public String JavaDoc getName() {
132         return getName("", TopComponent.getRegistry().getActivatedNodes()); // NOI18N
133
}
134     
135     /**
136      * Display name, it seeks action class bundle for:
137      * <ul>
138      * <li><code>getBaseName()</code> key
139      * <li><code>getBaseName() + "_Context"</code> key for one selected file
140      * <li><code>getBaseName() + "_Context_Multiple"</code> key for multiple selected files
141      * <li><code>getBaseName() + "_Project"</code> key for one selected project
142      * <li><code>getBaseName() + "_Projects"</code> key for multiple selected projects
143      * </ul>
144      */

145     public String JavaDoc getName(String JavaDoc role, Node[] activatedNodes) {
146         String JavaDoc baseName = getBaseName(activatedNodes) + role;
147         if (!isEnabled()) {
148             return NbBundle.getBundle(this.getClass()).getString(baseName);
149         }
150
151         File JavaDoc [] nodes = SvnUtils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles();
152         int objectCount = nodes.length;
153         // if all nodes represent project node the use plain name
154
// It avoids "Show changes 2 files" on project node
155
// caused by fact that project contains two source groups.
156

157         boolean projectsOnly = true;
158         for (int i = 0; i < activatedNodes.length; i++) {
159             Node activatedNode = activatedNodes[i];
160             Project project = (Project) activatedNode.getLookup().lookup(Project.class);
161             if (project == null) {
162                 projectsOnly = false;
163                 break;
164             }
165         }
166         if (projectsOnly) objectCount = activatedNodes.length;
167
168         if (objectCount == 0) {
169             return NbBundle.getBundle(this.getClass()).getString(baseName);
170         } else if (objectCount == 1) {
171             if (projectsOnly) {
172                 String JavaDoc dispName = ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName();
173                 return NbBundle.getMessage(this.getClass(), baseName + "_Context", // NOI18N
174
dispName);
175             }
176             String JavaDoc name;
177             FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class);
178             if (fo != null) {
179                 name = fo.getNameExt();
180             } else {
181                 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
182                 if (dao instanceof DataShadow) {
183                     dao = ((DataShadow) dao).getOriginal();
184                 }
185                 if (dao != null) {
186                     name = dao.getPrimaryFile().getNameExt();
187                 } else {
188                     name = activatedNodes[0].getDisplayName();
189                 }
190             }
191             return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context"), // NOI18N
192
new Object JavaDoc [] { name });
193         } else {
194             if (projectsOnly) {
195                 try {
196                     return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Projects"), // NOI18N
197
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
198                 } catch (MissingResourceException JavaDoc ex) {
199                     // ignore use files alternative bellow
200
}
201             }
202             return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context_Multiple"), // NOI18N
203
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
204         }
205     }
206     
207     /**
208      * Computes display name of the context this action will operate.
209      *
210      * @return String name of this action's context, e.g. "3 files", "MyProject", "2 projects", "Foo.java". Returns
211      * null if the context is empty
212      */

213     public String JavaDoc getContextDisplayName(Node [] activatedNodes) {
214         // TODO: reuse this code in getName()
215
File JavaDoc [] nodes = SvnUtils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles();
216         int objectCount = nodes.length;
217         // if all nodes represent project node the use plain name
218
// It avoids "Show changes 2 files" on project node
219
// caused by fact that project contains two source groups.
220

221         boolean projectsOnly = true;
222         for (int i = 0; i < activatedNodes.length; i++) {
223             Node activatedNode = activatedNodes[i];
224             Project project = (Project) activatedNode.getLookup().lookup(Project.class);
225             if (project == null) {
226                 projectsOnly = false;
227                 break;
228             }
229         }
230         if (projectsOnly) objectCount = activatedNodes.length;
231
232         if (objectCount == 0) {
233             return null;
234         } else if (objectCount == 1) {
235             if (projectsOnly) {
236                 return ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName();
237             }
238             FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class);
239             if (fo != null) {
240                 return fo.getNameExt();
241             } else {
242                 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class);
243                 if (dao instanceof DataShadow) {
244                     dao = ((DataShadow) dao).getOriginal();
245                 }
246                 if (dao != null) {
247                     return dao.getPrimaryFile().getNameExt();
248                 } else {
249                     return activatedNodes[0].getDisplayName();
250                 }
251             }
252         } else {
253             if (projectsOnly) {
254                 try {
255                     return MessageFormat.format(NbBundle.getBundle(ContextAction.class).getString("MSG_ActionContext_MultipleProjects"), // NOI18N
256
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
257                 } catch (MissingResourceException JavaDoc ex) {
258                     // ignore use files alternative bellow
259
}
260             }
261             return MessageFormat.format(NbBundle.getBundle(ContextAction.class).getString("MSG_ActionContext_MultipleFiles"), // NOI18N
262
new Object JavaDoc [] { new Integer JavaDoc(objectCount) });
263         }
264     }
265         
266     public HelpCtx getHelpCtx() {
267         return new HelpCtx(this.getClass());
268     }
269
270     protected Context getContext(Node[] nodes) {
271         return SvnUtils.getCurrentContext(nodes, getFileEnabledStatus(), getDirectoryEnabledStatus());
272     }
273     
274     protected int getFileEnabledStatus() {
275         return ~0;
276     }
277
278     protected int getDirectoryEnabledStatus() {
279         return FileInformation.STATUS_MANAGED & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED;
280     }
281     
282     protected boolean asynchronous() {
283         return false;
284     }
285     
286     protected RequestProcessor createRequestProcessor(Node[] nodes) {
287         SVNUrl repository = getSvnUrl(nodes);
288         return Subversion.getInstance().getRequestProcessor(repository);
289     }
290
291     protected abstract static class ProgressSupport extends SvnProgressSupport {
292
293         private final ContextAction action;
294         private final Node[] nodes;
295         private long progressStamp;
296         private String JavaDoc runningName;
297         public ProgressSupport(ContextAction action, Node[] nodes) {
298             this.action = action;
299             this.nodes = nodes;
300         }
301
302         public RequestProcessor.Task start(RequestProcessor rp) {
303             runningName = ActionUtils.cutAmpersand(action.getRunningName(nodes));
304             return start(rp, getSvnUrl(action.getContext(nodes)), runningName);
305         }
306
307         public abstract void perform();
308
309         protected void startProgress() {
310             getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + runningName); // NOI18N
311
ProgressHandle progress = getProgressHandle();
312             progress.setInitialDelay(500);
313             progressStamp = System.currentTimeMillis() + 500;
314             progress.start();
315         }
316
317         protected void finnishProgress() {
318             // TODO add failed and restart texts
319

320             final ProgressHandle progress = getProgressHandle();
321             progress.switchToDeterminate(100);
322             progress.progress(NbBundle.getMessage(ContextAction.class, "MSG_Progress_Done"), 100); // NOI18N
323
if (System.currentTimeMillis() > progressStamp) {
324                 RequestProcessor.getDefault().post(new Runnable JavaDoc() {
325                     public void run() {
326                         progress.finish();
327                     }
328                 }, 15 * 1000);
329             } else {
330                 progress.finish();
331             }
332
333             if (isCanceled() == false) {
334                 getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + runningName + " " + NbBundle.getMessage(ContextAction.class, "MSG_Progress_Finished")); // NOI18N
335
} else {
336                 getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date JavaDoc()) + " " + runningName + " " + NbBundle.getMessage(ContextAction.class, "MSG_Progress_Canceled")); // NOI18N
337
}
338         }
339     }
340 }
341
Popular Tags