1 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 ; 40 import java.util.logging.Level ; 41 import java.awt.Image ; 42 import java.awt.event.ActionEvent ; 43 import java.io.File ; 44 45 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 > roots = new HashSet<File >(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 annotateIcon(Image 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 annotateNameHtml(String 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 getName() { 131 return system.getDisplayName(); 132 } 133 134 public HelpCtx getHelpCtx() { 135 return new HelpCtx(system.getClass()); 136 } 137 138 public void actionPerformed(ActionEvent ev) { 139 } 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 e) { 163 } 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 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()); } 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 annotateName(String name, Set files) { 203 return name; } 205 206 public void refreshAllAnnotations(boolean icon, boolean text) { 207 Set<FileSystem> filesystems = new HashSet<FileSystem>(1); 208 File [] allRoots = File.listRoots(); 209 for (int i = 0; i < allRoots.length; i++) { 210 File 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 } 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 231 void refreshAnnotations(Set<File > 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 file : filesToRefresh) { 238 for (File parent = file.getParentFile(); parent != null; parent = parent.getParentFile()) { 239 try { 240 FileObject fo; 241 try { 243 fo = FileUtil.toFileObject(parent); 244 } catch (IllegalArgumentException 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 ex = new RuntimeException ("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 } 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 } 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 |