01: /*
02: * Created on Mar 25, 2005
03: *
04: * TODO To change the template for this generated file go to
05: * Window - Preferences - Java - Code Style - Code Templates
06: */
07: package net.refractions.udig.internal.ui.operations;
08:
09: import java.util.ArrayList;
10: import java.util.List;
11:
12: import net.refractions.udig.ui.operations.OpAction;
13:
14: import org.eclipse.core.runtime.IConfigurationElement;
15: import org.eclipse.jface.action.IAction;
16: import org.eclipse.jface.action.MenuManager;
17: import org.eclipse.jface.viewers.ISelection;
18:
19: /**
20: * A category of operations. Listens to the contained actions and disables it self
21: * if none of its contained Actions are visible.
22: *
23: * @author jeichar
24: */
25: public class OperationCategory extends MenuManager {
26: List<OpAction> actions = new ArrayList<OpAction>();
27: MenuManager manager;
28:
29: /**
30: * new instance
31: */
32: public OperationCategory(IConfigurationElement element) {
33: super (element.getAttribute("name"), element.getAttribute("id")); //$NON-NLS-1$ //$NON-NLS-2$
34: }
35:
36: public MenuManager createContextMenu() {
37: manager = new MenuManager(getMenuText(), getId());
38: for (OpAction action : actions) {
39: if (action.isEnabled()) {
40: manager.add(action);
41: }
42: }
43: return manager;
44: }
45:
46: /**
47: * All actions added must be instances of OpAction
48: * @see org.eclipse.jface.action.ContributionManager#add(org.eclipse.jface.action.IAction)
49: */
50: public void add(IAction action) {
51: assert action instanceof OpAction;
52: actions.add((OpAction) action);
53: ((OpAction) action).setCategory(this );
54: super .add(action);
55: }
56:
57: /**
58: * @param selection
59: */
60: public void setSelection(ISelection selection) {
61: for (OpAction action : actions) {
62: action.selectionChanged(null, selection);
63: }
64: }
65:
66: /**
67: * Contained OpActions call this to notify the category when its enablement has changed.
68: */
69: public void enablementChanged() {
70: boolean enabled = false;
71: for (OpAction action : actions) {
72: if (action.isEnabled()) {
73: enabled = true;
74: break;
75: }
76: }
77: if (getMenu() != null && getMenu().getParentItem() != null) {
78: getMenu().getParentItem().setEnabled(enabled);
79: }
80: }
81:
82: /**
83: * gets all the Operation actions in the category.
84: */
85: public List<OpAction> getActions() {
86: return actions;
87: }
88: }
|