1 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 ; 40 import java.text.DateFormat ; 41 import java.io.File ; 42 import java.util.MissingResourceException ; 43 import java.util.Date ; 44 import java.awt.event.ActionEvent ; 45 import org.netbeans.modules.subversion.Subversion; 46 import org.tigris.subversion.svnclientadapter.SVNUrl; 47 48 53 public abstract class ContextAction extends NodeAction { 54 55 58 protected ContextAction() { 59 setIcon(null); 60 putValue("noIconInMenu", Boolean.TRUE); } 62 63 67 protected abstract String getBaseName(Node[] activatedNodes); 68 69 protected boolean enable(Node[] nodes) { 70 return getContext(nodes).getRootFiles().length > 0; 71 } 72 73 77 protected void performAction(final Node[] nodes) { 78 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 [] roots = ctx.getRootFiles(); 92 return SvnUtils.getRepositoryRootUrl(roots[0]); 93 } 94 95 protected abstract void performContextAction(Node[] nodes); 96 97 98 public final boolean isEnabled() { 99 return super.isEnabled(); 100 } 101 102 103 public final void setEnabled(boolean enabled) { 104 super.setEnabled(enabled); 105 } 106 107 108 public final void actionPerformed(ActionEvent event) { 109 super.actionPerformed(event); 110 } 111 112 113 public final void performAction() { 114 super.performAction(); 115 } 116 117 127 public String getRunningName(Node [] activatedNodes) { 128 return getName("Running", activatedNodes); } 130 131 public String getName() { 132 return getName("", TopComponent.getRegistry().getActivatedNodes()); } 134 135 145 public String getName(String role, Node[] activatedNodes) { 146 String baseName = getBaseName(activatedNodes) + role; 147 if (!isEnabled()) { 148 return NbBundle.getBundle(this.getClass()).getString(baseName); 149 } 150 151 File [] nodes = SvnUtils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles(); 152 int objectCount = nodes.length; 153 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 dispName = ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName(); 173 return NbBundle.getMessage(this.getClass(), baseName + "_Context", dispName); 175 } 176 String 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"), new Object [] { name }); 193 } else { 194 if (projectsOnly) { 195 try { 196 return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Projects"), new Object [] { new Integer (objectCount) }); 198 } catch (MissingResourceException ex) { 199 } 201 } 202 return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context_Multiple"), new Object [] { new Integer (objectCount) }); 204 } 205 } 206 207 213 public String getContextDisplayName(Node [] activatedNodes) { 214 File [] nodes = SvnUtils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles(); 216 int objectCount = nodes.length; 217 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"), new Object [] { new Integer (objectCount) }); 257 } catch (MissingResourceException ex) { 258 } 260 } 261 return MessageFormat.format(NbBundle.getBundle(ContextAction.class).getString("MSG_ActionContext_MultipleFiles"), new Object [] { new Integer (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 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 ()) + " " + runningName); ProgressHandle progress = getProgressHandle(); 312 progress.setInitialDelay(500); 313 progressStamp = System.currentTimeMillis() + 500; 314 progress.start(); 315 } 316 317 protected void finnishProgress() { 318 320 final ProgressHandle progress = getProgressHandle(); 321 progress.switchToDeterminate(100); 322 progress.progress(NbBundle.getMessage(ContextAction.class, "MSG_Progress_Done"), 100); if (System.currentTimeMillis() > progressStamp) { 324 RequestProcessor.getDefault().post(new Runnable () { 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 ()) + " " + runningName + " " + NbBundle.getMessage(ContextAction.class, "MSG_Progress_Finished")); } else { 336 getLogger().logCommandLine("==[IDE]== " + DateFormat.getDateTimeInstance().format(new Date ()) + " " + runningName + " " + NbBundle.getMessage(ContextAction.class, "MSG_Progress_Canceled")); } 338 } 339 } 340 } 341 | Popular Tags |