Lines 50-141
Link Here
|
50 |
import javax.swing.Icon; |
50 |
import javax.swing.Icon; |
51 |
import org.netbeans.api.project.Project; |
51 |
import org.netbeans.api.project.Project; |
52 |
import org.netbeans.spi.project.ActionProvider; |
52 |
import org.netbeans.spi.project.ActionProvider; |
|
|
53 |
import org.netbeans.spi.project.ui.support.FileActionPerformer; |
53 |
import org.openide.awt.Actions; |
54 |
import org.openide.awt.Actions; |
54 |
import org.openide.filesystems.FileObject; |
55 |
import org.openide.filesystems.FileObject; |
55 |
import org.openide.loaders.DataObject; |
56 |
import org.openide.loaders.DataObject; |
|
|
57 |
import org.openide.util.ContextAwareAction; |
56 |
import org.openide.util.ImageUtilities; |
58 |
import org.openide.util.ImageUtilities; |
57 |
import org.openide.util.Lookup; |
59 |
import org.openide.util.Lookup; |
58 |
import org.openide.util.Mutex; |
60 |
import org.openide.util.Mutex; |
59 |
|
61 |
|
60 |
/** An action sensitive to selected node. Used for 1-off actions |
62 |
/** An action sensitive to selected node. Used for 1-off actions |
61 |
*/ |
63 |
*/ |
62 |
public final class FileCommandAction extends ProjectAction { |
64 |
public final class FileAction extends LookupSensitiveAction implements ContextAwareAction { |
63 |
|
65 |
private String command; |
64 |
public FileCommandAction( String command, String namePattern, String iconResource, Lookup lookup ) { |
66 |
private FileActionPerformer performer; |
|
|
67 |
private final String namePattern; |
68 |
|
69 |
public FileAction(String command, String namePattern, Icon icon, Lookup lookup) { |
70 |
this( command, null, namePattern, icon, lookup ); |
71 |
} |
72 |
|
73 |
public FileAction( String command, String namePattern, String iconResource, Lookup lookup ) { |
65 |
this( command, namePattern, ImageUtilities.loadImageIcon(iconResource, false), lookup ); |
74 |
this( command, namePattern, ImageUtilities.loadImageIcon(iconResource, false), lookup ); |
66 |
} |
75 |
} |
67 |
|
76 |
|
68 |
public FileCommandAction( String command, String namePattern, Icon icon, Lookup lookup ) { |
77 |
public FileAction( FileActionPerformer performer, String namePattern, Icon icon, Lookup lookup) { |
69 |
super( command, namePattern, icon, lookup ); |
78 |
this( null, performer, namePattern, icon, lookup ); |
70 |
assert namePattern != null : "Name patern must not be null"; |
79 |
} |
71 |
String presenterName = ActionsUtil.formatName( getNamePattern(), 0, "" ); |
80 |
|
|
|
81 |
@SuppressWarnings("LeakingThisInConstructor") |
82 |
private FileAction(String command, FileActionPerformer performer, String namePattern, Icon icon, Lookup lookup) { |
83 |
super(icon, lookup, new Class<?>[] {Project.class, DataObject.class}); |
84 |
|
85 |
assert (command != null || performer != null) && !(command != null && performer != null); // exactly one of the arguments must be provided |
86 |
|
87 |
this.command = command; |
88 |
if ( command != null ) { |
89 |
ActionsUtil.SHORCUTS_MANAGER.registerAction( command, this ); |
90 |
} |
91 |
this.performer = performer; |
92 |
this.namePattern = namePattern; |
93 |
|
94 |
String presenterName = ActionsUtil.formatName( namePattern, 0, "" ); |
72 |
setDisplayName( presenterName ); |
95 |
setDisplayName( presenterName ); |
73 |
putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); |
96 |
putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); |
74 |
} |
97 |
} |
75 |
|
98 |
|
|
|
99 |
public final @Override void putValue( String key, Object value ) { |
100 |
super.putValue( key, value ); |
101 |
|
102 |
if (Action.ACCELERATOR_KEY.equals(key)) { |
103 |
ActionsUtil.SHORCUTS_MANAGER.registerShortcut( command, value ); |
104 |
} |
105 |
} |
106 |
|
76 |
@Override |
107 |
@Override |
77 |
protected void refresh(final Lookup context, final boolean immediate) { |
108 |
protected void refresh(final Lookup context, final boolean immediate) { |
78 |
Runnable r = new Runnable() { |
109 |
final Runnable[] r = new Runnable[1]; |
79 |
@Override public void run() { |
110 |
final boolean[] enable = new boolean[1]; |
80 |
Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() ); |
111 |
final String[] presenterName = new String[1]; |
81 |
final boolean enable; |
112 |
if (command != null) { |
82 |
final String presenterName; |
113 |
r[0] = new Runnable() { |
83 |
if ( projects.length != 1 ) { |
114 |
@Override public void run() { |
84 |
if (projects.length == 0 && globalProvider(context) != null) { |
115 |
Project[] projects = ActionsUtil.getProjectsFromLookup( context, command ); |
85 |
enable = true; |
116 |
if ( projects.length != 1 ) { |
86 |
Collection<? extends DataObject> files = context.lookupAll(DataObject.class); |
117 |
if (projects.length == 0 && globalProvider(context) != null) { |
87 |
presenterName = ActionsUtil.formatName(getNamePattern(), files.size(), |
118 |
enable[0] = true; |
88 |
files.isEmpty() ? "" : files.iterator().next().getPrimaryFile().getNameExt()); // NOI18N |
119 |
Collection<? extends DataObject> files = context.lookupAll(DataObject.class); |
|
|
120 |
presenterName[0] = ActionsUtil.formatName(namePattern, files.size(), |
121 |
files.isEmpty() ? "" : files.iterator().next().getPrimaryFile().getNameExt()); // NOI18N |
122 |
} else { |
123 |
enable[0] = false; // Zero or more than one projects found or command not supported |
124 |
presenterName[0] = ActionsUtil.formatName(namePattern, 0, ""); |
125 |
} |
126 |
} |
127 |
else { |
128 |
FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] ); |
129 |
enable[0] = true; |
130 |
presenterName[0] = ActionsUtil.formatName( namePattern, files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N |
131 |
} |
132 |
} |
133 |
}; |
134 |
} else if (performer != null) { |
135 |
r[0] = new Runnable() { |
136 |
@Override public void run() { |
137 |
Collection<? extends DataObject> dobjs = context.lookupAll(DataObject.class); |
138 |
if (dobjs.size() == 1) { |
139 |
FileObject f = dobjs.iterator().next().getPrimaryFile(); |
140 |
|
141 |
enable[0] = performer.enable(f); |
142 |
presenterName[0] = ActionsUtil.formatName(namePattern, 1, f); |
143 |
} else { |
144 |
enable[0] = false; |
145 |
presenterName[0] = ActionsUtil.formatName(namePattern, 0, ""); // NOI18N |
146 |
} |
147 |
|
148 |
} |
149 |
}; |
150 |
} |
151 |
|
152 |
if (r != null) { |
153 |
Runnable delegate = new Runnable() { |
154 |
|
155 |
@Override |
156 |
public void run() { |
157 |
r[0].run(); |
158 |
Mutex.EVENT.writeAccess(new Runnable() { |
159 |
@Override public void run() { |
160 |
putValue("menuText", presenterName[0]); |
161 |
putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName[0])); |
162 |
setEnabled(enable[0]); |
163 |
} |
164 |
}); |
165 |
} |
166 |
}; |
167 |
if (immediate) { |
168 |
delegate.run(); |
89 |
} else { |
169 |
} else { |
90 |
enable = false; // Zero or more than one projects found or command not supported |
170 |
RP.post(delegate); |
91 |
presenterName = ActionsUtil.formatName(getNamePattern(), 0, ""); |
|
|
92 |
} |
171 |
} |
93 |
} |
172 |
} |
94 |
else { |
|
|
95 |
FileObject[] files = ActionsUtil.getFilesFromLookup( context, projects[0] ); |
96 |
enable = true; |
97 |
presenterName = ActionsUtil.formatName( getNamePattern(), files.length, files.length > 0 ? files[0].getNameExt() : "" ); // NOI18N |
98 |
} |
99 |
Mutex.EVENT.writeAccess(new Runnable() { |
100 |
@Override public void run() { |
101 |
putValue("menuText", presenterName); |
102 |
putValue(SHORT_DESCRIPTION, Actions.cutAmpersand(presenterName)); |
103 |
setEnabled(enable); |
104 |
} |
105 |
}); |
106 |
} |
107 |
}; |
108 |
if (immediate) { |
109 |
r.run(); |
110 |
} else { |
111 |
RP.post(r); |
112 |
} |
113 |
} |
173 |
} |
114 |
|
174 |
|
115 |
@Override |
175 |
@Override |
116 |
protected void actionPerformed( Lookup context ) { |
176 |
protected void actionPerformed( Lookup context ) { |
117 |
Project[] projects = ActionsUtil.getProjectsFromLookup( context, getCommand() ); |
177 |
if (command != null) { |
|
|
178 |
Project[] projects = ActionsUtil.getProjectsFromLookup( context, command ); |
118 |
|
179 |
|
119 |
if ( projects.length == 1 ) { |
180 |
if ( projects.length == 1 ) { |
120 |
ActionProvider ap = projects[0].getLookup().lookup(ActionProvider.class); |
181 |
ActionProvider ap = projects[0].getLookup().lookup(ActionProvider.class); |
121 |
ap.invokeAction( getCommand(), context ); |
182 |
ap.invokeAction( command, context ); |
122 |
return; |
183 |
return; |
123 |
} |
184 |
} |
124 |
|
185 |
|
125 |
ActionProvider provider = globalProvider(context); |
186 |
ActionProvider provider = globalProvider(context); |
126 |
if (provider != null) { |
187 |
if (provider != null) { |
127 |
provider.invokeAction(getCommand(), context); |
188 |
provider.invokeAction(command, context); |
|
|
189 |
} |
190 |
} else if (performer != null) { |
191 |
Collection<? extends DataObject> dobjs = context.lookupAll(DataObject.class); |
192 |
if (dobjs.size() == 1) { |
193 |
performer.perform(dobjs.iterator().next().getPrimaryFile()); |
194 |
} |
128 |
} |
195 |
} |
129 |
} |
196 |
} |
130 |
|
197 |
|
131 |
@Override |
198 |
@Override |
132 |
public Action createContextAwareInstance( Lookup actionContext ) { |
199 |
public Action createContextAwareInstance( Lookup actionContext ) { |
133 |
return new FileCommandAction( getCommand(), getNamePattern(), (Icon)getValue( SMALL_ICON ), actionContext ); |
200 |
return new FileAction( command, performer, namePattern, (Icon)getValue( SMALL_ICON ), actionContext ); |
134 |
} |
201 |
} |
135 |
|
202 |
|
136 |
private ActionProvider globalProvider(Lookup context) { |
203 |
private ActionProvider globalProvider(Lookup context) { |
137 |
for (ActionProvider ap : Lookup.getDefault().lookupAll(ActionProvider.class)) { |
204 |
for (ActionProvider ap : Lookup.getDefault().lookupAll(ActionProvider.class)) { |
138 |
if (Arrays.asList(ap.getSupportedActions()).contains(getCommand()) && ap.isActionEnabled(getCommand(), context)) { |
205 |
if (Arrays.asList(ap.getSupportedActions()).contains(command) && ap.isActionEnabled(command, context)) { |
139 |
return ap; |
206 |
return ap; |
140 |
} |
207 |
} |
141 |
} |
208 |
} |