1 19 20 package org.netbeans.modules.versioning.system.cvss.ui.actions; 21 22 import org.openide.util.actions.NodeAction; 23 import org.openide.util.HelpCtx; 24 import org.openide.util.NbBundle; 25 import org.openide.nodes.Node; 26 import org.openide.loaders.DataObject; 27 import org.openide.loaders.DataShadow; 28 import org.openide.filesystems.FileObject; 29 import org.openide.LifecycleManager; 30 import org.openide.windows.TopComponent; 31 import org.netbeans.modules.versioning.system.cvss.FileInformation; 32 import org.netbeans.modules.versioning.system.cvss.util.Utils; 33 import org.netbeans.modules.versioning.system.cvss.util.Context; 34 import org.netbeans.api.project.Project; 35 import org.netbeans.api.project.ProjectUtils; 36 37 import java.text.MessageFormat ; 38 import java.io.File ; 39 import java.util.MissingResourceException ; 40 import java.awt.event.ActionEvent ; 41 42 47 public abstract class AbstractSystemAction extends NodeAction { 48 49 53 protected abstract String getBaseName(Node [] activatedNodes); 54 55 protected AbstractSystemAction() { 56 setIcon(null); 57 putValue("noIconInMenu", Boolean.TRUE); } 59 60 64 protected void performAction(Node[] nodes) { 65 LifecycleManager.getDefault().saveAll(); 66 performCvsAction(nodes); 67 } 68 69 protected boolean enable(Node[] nodes) { 70 return getContext(nodes).getRootFiles().length > 0; 71 } 72 73 protected abstract void performCvsAction(Node[] nodes); 74 75 76 public final boolean isEnabled() { 77 return super.isEnabled(); 78 } 79 80 81 public final void setEnabled(boolean enabled) { 82 super.setEnabled(enabled); 83 } 84 85 86 public final void actionPerformed(ActionEvent event) { 87 super.actionPerformed(event); 88 } 89 90 91 public final void performAction() { 92 super.performAction(); 93 } 94 95 105 public String getName() { 106 return getName("", TopComponent.getRegistry().getActivatedNodes()); } 108 109 110 120 public String getRunningName(Node [] activatedNodes) { 121 return getName("Running", activatedNodes); } 123 124 private String getName(String role, Node [] activatedNodes) { 125 String baseName = getBaseName(activatedNodes) + role; 126 if (!isEnabled()) { 127 return NbBundle.getBundle(this.getClass()).getString(baseName); 128 } 129 130 File [] nodes = Utils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles(); 131 int objectCount = nodes.length; 132 136 boolean projectsOnly = true; 137 for (int i = 0; i < activatedNodes.length; i++) { 138 Node activatedNode = activatedNodes[i]; 139 Project project = (Project) activatedNode.getLookup().lookup(Project.class); 140 if (project == null) { 141 projectsOnly = false; 142 break; 143 } 144 } 145 if (projectsOnly) objectCount = activatedNodes.length; 146 147 if (objectCount == 0) { 148 return NbBundle.getBundle(this.getClass()).getString(baseName); 149 } else if (objectCount == 1) { 150 if (projectsOnly) { 151 String dispName = ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName(); 152 return NbBundle.getMessage(this.getClass(), baseName + "_Context", dispName); 154 } 155 String name; 156 FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class); 157 if (fo != null) { 158 name = fo.getNameExt(); 159 } else { 160 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class); 161 if (dao instanceof DataShadow) { 162 dao = ((DataShadow) dao).getOriginal(); 163 } 164 if (dao != null) { 165 name = dao.getPrimaryFile().getNameExt(); 166 } else { 167 name = activatedNodes[0].getDisplayName(); 168 } 169 } 170 return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context"), new Object [] { name }); 172 } else { 173 if (projectsOnly) { 174 try { 175 return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Projects"), new Object [] { new Integer (objectCount) }); 177 } catch (MissingResourceException ex) { 178 } 180 } 181 return MessageFormat.format(NbBundle.getBundle(this.getClass()).getString(baseName + "_Context_Multiple"), new Object [] { new Integer (objectCount) }); 183 } 184 } 185 186 192 public String getContextDisplayName(Node [] activatedNodes) { 193 File [] nodes = Utils.getCurrentContext(activatedNodes, getFileEnabledStatus(), getDirectoryEnabledStatus()).getFiles(); 195 int objectCount = nodes.length; 196 200 boolean projectsOnly = true; 201 for (int i = 0; i < activatedNodes.length; i++) { 202 Node activatedNode = activatedNodes[i]; 203 Project project = (Project) activatedNode.getLookup().lookup(Project.class); 204 if (project == null) { 205 projectsOnly = false; 206 break; 207 } 208 } 209 if (projectsOnly) objectCount = activatedNodes.length; 210 211 if (objectCount == 0) { 212 return null; 213 } else if (objectCount == 1) { 214 if (projectsOnly) { 215 return ProjectUtils.getInformation((Project) activatedNodes[0].getLookup().lookup(Project.class)).getDisplayName(); 216 } 217 FileObject fo = (FileObject) activatedNodes[0].getLookup().lookup(FileObject.class); 218 if (fo != null) { 219 return fo.getNameExt(); 220 } else { 221 DataObject dao = (DataObject) activatedNodes[0].getLookup().lookup(DataObject.class); 222 if (dao instanceof DataShadow) { 223 dao = ((DataShadow) dao).getOriginal(); 224 } 225 if (dao != null) { 226 return dao.getPrimaryFile().getNameExt(); 227 } else { 228 return activatedNodes[0].getDisplayName(); 229 } 230 } 231 } else { 232 if (projectsOnly) { 233 try { 234 return MessageFormat.format(NbBundle.getBundle(AbstractSystemAction.class).getString("MSG_ActionContext_MultipleProjects"), new Object [] { new Integer (objectCount) }); 236 } catch (MissingResourceException ex) { 237 } 239 } 240 return MessageFormat.format(NbBundle.getBundle(AbstractSystemAction.class).getString("MSG_ActionContext_MultipleFiles"), new Object [] { new Integer (objectCount) }); 242 } 243 } 244 245 public HelpCtx getHelpCtx() { 246 return new HelpCtx(this.getClass()); 247 } 248 249 protected Context getContext(Node[] nodes) { 250 return Utils.getCurrentContext(nodes, getFileEnabledStatus(), getDirectoryEnabledStatus()); 251 } 252 253 protected int getFileEnabledStatus() { 254 return ~0; 255 } 256 257 protected int getDirectoryEnabledStatus() { 258 return FileInformation.STATUS_MANAGED & ~FileInformation.STATUS_NOTVERSIONED_EXCLUDED; 259 } 260 } 261 | Popular Tags |