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