001: /*
002: @COPYRIGHT@
003: */
004: package demo.sharededitor.controls;
005:
006: //import java.awt.BasicStroke;
007: import java.awt.Color;
008: import java.awt.Image;
009: import java.awt.Stroke;
010: import java.awt.event.KeyEvent;
011: import java.awt.event.KeyListener;
012: import java.awt.event.MouseEvent;
013:
014: import javax.swing.event.MouseInputAdapter;
015:
016: import demo.sharededitor.events.ListListener;
017: import demo.sharededitor.models.BaseObject;
018: import demo.sharededitor.models.ObjectManager;
019: import demo.sharededitor.ui.Fontable;
020: import demo.sharededitor.ui.Texturable;
021: import demo.sharededitor.ui.Renderer;
022:
023: public final class Dispatcher extends MouseInputAdapter implements
024: KeyListener {
025: private ObjectManager objmgr;
026:
027: private transient Renderer renderer;
028:
029: private transient int lx;
030:
031: private transient int ly;
032:
033: private transient Image texture;
034:
035: private transient String drawTool;
036:
037: private transient Color foreground;
038:
039: private transient Color background;
040:
041: private transient int fillstyle;
042:
043: private transient Stroke stroke;
044:
045: private transient String fontName;
046:
047: private transient int fontSize;
048:
049: public Dispatcher(ObjectManager objects, Renderer renderer) {
050: this .renderer = renderer;
051: this .renderer.addMouseListener(this );
052: this .renderer.addMouseMotionListener(this );
053: this .renderer.addKeyListener(this );
054:
055: this .objmgr = objects;
056: this .objmgr.setListener((ListListener) this .renderer);
057: }
058:
059: public synchronized void mousePressed(MouseEvent e) {
060: this .renderer.requestFocusInWindow();
061: int x = e.getX();
062: int y = e.getY();
063: trackXY(x, y);
064:
065: if (objmgr.canGrabAt(x, y))
066: objmgr.grabAt(x, y, !e.isControlDown());
067: else {
068: BaseObject obj = objmgr.create(x, y, this .drawTool);
069: obj.setFillStyle(this .fillstyle);
070: obj.setForeground(this .foreground);
071: obj.setBackground(this .background);
072: obj.setStroke(this .stroke);
073: if ((obj instanceof Texturable)
074: && (BaseObject.FILLSTYLE_TEXTURED == fillstyle)) {
075: Texturable to = (Texturable) obj;
076: to.setTexture(this .texture);
077: }
078:
079: if (obj instanceof Fontable) {
080: Fontable fo = (Fontable) obj;
081: fo.setFontInfo(this .fontName, this .fontSize, "");
082: }
083: }
084: }
085:
086: public synchronized void mouseDragged(MouseEvent e) {
087: if (objmgr.lastGrabbed() == null)
088: return;
089:
090: int x = e.getX();
091: int y = e.getY();
092: BaseObject current = objmgr.lastGrabbed();
093:
094: if (current.isAnchorGrabbed())
095: current.resize(x, y);
096: else
097: current.move(x - lx, y - ly);
098:
099: trackXY(x, y);
100: }
101:
102: public synchronized void mouseClicked(MouseEvent e) {
103: BaseObject current = objmgr.grabAt(e.getX(), e.getY(), !e
104: .isControlDown());
105: if (current != null) {
106: switch (e.getClickCount()) {
107: case 1:
108: current.selectAction(true);
109: break;
110: case 2:
111: current.alternateSelectAction(true);
112: break;
113: }
114: }
115: trackXY(e.getX(), e.getY());
116: }
117:
118: public synchronized void mouseMoved(MouseEvent e) {
119: trackXY(e.getX(), e.getY());
120: }
121:
122: public synchronized void mouseReleased(MouseEvent e) {
123: trackXY(e.getX(), e.getY());
124: BaseObject current = objmgr.lastGrabbed();
125: if ((current != null) && current.isTransient()) {
126: objmgr.deleteSelection();
127: objmgr.selectAllWithin(current);
128: }
129: }
130:
131: public synchronized void keyPressed(KeyEvent e) {
132: processCommand(e);
133: }
134:
135: public synchronized void keyTyped(KeyEvent e) {
136: processCommand(e);
137: }
138:
139: public synchronized void keyReleased(KeyEvent e) {
140: // no need to do anything here
141: }
142:
143: private void processCommand(KeyEvent e) {
144: if (KeyEvent.KEY_TYPED == e.getID()) {
145: if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy1234567890!@#$%^&*() _+=-`~[]\\{}|:;'?/><,.\""
146: .indexOf(e.getKeyChar()) != -1)
147: sendCharToObject(e.getKeyChar());
148: } else {
149: int keyCode = e.getKeyCode();
150: if (e.isControlDown()) {
151: switch (keyCode) {
152: case KeyEvent.VK_BACK_SPACE:
153: this .objmgr.deleteSelection();
154: break;
155: case KeyEvent.VK_I:
156: this .objmgr.invertSelection();
157: break;
158: case KeyEvent.VK_A:
159: this .objmgr.toggleSelection();
160: break;
161: }
162: } else {
163: switch (keyCode) {
164: case KeyEvent.VK_BACK_SPACE:
165: sendKeyToObject(keyCode);
166: break;
167: case KeyEvent.VK_DELETE:
168: this .objmgr.deleteSelection();
169: break;
170: case KeyEvent.VK_ENTER:
171: case KeyEvent.VK_ESCAPE:
172: this .objmgr.clearSelection();
173: break;
174: }
175: }
176: }
177: }
178:
179: private void sendCharToObject(char c) {
180: BaseObject current = objmgr.lastGrabbed();
181: if ((current != null) && (current instanceof Fontable)) {
182: ((Fontable) current).appendToText(c);
183: //String text = ((IFontable) current).getText();
184: //((IFontable) current).setText(text + c);
185: }
186: }
187:
188: private void sendKeyToObject(int keyCode) {
189: BaseObject current = objmgr.lastGrabbed();
190: if ((current != null) && (current instanceof Fontable)) {
191: String text = ((Fontable) current).getText();
192: if (text.length() == 0)
193: return;
194: ((Fontable) current).setText(text.substring(0, text
195: .length() - 1));
196: }
197: }
198:
199: private void trackXY(int x, int y) {
200: lx = x;
201: ly = y;
202: }
203:
204: public synchronized void setDrawTool(String name) {
205: this .renderer.requestFocusInWindow();
206: this .drawTool = name;
207: }
208:
209: public synchronized void setTexture(Image texture) {
210: this .renderer.requestFocusInWindow();
211: this .texture = texture;
212: BaseObject current = objmgr.lastGrabbed();
213: if ((current != null) && (current instanceof Texturable)
214: && (BaseObject.FILLSTYLE_TEXTURED == fillstyle)) {
215: ((Texturable) current).clearTexture();
216: ((Texturable) current).setTexture(texture);
217: }
218: }
219:
220: public synchronized void setFontName(String fontName) {
221: this .renderer.requestFocusInWindow();
222: this .fontName = fontName;
223: BaseObject current = objmgr.lastGrabbed();
224: if ((current != null) && (current instanceof Fontable))
225: ((Fontable) current).setFontName(fontName);
226: }
227:
228: public synchronized void setFontSize(int fontSize) {
229: this .renderer.requestFocusInWindow();
230: this .fontSize = fontSize;
231: BaseObject current = objmgr.lastGrabbed();
232: if ((current != null) && (current instanceof Fontable))
233: ((Fontable) current).setFontSize(fontSize);
234: }
235:
236: public synchronized void setFillStyle(int fillstyle) {
237: this .renderer.requestFocusInWindow();
238: this .fillstyle = fillstyle;
239: }
240:
241: public synchronized void setStroke(Stroke stroke) {
242: this .renderer.requestFocusInWindow();
243: this .stroke = stroke;
244: BaseObject current = objmgr.lastGrabbed();
245: if (current != null)
246: current.setStroke(stroke);
247: }
248:
249: public synchronized void setForeground(Color foreground) {
250: this .renderer.requestFocusInWindow();
251: this .foreground = foreground;
252: BaseObject current = objmgr.lastGrabbed();
253: if (current != null)
254: current.setForeground(foreground);
255: }
256:
257: public synchronized void setBackground(Color background) {
258: this .renderer.requestFocusInWindow();
259: this .background = background;
260: BaseObject current = objmgr.lastGrabbed();
261: if (current != null)
262: current.setBackground(background);
263: }
264: }
|