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