1 package org.devaki.nextobjects.ui.menus;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.awt.Point;
22 import java.awt.event.ActionEvent;
23 import java.awt.event.ActionListener;
24 import java.awt.event.KeyEvent;
25 import java.util.Vector;
26 import javax.swing.JPopupMenu;
27 import javax.swing.KeyStroke;
28 import org.devaki.nextobjects.constants.CstImages;
29 import org.devaki.nextobjects.ui.components.CustomMenu;
30 import org.devaki.nextobjects.ui.components.CustomMenuItem;
31 import org.devaki.nextobjects.ui.toolbars.NOToolBar1;
32 import org.devaki.nextobjects.ui.workspace.models.EditorFactory;
33 import org.devaki.nextobjects.util.ModelMan;
34 import org.devaki.nextobjects.util.NOClipboard;
35 import org.devaki.nextobjects.workspace.models.ConceptualModel;
36 import org.devaki.nextobjects.workspace.models.PhysicalModel;
37 import org.devaki.nextobjects.workspace.models.objects.Association;
38 import org.devaki.nextobjects.workspace.models.objects.AssociationLink;
39 import org.devaki.nextobjects.workspace.models.objects.Constraint;
40 import org.devaki.nextobjects.workspace.models.objects.Entity;
41 import org.devaki.nextobjects.workspace.models.objects.Label;
42 import org.devaki.nextobjects.workspace.models.objects.Table;
43 import org.devaki.nextobjects.workspace.models.objects.BaseClass;
44 import org.devaki.nextobjects.workspace.models.objects.BaseLine;
45 import org.devaki.nextobjects.workspace.models.graphics.ClassView;
46 import org.devaki.nextobjects.workspace.models.graphics.LineView;
47 import org.devaki.nextobjects.workspace.models.graphics.LabelView;
48 import org.devaki.nextobjects.workspace.models.styles.ClassStyle;
49 import org.devaki.nextobjects.workspace.models.styles.LineStyle;
50 /***
51 * This is the the models popup menu
52 * @author eflorent
53 */
54 public class ModelPopupMenu extends JPopupMenu
55 {
56 /*** the model is physical (PDM) */
57 private static boolean isPDM;
58 /*** the model is conceptual (CDM) */
59 private static boolean isCDM;
60 /*** X Coordinates of the mouse click */
61 private static int cX;
62 /*** Y Coordinates of the mouse click */
63 private static int cY;
64 /*** the object is a relation */
65 private static boolean relation;
66 /*** Menu style */
67 private static CustomMenu jMenuStyle =
68 new CustomMenu("Style", KeyEvent.VK_S, CstImages.ICN_BLANK);
69 /*** Menu Item resize */
70 private static CustomMenuItem jMenuItemResize =
71 new CustomMenuItem("Resize...", KeyEvent.VK_F3, true);
72 /*** Menu Item new */
73 private CustomMenuItem jMenuItemNew =
74 new CustomMenuItem(
75 "New...",
76 CstImages.ICN_TABLE,
77 KeyEvent.VK_N,
78 true);
79 /*** Menu Item cut */
80 private static CustomMenuItem jMenuItemCut =
81 new CustomMenuItem("Cut", CstImages.ICN_CUT, KeyEvent.VK_T, true);
82 /*** Menu Item copy */
83 private static CustomMenuItem jMenuItemCopy =
84 new CustomMenuItem(
85 "Copy",
86 CstImages.ICN_COPY,
87 KeyEvent.VK_C,
88 false);
89 /*** Menu Item paste */
90 private static CustomMenuItem jMenuItemPaste =
91 new CustomMenuItem(
92 "Paste",
93 CstImages.ICN_PASTE,
94 KeyEvent.VK_P,
95 true);
96 /*** Menu Item delete */
97 private static CustomMenuItem jMenuItemDelete =
98 new CustomMenuItem(
99 "Delete",
100 KeyEvent.VK_D,
101 KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
102 true);
103 /*** Menu Item style editor */
104 private static CustomMenuItem jMenuItemStyleEdit =
105 new CustomMenuItem("Edit...", KeyEvent.VK_E, true);
106 /*** Menu Item style copy */
107 private static CustomMenuItem jMenuItemStyleCopy =
108 new CustomMenuItem("Copy", CstImages.ICN_COPY, KeyEvent.VK_C, true);
109 /*** Menu Item style paste */
110 private static CustomMenuItem jMenuItemStylePaste =
111 new CustomMenuItem(
112 "Paste",
113 CstImages.ICN_PASTE,
114 KeyEvent.VK_P,
115 false);
116 /*** Menu Item verify */
117 private CustomMenuItem jMenuItemVerify =
118 new CustomMenuItem(
119 "Verify",
120 KeyEvent.VK_V,
121 KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0),
122 true);
123 /*** Menu Item model properties */
124 private CustomMenuItem jMenuItemProperties =
125 new CustomMenuItem("Model properties ...", KeyEvent.VK_O, true);
126 /***
127 * Create a new 'ModelPopupMenu' object
128 */
129 public ModelPopupMenu()
130 {
131 /*** Action Listeners **/
132 jMenuItemResize.addActionListener(new ActionListener()
133 {
134 public final void actionPerformed(final ActionEvent e)
135 {
136 if (ModelMan.getCurrentObject() == null)
137 {
138 ModelMan.resizeModelObjects(ModelMan.getCurrentModel());
139 ModelMan.getCurrentModel().getModelView().center(true);
140 }
141 else
142 {
143 if (ModelMan.getCurrentObject() instanceof BaseClass)
144 {
145 (
146 (ClassView) ModelMan
147 .getCurrentObject()
148 .getObjectView())
149 .autoResize(
150 true);
151 }
152 else if (
153 ModelMan.getCurrentObject() instanceof BaseLine)
154 {
155 ((LineView) ModelMan
156 .getCurrentObject()
157 .getObjectView())
158 .computeBestPoints();
159 }
160 else if (ModelMan.getCurrentObject() instanceof Label)
161 {
162 (
163 (LabelView) ((Label) ModelMan
164 .getCurrentObject())
165 .getObjectView())
166 .autoResize(
167 true);
168 }
169 }
170 ModelMan.getCurrentModel().getModelView().setFullRefresh(
171 true);
172 ModelMan.getCurrentModel().getModelView().repaint();
173 }
174 });
175 this.jMenuItemNew.addActionListener(new ActionListener()
176 {
177 public final void actionPerformed(final ActionEvent e)
178 {
179 if (ModelMan.getCurrentModel() instanceof ConceptualModel)
180 {
181 ModelMan.newObject(new Point(cX, cY));
182 }
183 else if (
184 ModelMan.getCurrentModel() instanceof PhysicalModel)
185 {
186 ModelMan.newObject(new Point(cX, cY));
187 }
188 }
189 });
190 jMenuItemCut.addActionListener(new ActionListener()
191 {
192 public final void actionPerformed(final ActionEvent e)
193 {
194 NOClipboard.cut(ModelMan.getCurrentObjects());
195 NOToolBar1.fixIcons();
196 NOMenuBar.fixEditMenu();
197 }
198 });
199 jMenuItemCopy.addActionListener(new ActionListener()
200 {
201 public final void actionPerformed(final ActionEvent e)
202 {
203 NOClipboard.copy(ModelMan.getCurrentObjects());
204 NOToolBar1.fixIcons();
205 NOMenuBar.fixEditMenu();
206 }
207 });
208 jMenuItemPaste.addActionListener(new ActionListener()
209 {
210 public final void actionPerformed(final ActionEvent e)
211 {
212 NOClipboard.pasteBaseObject(cX, cY);
213 }
214 });
215 jMenuItemDelete.addActionListener(new ActionListener()
216 {
217 public final void actionPerformed(final ActionEvent e)
218 {
219 ModelMan.delete();
220
221 ModelMan.getCurrentModel().getPanel().repaint();
222 }
223 });
224 this.jMenuItemVerify.addActionListener(new ActionListener()
225 {
226 public final void actionPerformed(final ActionEvent e)
227 {
228 ModelMan.verify(ModelMan.getCurrentModel());
229 }
230 });
231 jMenuItemStyleEdit.addActionListener(new ActionListener()
232 {
233 public final void actionPerformed(final ActionEvent e)
234 {
235 EditorFactory.getObjectStyle(
236 ModelMan.getCurrentObject().getObjectView());
237
238 ModelMan.getCurrentModel().getPanel().repaint();
239 }
240 });
241 jMenuItemStyleCopy.addActionListener(new ActionListener()
242 {
243 public final void actionPerformed(final ActionEvent e)
244 {
245 if (ModelMan.getCurrentObject() instanceof BaseClass)
246 {
247 NOClipboard.copy(
248 (ClassStyle) ((ClassView) ModelMan
249 .getCurrentObject()
250 .getObjectView())
251 .getStyle());
252 }
253 else if (ModelMan.getCurrentObject() instanceof BaseLine)
254 {
255 NOClipboard.copy(
256 (LineStyle) ((LineView) ModelMan
257 .getCurrentObject()
258 .getObjectView())
259 .getStyle());
260 }
261 }
262 });
263 jMenuItemStylePaste.addActionListener(new ActionListener()
264 {
265 public final void actionPerformed(final ActionEvent e)
266 {
267
268 Vector vObjects = ModelMan.getCurrentObjects();
269 for (int i = 0; i < vObjects.size(); i++)
270 {
271 if (vObjects.elementAt(i) instanceof BaseClass)
272 {
273 NOClipboard.pasteClassStyle(
274 (ClassView) ((BaseClass) vObjects.elementAt(i))
275 .getObjectView());
276 }
277 else if (vObjects.elementAt(i) instanceof BaseLine)
278 {
279 NOClipboard.pasteLineStyle(
280 (LineView) ((BaseLine) vObjects.elementAt(i))
281 .getObjectView());
282 }
283 }
284
285 ModelMan.getCurrentModel().getPanel().repaint();
286 }
287 });
288 this.jMenuItemProperties.addActionListener(new ActionListener()
289 {
290 public final void actionPerformed(final ActionEvent e)
291 {
292 if (isCDM)
293 {
294 if (ModelMan.getCurrentObject() instanceof Entity)
295 {
296 EditorFactory.getEntityEdit(
297 (Entity) ModelMan.getCurrentObject());
298 }
299 else if (
300 ModelMan.getCurrentObject() instanceof Association)
301 {
302 EditorFactory.getAssociationEdit(
303 (Association) ModelMan.getCurrentObject());
304 }
305 else if (
306 ModelMan.getCurrentObject()
307 instanceof AssociationLink)
308 {
309 EditorFactory.getAssociationLinkEdit(
310 (AssociationLink) ModelMan.getCurrentObject());
311 }
312 else
313 {
314 EditorFactory.getModelEdit();
315 }
316 }
317 else if (isPDM)
318 {
319 if (ModelMan.getCurrentObject() instanceof Table)
320 {
321 EditorFactory.getTableEdit(
322 (Table) ModelMan.getCurrentObject());
323 }
324 else if (
325 ModelMan.getCurrentObject() instanceof Constraint)
326 {
327 EditorFactory.getConstraintEdit(
328 (Constraint) ModelMan.getCurrentObject());
329 }
330 else
331 {
332 EditorFactory.getModelEdit();
333 }
334 }
335 }
336 });
337 /*** Assembling components **/
338
339 jMenuStyle.add(jMenuItemStyleEdit);
340 jMenuStyle.add(jMenuItemStyleCopy);
341 jMenuStyle.add(jMenuItemStylePaste);
342
343 add(jMenuItemResize);
344 this.add(this.jMenuItemNew);
345 this.addSeparator();
346 this.add(jMenuItemCut);
347 this.add(jMenuItemCopy);
348 this.add(jMenuItemPaste);
349 this.add(jMenuItemDelete);
350 this.addSeparator();
351 this.add(jMenuItemVerify);
352 this.addSeparator();
353 this.add(jMenuStyle);
354 this.addSeparator();
355 this.add(this.jMenuItemProperties);
356 }
357 /***
358 * Initializing data when calling the menu
359 * @param pX The horizontal point of the mouse click
360 * @param pY The vertical point of the mouse click
361 */
362 public final void setData(final int pX, final int pY)
363 {
364 cX = pX;
365 cY = pY;
366 if (ModelMan.getCurrentModel() instanceof ConceptualModel)
367 {
368 isCDM = true;
369 isPDM = false;
370 if (ModelMan.getCurrentObject() != null)
371 {
372 if (ModelMan.getCurrentObject() instanceof Entity)
373 {
374 relation = false;
375 }
376 else if (
377 ModelMan.getCurrentObject() instanceof Association)
378 {
379 relation = true;
380 }
381 }
382 }
383 else
384 {
385 isCDM = false;
386 isPDM = true;
387 if (ModelMan.getCurrentObject() != null)
388 {
389 if (ModelMan.getCurrentObject() instanceof Table)
390 {
391 relation = false;
392 }
393 else if (ModelMan.getCurrentObject() instanceof Constraint)
394 {
395 relation = true;
396 }
397 }
398
399 jMenuItemResize.setEnabled(true);
400 jMenuItemCut.setEnabled(ModelMan.getCurrentObject() != null);
401 jMenuItemCopy.setEnabled(ModelMan.getCurrentObject() != null);
402 jMenuItemPaste.setEnabled(NOClipboard.isBaseObjectClipped());
403 jMenuItemDelete.setEnabled(ModelMan.getCurrentObject() != null);
404 jMenuStyle.setEnabled(ModelMan.getCurrentObjects().size() > 0);
405 jMenuItemStyleEdit.setEnabled(
406 ModelMan.getCurrentObjects().size() == 1);
407 jMenuItemStyleCopy.setEnabled(
408 ModelMan.getCurrentObjects().size() == 1);
409 if ((ModelMan.getCurrentObject() instanceof BaseClass)
410 && NOClipboard.isClassStyleClipped())
411 {
412 jMenuItemStylePaste.setEnabled(true);
413 }
414 else if (
415 (ModelMan.getCurrentObject() instanceof BaseLine)
416 && NOClipboard.isLineStyleClipped())
417 {
418 jMenuItemStylePaste.setEnabled(true);
419 }
420 else
421 {
422 jMenuItemStylePaste.setEnabled(false);
423 }
424 }
425 }
426 }