1 19 20 package org.netbeans.modules.project.ui.actions; 21 22 import java.text.MessageFormat ; 23 import java.util.Arrays ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Set ; 29 import javax.swing.Action ; 30 import org.netbeans.api.project.FileOwnerQuery; 31 import org.netbeans.api.project.Project; 32 import org.netbeans.spi.project.ActionProvider; 33 import org.netbeans.api.project.ProjectUtils; 34 import org.openide.filesystems.FileObject; 35 import org.openide.loaders.DataObject; 36 import org.openide.util.Lookup; 37 import org.openide.util.WeakSet; 38 39 43 class ActionsUtil { 44 45 48 49 public static final ShortcutsManager SHORCUTS_MANAGER = new ShortcutsManager(); 50 51 public static HashMap <String ,MessageFormat > pattern2format = new HashMap <String ,MessageFormat >(); 52 53 58 public static Project[] getProjectsFromLookup( Lookup lookup, String command ) { 59 66 Set <Project> result = new HashSet <Project>(); 69 for (Project p : lookup.lookupAll(Project.class)) { 70 result.add(p); 71 } 72 for (DataObject dObj : lookup.lookupAll(DataObject.class)) { 74 FileObject fObj = dObj.getPrimaryFile(); 75 Project p = FileOwnerQuery.getOwner(fObj); 76 if ( p != null ) { 77 result.add( p ); 78 } 79 } 80 Project[] projectsArray = result.toArray(new Project[result.size()]); 81 82 if ( command != null ) { 83 for (Project p : projectsArray) { 85 if (!commandSupported(p, command, lookup)) { 86 return new Project[0]; 87 } 88 } 89 } 90 91 return projectsArray; 92 } 93 94 97 public static FileObject[] getFilesFromLookup( Lookup lookup, Project project ) { 98 HashSet <FileObject> result = new HashSet <FileObject>(); 99 for (DataObject dObj : lookup.lookupAll(DataObject.class)) { 100 FileObject fObj = dObj.getPrimaryFile(); 101 Project p = FileOwnerQuery.getOwner(fObj); 102 if ( p != null && p.equals( project ) ) { 103 result.add( fObj ); 104 } 105 106 } 107 108 FileObject[] fos = new FileObject[ result.size() ]; 109 result.toArray( fos ); 110 return fos; 111 } 112 113 114 122 public static boolean commandSupported( Project project, String command, Lookup context ) { 123 ActionProvider ap = (ActionProvider)project.getLookup().lookup( ActionProvider.class ); 125 if ( ap != null ) { 126 List commands = Arrays.asList( ap.getSupportedActions() ); 127 if ( commands.contains( command ) ) { 128 if (context == null || ap.isActionEnabled(command, context)) { 129 return true; 131 } 132 } 133 } 134 return false; 136 } 137 138 139 140 public static String formatProjectSensitiveName( String namePattern, Project projects[] ) { 141 142 if ( projects == null || projects.length == 0 ) { 144 return ActionsUtil.formatName( namePattern, 0, null ); 146 } 147 else { 148 return ActionsUtil.formatName( namePattern, projects.length, ProjectUtils.getInformation( projects[0] ).getDisplayName() ); 151 } 152 } 153 154 155 160 public static String formatName( String namePattern, int numberOfObjects, String firstObjectName ) { 161 162 MessageFormat mf = null; 163 164 synchronized ( pattern2format ) { 165 mf = (MessageFormat )pattern2format.get( namePattern ); 166 if ( mf == null ) { 167 mf = new MessageFormat ( namePattern ); 168 pattern2format.put( namePattern, mf ); 169 } 170 } 171 172 StringBuffer result = new StringBuffer (); 173 174 mf.format( 175 new Object [] { 176 numberOfObjects, 177 firstObjectName == null ? "" : firstObjectName, 178 }, 179 result, 180 null ); 181 182 return result.toString(); 183 } 184 185 186 188 191 192 public static class ShortcutsManager { 193 194 Map <String ,Object > shorcuts = new HashMap <String , Object >(); 196 197 HashMap <String , Set <Action >> actions = new HashMap <String , Set <Action >>(); 199 200 201 public void registerAction( String command, Action action ) { 202 203 synchronized ( this ) { 204 Set <Action > commandActions = actions.get( command ); 205 206 if ( commandActions == null ) { 207 commandActions = new WeakSet<Action >(); 208 actions.put( command, commandActions ); 209 } 210 211 commandActions.add( action ); 212 213 } 214 215 Object shorcut = getShortcut( command ); 216 217 if ( shorcut != null ) { 218 action.putValue( Action.ACCELERATOR_KEY, shorcut ); 219 } 220 221 } 222 223 224 public void registerShortcut( String command, Object shortcut ) { 225 226 Set <Action > actionsToChange = null; 227 228 synchronized ( this ) { 229 230 Object exShorcut = getShortcut( command ); 231 232 if ( ( exShorcut != null && exShorcut.equals( shortcut ) ) || ( exShorcut == null && shortcut == null ) ) { return; } 236 237 shorcuts.put( command, shortcut ); 238 239 Set <Action > commandActions = actions.get( command ); 240 if ( commandActions != null && !commandActions.isEmpty() ) { 241 actionsToChange = new HashSet <Action >(); 242 actionsToChange.addAll( commandActions ); 243 } 244 245 } 246 247 if ( actionsToChange != null ) { 248 for (Action a : actionsToChange) { 250 if ( a != null ) { 251 a.putValue( Action.ACCELERATOR_KEY, shortcut ); 252 } 253 } 254 } 255 256 } 257 258 public synchronized Object getShortcut( String command ) { 259 return shorcuts.get( command ); 260 } 261 262 } 263 264 274 376 377 } 378 | Popular Tags |