Lines 54-59
Link Here
|
54 |
import javax.swing.*; |
54 |
import javax.swing.*; |
55 |
import javax.swing.event.*; |
55 |
import javax.swing.event.*; |
56 |
import org.openide.awt.DynamicMenuContent; |
56 |
import org.openide.awt.DynamicMenuContent; |
|
|
57 |
import org.openide.util.lookup.Lookups; |
57 |
|
58 |
|
58 |
|
59 |
|
59 |
/** A "meta-action" that displays (in a submenu) a list of enabled actions provided by modules. |
60 |
/** A "meta-action" that displays (in a submenu) a list of enabled actions provided by modules. |
Lines 64-73
Link Here
|
64 |
* It is desirable for most nodes to include this action somewhere in their popup menu. |
65 |
* It is desirable for most nodes to include this action somewhere in their popup menu. |
65 |
* |
66 |
* |
66 |
* <p><em>Note:</em> you do not need to touch this class to add a service action! |
67 |
* <p><em>Note:</em> you do not need to touch this class to add a service action! |
67 |
* Just add the action to a module manifest in an <code>Action</code> section. |
68 |
* Just register your action into <code>UI/ToolActions</code> layer folder |
68 |
* |
69 |
* (read <a href="@org-openide-modules@/org/openide/modules/doc-files/api.html#how-layer">more about layers</a>) |
69 |
* <p>The list of registered service actions is provided to this action from the implementation |
70 |
* since version 6.15. |
70 |
* by means of {@link ActionManager}. |
|
|
71 |
* |
71 |
* |
72 |
* @author Jaroslav Tulach |
72 |
* @author Jaroslav Tulach |
73 |
*/ |
73 |
*/ |
Lines 90-123
Link Here
|
90 |
|
90 |
|
91 |
/* @return name |
91 |
/* @return name |
92 |
*/ |
92 |
*/ |
|
|
93 |
@Override |
93 |
public String getName() { |
94 |
public String getName() { |
94 |
return getActionName(); |
95 |
return getActionName(); |
95 |
} |
96 |
} |
96 |
|
97 |
|
97 |
/* @return help for this action |
98 |
/* @return help for this action |
98 |
*/ |
99 |
*/ |
|
|
100 |
@Override |
99 |
public HelpCtx getHelpCtx() { |
101 |
public HelpCtx getHelpCtx() { |
100 |
return new HelpCtx(ToolsAction.class); |
102 |
return new HelpCtx(ToolsAction.class); |
101 |
} |
103 |
} |
102 |
|
104 |
|
103 |
/* @return menu presenter for the action |
105 |
/* @return menu presenter for the action |
104 |
*/ |
106 |
*/ |
|
|
107 |
@Override |
105 |
public JMenuItem getMenuPresenter() { |
108 |
public JMenuItem getMenuPresenter() { |
106 |
return new Inline(this); |
109 |
return new Inline(this); |
107 |
} |
110 |
} |
108 |
|
111 |
|
109 |
/* @return menu presenter for the action |
112 |
/* @return menu presenter for the action |
110 |
*/ |
113 |
*/ |
|
|
114 |
@Override |
111 |
public JMenuItem getPopupPresenter() { |
115 |
public JMenuItem getPopupPresenter() { |
112 |
return new Popup(this); |
116 |
return new Popup(this); |
113 |
} |
117 |
} |
114 |
|
118 |
|
115 |
/* Does nothing. |
119 |
/* Does nothing. |
116 |
*/ |
120 |
*/ |
|
|
121 |
@Override |
117 |
public void actionPerformed(java.awt.event.ActionEvent ev) { |
122 |
public void actionPerformed(java.awt.event.ActionEvent ev) { |
118 |
assert false; |
123 |
assert false; |
119 |
} |
124 |
} |
120 |
|
125 |
|
|
|
126 |
@Override |
121 |
public Action createContextAwareInstance(Lookup actionContext) { |
127 |
public Action createContextAwareInstance(Lookup actionContext) { |
122 |
return new DelegateAction(this, actionContext); |
128 |
return new DelegateAction(this, actionContext); |
123 |
} |
129 |
} |
Lines 128-133
Link Here
|
128 |
return NbBundle.getMessage(ToolsAction.class, "CTL_Tools"); |
134 |
return NbBundle.getMessage(ToolsAction.class, "CTL_Tools"); |
129 |
} |
135 |
} |
130 |
|
136 |
|
|
|
137 |
static List<Action> getToolActions() { |
138 |
ActionManager am = ActionManager.getDefault(); |
139 |
List<Action> arr = new ArrayList<Action>(); |
140 |
arr.addAll(Arrays.<Action>asList(am.getContextActions())); |
141 |
|
142 |
String pref = arr.isEmpty() ? null : ""; |
143 |
Lookup.Result<Action> result = Lookups.forPath("UI/ToolActions").lookupResult(Action.class); // NOI18N |
144 |
for (Lookup.Item<Action> item : result.allItems()) { |
145 |
final Action action = item.getInstance(); |
146 |
if (action == null) { |
147 |
continue; |
148 |
} |
149 |
String where = item.getId().replaceFirst("[^/]*$", ""); // NOI18N |
150 |
if (pref != null && !pref.equals(where)) { |
151 |
arr.add(null); |
152 |
} |
153 |
pref = where; |
154 |
arr.add(action); |
155 |
} |
156 |
return arr; |
157 |
} |
158 |
|
159 |
|
131 |
/** Implementation method that regenerates the items in the menu or |
160 |
/** Implementation method that regenerates the items in the menu or |
132 |
* in the array. |
161 |
* in the array. |
133 |
* |
162 |
* |
Lines 135-143
Link Here
|
135 |
* @param list (can be null) |
164 |
* @param list (can be null) |
136 |
*/ |
165 |
*/ |
137 |
private static List<JMenuItem> generate(Action toolsAction, boolean forMenu) { |
166 |
private static List<JMenuItem> generate(Action toolsAction, boolean forMenu) { |
138 |
ActionManager am = ActionManager.getDefault(); |
167 |
List<Action> actions = getToolActions(); |
139 |
SystemAction[] actions = am.getContextActions(); |
168 |
List<JMenuItem> list = new ArrayList<JMenuItem>(actions.size()); |
140 |
List<JMenuItem> list = new ArrayList<JMenuItem>(actions.length); |
|
|
141 |
|
169 |
|
142 |
boolean separator = false; |
170 |
boolean separator = false; |
143 |
boolean firstItemAdded = false; // flag to prevent adding separator before actual menu items |
171 |
boolean firstItemAdded = false; // flag to prevent adding separator before actual menu items |
Lines 211-217
Link Here
|
211 |
/** @deprecated Useless, see {@link ActionManager}. */ |
239 |
/** @deprecated Useless, see {@link ActionManager}. */ |
212 |
@Deprecated |
240 |
@Deprecated |
213 |
public static interface Model { |
241 |
public static interface Model { |
214 |
public SystemAction[] getActions(); |
242 |
public Action[] getActions(); |
215 |
|
243 |
|
216 |
public void addChangeListener(javax.swing.event.ChangeListener l); |
244 |
public void addChangeListener(javax.swing.event.ChangeListener l); |
217 |
|
245 |
|
Lines 235-240
Link Here
|
235 |
|
263 |
|
236 |
|
264 |
|
237 |
|
265 |
|
|
|
266 |
@Override |
238 |
public JComponent[] synchMenuPresenters(JComponent[] items) { |
267 |
public JComponent[] synchMenuPresenters(JComponent[] items) { |
239 |
if (timestamp == gl().getTimestamp()) { |
268 |
if (timestamp == gl().getTimestamp()) { |
240 |
return items; |
269 |
return items; |
Lines 246-251
Link Here
|
246 |
} |
275 |
} |
247 |
|
276 |
|
248 |
|
277 |
|
|
|
278 |
@Override |
249 |
public JComponent[] getMenuPresenters() { |
279 |
public JComponent[] getMenuPresenters() { |
250 |
return synchMenuPresenters(new JComponent[0]); |
280 |
return synchMenuPresenters(new JComponent[0]); |
251 |
} |
281 |
} |
Lines 271-281
Link Here
|
271 |
} |
301 |
} |
272 |
|
302 |
|
273 |
|
303 |
|
|
|
304 |
@Override |
274 |
public JComponent[] synchMenuPresenters(JComponent[] items) { |
305 |
public JComponent[] synchMenuPresenters(JComponent[] items) { |
275 |
return gl().isPopupEnabled(toolsAction) ? new JMenuItem[] { menu } : new JMenuItem[0]; |
306 |
return gl().isPopupEnabled(toolsAction) ? new JMenuItem[] { menu } : new JMenuItem[0]; |
276 |
} |
307 |
} |
277 |
|
308 |
|
278 |
|
309 |
|
|
|
310 |
@Override |
279 |
public JComponent[] getMenuPresenters() { |
311 |
public JComponent[] getMenuPresenters() { |
280 |
return synchMenuPresenters(new JComponent[0]); |
312 |
return synchMenuPresenters(new JComponent[0]); |
281 |
} |
313 |
} |
Lines 291-296
Link Here
|
291 |
super(getActionName()); |
323 |
super(getActionName()); |
292 |
} |
324 |
} |
293 |
|
325 |
|
|
|
326 |
@Override |
294 |
public JPopupMenu getPopupMenu() { |
327 |
public JPopupMenu getPopupMenu() { |
295 |
JPopupMenu popup = super.getPopupMenu(); |
328 |
JPopupMenu popup = super.getPopupMenu(); |
296 |
fillSubmenu(popup); |
329 |
fillSubmenu(popup); |
Lines 326-337
Link Here
|
326 |
} |
359 |
} |
327 |
} |
360 |
} |
328 |
|
361 |
|
|
|
362 |
@Override |
329 |
public void popupMenuCanceled(PopupMenuEvent e) { |
363 |
public void popupMenuCanceled(PopupMenuEvent e) { |
330 |
} |
364 |
} |
331 |
|
365 |
|
|
|
366 |
@Override |
332 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
367 |
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
333 |
} |
368 |
} |
334 |
|
369 |
|
|
|
370 |
@Override |
335 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
371 |
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { |
336 |
lastPopup.removePopupMenuListener(this); |
372 |
lastPopup.removePopupMenuListener(this); |
337 |
lastPopup = null; // clear the status and stop listening |
373 |
lastPopup = null; // clear the status and stop listening |
Lines 344-350
Link Here
|
344 |
private static class G implements PropertyChangeListener { |
380 |
private static class G implements PropertyChangeListener { |
345 |
public static final String PROP_STATE = "actionsState"; // NOI18N |
381 |
public static final String PROP_STATE = "actionsState"; // NOI18N |
346 |
private int timestamp = 1; |
382 |
private int timestamp = 1; |
347 |
private SystemAction[] actions = null; |
383 |
private Action[] actions = null; |
348 |
private PropertyChangeSupport supp = new PropertyChangeSupport(this); |
384 |
private PropertyChangeSupport supp = new PropertyChangeSupport(this); |
349 |
|
385 |
|
350 |
public G() { |
386 |
public G() { |
Lines 369-379
Link Here
|
369 |
timestamp++; |
405 |
timestamp++; |
370 |
|
406 |
|
371 |
// deregister all actions listeners |
407 |
// deregister all actions listeners |
372 |
SystemAction[] copy = actions; |
408 |
Action[] copy = actions; |
373 |
|
409 |
|
374 |
if (copy != null) { |
410 |
if (copy != null) { |
375 |
for (int i = 0; i < copy.length; i++) { |
411 |
for (int i = 0; i < copy.length; i++) { |
376 |
SystemAction act = copy[i]; |
412 |
Action act = copy[i]; |
377 |
|
413 |
|
378 |
if (act != null) { |
414 |
if (act != null) { |
379 |
act.removePropertyChangeListener(this); |
415 |
act.removePropertyChangeListener(this); |
Lines 385-391
Link Here
|
385 |
copy = am.getContextActions(); |
421 |
copy = am.getContextActions(); |
386 |
|
422 |
|
387 |
for (int i = 0; i < copy.length; i++) { |
423 |
for (int i = 0; i < copy.length; i++) { |
388 |
SystemAction act = copy[i]; |
424 |
Action act = copy[i]; |
389 |
|
425 |
|
390 |
if (act != null) { |
426 |
if (act != null) { |
391 |
act.addPropertyChangeListener(this); |
427 |
act.addPropertyChangeListener(this); |
Lines 402-413
Link Here
|
402 |
firePropertyChange(PROP_STATE, null, null); // tell the world |
438 |
firePropertyChange(PROP_STATE, null, null); // tell the world |
403 |
} |
439 |
} |
404 |
|
440 |
|
|
|
441 |
@Override |
405 |
public void propertyChange(PropertyChangeEvent ev) { |
442 |
public void propertyChange(PropertyChangeEvent ev) { |
406 |
String prop = ev.getPropertyName(); |
443 |
String prop = ev.getPropertyName(); |
407 |
|
444 |
|
408 |
if ((prop == null) || prop.equals(ActionManager.PROP_CONTEXT_ACTIONS)) { |
445 |
if ((prop == null) || prop.equals(ActionManager.PROP_CONTEXT_ACTIONS)) { |
409 |
actionsListChanged(); |
446 |
actionsListChanged(); |
410 |
} else if (prop.equals(SystemAction.PROP_ENABLED)) { |
447 |
} else if (prop.equals("enabled")) { |
411 |
actionStateChanged(); |
448 |
actionStateChanged(); |
412 |
} |
449 |
} |
413 |
} |
450 |
} |
Lines 417-423
Link Here
|
417 |
*/ |
454 |
*/ |
418 |
private boolean isPopupEnabled(Action toolsAction) { |
455 |
private boolean isPopupEnabled(Action toolsAction) { |
419 |
boolean en = false; |
456 |
boolean en = false; |
420 |
SystemAction[] copy = actions; |
457 |
Action[] copy = actions; |
421 |
|
458 |
|
422 |
// Get action conext. |
459 |
// Get action conext. |
423 |
Lookup lookup; |
460 |
Lookup lookup; |
Lines 470-517
Link Here
|
470 |
} |
507 |
} |
471 |
|
508 |
|
472 |
/** Overrides superclass method, adds delegate description. */ |
509 |
/** Overrides superclass method, adds delegate description. */ |
|
|
510 |
@Override |
473 |
public String toString() { |
511 |
public String toString() { |
474 |
return super.toString() + "[delegate=" + delegate + "]"; // NOI18N |
512 |
return super.toString() + "[delegate=" + delegate + "]"; // NOI18N |
475 |
} |
513 |
} |
476 |
|
514 |
|
477 |
/** Implements <code>Lookup.Provider</code>. */ |
515 |
/** Implements <code>Lookup.Provider</code>. */ |
|
|
516 |
@Override |
478 |
public Lookup getLookup() { |
517 |
public Lookup getLookup() { |
479 |
return lookup; |
518 |
return lookup; |
480 |
} |
519 |
} |
481 |
|
520 |
|
|
|
521 |
@Override |
482 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
522 |
public void actionPerformed(java.awt.event.ActionEvent e) { |
483 |
} |
523 |
} |
484 |
|
524 |
|
|
|
525 |
@Override |
485 |
public void putValue(String key, Object o) { |
526 |
public void putValue(String key, Object o) { |
486 |
} |
527 |
} |
487 |
|
528 |
|
|
|
529 |
@Override |
488 |
public Object getValue(String key) { |
530 |
public Object getValue(String key) { |
489 |
return delegate.getValue(key); |
531 |
return delegate.getValue(key); |
490 |
} |
532 |
} |
491 |
|
533 |
|
|
|
534 |
@Override |
492 |
public boolean isEnabled() { |
535 |
public boolean isEnabled() { |
493 |
// Irrelevant see G#isPopupEnabled(..). |
536 |
// Irrelevant see G#isPopupEnabled(..). |
494 |
return delegate.isEnabled(); |
537 |
return delegate.isEnabled(); |
495 |
} |
538 |
} |
496 |
|
539 |
|
|
|
540 |
@Override |
497 |
public void setEnabled(boolean b) { |
541 |
public void setEnabled(boolean b) { |
498 |
// Irrelevant see G#isPopupEnabled(..). |
542 |
// Irrelevant see G#isPopupEnabled(..). |
499 |
} |
543 |
} |
500 |
|
544 |
|
|
|
545 |
@Override |
501 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
546 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
502 |
support.addPropertyChangeListener(listener); |
547 |
support.addPropertyChangeListener(listener); |
503 |
} |
548 |
} |
504 |
|
549 |
|
|
|
550 |
@Override |
505 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
551 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
506 |
support.removePropertyChangeListener(listener); |
552 |
support.removePropertyChangeListener(listener); |
507 |
} |
553 |
} |
508 |
|
554 |
|
509 |
/** Implements <code>Presenter.Menu</code>. */ |
555 |
/** Implements <code>Presenter.Menu</code>. */ |
|
|
556 |
@Override |
510 |
public javax.swing.JMenuItem getMenuPresenter() { |
557 |
public javax.swing.JMenuItem getMenuPresenter() { |
511 |
return new Inline(this); |
558 |
return new Inline(this); |
512 |
} |
559 |
} |
513 |
|
560 |
|
514 |
/** Implements <code>Presenter.Popup</code>. */ |
561 |
/** Implements <code>Presenter.Popup</code>. */ |
|
|
562 |
@Override |
515 |
public javax.swing.JMenuItem getPopupPresenter() { |
563 |
public javax.swing.JMenuItem getPopupPresenter() { |
516 |
return new ToolsAction.Popup(this); |
564 |
return new ToolsAction.Popup(this); |
517 |
} |
565 |
} |