Lines 41-54
Link Here
|
41 |
|
41 |
|
42 |
package org.netbeans.spi.debugger; |
42 |
package org.netbeans.spi.debugger; |
43 |
|
43 |
|
|
|
44 |
import java.beans.PropertyChangeEvent; |
45 |
import java.beans.PropertyChangeListener; |
44 |
import java.lang.annotation.ElementType; |
46 |
import java.lang.annotation.ElementType; |
45 |
import java.lang.annotation.Retention; |
47 |
import java.lang.annotation.Retention; |
46 |
import java.lang.annotation.RetentionPolicy; |
48 |
import java.lang.annotation.RetentionPolicy; |
47 |
import java.lang.annotation.Target; |
49 |
import java.lang.annotation.Target; |
|
|
50 |
import java.lang.reflect.InvocationTargetException; |
51 |
import java.util.ArrayList; |
52 |
import java.util.Arrays; |
53 |
import java.util.Collections; |
54 |
import java.util.HashSet; |
55 |
import java.util.List; |
48 |
import java.util.Map; |
56 |
import java.util.Map; |
49 |
import java.util.Set; |
57 |
import java.util.Set; |
|
|
58 |
|
50 |
import org.netbeans.debugger.registry.ContextAwareServiceHandler; |
59 |
import org.netbeans.debugger.registry.ContextAwareServiceHandler; |
51 |
import org.netbeans.spi.debugger.ContextAwareSupport; |
60 |
import org.openide.filesystems.FileObject; |
|
|
61 |
import org.openide.util.Exceptions; |
62 |
import org.openide.util.Lookup; |
52 |
import org.openide.util.RequestProcessor; |
63 |
import org.openide.util.RequestProcessor; |
53 |
|
64 |
|
54 |
/** |
65 |
/** |
Lines 134-166
Link Here
|
134 |
*/ |
145 |
*/ |
135 |
String path() default ""; |
146 |
String path() default ""; |
136 |
|
147 |
|
|
|
148 |
/** |
149 |
* Provide the list of actions that this provider supports. |
150 |
* This list is used before an instance of the registered class is created, |
151 |
* it's necessary when {@link #enabledOnMIMETypes()} is overriden |
152 |
* to prevent from the class instantiation. |
153 |
* @return The list of actions. |
154 |
* @since 1.23 |
155 |
*/ |
156 |
String[] actions() default {}; |
157 |
|
158 |
/** |
159 |
* Provide a list of MIME types that are compared to the MIME type of |
160 |
* a file currently active in the IDE and when matched, this provider |
161 |
* is activated (an instance of the registered class is created). |
162 |
* By default, the provider instance is created immediately. |
163 |
* This method can be used to delay the instantiation of the |
164 |
* implementation class for performance reasons. |
165 |
* @return The list of MIME types |
166 |
* @since 1.23 |
167 |
*/ |
168 |
String[] enabledOnMIMETypes() default {}; |
169 |
|
137 |
} |
170 |
} |
138 |
|
171 |
|
139 |
static class ContextAware extends ActionsProvider implements ContextAwareService<ActionsProvider> { |
172 |
static class ContextAware extends ActionsProvider implements ContextAwareService<ActionsProvider> { |
|
|
173 |
|
174 |
private static final String ERROR = "error in getting MIMEType"; // NOI18N |
140 |
|
175 |
|
141 |
private String serviceName; |
176 |
private String serviceName; |
142 |
private ContextProvider context; |
177 |
private ContextProvider context; |
143 |
private ActionsProvider delegate; |
178 |
private ActionsProvider delegate; |
144 |
|
179 |
|
145 |
private ContextAware(String serviceName) { |
180 |
private Set actions; |
|
|
181 |
private Set<String> enabledOnMIMETypes; |
182 |
private List<ActionsProviderListener> listeners = new ArrayList<ActionsProviderListener>(); |
183 |
private PropertyChangeListener contextDispatcherListener; |
184 |
|
185 |
private ContextAware(String serviceName, Set actions, Set<String> enabledOnMIMETypes) { |
146 |
this.serviceName = serviceName; |
186 |
this.serviceName = serviceName; |
|
|
187 |
this.actions = actions; |
188 |
this.enabledOnMIMETypes = enabledOnMIMETypes; |
147 |
} |
189 |
} |
148 |
|
190 |
|
149 |
private ContextAware(String serviceName, ContextProvider context) { |
191 |
private ContextAware(String serviceName, Set actions, Set<String> enabledOnMIMETypes, |
|
|
192 |
ContextProvider context) { |
150 |
this.serviceName = serviceName; |
193 |
this.serviceName = serviceName; |
|
|
194 |
this.actions = actions; |
195 |
this.enabledOnMIMETypes = enabledOnMIMETypes; |
151 |
this.context = context; |
196 |
this.context = context; |
152 |
} |
197 |
} |
153 |
|
198 |
|
154 |
private synchronized ActionsProvider getDelegate() { |
199 |
private synchronized ActionsProvider getDelegate() { |
155 |
if (delegate == null) { |
200 |
if (delegate == null) { |
156 |
delegate = (ActionsProvider) ContextAwareSupport.createInstance(serviceName, context); |
201 |
delegate = (ActionsProvider) ContextAwareSupport.createInstance(serviceName, context); |
|
|
202 |
for (ActionsProviderListener l : listeners) { |
203 |
delegate.addActionsProviderListener(l); |
204 |
} |
205 |
listeners.clear(); |
206 |
if (contextDispatcherListener != null) { |
207 |
detachContextDispatcherListener(); |
208 |
} |
157 |
} |
209 |
} |
158 |
return delegate; |
210 |
return delegate; |
159 |
} |
211 |
} |
160 |
|
212 |
|
161 |
@Override |
213 |
@Override |
162 |
public Set getActions() { |
214 |
public Set getActions() { |
163 |
return getDelegate().getActions(); |
215 |
ActionsProvider actionsDelegate; |
|
|
216 |
if (actions != null) { |
217 |
synchronized (this) { |
218 |
actionsDelegate = this.delegate; |
219 |
} |
220 |
} else { |
221 |
actionsDelegate = getDelegate(); |
222 |
} |
223 |
if (actionsDelegate == null) { |
224 |
return actions; |
225 |
} |
226 |
return actionsDelegate.getActions(); |
164 |
} |
227 |
} |
165 |
|
228 |
|
166 |
@Override |
229 |
@Override |
Lines 175-198
Link Here
|
175 |
|
238 |
|
176 |
@Override |
239 |
@Override |
177 |
public boolean isEnabled(Object action) { |
240 |
public boolean isEnabled(Object action) { |
|
|
241 |
ActionsProvider actionsDelegate; |
242 |
if (enabledOnMIMETypes != null) { |
243 |
synchronized (this) { |
244 |
actionsDelegate = this.delegate; |
245 |
} |
246 |
} else { |
247 |
actionsDelegate = getDelegate(); |
248 |
} |
249 |
if (actionsDelegate == null) { |
250 |
String currentMIMEType = getCurrentMIMEType(); |
251 |
if (currentMIMEType != ERROR) { |
252 |
if (!enabledOnMIMETypes.contains(currentMIMEType)) { |
253 |
return false; |
254 |
} |
255 |
} |
256 |
} |
178 |
return getDelegate().isEnabled(action); |
257 |
return getDelegate().isEnabled(action); |
179 |
} |
258 |
} |
180 |
|
259 |
|
181 |
@Override |
260 |
@Override |
182 |
public void addActionsProviderListener(ActionsProviderListener l) { |
261 |
public void addActionsProviderListener(ActionsProviderListener l) { |
183 |
getDelegate().addActionsProviderListener(l); |
262 |
ActionsProvider actionsDelegate; |
|
|
263 |
synchronized (this) { |
264 |
actionsDelegate = delegate; |
265 |
if (actionsDelegate == null) { |
266 |
listeners.add(l); |
267 |
if (contextDispatcherListener == null && enabledOnMIMETypes != null) { |
268 |
contextDispatcherListener = attachContextDispatcherListener(); |
269 |
} |
270 |
return ; |
271 |
} |
272 |
} |
273 |
actionsDelegate.addActionsProviderListener(l); |
184 |
} |
274 |
} |
185 |
|
275 |
|
186 |
@Override |
276 |
@Override |
187 |
public void removeActionsProviderListener(ActionsProviderListener l) { |
277 |
public void removeActionsProviderListener(ActionsProviderListener l) { |
188 |
getDelegate().removeActionsProviderListener(l); |
278 |
ActionsProvider actionsDelegate; |
|
|
279 |
synchronized (this) { |
280 |
actionsDelegate = delegate; |
281 |
if (actionsDelegate == null) { |
282 |
listeners.remove(l); |
283 |
if (listeners.size() == 0 && contextDispatcherListener != null) { |
284 |
detachContextDispatcherListener(); |
285 |
} |
286 |
return ; |
287 |
} |
288 |
} |
289 |
actionsDelegate.removeActionsProviderListener(l); |
189 |
} |
290 |
} |
190 |
|
291 |
|
191 |
public ActionsProvider forContext(ContextProvider context) { |
292 |
public ActionsProvider forContext(ContextProvider context) { |
192 |
if (context == this.context) { |
293 |
if (context == this.context) { |
193 |
return this; |
294 |
return this; |
194 |
} else { |
295 |
} else { |
195 |
return new ActionsProvider.ContextAware(serviceName, context); |
296 |
return new ActionsProvider.ContextAware(serviceName, actions, enabledOnMIMETypes, context); |
196 |
} |
297 |
} |
197 |
} |
298 |
} |
198 |
|
299 |
|
Lines 205-211
Link Here
|
205 |
*/ |
306 |
*/ |
206 |
static ContextAwareService createService(Map attrs) throws ClassNotFoundException { |
307 |
static ContextAwareService createService(Map attrs) throws ClassNotFoundException { |
207 |
String serviceName = (String) attrs.get(ContextAwareServiceHandler.SERVICE_NAME); |
308 |
String serviceName = (String) attrs.get(ContextAwareServiceHandler.SERVICE_NAME); |
208 |
return new ActionsProvider.ContextAware(serviceName); |
309 |
String actionsStr = (String) attrs.get(ContextAwareServiceHandler.SERVICE_ACTIONS); |
|
|
310 |
String enabledOnMIMETypesStr = (String) attrs.get(ContextAwareServiceHandler.SERVICE_ENABLED_MIMETYPES); |
311 |
String[] actions = parseArray(actionsStr); |
312 |
String[] enabledOnMIMETypes = parseArray(enabledOnMIMETypesStr); |
313 |
return new ActionsProvider.ContextAware(serviceName, |
314 |
createSet(actions), |
315 |
createSet(enabledOnMIMETypes)); |
316 |
} |
317 |
|
318 |
private static String[] parseArray(String strArray) { |
319 |
if (strArray == null) { |
320 |
return null; |
321 |
} |
322 |
if (strArray.startsWith("[")) strArray = strArray.substring(1); |
323 |
if (strArray.endsWith("]")) strArray = strArray.substring(0, strArray.length() - 1); |
324 |
strArray = strArray.trim(); |
325 |
int index = 0; |
326 |
List<String> strings = new ArrayList<String>(); |
327 |
while (index < strArray.length()) { |
328 |
int index2 = strArray.indexOf(',', index); |
329 |
if (index2 < 0) index2 = strArray.length(); |
330 |
if (index2 > index) { |
331 |
String s = strArray.substring(index, index2).trim(); |
332 |
if (s.length() > 0) { // Can be trimmed to 0 length |
333 |
strings.add(s); |
334 |
} |
335 |
index = index2 + 1; |
336 |
} else { |
337 |
index++; |
338 |
continue; |
339 |
} |
340 |
} |
341 |
return strings.toArray(new String[0]); |
342 |
} |
343 |
|
344 |
private static <T> Set<T> createSet(T[] array) { |
345 |
if (array != null) { |
346 |
return Collections.unmodifiableSet(new HashSet(Arrays.asList(array))); |
347 |
} else { |
348 |
return null; |
349 |
} |
350 |
} |
351 |
|
352 |
private static String getCurrentMIMEType() { |
353 |
// Ask EditorContextDispatcher.getDefault().getMostRecentFile() |
354 |
// It's not in a dependent module, therefore we have to find it dynamically: |
355 |
try { |
356 |
Class editorContextDispatcherClass = Lookup.getDefault().lookup(ClassLoader.class).loadClass("org.netbeans.spi.debugger.ui.EditorContextDispatcher"); |
357 |
try { |
358 |
try { |
359 |
Object editorContextDispatcher = editorContextDispatcherClass.getMethod("getDefault").invoke(null); |
360 |
FileObject file = (FileObject) editorContextDispatcherClass.getMethod("getMostRecentFile").invoke(editorContextDispatcher); |
361 |
if (file != null) { |
362 |
return file.getMIMEType(); |
363 |
} else { |
364 |
return null; |
365 |
} |
366 |
} catch (IllegalAccessException ex) { |
367 |
Exceptions.printStackTrace(ex); |
368 |
} catch (IllegalArgumentException ex) { |
369 |
Exceptions.printStackTrace(ex); |
370 |
} catch (InvocationTargetException ex) { |
371 |
Exceptions.printStackTrace(ex); |
372 |
} |
373 |
} catch (NoSuchMethodException ex) { |
374 |
Exceptions.printStackTrace(ex); |
375 |
} catch (SecurityException ex) { |
376 |
Exceptions.printStackTrace(ex); |
377 |
} |
378 |
} catch (ClassNotFoundException ex) { |
379 |
} |
380 |
return ERROR; |
381 |
} |
382 |
|
383 |
private PropertyChangeListener attachContextDispatcherListener() { |
384 |
// Call EditorContextDispatcher.getDefault().addPropertyChangeListener(String MIMEType, PropertyChangeListener l) |
385 |
// It's not in a dependent module, therefore we have to find it dynamically: |
386 |
PropertyChangeListener l = null; |
387 |
try { |
388 |
Class editorContextDispatcherClass = Lookup.getDefault().lookup(ClassLoader.class).loadClass("org.netbeans.spi.debugger.ui.EditorContextDispatcher"); |
389 |
try { |
390 |
try { |
391 |
Object editorContextDispatcher = editorContextDispatcherClass.getMethod("getDefault").invoke(null); |
392 |
java.lang.reflect.Method m = editorContextDispatcherClass.getMethod( |
393 |
"addPropertyChangeListener", |
394 |
String.class, |
395 |
PropertyChangeListener.class); |
396 |
l = new ContextDispatcherListener(); |
397 |
for (String mimeType : enabledOnMIMETypes) { |
398 |
m.invoke(editorContextDispatcher, mimeType, l); |
399 |
} |
400 |
} catch (IllegalAccessException ex) { |
401 |
Exceptions.printStackTrace(ex); |
402 |
} catch (IllegalArgumentException ex) { |
403 |
Exceptions.printStackTrace(ex); |
404 |
} catch (InvocationTargetException ex) { |
405 |
Exceptions.printStackTrace(ex); |
406 |
} |
407 |
} catch (NoSuchMethodException ex) { |
408 |
Exceptions.printStackTrace(ex); |
409 |
} catch (SecurityException ex) { |
410 |
Exceptions.printStackTrace(ex); |
411 |
} |
412 |
} catch (ClassNotFoundException ex) { |
413 |
} |
414 |
return l; |
415 |
} |
416 |
|
417 |
private void detachContextDispatcherListener() { |
418 |
// Call EditorContextDispatcher.getDefault().removePropertyChangeListener(PropertyChangeListener l) |
419 |
// It's not in a dependent module, therefore we have to find it dynamically: |
420 |
PropertyChangeListener l = null; |
421 |
try { |
422 |
Class editorContextDispatcherClass = Lookup.getDefault().lookup(ClassLoader.class).loadClass("org.netbeans.spi.debugger.ui.EditorContextDispatcher"); |
423 |
try { |
424 |
try { |
425 |
Object editorContextDispatcher = editorContextDispatcherClass.getMethod("getDefault").invoke(null); |
426 |
java.lang.reflect.Method m = editorContextDispatcherClass.getMethod( |
427 |
"removePropertyChangeListener", |
428 |
PropertyChangeListener.class); |
429 |
m.invoke(editorContextDispatcher, contextDispatcherListener); |
430 |
contextDispatcherListener = null; |
431 |
} catch (IllegalAccessException ex) { |
432 |
Exceptions.printStackTrace(ex); |
433 |
} catch (IllegalArgumentException ex) { |
434 |
Exceptions.printStackTrace(ex); |
435 |
} catch (InvocationTargetException ex) { |
436 |
Exceptions.printStackTrace(ex); |
437 |
} |
438 |
} catch (NoSuchMethodException ex) { |
439 |
Exceptions.printStackTrace(ex); |
440 |
} catch (SecurityException ex) { |
441 |
Exceptions.printStackTrace(ex); |
442 |
} |
443 |
} catch (ClassNotFoundException ex) { |
444 |
} |
445 |
} |
446 |
|
447 |
private class ContextDispatcherListener implements PropertyChangeListener { |
448 |
|
449 |
@Override |
450 |
public void propertyChange(PropertyChangeEvent evt) { |
451 |
List<ActionsProviderListener> ls; |
452 |
synchronized (ContextAware.this) { |
453 |
ls = new ArrayList<ActionsProviderListener>(listeners); |
454 |
} |
455 |
for (ActionsProviderListener l : ls) { |
456 |
for (Object action : actions) { |
457 |
l.actionStateChange(action, isEnabled(action)); |
458 |
} |
459 |
} |
460 |
} |
461 |
|
209 |
} |
462 |
} |
210 |
|
463 |
|
211 |
} |
464 |
} |