1
2
3
4
5
6
7 package nl.tudelft.simulation.dsol.gui.editor2D;
8
9 import java.awt.Color;
10 import java.awt.Dimension;
11 import java.awt.Graphics;
12 import java.awt.event.MouseListener;
13 import java.awt.event.MouseMotionListener;
14 import java.awt.geom.Point2D;
15 import java.awt.geom.Rectangle2D;
16 import java.lang.reflect.Constructor;
17
18 import javax.naming.event.NamingEvent;
19
20 import nl.tudelft.simulation.dsol.animation.Editable;
21 import nl.tudelft.simulation.dsol.animation.D2.EditableRenderable2DInterface;
22 import nl.tudelft.simulation.dsol.animation.D2.Renderable2DInterface;
23 import nl.tudelft.simulation.dsol.gui.DSOLApplicationInterface;
24 import nl.tudelft.simulation.dsol.gui.animation2D.AnimationPanel;
25 import nl.tudelft.simulation.dsol.gui.animation2D.mouse.InputListener;
26 import nl.tudelft.simulation.dsol.gui.editor2D.actions.EditorUtilities;
27 import nl.tudelft.simulation.dsol.gui.editor2D.mouse.EditorInputListener;
28 import nl.tudelft.simulation.language.d3.CartesianPoint;
29 import nl.tudelft.simulation.language.d3.DirectedPoint;
30 import nl.tudelft.simulation.language.reflection.ClassUtil;
31 import nl.tudelft.simulation.logger.Logger;
32
33 /***
34 * Editor2DPanel.java <br>
35 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
36 * University of Technology </a>, the Netherlands. <br>
37 * See for project information <a
38 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
39 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
40 * General Public License (LGPL) </a>, no warranty.
41 *
42 * @version $Revision$ $Date$
43 * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
44 * </a>
45 */
46 public class Editor2DPanel extends AnimationPanel
47 {
48 /*** Idle Mode */
49 public static final int MODE_IDLE = 0;
50
51 /*** Selection Mode */
52 public static final int MODE_SELECT = 1;
53
54 /*** New Object mMode */
55 public static final int MODE_NEW = 2;
56
57 /*** Edit Object Mode */
58 public static final int MODE_EDIT = 3;
59
60 /*** Delete Object Mode */
61 public static final int MODE_DELETE = 4;
62
63 /*** Edit mode is 'move' */
64 public static final int EDIT_MODE_MOVE = 1;
65
66 /*** Edit mode is 'rotate' */
67 public static final int EDIT_MODE_ROTATE = 2;
68
69 /*** Placeholder size */
70 public static final int PLACEHOLDERSIZE = 2;
71
72 /*** Line size of the representation of the local axis system */
73 public static final int LOCALAXISSIZE = 20;
74
75 /*** Edit menu */
76 private EditMenu editMenu = null;
77
78
79
80 /*** The selected mode */
81 private int selectedMode = Editor2DPanel.MODE_IDLE;
82
83 /*** The selected edit mode */
84 private int selectedEditMode = Editor2DPanel.EDIT_MODE_MOVE;
85
86 /*** The selected editable */
87 private EditableRenderable2DInterface selectedEditableRenderable = null;
88
89 /*** Selected renderable class */
90 private Class selectedRenderableClass = null;
91
92 /*** Selected control point */
93 private CartesianPoint selectedPoint = null;
94
95 /***
96 * Constructor
97 *
98 * @param extent Extend
99 * @param size Size
100 * @param application DSOL Application
101 */
102 public Editor2DPanel(final Rectangle2D extent, final Dimension size,
103 final DSOLApplicationInterface application)
104 {
105 super(extent, size, application);
106
107
108
109 MouseListener[] mouseListeners = this.getMouseListeners();
110 for (int i = 0; i < mouseListeners.length; i++)
111 {
112 this.removeMouseListener(mouseListeners[i]);
113 }
114 MouseMotionListener[] mouseMotionListeners = this
115 .getMouseMotionListeners();
116 for (int i = 0; i < mouseMotionListeners.length; i++)
117 {
118 this.removeMouseMotionListener(mouseMotionListeners[i]);
119 }
120
121
122
123 String mouseEditorClassName = application.getProperties().getProperty(
124 "dsol-gui.editor2D.panel.editorInputListener");
125 if (mouseEditorClassName != null)
126 {
127 try
128 {
129 Class editorClass = Class.forName(mouseEditorClassName);
130 Constructor constructor = ClassUtil.resolveConstructor(
131 editorClass, new Class[]{
132 DSOLApplicationInterface.class,
133 Editor2DPanel.class});
134 InputListener mouseListener = (InputListener) constructor
135 .newInstance(new Object[]{application, this});
136 this.addMouseListener(mouseListener);
137 this.addMouseMotionListener(mouseListener);
138 this.addMouseWheelListener(mouseListener);
139 } catch (Exception exception)
140 {
141 Logger.warning(this, "<init>", "could not load "
142 + mouseEditorClassName);
143
144 InputListener mouse = new EditorInputListener(application, this);
145 this.addMouseListener(mouse);
146 this.addMouseMotionListener(mouse);
147 this.addMouseWheelListener(mouse);
148 }
149 } else
150 {
151
152 InputListener mouse = new EditorInputListener(application, this);
153 this.addMouseListener(mouse);
154 this.addMouseMotionListener(mouse);
155 this.addMouseWheelListener(mouse);
156 }
157
158
159 this.editMenu = new EditMenu("Edit", this);
160
161 }
162
163 /***
164 * @see javax.naming.event.NamespaceChangeListener#objectAdded(javax.naming.event.NamingEvent)
165 */
166 public void objectAdded(final NamingEvent namingEvent)
167 {
168 super.objectAdded(namingEvent);
169
170 Renderable2DInterface element = (Renderable2DInterface) namingEvent
171 .getNewBinding().getObject();
172 if (element instanceof EditableRenderable2DInterface)
173 {
174 this
175 .setSelectedEditableRenderable((EditableRenderable2DInterface) element);
176 }
177 }
178
179 /***
180 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
181 */
182 public void paintComponent(final Graphics g)
183 {
184 super.paintComponent(g);
185
186
187 if (this.getSelectedEditableRenderable() != null)
188 {
189 try
190 {
191 g.setColor(Color.RED);
192 EditableRenderable2DInterface selected = this
193 .getSelectedEditableRenderable();
194 Editable editable = (Editable) selected.getSource();
195 CartesianPoint[] vertices = editable.getVertices();
196 CartesianPoint[] positions = new CartesianPoint[vertices.length];
197 for (int i = 0; i < positions.length; i++)
198 {
199 positions[i] = EditorUtilities.convertToGlobalCoordinates(
200 vertices[i], editable.getLocation());
201 }
202
203 for (int i = 0; i < positions.length - 1; i++)
204 {
205 Point2D p1 = Renderable2DInterface.Util
206 .getScreenCoordinates(new Point2D.Double(
207 positions[i].x, positions[i].y),
208 this.extent, this.getSize());
209 Point2D p2 = Renderable2DInterface.Util
210 .getScreenCoordinates(new Point2D.Double(
211 positions[i + 1].x, positions[i + 1].y),
212 this.extent, this.getSize());
213
214 g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2
215 .getX(), (int) p2.getY());
216
217 if (positions[i] == this.getSelectedPoint())
218 {
219 g.setColor(Color.YELLOW);
220 }
221 g.fillRect((int) p1.getX() - Editor2DPanel.PLACEHOLDERSIZE,
222 (int) p1.getY() - Editor2DPanel.PLACEHOLDERSIZE,
223 Editor2DPanel.PLACEHOLDERSIZE * 2 + 1,
224 Editor2DPanel.PLACEHOLDERSIZE * 2 + 1);
225 g.setColor(Color.RED);
226 }
227
228 Point2D p1 = Renderable2DInterface.Util.getScreenCoordinates(
229 new Point2D.Double(positions[0].x, positions[0].y),
230 this.extent, this.getSize());
231 Point2D p2 = Renderable2DInterface.Util.getScreenCoordinates(
232 new Point2D.Double(positions[positions.length - 1].x,
233 positions[positions.length - 1].y),
234 this.extent, this.getSize());
235
236 if (selected.isClosedShape())
237 {
238 g.drawLine((int) p1.getX(), (int) p1.getY(), (int) p2
239 .getX(), (int) p2.getY());
240 }
241 if (positions[positions.length - 1] == this.getSelectedPoint())
242 {
243 g.setColor(Color.YELLOW);
244 }
245 g.fillRect((int) p2.getX() - Editor2DPanel.PLACEHOLDERSIZE,
246 (int) p2.getY() - Editor2DPanel.PLACEHOLDERSIZE,
247 Editor2DPanel.PLACEHOLDERSIZE * 2 + 1,
248 Editor2DPanel.PLACEHOLDERSIZE * 2 + 1);
249 g.setColor(Color.RED);
250
251
252 DirectedPoint location = ((Renderable2DInterface) selected)
253 .getSource().getLocation();
254 Point2D loc = Renderable2DInterface.Util.getScreenCoordinates(
255 location.to2D(), this.extent, this.getSize());
256 g.drawLine((int) (loc.getX() - 2), (int) (loc.getY()),
257 (int) (loc.getX() + 2), (int) (loc.getY()));
258 g.drawLine((int) (loc.getX()), (int) (loc.getY() - 2),
259 (int) (loc.getX()), (int) (loc.getY() + 2));
260
261 g.drawLine((int) (loc.getX()), (int) (loc.getY()), (int) (loc
262 .getX() + Math.cos(location.getRotZ())
263 * Editor2DPanel.LOCALAXISSIZE),
264 (int) (loc.getY() - Math.sin(location.getRotZ())
265 * Editor2DPanel.LOCALAXISSIZE));
266 g.drawLine((int) (loc.getX()), (int) (loc.getY()), (int) (loc
267 .getX() - Math.sin(location.getRotZ())
268 * Editor2DPanel.LOCALAXISSIZE),
269 (int) (loc.getY() - Math.cos(location.getRotZ())
270 * Editor2DPanel.LOCALAXISSIZE));
271 g.drawString("x", (int) (loc.getX() + Math.cos(location
272 .getRotZ())
273 * Editor2DPanel.LOCALAXISSIZE),
274 (int) (loc.getY() - Math.sin(location.getRotZ())
275 * Editor2DPanel.LOCALAXISSIZE));
276 g.drawString("y", (int) (loc.getX() - Math.sin(location
277 .getRotZ())
278 * Editor2DPanel.LOCALAXISSIZE),
279 (int) (loc.getY() - Math.cos(location.getRotZ())
280 * Editor2DPanel.LOCALAXISSIZE));
281 } catch (Exception exception)
282 {
283 Logger.warning(this, "paintComponent", exception);
284 }
285 }
286
287 }
288
289 /***
290 * @return returns the editMenu.
291 */
292 public EditMenu getEditMenu()
293 {
294 return this.editMenu;
295 }
296
297
298 /***
299 * @return returns the selectedMode.
300 */
301 public int getSelectedMode()
302 {
303 return this.selectedMode;
304 }
305
306 /***
307 * @param mode the selectedMode to set.
308 */
309 public void setSelectedMode(final int mode)
310 {
311 this.selectedMode = mode;
312 }
313
314 /***
315 * @return returns the selected editable renderable.
316 */
317 public EditableRenderable2DInterface getSelectedEditableRenderable()
318 {
319 return this.selectedEditableRenderable;
320 }
321
322 /***
323 * @param selectedEditableRenderable The selected editable renderable to
324 * set.
325 */
326 public void setSelectedEditableRenderable(
327 final EditableRenderable2DInterface selectedEditableRenderable)
328 {
329 this.selectedEditableRenderable = selectedEditableRenderable;
330 }
331
332 /***
333 * @return returns the selectedRenderableClass.
334 */
335 public Class getSelectedRenderableClass()
336 {
337 return this.selectedRenderableClass;
338 }
339
340 /***
341 * @param selectedRenderableClass The selectedRenderableClass to set.
342 */
343 public void setSelectedRenderableClass(final Class selectedRenderableClass)
344 {
345 this.selectedRenderableClass = selectedRenderableClass;
346 }
347
348 /***
349 * @return returns the selectedEditMode.
350 */
351 public int getSelectedEditMode()
352 {
353 return this.selectedEditMode;
354 }
355
356 /***
357 * @param selectedEditMode The selectedEditMode to set.
358 */
359 public void setSelectedEditMode(final int selectedEditMode)
360 {
361 this.selectedEditMode = selectedEditMode;
362 }
363
364 /***
365 * @return returns the selectedPoint.
366 */
367 public CartesianPoint getSelectedPoint()
368 {
369 return this.selectedPoint;
370 }
371
372 /***
373 * @param selectedPoint The selectedPoint to set.
374 */
375 public void setSelectedPoint(final CartesianPoint selectedPoint)
376 {
377 this.selectedPoint = selectedPoint;
378 }
379 }