KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > VersioningAnnotationProvider


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 package org.netbeans.modules.versioning;
20
21 import org.netbeans.modules.masterfs.providers.InterceptionListener;
22 import org.netbeans.modules.masterfs.providers.AnnotationProvider;
23 import org.netbeans.modules.versioning.spi.VersioningSystem;
24 import org.netbeans.modules.versioning.spi.VCSAnnotator;
25 import org.netbeans.modules.versioning.spi.VCSContext;
26 import org.netbeans.modules.versioning.util.FlatFolder;
27 import org.netbeans.modules.versioning.util.Utils;
28 import org.netbeans.api.fileinfo.NonRecursiveFolder;
29 import org.openide.filesystems.*;
30 import org.openide.util.actions.SystemAction;
31 import org.openide.util.actions.Presenter;
32 import org.openide.util.ContextAwareAction;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.Lookup;
35 import org.openide.awt.Mnemonics;
36
37 import javax.swing.*;
38 import java.util.*;
39 import java.util.logging.Logger JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.awt.Image JavaDoc;
42 import java.awt.event.ActionEvent JavaDoc;
43 import java.io.File JavaDoc;
44
45 /**
46  * Plugs into IDE filesystem and delegates annotation work to registered versioning systems.
47  *
48  * @author Maros Sandor
49  */

50 public class VersioningAnnotationProvider extends AnnotationProvider {
51     
52     static VersioningAnnotationProvider instance;
53
54     public VersioningAnnotationProvider() {
55         instance = this;
56     }
57     
58     private VCSContext createContext(Set<FileObject> files) {
59         Set<File JavaDoc> roots = new HashSet<File JavaDoc>(files.size());
60         if (files instanceof NonRecursiveFolder) {
61             FileObject folder = ((NonRecursiveFolder) files).getFolder();
62             roots.add(new FlatFolder(FileUtil.toFile(folder).getAbsolutePath()));
63         } else {
64             for (FileObject fo : files) {
65                 roots.add(FileUtil.toFile(fo));
66             }
67         }
68         return VCSContext.forFiles(roots);
69     }
70
71     public Image JavaDoc annotateIcon(Image JavaDoc icon, int iconType, Set files) {
72         FileObject fo = (FileObject) files.iterator().next();
73         VersioningSystem vs = VersioningManager.getInstance().getOwner(FileUtil.toFile(fo));
74         
75         if (vs == null) return null;
76         VCSAnnotator an = vs.getVCSAnnotator();
77         if (an == null) return null;
78
79         VCSContext context = createContext(files);
80         return an.annotateIcon(icon, context);
81     }
82
83     public String JavaDoc annotateNameHtml(String JavaDoc name, Set files) {
84         FileObject fo = (FileObject) files.iterator().next();
85         VersioningSystem vs = VersioningManager.getInstance().getOwner(FileUtil.toFile(fo));
86         
87         if (vs == null) return null;
88         VCSAnnotator an = vs.getVCSAnnotator();
89         if (an == null) return null;
90
91         VCSContext context = createContext(files);
92         return an.annotateName(name, context);
93     }
94
95     public Action[] actions(Set files) {
96         FileObject fo = (FileObject) files.iterator().next();
97         VersioningSystem vs = VersioningManager.getInstance().getOwner(FileUtil.toFile(fo));
98         
99         List<Action> actions = new ArrayList<Action>();
100         
101         VCSAnnotator an = null;
102         if (vs != null) {
103             an = vs.getVCSAnnotator();
104         }
105         if (an != null) {
106             VersioningSystemActions action = SystemAction.get(VersioningSystemActions.class);
107             action.setVersioninSystem(vs);
108             actions.add(action);
109         }
110         
111         VersioningSystem localHistory = VersioningManager.getInstance().getLocalHistory(FileUtil.toFile(fo));
112         if(localHistory != null && localHistory.getVCSAnnotator() != null) {
113             LocalHistoryActions localHistoryAction = SystemAction.get(LocalHistoryActions.class);
114             localHistoryAction.setVersioninSystem(localHistory);
115             actions.add(localHistoryAction);
116         }
117         return actions.toArray(new Action [actions.size()]);
118     }
119     
120     public static class VersioningSystemActions extends AbstractVersioningSystemActions {
121     }
122
123     public static class LocalHistoryActions extends AbstractVersioningSystemActions {
124     }
125     
126     public abstract static class AbstractVersioningSystemActions extends SystemAction implements ContextAwareAction {
127         
128         private VersioningSystem system;
129
130         public String JavaDoc getName() {
131             return system.getDisplayName();
132         }
133
134         public HelpCtx getHelpCtx() {
135             return new HelpCtx(system.getClass());
136         }
137
138         public void actionPerformed(ActionEvent JavaDoc ev) {
139             // this item does nothing, this is not a real action
140
}
141
142         public Action createContextAwareInstance(Lookup actionContext) {
143             return new RealVersioningSystemActions(system, VCSContext.forLookup(actionContext));
144         }
145
146         public void setVersioninSystem(VersioningSystem system) {
147             this.system = system;
148         }
149     }
150     
151     private static class RealVersioningSystemActions extends AbstractAction implements Presenter.Popup {
152
153         private final VersioningSystem system;
154         private final VCSContext context;
155
156         public RealVersioningSystemActions(VersioningSystem system, VCSContext context) {
157             super(system.getDisplayName());
158             this.system = system;
159             this.context = context;
160         }
161
162         public void actionPerformed(ActionEvent JavaDoc e) {
163             // this item does also nothing, it displays a popup ;)
164
}
165
166         public JMenuItem getPopupPresenter() {
167             return new VersioningSystemMenuItem();
168         }
169         
170         private class VersioningSystemMenuItem extends JMenu {
171         
172             private boolean popupContructed;
173
174             public VersioningSystemMenuItem() {
175                 Mnemonics.setLocalizedText(this, system.getDisplayName());
176             }
177
178             public void setSelected(boolean selected) {
179                 if (selected && popupContructed == false) {
180                     // lazy submenu construction
181
Action [] actions = system.getVCSAnnotator().getActions(context, VCSAnnotator.ActionDestination.PopupMenu);
182                     for (int i = 0; i < actions.length; i++) {
183                         Action action = actions[i];
184                         if (action == null) {
185                             add(new JSeparator()); // workaround openide bug
186
} else {
187                             JMenuItem item = Utils.toMenuItem(action);
188                             add(item);
189                         }
190                     }
191                     popupContructed = true;
192                 }
193                 super.setSelected(selected);
194             }
195         }
196     }
197
198     public InterceptionListener getInterceptionListener() {
199         return VersioningManager.getInstance().getInterceptionListener();
200     }
201
202     public String JavaDoc annotateName(String JavaDoc name, Set files) {
203         return name; // do not support 'plain' annotations
204
}
205
206     public void refreshAllAnnotations(boolean icon, boolean text) {
207         Set<FileSystem> filesystems = new HashSet<FileSystem>(1);
208         File JavaDoc[] allRoots = File.listRoots();
209         for (int i = 0; i < allRoots.length; i++) {
210             File JavaDoc root = allRoots[i];
211             FileObject fo = FileUtil.toFileObject(root);
212             if (fo != null) {
213                 try {
214                     filesystems.add(fo.getFileSystem());
215                 } catch (FileStateInvalidException e) {
216                     // ignore invalid filesystems
217
}
218             }
219         }
220         for (Iterator<FileSystem> i = filesystems.iterator(); i.hasNext();) {
221             FileSystem fileSystem = i.next();
222             fireFileStatusChanged(new FileStatusEvent(fileSystem, icon, text));
223         }
224     }
225     
226     /**
227      * Refreshes annotations for all given files and all parent folders of those files.
228      *
229      * @param filesToRefresh files to refresh
230      */

231     void refreshAnnotations(Set<File JavaDoc> filesToRefresh) {
232         if (filesToRefresh == null) {
233             refreshAllAnnotations(true, true);
234             return;
235         }
236         Map<FileSystem, Set<FileObject>> folders = new HashMap<FileSystem, Set<FileObject>>();
237         for (File JavaDoc file : filesToRefresh) {
238             for (File JavaDoc parent = file.getParentFile(); parent != null; parent = parent.getParentFile()) {
239                 try {
240                     FileObject fo;
241                     // TODO: #73233 diagnostics: remove afterwards
242
try {
243                         fo = FileUtil.toFileObject(parent);
244                     } catch (IllegalArgumentException JavaDoc e) {
245                         Logger.getLogger(VersioningAnnotationProvider.class.getName()).log(Level.INFO, "Issue #73233 log begins:");
246                         Logger.getLogger(VersioningAnnotationProvider.class.getName()).log(Level.INFO, "Original File: " + file.getAbsolutePath());
247                         Logger.getLogger(VersioningAnnotationProvider.class.getName()).log(Level.INFO, "Illegal file: " + parent.getAbsolutePath());
248                         RuntimeException JavaDoc ex = new RuntimeException JavaDoc("Please report this and append your messages.log file to issue http://www.netbeans.org/issues/show_bug.cgi?id=73233");
249                         ex.initCause(e);
250                         throw ex;
251                     }
252                     if (fo != null) {
253                         FileSystem fs = fo.getFileSystem();
254                         Set<FileObject> fsFolders = folders.get(fs);
255                         if (fsFolders == null) {
256                             fsFolders = new HashSet<FileObject>();
257                             folders.put(fs, fsFolders);
258                         }
259                         fsFolders.add(fo);
260                     }
261                 } catch (FileStateInvalidException e) {
262                     // ignore files in invalid filesystems
263
}
264             }
265             FileObject fo = FileUtil.toFileObject(file);
266             if (fo != null) {
267                 try {
268                     fireFileStatusChanged(new FileStatusEvent(fo.getFileSystem(), fo, fo.isFolder(), true));
269                 } catch (FileStateInvalidException e) {
270                     // ignore files in invalid filesystems
271
}
272             }
273         }
274         for (Iterator i = folders.keySet().iterator(); i.hasNext();) {
275             FileSystem fs = (FileSystem) i.next();
276             Set files = folders.get(fs);
277             fireFileStatusChanged(new FileStatusEvent(fs, files, true, false));
278         }
279     }
280 }
281
Popular Tags