KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > Annotator


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.versioning.system.cvss;
21
22 import org.openide.util.actions.SystemAction;
23 import org.openide.util.NbBundle;
24 import org.openide.util.Utilities;
25 import org.openide.util.Lookup;
26 import org.openide.util.lookup.Lookups;
27 import org.openide.nodes.Node;
28 import org.netbeans.modules.versioning.system.cvss.ui.actions.status.StatusAction;
29 import org.netbeans.modules.versioning.system.cvss.ui.actions.checkout.CheckoutAction;
30 import org.netbeans.modules.versioning.system.cvss.ui.actions.project.UpdateWithDependenciesAction;
31 import org.netbeans.modules.versioning.system.cvss.ui.actions.project.AddToRepositoryAction;
32 import org.netbeans.modules.versioning.system.cvss.ui.actions.ignore.IgnoreAction;
33 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.AnnotationsAction;
34 import org.netbeans.modules.versioning.system.cvss.ui.actions.log.SearchHistoryAction;
35 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.DiffAction;
36 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.ResolveConflictsAction;
37 import org.netbeans.modules.versioning.system.cvss.ui.actions.diff.ExportDiffAction;
38 import org.netbeans.modules.versioning.system.cvss.ui.actions.tag.*;
39 import org.netbeans.modules.versioning.system.cvss.ui.actions.commit.CommitAction;
40 import org.netbeans.modules.versioning.system.cvss.ui.actions.commit.ExcludeFromCommitAction;
41 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.UpdateAction;
42 import org.netbeans.modules.versioning.system.cvss.ui.actions.update.GetCleanAction;
43 import org.netbeans.modules.versioning.system.cvss.ui.actions.ChangeCVSRootAction;
44 import org.netbeans.modules.versioning.system.cvss.ui.history.ViewRevisionAction;
45 import org.netbeans.modules.versioning.system.cvss.util.Utils;
46 import org.netbeans.modules.versioning.spi.VCSContext;
47 import org.netbeans.modules.versioning.spi.VCSAnnotator;
48 import org.netbeans.modules.versioning.util.FlatFolder;
49 import org.netbeans.lib.cvsclient.admin.Entry;
50 import org.netbeans.api.project.Project;
51
52 import javax.swing.*;
53 import java.util.*;
54 import java.util.List JavaDoc;
55 import java.util.logging.Logger JavaDoc;
56 import java.util.logging.Level JavaDoc;
57 import java.util.regex.Pattern JavaDoc;
58 import java.text.MessageFormat JavaDoc;
59 import java.io.File JavaDoc;
60 import java.awt.*;
61 import java.lang.reflect.Field JavaDoc;
62 import org.netbeans.modules.versioning.util.SystemActionBridge;
63
64 /**
65  * Annotates names for display in Files and Projects view (and possible elsewhere). Uses
66  * Filesystem support for this feature (to be replaced later in Core by something more generic).
67  *
68  * @author Maros Sandor
69  */

70 public class Annotator {
71
72     private static MessageFormat JavaDoc uptodateFormat = getFormat("uptodateFormat"); // NOI18N
73
private static MessageFormat JavaDoc newLocallyFormat = getFormat("newLocallyFormat"); // NOI18N
74
private static MessageFormat JavaDoc addedLocallyFormat = getFormat("addedLocallyFormat"); // NOI18N
75
private static MessageFormat JavaDoc modifiedLocallyFormat = getFormat("modifiedLocallyFormat"); // NOI18N
76
private static MessageFormat JavaDoc removedLocallyFormat = getFormat("removedLocallyFormat"); // NOI18N
77
private static MessageFormat JavaDoc deletedLocallyFormat = getFormat("deletedLocallyFormat"); // NOI18N
78
private static MessageFormat JavaDoc newInRepositoryFormat = getFormat("newInRepositoryFormat"); // NOI18N
79
private static MessageFormat JavaDoc modifiedInRepositoryFormat = getFormat("modifiedInRepositoryFormat"); // NOI18N
80
private static MessageFormat JavaDoc removedInRepositoryFormat = getFormat("removedInRepositoryFormat"); // NOI18N
81
private static MessageFormat JavaDoc conflictFormat = getFormat("conflictFormat"); // NOI18N
82
private static MessageFormat JavaDoc mergeableFormat = getFormat("mergeableFormat"); // NOI18N
83
private static MessageFormat JavaDoc excludedFormat = getFormat("excludedFormat"); // NOI18N
84

85     private static final int STATUS_TEXT_ANNOTABLE = FileInformation.STATUS_NOTVERSIONED_EXCLUDED |
86             FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_VERSIONED_UPTODATE |
87             FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY | FileInformation.STATUS_VERSIONED_CONFLICT | FileInformation.STATUS_VERSIONED_MERGE |
88             FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY | FileInformation.STATUS_VERSIONED_DELETEDLOCALLY |
89             FileInformation.STATUS_VERSIONED_ADDEDLOCALLY;
90
91     private static final Pattern JavaDoc lessThan = Pattern.compile("<"); // NOI18N
92

93     private final FileStatusCache cache;
94     
95     private String JavaDoc lastAnnotationsFormat;
96     private MessageFormat JavaDoc lastMessageFormat;
97     private String JavaDoc lastEmptyAnnotation;
98
99     Annotator(CvsVersioningSystem cvs) {
100         cache = cvs.getStatusCache();
101         initDefaults();
102     }
103
104     private void initDefaults() {
105         Field JavaDoc [] fields = Annotator.class.getDeclaredFields();
106         for (int i = 0; i < fields.length; i++) {
107             String JavaDoc name = fields[i].getName();
108             if (name.endsWith("Format")) { // NOI18N
109
initDefaultColor(name.substring(0, name.length() - 6));
110             }
111         }
112     }
113
114     private void initDefaultColor(String JavaDoc name) {
115         String JavaDoc color = System.getProperty("cvs.color." + name); // NOI18N
116
if (color == null) return;
117         setAnnotationColor(name, color);
118     }
119
120     /**
121      * Changes annotation color of files.
122      *
123      * @param name name of the color to change. Can be one of:
124      * newLocally, addedLocally, modifiedLocally, removedLocally, deletedLocally, newInRepository, modifiedInRepository,
125      * removedInRepository, conflict, mergeable, excluded.
126      * @param colorString new color in the format: 4455AA (RGB hexadecimal)
127      */

128     private void setAnnotationColor(String JavaDoc name, String JavaDoc colorString) {
129         try {
130             Field JavaDoc field = Annotator.class.getDeclaredField(name + "Format"); // NOI18N
131
MessageFormat JavaDoc format = new MessageFormat JavaDoc("<font color=\"" + colorString + "\">{0}</font><font color=\"#999999\">{1}</font>"); // NOI18N
132
field.set(null, format);
133         } catch (Exception JavaDoc e) {
134             throw new IllegalArgumentException JavaDoc("Invalid color name"); // NOI18N
135
}
136     }
137     
138     /**
139      * Adds rendering attributes to an arbitrary String based on a CVS status. The name is usually a file or folder
140      * display name and status is usually its CVS status as reported by FileStatusCache.
141      *
142      * @param name name to annotate
143      * @param info status that an object with the given name has
144      * @param file file this annotation belongs to. It is used to determine sticky tags for textual annotations. Pass
145      * null if you do not want textual annotations to appear in returned markup
146      * @return String html-annotated name that can be used in Swing controls that support html rendering. Note: it may
147      * also return the original name String
148      */

149     public String JavaDoc annotateNameHtml(String JavaDoc name, FileInformation info, File JavaDoc file) {
150         name = htmlEncode(name);
151         int status = info.getStatus();
152         String JavaDoc textAnnotation;
153         boolean annotationsVisible = CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_ANNOTATIONS_VISIBLE, false);
154         if (annotationsVisible && file != null && (status & STATUS_TEXT_ANNOTABLE) != 0) {
155             textAnnotation = formatAnnotation(info, file);
156             if (textAnnotation.equals(lastEmptyAnnotation)) textAnnotation = ""; // NOI18N
157
} else {
158             textAnnotation = ""; // NOI18N
159
}
160         if (textAnnotation.length() > 0) {
161             textAnnotation = NbBundle.getMessage(Annotator.class, "textAnnotation", textAnnotation);
162         }
163         
164         switch (status) {
165         case FileInformation.STATUS_UNKNOWN:
166         case FileInformation.STATUS_NOTVERSIONED_NOTMANAGED:
167             return name;
168         case FileInformation.STATUS_VERSIONED_UPTODATE:
169             return uptodateFormat.format(new Object JavaDoc [] { name, textAnnotation });
170         case FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY:
171             return modifiedLocallyFormat.format(new Object JavaDoc [] { name, textAnnotation });
172         case FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY:
173             return newLocallyFormat.format(new Object JavaDoc [] { name, textAnnotation });
174         case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
175             return removedLocallyFormat.format(new Object JavaDoc [] { name, textAnnotation });
176         case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
177             return deletedLocallyFormat.format(new Object JavaDoc [] { name, textAnnotation });
178         case FileInformation.STATUS_VERSIONED_NEWINREPOSITORY:
179             return newInRepositoryFormat.format(new Object JavaDoc [] { name, textAnnotation });
180         case FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY:
181             return modifiedInRepositoryFormat.format(new Object JavaDoc [] { name, textAnnotation });
182         case FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY:
183             return removedInRepositoryFormat.format(new Object JavaDoc [] { name, textAnnotation });
184         case FileInformation.STATUS_VERSIONED_ADDEDLOCALLY:
185             return addedLocallyFormat.format(new Object JavaDoc [] { name, textAnnotation });
186         case FileInformation.STATUS_VERSIONED_MERGE:
187             return mergeableFormat.format(new Object JavaDoc [] { name, textAnnotation });
188         case FileInformation.STATUS_VERSIONED_CONFLICT:
189             return conflictFormat.format(new Object JavaDoc [] { name, textAnnotation });
190         case FileInformation.STATUS_NOTVERSIONED_EXCLUDED:
191             return excludedFormat.format(new Object JavaDoc [] { name, textAnnotation });
192         default:
193             throw new IllegalArgumentException JavaDoc("Unknown status: " + status); // NOI18N
194
}
195     }
196
197     private String JavaDoc formatAnnotation(FileInformation info, File JavaDoc file) {
198         updateMessageFormat();
199
200         String JavaDoc statusString = ""; // NOI18N
201
int status = info.getStatus();
202         if (status != FileInformation.STATUS_VERSIONED_UPTODATE) {
203             statusString = info.getShortStatusText();
204         }
205
206         String JavaDoc revisionString = ""; // NOI18N
207
String JavaDoc binaryString = ""; // NOI18N
208
Entry entry = info.getEntry(file);
209         if (entry != null) {
210             revisionString = entry.getRevision();
211             binaryString = entry.getOptions();
212             if ("-kb".equals(binaryString) == false) { // NOI18N
213
binaryString = ""; // NOI18N
214
}
215         }
216         String JavaDoc stickyString = Utils.getSticky(file);
217         if (stickyString != null) {
218             stickyString = stickyString.substring(1);
219         } else {
220             stickyString = ""; // NOI18N
221
}
222
223         Object JavaDoc[] arguments = new Object JavaDoc[] {
224             revisionString,
225             statusString,
226             stickyString,
227             binaryString
228         };
229         return lastMessageFormat.format(arguments, new StringBuffer JavaDoc(), null).toString().trim();
230     }
231
232     private void updateMessageFormat() {
233         String JavaDoc taf = CvsModuleConfig.getDefault().getPreferences().get(CvsModuleConfig.PROP_ANNOTATIONS_FORMAT, CvsModuleConfig.DEFAULT_ANNOTATIONS_FORMAT);
234         if (lastMessageFormat == null || !taf.equals(lastAnnotationsFormat)) {
235             for (;;) { // executes at most 2 times
236
lastAnnotationsFormat = taf;
237                 taf = taf.replaceAll("\\{revision}", "{0}").replaceAll("\\{status}", "{1}").replaceAll("\\{tag}", "{2}").replaceAll("\\{binary}", "{3}"); // NOI18N
238
try {
239                     lastMessageFormat = new MessageFormat JavaDoc(taf);
240                     lastEmptyAnnotation = lastMessageFormat.format(new Object JavaDoc [] { "", "", "", "" }); // NOI18N
241
break;
242                 } catch (Exception JavaDoc e) {
243                     Logger.getLogger(Annotator.class.getName()).log(Level.SEVERE, lastAnnotationsFormat, e);
244                     taf = CvsModuleConfig.DEFAULT_ANNOTATIONS_FORMAT;
245                 }
246             }
247         }
248     }
249
250     private String JavaDoc annotateFolderNameHtml(String JavaDoc name, FileInformation info, File JavaDoc file) {
251         name = htmlEncode(name);
252         int status = info.getStatus();
253         String JavaDoc textAnnotation;
254         boolean annotationsVisible = CvsModuleConfig.getDefault().getPreferences().getBoolean(CvsModuleConfig.PROP_ANNOTATIONS_VISIBLE, false);
255         if (annotationsVisible && file != null && (status & FileInformation.STATUS_MANAGED) != 0) {
256             textAnnotation = formatAnnotation(info, file);
257             if (textAnnotation.equals(lastEmptyAnnotation)) textAnnotation = ""; // NOI18N
258
} else {
259             textAnnotation = ""; // NOI18N
260
}
261         if (textAnnotation.length() > 0) {
262             textAnnotation = NbBundle.getMessage(Annotator.class, "textAnnotation", textAnnotation);
263         }
264         
265         switch (status) {
266         case FileInformation.STATUS_VERSIONED_REMOVEDLOCALLY:
267         case FileInformation.STATUS_VERSIONED_DELETEDLOCALLY:
268         case FileInformation.STATUS_VERSIONED_NEWINREPOSITORY:
269         case FileInformation.STATUS_VERSIONED_MODIFIEDINREPOSITORY:
270         case FileInformation.STATUS_VERSIONED_REMOVEDINREPOSITORY:
271         case FileInformation.STATUS_NOTVERSIONED_NOTMANAGED:
272         case FileInformation.STATUS_VERSIONED_MERGE:
273         case FileInformation.STATUS_UNKNOWN:
274         case FileInformation.STATUS_VERSIONED_MODIFIEDLOCALLY:
275         case FileInformation.STATUS_VERSIONED_CONFLICT:
276             return name;
277         case FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY:
278         case FileInformation.STATUS_VERSIONED_ADDEDLOCALLY:
279         case FileInformation.STATUS_VERSIONED_UPTODATE:
280             return uptodateFormat.format(new Object JavaDoc [] { name, textAnnotation });
281         case FileInformation.STATUS_NOTVERSIONED_EXCLUDED:
282             return excludedFormat.format(new Object JavaDoc [] { name, textAnnotation });
283         default:
284             throw new IllegalArgumentException JavaDoc("Unknown status: " + status); // NOI18N
285
}
286     }
287     
288     private String JavaDoc htmlEncode(String JavaDoc name) {
289         if (name.indexOf('<') == -1) return name;
290         return lessThan.matcher(name).replaceAll("&lt;"); // NOI18N
291
}
292
293     public String JavaDoc annotateNameHtml(File JavaDoc file, FileInformation info) {
294         return annotateNameHtml(file.getName(), info, file);
295     }
296     
297     public String JavaDoc annotateNameHtml(String JavaDoc name, VCSContext context, int includeStatus) {
298         FileInformation mostImportantInfo = null;
299         File JavaDoc mostImportantFile = null;
300         boolean folderAnnotation = false;
301         
302         for (File JavaDoc file : context.getRootFiles()) {
303             FileInformation info = cache.getStatus(file);
304             int status = info.getStatus();
305             if ((status & includeStatus) == 0) continue;
306             
307             if (isMoreImportant(info, mostImportantInfo)) {
308                 mostImportantInfo = info;
309                 mostImportantFile = file;
310                 folderAnnotation = file.isDirectory();
311             }
312         }
313
314         if (folderAnnotation == false && context.getRootFiles().size() > 1) {
315             folderAnnotation = !org.netbeans.modules.versioning.util.Utils.shareCommonDataObject(context.getRootFiles().toArray(new File JavaDoc[context.getRootFiles().size()]));
316         }
317
318         if (mostImportantInfo == null) return null;
319         return folderAnnotation ?
320                 annotateFolderNameHtml(name, mostImportantInfo, mostImportantFile) :
321                 annotateNameHtml(name, mostImportantInfo, mostImportantFile);
322     }
323     
324     private boolean isMoreImportant(FileInformation a, FileInformation b) {
325         if (b == null) return true;
326         if (a == null) return false;
327         return Utils.getComparableStatus(a.getStatus()) < Utils.getComparableStatus(b.getStatus());
328     }
329
330     /**
331      * Annotates icon of a node based on its versioning status.
332      *
333      * @param roots files that the node represents
334      * @param icon original node icon
335      * @return Image newly annotated icon or the original one
336      */

337     Image annotateFolderIcon(Set roots, Image icon) {
338         CvsModuleConfig config = CvsModuleConfig.getDefault();
339         boolean allExcluded = true;
340         boolean modified = false;
341
342         Map<File JavaDoc, FileInformation> map = cache.getAllModifiedFiles();
343         Map<File JavaDoc, FileInformation> modifiedFiles = new HashMap<File JavaDoc, FileInformation>();
344         for (Iterator i = map.keySet().iterator(); i.hasNext();) {
345             File JavaDoc file = (File JavaDoc) i.next();
346             FileInformation info = (FileInformation) map.get(file);
347             if (!info.isDirectory() && (info.getStatus() & FileInformation.STATUS_LOCAL_CHANGE) != 0) modifiedFiles.put(file, info);
348         }
349
350         for (Iterator i = roots.iterator(); i.hasNext();) {
351             File JavaDoc file = (File JavaDoc) i.next();
352             if (file instanceof FlatFolder) {
353                 for (Iterator j = modifiedFiles.keySet().iterator(); j.hasNext();) {
354                     File JavaDoc mf = (File JavaDoc) j.next();
355                     if (mf.getParentFile().equals(file)) {
356                         FileInformation info = (FileInformation) modifiedFiles.get(mf);
357                         if (info.isDirectory()) continue;
358                         int status = info.getStatus();
359                         if (status == FileInformation.STATUS_VERSIONED_CONFLICT) {
360                             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/conflicts-badge.png", true); // NOI18N
361
return Utilities.mergeImages(icon, badge, 16, 9);
362                         }
363                         modified = true;
364                         allExcluded &= config.isExcludedFromCommit(mf);
365                     }
366                 }
367             } else {
368                 for (Iterator j = modifiedFiles.keySet().iterator(); j.hasNext();) {
369                     File JavaDoc mf = (File JavaDoc) j.next();
370                     if (Utils.isParentOrEqual(file, mf)) {
371                         FileInformation info = (FileInformation) modifiedFiles.get(mf);
372                         int status = info.getStatus();
373                         if (status == FileInformation.STATUS_VERSIONED_CONFLICT) {
374                             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/conflicts-badge.png", true); // NOI18N
375
return Utilities.mergeImages(icon, badge, 16, 9);
376                         }
377                         modified = true;
378                         allExcluded &= config.isExcludedFromCommit(mf);
379                     }
380                 }
381             }
382         }
383
384         if (modified && !allExcluded) {
385             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/modified-badge.png", true); // NOI18N
386
return Utilities.mergeImages(icon, badge, 16, 9);
387         } else {
388             return null;
389         }
390     }
391
392     /**
393      * Returns array of versioning actions that may be used to construct a popup menu. These actions
394      * will act on the supplied context.
395      *
396      * @param ctx context similar to {@link org.openide.util.ContextAwareAction#createContextAwareInstance(org.openide.util.Lookup)}
397      * @return Action[] array of versioning actions that may be used to construct a popup menu. These actions
398      * will act on currently activated nodes.
399      */

400     public static Action [] getActions(VCSContext ctx, VCSAnnotator.ActionDestination destination) {
401         ResourceBundle loc = NbBundle.getBundle(Annotator.class);
402         Node [] nodes = ctx.getNodes();
403         File JavaDoc [] files = ctx.getRootFiles().toArray(new File JavaDoc[ctx.getRootFiles().size()]);
404         Lookup context = ctx.getNodes() != null ? Lookups.fixed(ctx.getNodes()) : null;
405         boolean noneVersioned = isNothingVersioned(files);
406         boolean onlyFolders = onlyFolders(files);
407         boolean onlyProjects = onlyProjects(ctx.getNodes());
408         
409         List JavaDoc<Action> actions = new ArrayList<Action>(20);
410         if (destination == VCSAnnotator.ActionDestination.MainMenu) {
411             actions.add(SystemAction.get(CheckoutAction.class));
412             actions.add(SystemAction.get(AddToRepositoryAction.class));
413             actions.add(new ChangeCVSRootAction(loc.getString("CTL_MenuItem_ChangeCVSRoot"), ctx));
414             actions.add(null);
415             actions.add(SystemAction.get(UpdateWithDependenciesAction.class));
416             actions.add(null);
417             actions.add(SystemAction.get(StatusAction.class));
418             actions.add(SystemAction.get(DiffAction.class));
419             actions.add(SystemAction.get(UpdateAction.class));
420             actions.add(SystemAction.get(CommitAction.class));
421             actions.add(null);
422             actions.add(SystemAction.get(ExportDiffAction.class));
423             actions.add(null);
424             actions.add(SystemAction.get(TagAction.class));
425             actions.add(new BranchesMenu());
426             actions.add(null);
427             actions.add(SystemAction.get(AnnotationsAction.class));
428             actions.add(new ViewRevisionAction(ctx));
429             actions.add(SystemAction.get(SearchHistoryAction.class));
430             actions.add(null);
431             actions.add(SystemAction.get(GetCleanAction.class));
432             actions.add(SystemAction.get(ResolveConflictsAction.class));
433             actions.add(SystemAction.get(IgnoreAction.class));
434             actions.add(new ExcludeFromCommitAction(ctx));
435         } else {
436             if (noneVersioned) {
437                 actions.add(SystemActionBridge.createAction(SystemAction.get(AddToRepositoryAction.class).createContextAwareInstance(context), loc.getString("CTL_PopupMenuItem_Import"), context));
438             } else {
439                 actions.add(SystemActionBridge.createAction(SystemAction.get(StatusAction.class), loc.getString("CTL_PopupMenuItem_Status"), context));
440                 actions.add(SystemActionBridge.createAction(SystemAction.get(DiffAction.class), loc.getString("CTL_PopupMenuItem_Diff"), context));
441                 actions.add(SystemActionBridge.createAction(SystemAction.get(UpdateAction.class), loc.getString("CTL_PopupMenuItem_Update"), context));
442                 if (onlyProjects) {
443                     actions.add(new SystemActionBridge(SystemAction.get(UpdateWithDependenciesAction.class), loc.getString("CTL_PopupMenuItem_UpdateWithDeps")));
444                 }
445                 actions.add(SystemActionBridge.createAction(SystemAction.get(CommitAction.class), loc.getString("CTL_PopupMenuItem_Commit"), context));
446                 actions.add(null);
447                 actions.add(SystemActionBridge.createAction(SystemAction.get(TagAction.class), loc.getString("CTL_PopupMenuItem_Tag"), context));
448                 actions.add(null);
449                 actions.add(SystemActionBridge.createAction(SystemAction.get(BranchAction.class), loc.getString("CTL_PopupMenuItem_Branch"), context));
450                 actions.add(SystemActionBridge.createAction(SystemAction.get(SwitchBranchAction.class), loc.getString("CTL_PopupMenuItem_SwitchBranch"), context));
451                 actions.add(SystemActionBridge.createAction(SystemAction.get(MergeBranchAction.class), loc.getString("CTL_PopupMenuItem_MergeBranch"), context));
452                 actions.add(null);
453                 if (!onlyFolders) {
454                     actions.add(SystemActionBridge.createAction(SystemAction.get(AnnotationsAction.class),
455                                                                 ((AnnotationsAction)SystemAction.get(AnnotationsAction.class)).visible(nodes) ?
456                                                                         loc.getString("CTL_PopupMenuItem_HideAnnotations") :
457                                                                         loc.getString("CTL_PopupMenuItem_ShowAnnotations"), context));
458                 }
459                 actions.add(new ViewRevisionAction(loc.getString("CTL_PopupMenuItem_ViewRevision"), ctx)); // NOI18N
460
actions.add(SystemActionBridge.createAction(SystemAction.get(SearchHistoryAction.class), loc.getString("CTL_PopupMenuItem_SearchHistory"), context));
461                 actions.add(null);
462                 actions.add(SystemActionBridge.createAction(SystemAction.get(GetCleanAction.class), loc.getString("CTL_PopupMenuItem_GetClean"), context));
463                 actions.add(SystemActionBridge.createAction(SystemAction.get(ResolveConflictsAction.class), loc.getString("CTL_PopupMenuItem_ResolveConflicts"), context));
464                 if (!onlyProjects) {
465                     actions.add(SystemActionBridge.createAction(SystemAction.get(IgnoreAction.class),
466                                                                 ((IgnoreAction)SystemAction.get(IgnoreAction.class)).getActionStatus(nodes) == IgnoreAction.UNIGNORING ?
467                                                                         loc.getString("CTL_PopupMenuItem_Unignore") :
468                                                                         loc.getString("CTL_PopupMenuItem_Ignore"), context));
469                 }
470                 actions.add(new ExcludeFromCommitAction(ctx));
471             }
472         }
473         return actions.toArray(new Action[actions.size()]);
474     }
475
476     private static boolean isNothingVersioned(File JavaDoc[] files) {
477         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
478         for (File JavaDoc file : files) {
479             if ((cache.getStatus(file).getStatus() & FileInformation.STATUS_MANAGED) != 0) return false;
480         }
481         return true;
482     }
483
484     private static boolean onlyProjects(Node[] nodes) {
485         if (nodes == null) return false;
486         for (Node node : nodes) {
487             if (node.getLookup().lookup(Project.class) == null) return false;
488         }
489         return true;
490     }
491
492     private static boolean onlyFolders(File JavaDoc[] files) {
493         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
494         for (int i = 0; i < files.length; i++) {
495             if (files[i].isFile()) return false;
496             if (!files[i].exists() && !cache.getStatus(files[i]).isDirectory()) return false;
497         }
498         return true;
499     }
500
501     private static MessageFormat JavaDoc getFormat(String JavaDoc key) {
502         String JavaDoc format = NbBundle.getMessage(Annotator.class, key);
503         return new MessageFormat JavaDoc(format);
504     }
505
506     private static final int STATUS_BADGEABLE = FileInformation.STATUS_VERSIONED_UPTODATE | FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY;
507     
508     public Image annotateIcon(Image icon, VCSContext context) {
509         boolean folderAnnotation = false;
510         for (File JavaDoc file : context.getRootFiles()) {
511             if (file.isDirectory()) {
512                 folderAnnotation = true;
513                 break;
514             }
515         }
516         
517         if (folderAnnotation == false && context.getRootFiles().size() > 1) {
518             folderAnnotation = !org.netbeans.modules.versioning.util.Utils.shareCommonDataObject(context.getRootFiles().toArray(new File JavaDoc[context.getRootFiles().size()]));
519         }
520
521         if (folderAnnotation == false) {
522             return null;
523         }
524
525         FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
526         boolean isVersioned = false;
527         for (Iterator<File JavaDoc> i = context.getRootFiles().iterator(); i.hasNext();) {
528             File JavaDoc file = i.next();
529             if ((cache.getStatus(file).getStatus() & STATUS_BADGEABLE) != 0) {
530                 isVersioned = true;
531                 break;
532             }
533         }
534         if (!isVersioned) return null;
535
536         
537         
538         
539         
540         CvsModuleConfig config = CvsModuleConfig.getDefault();
541         boolean allExcluded = true;
542         boolean modified = false;
543
544         Map<File JavaDoc, FileInformation> map = cache.getAllModifiedFiles();
545         Map<File JavaDoc, FileInformation> modifiedFiles = new HashMap<File JavaDoc, FileInformation>();
546         for (Map.Entry<File JavaDoc, FileInformation> entry : map.entrySet()) {
547             FileInformation info = entry.getValue();
548             if (!info.isDirectory() && (info.getStatus() & FileInformation.STATUS_LOCAL_CHANGE) != 0) modifiedFiles.put(entry.getKey(), info);
549         }
550
551         for (Iterator<File JavaDoc> i = context.getRootFiles().iterator(); i.hasNext();) {
552             File JavaDoc file = i.next();
553             if (file instanceof FlatFolder) {
554                 for (Iterator<File JavaDoc> j = modifiedFiles.keySet().iterator(); j.hasNext();) {
555                     File JavaDoc mf = j.next();
556                     if (mf.getParentFile().equals(file)) {
557                         FileInformation info = modifiedFiles.get(mf);
558                         if (info.isDirectory()) continue;
559                         int status = info.getStatus();
560                         if (status == FileInformation.STATUS_VERSIONED_CONFLICT) {
561                             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/conflicts-badge.png", true); // NOI18N
562
return Utilities.mergeImages(icon, badge, 16, 9);
563                         }
564                         modified = true;
565                         allExcluded &= config.isExcludedFromCommit(mf);
566                     }
567                 }
568             } else {
569                 for (Iterator<File JavaDoc> j = modifiedFiles.keySet().iterator(); j.hasNext();) {
570                     File JavaDoc mf = j.next();
571                     if (Utils.isParentOrEqual(file, mf)) {
572                         FileInformation info = modifiedFiles.get(mf);
573                         int status = info.getStatus();
574                         if (status == FileInformation.STATUS_VERSIONED_CONFLICT) {
575                             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/conflicts-badge.png", true); // NOI18N
576
return Utilities.mergeImages(icon, badge, 16, 9);
577                         }
578                         modified = true;
579                         allExcluded &= config.isExcludedFromCommit(mf);
580                     }
581                 }
582             }
583         }
584
585         if (modified && !allExcluded) {
586             Image badge = Utilities.loadImage("org/netbeans/modules/versioning/system/cvss/resources/icons/modified-badge.png", true); // NOI18N
587
return Utilities.mergeImages(icon, badge, 16, 9);
588         } else {
589             return null;
590         }
591     }
592 }
593
Popular Tags