Package: EditingTool$State
EditingTool$State
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
EditingTool.State(EditingTool) |
|
|
|
|
|
||||||||||||||||||||
keyPressed(KeyEvent) |
|
|
|
|
|
||||||||||||||||||||
keyReleased(KeyEvent) |
|
|
|
|
|
||||||||||||||||||||
keyTyped(KeyEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseClicked(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseDragged(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseEntered(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseExited(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseMoved(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mousePressed(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
mouseReleased(MouseEvent) |
|
|
|
|
|
||||||||||||||||||||
paint(Graphics2D, EditableImageRenderer) |
|
|
|
|
|
||||||||||||||||||||
start() |
|
|
|
|
|
||||||||||||||||||||
stop() |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * Mistral: open source imaging engine
5: * http://tidalwave.it/projects/mistral
6: *
7: * Copyright (C) 2003 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/mistral-src
23: * git clone https://github.com/tidalwave-it/mistral-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.image.render;
28:
29: import java.util.HashMap;
30: import java.util.Map;
31: import java.awt.Cursor;
32: import java.awt.Graphics2D;
33: import java.awt.Point;
34: import java.awt.Toolkit;
35: import java.awt.event.ActionEvent;
36: import java.awt.event.KeyEvent;
37: import java.awt.event.KeyListener;
38: import java.awt.event.MouseEvent;
39: import java.awt.event.MouseListener;
40: import java.awt.event.MouseMotionListener;
41: import javax.swing.AbstractAction;
42: import javax.swing.Action;
43: import javax.swing.Icon;
44: import javax.swing.ImageIcon;
45: import javax.swing.JToggleButton;
46:
47: /***********************************************************************************************************************
48: *
49: * @author Fabrizio Giudici
50: *
51: **********************************************************************************************************************/
52: public class EditingTool implements MouseListener, MouseMotionListener, KeyListener, Overlay
53: {
54: public static final String CHANGED_ATTRIBUTE = EditingTool.class.getName() + ".changed";
55:
56: /*******************************************************************************************************************
57: *
58: *
59: ******************************************************************************************************************/
60: public class State // implements MouseListener, MouseMotionListener
61: {
62: public void mouseClicked (final MouseEvent event)
63: {
64: }
65:
66: public void mousePressed (final MouseEvent event)
67: {
68: }
69:
70: public void mouseReleased (final MouseEvent event)
71: {
72: }
73:
74: public void mouseEntered (final MouseEvent event)
75: {
76: }
77:
78: public void mouseExited (final MouseEvent event)
79: {
80: }
81:
82: public void mouseDragged (final MouseEvent event)
83: {
84: }
85:
86: public void mouseMoved (final MouseEvent event)
87: {
88: }
89:
90: public void keyTyped (final KeyEvent event)
91: {
92: }
93:
94: public void keyPressed (final KeyEvent event)
95: {
96:• if (event.getKeyCode() == KeyEvent.VK_ESCAPE)
97: {
98: deactivate();
99: }
100: }
101:
102: public void keyReleased (final KeyEvent event)
103: {
104: System.err.println("EVENT " + event);
105: }
106:
107: public void paint (final Graphics2D g, final EditableImageRenderer imageRenderer)
108: {
109: }
110:
111: public void start()
112: {
113: }
114:
115: public void stop()
116: {
117: }
118: }
119:
120: protected final EditableImageRenderer imageRenderer;
121:
122: private final Map<Class<? extends State>, State> stateMap = new HashMap<>();
123:
124: private final State NULL_STATE = new State();
125:
126: private State state = NULL_STATE;
127:
128: private Class<? extends State> initialState;
129:
130: private boolean active = false;
131:
132: private boolean oneShot = false;
133:
134: private Icon icon;
135:
136: private final JToggleButton.ToggleButtonModel buttonModel = new JToggleButton.ToggleButtonModel();
137:
138: /*******************************************************************************************************************
139: *
140: *
141: ******************************************************************************************************************/
142: private final Action action = new AbstractAction()
143: {
144: @Override
145: public void actionPerformed (final ActionEvent event)
146: {
147: if (!active)
148: {
149: activate();
150: }
151:
152: else
153: {
154: deactivate();
155: }
156: }
157: };
158:
159: /*******************************************************************************************************************
160: *
161: *
162: ******************************************************************************************************************/
163: protected EditingTool (final EditableImageRenderer imageRenderer)
164: {
165: this.imageRenderer = imageRenderer;
166: }
167:
168: /*******************************************************************************************************************
169: *
170: *
171: ******************************************************************************************************************/
172: public void setEnabled (final boolean enabled)
173: {
174: action.setEnabled(enabled);
175: }
176:
177: /*******************************************************************************************************************
178: *
179: *
180: ******************************************************************************************************************/
181: public boolean isEnabled()
182: {
183: return action.isEnabled();
184: }
185:
186: /*******************************************************************************************************************
187: *
188: *
189: ******************************************************************************************************************/
190: public boolean isActive()
191: {
192: return active;
193: }
194:
195: /*******************************************************************************************************************
196: *
197: * Programmatically commits the changes. E.g. called by the save of an image
198: * editor when a tools is currently on.
199: *
200: ******************************************************************************************************************/
201: public void commitChanges()
202: {
203: }
204:
205: /*******************************************************************************************************************
206: *
207: *
208: ******************************************************************************************************************/
209: public void activate()
210: {
211: if (active)
212: {
213: throw new IllegalStateException("Already active");
214: }
215:
216: if (imageRenderer.editingTool != null)
217: {
218: imageRenderer.editingTool.deactivate();
219: }
220:
221: active = true;
222: buttonModel.setSelected(true);
223: setState(initialState);
224: imageRenderer.addMouseListener(this);
225: imageRenderer.addMouseMotionListener(this);
226: imageRenderer.addKeyListener(this);
227: imageRenderer.addOverlay(this);
228: imageRenderer.setCursor(makeCursor(icon, "xxx"));
229: imageRenderer.fireEditingToolActivated(this);
230: imageRenderer.editingTool = this;
231: }
232:
233: /*******************************************************************************************************************
234: *
235: *
236: ******************************************************************************************************************/
237: public void deactivate()
238: {
239: if (!active)
240: {
241: throw new IllegalStateException("Not active");
242: }
243:
244: active = false;
245: buttonModel.setSelected(false);
246: imageRenderer.removeMouseListener(this);
247: imageRenderer.removeMouseMotionListener(this);
248: imageRenderer.removeKeyListener(this);
249: imageRenderer.removeOverlay(this);
250: imageRenderer.repaint();
251: imageRenderer.setCursor(null);
252: imageRenderer.fireEditingToolDeactivated(this);
253: imageRenderer.editingTool = null;
254: }
255:
256: /*******************************************************************************************************************
257: *
258: * Invoked when the renderer changes the image being edited by this tool.
259: * By default, the tool is deactivated (by invoking
260: * <code>deactivate()</code>), but you can change the default behaviour by
261: * overriding this method (for instance, committing changes).
262: *
263: ******************************************************************************************************************/
264: public void imageChanged()
265: {
266: deactivate();
267: }
268:
269: /*******************************************************************************************************************
270: *
271: *
272: ******************************************************************************************************************/
273: public void reset()
274: {
275: if (oneShot)
276: {
277: deactivate();
278: }
279:
280: else
281: {
282: imageRenderer.repaint();
283: setState(initialState);
284: }
285: }
286:
287: /*******************************************************************************************************************
288: *
289: *
290: ******************************************************************************************************************/
291: public void setIcon (final Icon icon)
292: {
293: this.icon = icon;
294: action.putValue(Action.SMALL_ICON, icon);
295: }
296:
297: /*******************************************************************************************************************
298: *
299: *
300: ******************************************************************************************************************/
301: @Override
302: public final boolean isVisible()
303: {
304: return true;
305: }
306:
307: /*******************************************************************************************************************
308: *
309: *
310: ******************************************************************************************************************/
311: public void connectButton (final JToggleButton button)
312: {
313: action.putValue(Action.SHORT_DESCRIPTION, button.getToolTipText());
314: button.setAction(action);
315: button.setModel(buttonModel);
316: }
317:
318: /*******************************************************************************************************************
319: *
320: *
321: ******************************************************************************************************************/
322: protected void setInitialState (final Class<? extends State> initialStateClass)
323: {
324: this.initialState = initialStateClass;
325: }
326:
327: /*******************************************************************************************************************
328: *
329: *
330: ******************************************************************************************************************/
331: protected void setState (final Class<? extends State> newStateClass)
332: {
333: if (state != null)
334: {
335: state.stop();
336: }
337:
338: final var newState = stateMap.get(newStateClass);
339:
340: if (newState == null)
341: {
342: throw new IllegalArgumentException("Invalid or unregistered state: " + newStateClass);
343: }
344:
345: this.state = newState;
346: state.start();
347: System.err.println("CURRENT STATE: " + this.state);
348: }
349:
350: /*******************************************************************************************************************
351: *
352: *
353: ******************************************************************************************************************/
354: protected State getCurrentState()
355: {
356: return state;
357: }
358:
359: /*******************************************************************************************************************
360: *
361: *
362: ******************************************************************************************************************/
363: protected void registerState (final State state)
364: {
365: stateMap.put(state.getClass(), state);
366: }
367:
368: /*******************************************************************************************************************
369: *
370: *
371: ******************************************************************************************************************/
372: protected void repaint()
373: {
374: imageRenderer.repaint();
375: }
376:
377: /*******************************************************************************************************************
378: *
379: *
380: ******************************************************************************************************************/
381: protected Cursor makeCursor (final Icon icon, final String name)
382: {
383: final var toolkit = Toolkit.getDefaultToolkit();
384: final var image = ((ImageIcon)icon).getImage();
385: return toolkit.createCustomCursor(image, new Point(0, 0), name);
386: }
387:
388: //// The following methods just delegate to the current State
389:
390: @Override
391: public final void mouseClicked (final MouseEvent event)
392: {
393: state.mouseClicked(event);
394: }
395:
396: @Override
397: public final void mousePressed (final MouseEvent event)
398: {
399: state.mousePressed(event);
400: }
401:
402: @Override
403: public final void mouseReleased (final MouseEvent event)
404: {
405: state.mouseReleased(event);
406: }
407:
408: @Override
409: public final void mouseEntered (final MouseEvent event)
410: {
411: state.mouseEntered(event);
412: }
413:
414: @Override
415: public final void mouseExited (final MouseEvent event)
416: {
417: state.mouseExited(event);
418: }
419:
420: @Override
421: public final void mouseDragged (final MouseEvent event)
422: {
423: state.mouseDragged(event);
424: }
425:
426: @Override
427: public final void mouseMoved (final MouseEvent event)
428: {
429: state.mouseMoved(event);
430: }
431:
432: @Override
433: public final void keyTyped (final KeyEvent event)
434: {
435: state.keyTyped(event);
436: }
437:
438: @Override
439: public final void keyPressed (final KeyEvent event)
440: {
441: state.keyPressed(event);
442: }
443:
444: @Override
445: public final void keyReleased (final KeyEvent event)
446: {
447: state.keyReleased(event);
448: }
449:
450: @Override
451: public final void paint (final Graphics2D g, final EditableImageRenderer imageRenderer)
452: {
453: state.paint(g, imageRenderer);
454: }
455: }