001: /*
002: * AbstractEngine.java
003: *
004: * Created on January 24, 2004, 1:36 PM
005: */
006:
007: package org.netbeans.actions.engine.spi;
008:
009: import java.util.HashSet;
010: import java.util.Map;
011: import java.util.Set;
012: import javax.swing.Action;
013: import javax.swing.JComponent;
014: import javax.swing.JMenu;
015: import javax.swing.JMenuBar;
016: import javax.swing.JMenuItem;
017: import javax.swing.JPopupMenu;
018: import javax.swing.JToolBar;
019: import org.netbeans.actions.api.ContextProvider;
020: import org.netbeans.actions.api.Engine;
021: import org.netbeans.actions.engine.spi.MenuFactory;
022: import org.netbeans.actions.engine.spi.ToolbarFactory;
023: import org.netbeans.actions.spi.ActionProvider;
024: import org.netbeans.actions.spi.ContainerProvider;
025:
026: /** Convenience basic impl of Engine. Mostly it connects individual
027: * infrastructure pieces with each other.
028: *
029: * @author Tim Boudreau
030: */
031: public abstract class AbstractEngine extends Engine {
032: private ActionProvider actionProvider;
033: private ContainerProvider containerProvider;
034: private MenuFactory menuFactory = null;
035: private ToolbarFactory toolbarFactory = null;
036: private ActionFactory actionFactory = null;
037:
038: /** Creates a new instance of AbstractEngine */
039: protected AbstractEngine(ActionProvider actionProvider,
040: ContainerProvider containerProvider) {
041: this .actionProvider = actionProvider;
042: this .containerProvider = containerProvider;
043: }
044:
045: private ContextProvider contextProvider;
046:
047: public void setContextProvider(ContextProvider ctx) {
048: this .contextProvider = ctx;
049: }
050:
051: public ContextProvider getContextProvider() {
052: return contextProvider;
053: }
054:
055: protected final ActionProvider getActionProvider() {
056: return actionProvider;
057: }
058:
059: protected final ContainerProvider getContainerProvider() {
060: return containerProvider;
061: }
062:
063: protected Action getAction(String containerCtx, String action) {
064: return getActionFactory().getAction(action, containerCtx,
065: getContextProvider().getContext());
066: }
067:
068: protected abstract MenuFactory createMenuFactory();
069:
070: protected abstract ToolbarFactory createToolbarFactory();
071:
072: protected abstract ActionFactory createActionFactory();
073:
074: protected abstract ContextMenuFactory createContextMenuFactory();
075:
076: void notifyWillPerform(String action, String containerCtx) {
077:
078: }
079:
080: void notifyPerformed(String action, String containerCtx) {
081: update();
082: }
083:
084: private ContextMenuFactory contextMenuFactory = null;
085:
086: public final ContextMenuFactory getContextMenuFactory() {
087: if (contextMenuFactory == null) {
088: contextMenuFactory = createContextMenuFactory();
089: }
090: return contextMenuFactory;
091: }
092:
093: public final MenuFactory getMenuFactory() {
094: if (menuFactory == null) {
095: menuFactory = createMenuFactory();
096: }
097: return menuFactory;
098: }
099:
100: public final ToolbarFactory getToolbarFactory() {
101: if (toolbarFactory == null) {
102: toolbarFactory = createToolbarFactory();
103: }
104: return toolbarFactory;
105: }
106:
107: public final ActionFactory getActionFactory() {
108: if (actionFactory == null) {
109: actionFactory = createActionFactory();
110: }
111: return actionFactory;
112: }
113:
114: void notifyOpened(String containerCtx, Object type) {
115:
116: }
117:
118: void notifyClosed(String containerCtx, Object type) {
119:
120: }
121:
122: boolean isOpen(String containerCtx, Object type) {
123: if (type == ContainerProvider.TYPE_MENU) {
124: return showingMenus.contains(containerCtx);
125: } else if (type == ContainerProvider.TYPE_TOOLBAR) {
126: return showingToolbars.contains(containerCtx);
127: } else {
128: return false;
129: }
130: }
131:
132: /** Indicates a menu has been displayed onscreen, not that it has been opened.
133: */
134: protected void notifyMenuShown(String containerCtx, JMenu menu) {
135: showingMenus.add(containerCtx);
136: }
137:
138: protected void notifyMenuHidden(String containerCtx, JMenu menu) {
139: showingMenus.remove(containerCtx);
140: }
141:
142: protected void notifyToolbarShown(String containerCtx,
143: JToolBar toolbar) {
144: showingToolbars.add(containerCtx);
145: }
146:
147: protected void notifyToolbarHidden(String containerCtx,
148: JToolBar toolbar) {
149: showingToolbars.remove(containerCtx);
150: }
151:
152: public void update() {
153: updateToolbars();
154: updateMenus();
155: }
156:
157: protected void updateToolbars() {
158: int count = showingToolbars.size();
159: String[] toolbars = new String[count];
160: toolbars = (String[]) showingToolbars.toArray(toolbars);
161: for (int i = 0; i < count; i++) {
162: getToolbarFactory().update(toolbars[i]);
163: }
164: }
165:
166: protected void updateMenus() {
167: int count = showingMenus.size();
168: String[] menus = new String[count];
169: menus = (String[]) showingMenus.toArray(menus);
170: for (int i = 0; i < count; i++) {
171: getMenuFactory().update(menus[i]);
172: }
173: }
174:
175: public final JMenuBar createMenuBar() {
176: JMenuBar result = new JMenuBar();
177: ContainerProvider cp = getContainerProvider();
178: String[] menus = cp.getMenuContainerContexts();
179: for (int i = 0; i < menus.length; i++) {
180: JMenu menu = getMenuFactory().createMenu(menus[i]);
181: result.add(menu);
182: }
183: return result;
184: }
185:
186: public final JToolBar[] createToolbars() {
187: ContainerProvider cp = getContainerProvider();
188: String[] toolbars = cp.getToolbarContainerContexts();
189: JToolBar[] result = new JToolBar[toolbars.length];
190: for (int i = 0; i < toolbars.length; i++) {
191: result[i] = getToolbarFactory().createToolbar(toolbars[i]);
192: }
193: return result;
194: }
195:
196: public JPopupMenu createPopupMenu() {
197: return getContextMenuFactory().createMenu(
198: getContextProvider().getContext());
199: }
200:
201: private Set showingMenus = new HashSet();
202: private Set showingToolbars = new HashSet();
203:
204: }
|