001: /*
002: * SimpleEngine.java
003: *
004: * Created on January 25, 2004, 1:42 PM
005: */
006:
007: package org.netbeans.actions.simple;
008:
009: import java.awt.Toolkit;
010: import java.awt.event.ActionEvent;
011: import java.awt.event.ActionListener;
012: import java.io.IOException;
013: import java.io.ObjectInputStream;
014: import java.io.ObjectOutputStream;
015: import java.net.URL;
016: import java.util.Map;
017: import java.util.ResourceBundle;
018: import javax.swing.AbstractAction;
019: import javax.swing.Action;
020: import javax.swing.ActionMap;
021: import javax.swing.ComponentInputMap;
022: import javax.swing.InputMap;
023: import javax.swing.JComponent;
024: import javax.swing.KeyStroke;
025: import javax.swing.Timer;
026: import javax.swing.text.Keymap;
027: import org.netbeans.actions.api.ContextProvider;
028: import org.netbeans.actions.api.Engine;
029: import org.netbeans.actions.engine.spi.AbstractContextMenuFactory;
030: import org.netbeans.actions.engine.spi.MenuFactory;
031: import org.netbeans.actions.engine.spi.ToolbarFactory;
032: import org.netbeans.actions.engine.spi.AbstractEngine;
033: import org.netbeans.actions.engine.spi.AbstractMenuFactory;
034: import org.netbeans.actions.engine.spi.AbstractToolbarFactory;
035: import org.netbeans.actions.engine.spi.ActionFactory;
036: import org.netbeans.actions.engine.spi.ContextMenuFactory;
037: import org.netbeans.actions.spi.ActionProvider;
038: import org.netbeans.actions.spi.ContainerProvider;
039:
040: /** A basic test implementation
041: *
042: * @author tim
043: */
044: public class SimpleEngine extends AbstractEngine implements
045: ActionListener {
046: Interpreter interp;
047: ResourceBundle bundle;
048: private Timer timer = new javax.swing.Timer(100, this );
049:
050: public static Engine createEngine(URL actionDefs,
051: ResourceBundle bundle) {
052: Interpreter interp = new Interpreter(actionDefs);
053: return new SimpleEngine(interp, bundle);
054: }
055:
056: /** Creates a new instance of SimpleEngine */
057: private SimpleEngine(Interpreter interp, ResourceBundle bundle) {
058: super (new SimpleActionProvider(interp, bundle),
059: new SimpleContainerProvider(interp, bundle));
060: this .bundle = bundle;
061: this .interp = interp;
062: }
063:
064: public ContextProvider getContextProvider() {
065: return super .getContextProvider();
066: }
067:
068: protected ActionFactory createActionFactory() {
069: return new SimpleActionFactory();
070: }
071:
072: protected MenuFactory createMenuFactory() {
073: return new SimpleMenuFactory();
074: }
075:
076: protected ToolbarFactory createToolbarFactory() {
077: return new SimpleToolbarFactory();
078: }
079:
080: protected ContextMenuFactory createContextMenuFactory() {
081: return new SimpleContextMenuFactory();
082: }
083:
084: public InputMap createInputMap(JComponent jc) {
085: return new SimpleInputMap(jc);
086: }
087:
088: public ActionMap createActionMap() {
089: return new SimpleActionMap();
090: }
091:
092: private Keymap keymap = null;
093:
094: public Keymap createKeymap() {
095: if (keymap == null) {
096: keymap = new SimpleKeymap(this , interp);
097: }
098: return keymap;
099: }
100:
101: boolean updateRecommended = false;
102:
103: public void recommendUpdate() {
104: updateRecommended = true;
105: timer.setRepeats(true);
106: if (!timer.isRunning()) {
107: timer.start();
108: }
109: }
110:
111: public void actionPerformed(ActionEvent e) {
112: if (updateRecommended) {
113: update();
114: updateRecommended = false;
115: } else {
116: timer.stop();
117: }
118: }
119:
120: Action getAction(String action) {
121: //Used by keymap
122: return getActionFactory().getAction(action, null,
123: getContextProvider().getContext());
124: }
125:
126: private class SimpleActionFactory implements ActionFactory {
127: public Action getAction(final String action,
128: String containerCtx, final Map context) {
129: return new AbstractAction() {
130: public void actionPerformed(ActionEvent ae) {
131: SimpleInvoker invoker = interp.getInvoker(action);
132: System.err.println("Invoker is " + invoker);
133: System.err.println("Invoking " + action + " on "
134: + context);
135: if (invoker != null) {
136: invoker.invoke(context);
137: } else {
138: Toolkit.getDefaultToolkit().beep();
139: }
140: }
141: };
142: }
143: }
144:
145: private class SimpleToolbarFactory extends AbstractToolbarFactory {
146: public SimpleToolbarFactory() {
147: super (SimpleEngine.this );
148: }
149: }
150:
151: private class SimpleMenuFactory extends AbstractMenuFactory {
152: public SimpleMenuFactory() {
153: super (SimpleEngine.this );
154: }
155: }
156:
157: private class SimpleContextMenuFactory extends
158: AbstractContextMenuFactory {
159: public SimpleContextMenuFactory() {
160: super (SimpleEngine.this );
161: }
162: }
163:
164: private class SimpleInputMap extends ComponentInputMap {
165: public SimpleInputMap(JComponent jc) {
166: super (jc);
167: }
168:
169: public Object get(KeyStroke keyStroke) {
170: return interp.getActionForKeystroke(keyStroke);
171: }
172:
173: public void remove(Object key) {
174: throw new UnsupportedOperationException();
175: }
176:
177: public KeyStroke[] keys() {
178: return interp.getAllKeystrokes();
179: }
180:
181: public int size() {
182: return super .size() + keys().length;
183: }
184:
185: public KeyStroke[] allKeys() {
186: return keys(); //XXX merge w/ super
187: }
188:
189: private void writeObject(ObjectOutputStream s)
190: throws IOException {
191: //do nothing
192: }
193:
194: private void readObject(ObjectInputStream s)
195: throws ClassNotFoundException, IOException {
196: //do nothing
197: }
198: }
199:
200: private class SimpleActionMap extends ActionMap {
201: public Action get(Object key) {
202: Action a = super .get(key);
203: if (a == null && key instanceof String) {
204: a = getAction((String) key);
205: }
206: return a;
207: }
208:
209: public Object[] keys() {
210: return interp.getAllActionsBoundToKeystrokes();
211: }
212:
213: }
214:
215: }
|