View Javadoc

1   package org.devaki.nextobjects.ui.menus;
2   /*
3    *  nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4    *  This program is free software; you can redistribute it and/or modify
5    *  it under the terms of the GNU General Public License as published by the
6    *  Free Software Foundation; either version 2 of the License, or (at your
7    *  option) any later version.
8    *  This program is distributed in the hope that it will
9    *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10   *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11   *  PURPOSE. See the GNU General Public License for more details.
12   *  You should have received a copy of the GNU General Public License along
13   *  with this program; if not, write to the Free Software Foundation, Inc., 59
14   *  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15   */
16  import java.awt.Event;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.ActionListener;
19  import java.awt.event.KeyEvent;
20  import javax.swing.JPopupMenu;
21  import javax.swing.KeyStroke;
22  import org.devaki.nextobjects.constants.CstImages;
23  import org.devaki.nextobjects.ui.components.CustomMenu;
24  import org.devaki.nextobjects.ui.components.CustomMenuItem;
25  import org.devaki.nextobjects.ui.main.NOTreeView;
26  import org.devaki.nextobjects.ui.workspace.models.EditorFactory;
27  import org.devaki.nextobjects.util.ModelMan;
28  import org.devaki.nextobjects.util.NOFileManager;
29  import org.devaki.nextobjects.util.NOClipboard;
30  import org.devaki.nextobjects.workspace.models.BaseModel;
31  import org.devaki.nextobjects.workspace.models.ConceptualModel;
32  import org.devaki.nextobjects.workspace.models.PhysicalModel;
33  import org.devaki.nextobjects.workspace.models.columns.Column;
34  import org.devaki.nextobjects.workspace.models.objects.Association;
35  import org.devaki.nextobjects.workspace.models.objects.BaseObject;
36  import org.devaki.nextobjects.workspace.models.objects.Constraint;
37  import org.devaki.nextobjects.workspace.models.objects.Entity;
38  import org.devaki.nextobjects.workspace.models.objects.Table;
39  /***
40   *  Description of the Class
41   *
42   *@author     eflorent
43   *@created    April 13, 2003
44   */
45  public class TreePopupMenu extends JPopupMenu
46  {
47      /*** Node objects */
48      private Object object;
49      /*** Parent node object */
50      private Object objectParent;
51      /*** jMenuWorkspace */
52      private CustomMenu jMenuWorkspaceNew =
53          new CustomMenu("New", KeyEvent.VK_N, CstImages.ICN_NEW);
54      /*** jMenuItemWorskpaceNewCDM */
55      private CustomMenuItem jMenuItemWorkspaceNewCDM =
56          new CustomMenuItem(
57              "New CDM",
58              CstImages.ICN_CDM,
59              KeyEvent.VK_C,
60              KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK),
61              true);
62      /*** jMenuItemWorskpaceNewPDM */
63      private CustomMenuItem jMenuItemWorkspaceNewPDM =
64          new CustomMenuItem(
65              "New PDM",
66              CstImages.ICN_PDM,
67              KeyEvent.VK_P,
68              KeyStroke.getKeyStroke(
69                  KeyEvent.VK_N,
70                  Event.CTRL_MASK + Event.SHIFT_MASK),
71              true);
72      /*** jMenuItemWorskpaceClose */
73      private CustomMenuItem jMenuItemWorkspaceClose =
74          new CustomMenuItem("Close", KeyEvent.VK_C, false);
75      /*** jMenuItemWorskpaceSave */
76      private CustomMenuItem jMenuItemWorkspaceSave =
77          new CustomMenuItem("Save", CstImages.ICN_SAVE, KeyEvent.VK_S, false);
78      /*** jMenuItemWorskpaceSaveAs */
79      private CustomMenuItem jMenuItemWorkspaceSaveAs =
80          new CustomMenuItem("Save As", KeyEvent.VK_A, false);
81      /*** jMenuItemModelClose */
82      private CustomMenuItem jMenuItemModelClose =
83          new CustomMenuItem("Close", KeyEvent.VK_L, true);
84      /*** jMenuItemModelSave */
85      private CustomMenuItem jMenuItemModelSave =
86          new CustomMenuItem("Save", CstImages.ICN_SAVE, KeyEvent.VK_S, true);
87      /*** jMenuItemModelSaveAs */
88      private CustomMenuItem jMenuItemModelSaveAs =
89          new CustomMenuItem("Save As", KeyEvent.VK_A, true);
90      /*** jMenuItemModelCut */
91      private CustomMenuItem jMenuItemModelCut =
92          new CustomMenuItem("Cut", CstImages.ICN_CUT, KeyEvent.VK_T, false);
93      /*** jMenuItemModelCopy */
94      private CustomMenuItem jMenuItemModelCopy =
95          new CustomMenuItem("Copy", CstImages.ICN_COPY, KeyEvent.VK_C, false);
96      /*** jMenuItemModelPaste */
97      private CustomMenuItem jMenuItemModelPaste =
98          new CustomMenuItem("Paste", CstImages.ICN_PASTE, KeyEvent.VK_P, true);
99      /*** jMenuItemModelVerify */
100     private CustomMenuItem jMenuItemModelVerify =
101         new CustomMenuItem(
102             "Verify",
103             KeyEvent.VK_V,
104             KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0),
105             true);
106     /*** jMenuItemModelCut */
107     private CustomMenuItem jMenuItemBaseObjectCut =
108         new CustomMenuItem("Cut", CstImages.ICN_CUT, KeyEvent.VK_T, true);
109     /*** jMenuItemModelCopy */
110     private CustomMenuItem jMenuItemBaseObjectCopy =
111         new CustomMenuItem("Copy", CstImages.ICN_COPY, KeyEvent.VK_C, true);
112     /*** jMenuItemModelPaste */
113     private CustomMenuItem jMenuItemBaseObjectPaste =
114         new CustomMenuItem("Paste", CstImages.ICN_PASTE, KeyEvent.VK_P, false);
115     /*** jMenuItemModelDelete */
116     private CustomMenuItem jMenuItemBaseObjectDelete =
117         new CustomMenuItem("Delete", KeyEvent.VK_D, true);
118     /*** jMenuItemModelColumnCut */
119     private CustomMenuItem jMenuItemColumnCut =
120         new CustomMenuItem("Cut", CstImages.ICN_CUT, KeyEvent.VK_T, true);
121     /*** jMenuItemModelColumnCopy */
122     private CustomMenuItem jMenuItemColumnCopy =
123         new CustomMenuItem("Copy", CstImages.ICN_COPY, KeyEvent.VK_C, true);
124     /*** jMenuItemModelColumnPaste */
125     private CustomMenuItem jMenuItemColumnDelete =
126         new CustomMenuItem("Delete", KeyEvent.VK_D, true);
127     /*** jMenuItemModelRename */
128     private CustomMenuItem jMenuItemRename =
129         new CustomMenuItem(
130             "Rename",
131             KeyEvent.VK_R,
132             KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0),
133             true);
134     /*** jMenuItemModelProperties */
135     private CustomMenuItem jMenuItemProperties =
136         new CustomMenuItem("Properties...", KeyEvent.VK_R, true);
137     /***
138      *  Construct a new 'TreePopupMenu' object
139      */
140     public TreePopupMenu()
141     {
142         this.jMenuWorkspaceNew.add(this.jMenuItemWorkspaceNewCDM);
143         this.jMenuWorkspaceNew.add(this.jMenuItemWorkspaceNewPDM);
144         // Action Listeners (Workspace)
145         this.jMenuItemWorkspaceNewCDM.addActionListener(new ActionListener()
146         {
147             public final void actionPerformed(final ActionEvent e)
148             {
149                 ConceptualModel tmp =
150                     new ConceptualModel(
151                         new StringBuffer()
152                             .append("Model ")
153                             .append(BaseModel.getCountInstance() + 1)
154                             .toString());
155                 ModelMan.newConceptualDatamodel(tmp);
156             }
157         });
158         jMenuItemWorkspaceNewPDM.addActionListener(new ActionListener()
159         {
160             public final void actionPerformed(final ActionEvent e)
161             {
162                 PhysicalModel tmp =
163                     new PhysicalModel(
164                         new StringBuffer()
165                             .append("Model ")
166                             .append(BaseModel.getCountInstance() + 1)
167                             .toString());
168                 ModelMan.newPhysicalDatamodel(tmp);
169             }
170         });
171         // Action Listeners (Model)
172         this.jMenuItemModelClose.addActionListener(new ActionListener()
173         {
174             public final void actionPerformed(final ActionEvent e)
175             {
176                 NOFileManager.close((BaseModel) object);
177                 ModelMan.setCurrentObject(null);
178             }
179         });
180         this.jMenuItemModelSave.addActionListener(new ActionListener()
181         {
182             public final void actionPerformed(final ActionEvent e)
183             {
184                 NOFileManager.save(ModelMan.getCurrentModel());
185             }
186         });
187         this.jMenuItemModelSaveAs.addActionListener(new ActionListener()
188         {
189             public final void actionPerformed(final ActionEvent e)
190             {
191                 NOFileManager.saveToFile();
192             }
193         });
194         this.jMenuItemModelPaste.addActionListener(new ActionListener()
195         {
196             public final void actionPerformed(final ActionEvent e)
197             {
198                 ModelMan.setCurrentModel((BaseModel) object);
199                 NOClipboard.pasteBaseObject(5350, 2750);
200                 ModelMan.getCurrentModel().getPanel().repaint();
201             }
202         });
203         this.jMenuItemModelVerify.addActionListener(new ActionListener()
204         {
205             public final void actionPerformed(final ActionEvent e)
206             {
207                 ModelMan.setCurrentModel((BaseModel) object);
208                 ModelMan.verify(ModelMan.getCurrentModel());
209             }
210         });
211         // Action Listeners (NextGraphicsObject)
212         this.jMenuItemBaseObjectCut.addActionListener(new ActionListener()
213         {
214             public final void actionPerformed(final ActionEvent e)
215             {
216                 ModelMan.setCurrentModel((BaseModel) objectParent);
217                 ModelMan.setCurrentObject((BaseObject) object);
218                 // Check if object is a relation
219                 NOClipboard.cut(ModelMan.getCurrentObjects());
220                 ModelMan.getCurrentModel().getPanel().repaint();
221             }
222         });
223         this.jMenuItemBaseObjectCopy.addActionListener(new ActionListener()
224         {
225             public final void actionPerformed(final ActionEvent e)
226             {
227                 ModelMan.setCurrentModel((BaseModel) objectParent);
228                 ModelMan.setCurrentObject((BaseObject) object);
229                 NOClipboard.copy(ModelMan.getCurrentObjects());
230                 ModelMan.getCurrentModel().getPanel().repaint();
231             }
232         });
233         this.jMenuItemBaseObjectDelete.addActionListener(new ActionListener()
234         {
235             public final void actionPerformed(final ActionEvent e)
236             {
237                 ModelMan.setCurrentObject((BaseObject) object);
238                 if (objectParent instanceof ConceptualModel)
239                 {
240                     ModelMan.delete();
241                     ((ConceptualModel) ModelMan.getCurrentModel())
242                         .getModelView()
243                         .repaint();
244                 }
245                 else if (objectParent instanceof PhysicalModel)
246                 {
247                     ModelMan.delete();
248                     ((PhysicalModel) ModelMan.getCurrentModel())
249                         .getModelView()
250                         .repaint();
251                 }
252                 ModelMan.getCurrentModel().getPanel().repaint();
253             }
254         });
255         this.jMenuItemBaseObjectPaste.addActionListener(new ActionListener()
256         {
257             public final void actionPerformed(final ActionEvent e)
258             {
259                 NOClipboard.pasteColumn((BaseObject) object);
260             }
261         });
262         // Action Listeners (Field)
263         this.jMenuItemColumnCut.addActionListener(new ActionListener()
264         {
265             public final void actionPerformed(final ActionEvent e)
266             {
267                 // Copy object in clipboard
268                 NOClipboard.cut(ModelMan.getCurrentObjects());
269             }
270         });
271         this.jMenuItemColumnCopy.addActionListener(new ActionListener()
272         {
273             public final void actionPerformed(final ActionEvent e)
274             {
275                 NOClipboard.copy(ModelMan.getCurrentObjects());
276             }
277         });
278         this.jMenuItemColumnDelete.addActionListener(new ActionListener()
279         {
280             public final void actionPerformed(final ActionEvent e)
281             {
282                 // Delete it
283                  ((Column) object).getParent().getData().removeElement(object);
284                 // Reload tree from the parent node
285                 NOTreeView.reloadBaseClassChildrens(
286                     ((Column) object).getParent().getDynamicTreeNode());
287                 // Repaint associate model only if it is the current model
288                 if (((Column) object)
289                     .getParent()
290                     .getMyModel()
291                     .getPanel()
292                     .isShowing())
293                 {
294                     ModelMan.getCurrentModel().getPanel().repaint();
295                 }
296             }
297         });
298         // Action Listeners (Common)
299         this.jMenuItemRename.addActionListener(new ActionListener()
300         {
301             public final void actionPerformed(final ActionEvent e)
302             {
303                 NOTreeView.renameNode();
304             }
305         });
306         this.jMenuItemProperties.addActionListener(new ActionListener()
307         {
308             public final void actionPerformed(final ActionEvent e)
309             {
310                 // If the clicked node is an Entity from a Conceptual Data Model
311                 if (object instanceof Entity)
312                 {
313                     ModelMan.getCurrentModel().getPanel().repaint();
314                     ModelMan.setCurrentModel((BaseModel) objectParent);
315                     ModelMan.setCurrentObject((BaseObject) object);
316                     EditorFactory.getEntityEdit(
317                         (Entity) ModelMan.getCurrentObject());
318                 }
319                 else if (object instanceof Association)
320                 {
321                     ModelMan.setCurrentModel((BaseModel) objectParent);
322                     ModelMan.setCurrentObject((BaseObject) object);
323                     EditorFactory.getAssociationEdit(
324                         (Association) ModelMan.getCurrentObject());
325                 }
326                 // If the clicked node is a Table from a Physical Data Model
327                 else if (object instanceof Table)
328                 {
329                     ModelMan.setCurrentModel((BaseModel) objectParent);
330                     ModelMan.setCurrentObject((BaseObject) object);
331                     EditorFactory.getTableEdit(
332                         (Table) ModelMan.getCurrentObject());
333                 }
334                 else if (object instanceof Constraint)
335                 {
336                     ModelMan.setCurrentModel((BaseModel) objectParent);
337                     ModelMan.setCurrentObject((BaseObject) object);
338                     EditorFactory.getConstraintEdit(
339                         (Constraint) ModelMan.getCurrentObject());
340                 }
341                 // If the clicked node is a Conceptual Data Model
342                 else if (object instanceof ConceptualModel)
343                 {
344                     ModelMan.setCurrentModel((BaseModel) object);
345                     EditorFactory.getModelEdit();
346                 }
347                 // If the clicked node is a Physical Data Model
348                 else if (object instanceof PhysicalModel)
349                 {
350                     ModelMan.setCurrentModel((BaseModel) object);
351                     EditorFactory.getModelEdit();
352                 }
353             }
354         });
355     }
356     /***
357      *  Initializing data when calling the menu
358      *
359      *@param  value   The new data value
360      *@param  parent  The new data value
361      */
362     public final void setData(final Object value, final Object parent)
363     {
364         this.object = value;
365         this.objectParent = parent;
366         if (this.object instanceof Column)
367         {
368             this.fixFieldMenu();
369         }
370         else if (this.object instanceof Entity)
371         {
372             this.fixObjectMenu();
373         }
374         else if (this.object instanceof Association)
375         {
376             this.fixObjectMenu();
377         }
378         else if (this.object instanceof Table)
379         {
380             this.fixObjectMenu();
381         }
382         else if (this.object instanceof Constraint)
383         {
384             this.fixObjectMenu();
385         }
386         else if (this.object instanceof ConceptualModel)
387         {
388             this.fixModelMenu();
389         }
390         else if (this.object instanceof PhysicalModel)
391         {
392             this.fixModelMenu();
393         }
394         else
395         {
396             this.fixWorkspaceMenu();
397         }
398     }
399     /***
400      *  Fix menu for a Workspace
401      */
402     private void fixWorkspaceMenu()
403     {
404         this.removeAll();
405         // Construct
406         this.add(this.jMenuWorkspaceNew);
407         this.addSeparator();
408         this.add(this.jMenuItemWorkspaceClose);
409         this.addSeparator();
410         this.add(this.jMenuItemWorkspaceSave);
411         this.add(this.jMenuItemWorkspaceSaveAs);
412         this.addSeparator();
413         this.add(this.jMenuItemRename);
414     }
415     /***
416      *  Fix menu for a Model
417      */
418     private void fixModelMenu()
419     {
420         this.removeAll();
421         // Construct
422         this.add(this.jMenuItemModelClose);
423         this.addSeparator();
424         this.add(this.jMenuItemModelSave);
425         this.add(this.jMenuItemModelSaveAs);
426         this.addSeparator();
427         this.add(this.jMenuItemModelCut);
428         this.add(this.jMenuItemModelCopy);
429         this.add(this.jMenuItemModelPaste);
430         this.addSeparator();
431         this.add(this.jMenuItemRename);
432         this.addSeparator();
433         this.add(this.jMenuItemModelVerify);
434         this.addSeparator();
435         this.add(this.jMenuItemProperties);
436         // Enable/Disable jMenuItemSave
437         if (((BaseModel) object).getFileForSave() == null)
438         {
439             jMenuItemModelSave.setEnabled(false);
440         }
441         else
442         {
443             jMenuItemModelSave.setEnabled(true);
444         }
445         // Enable/Disable jMenuItemModelPaste
446         if (NOClipboard.isBaseObjectClipped())
447         {
448             this.jMenuItemModelPaste.setEnabled(true);
449         }
450         else
451         {
452             this.jMenuItemModelPaste.setEnabled(false);
453         }
454     }
455     /***
456      *  Fix menu for an Object
457      */
458     private void fixObjectMenu()
459     {
460         this.removeAll();
461         // Construct
462         this.add(this.jMenuItemBaseObjectCut);
463         this.add(this.jMenuItemBaseObjectCopy);
464         this.add(this.jMenuItemBaseObjectPaste);
465         this.add(this.jMenuItemBaseObjectDelete);
466         this.addSeparator();
467         this.add(this.jMenuItemRename);
468         this.addSeparator();
469         this.add(this.jMenuItemProperties);
470         // Enable/Disable jMenuItemNextGraphicsObjectPaste
471         if (NOClipboard.isColumnClipped())
472         {
473             this.jMenuItemBaseObjectPaste.setEnabled(true);
474         }
475         else
476         {
477             this.jMenuItemBaseObjectPaste.setEnabled(false);
478         }
479     }
480     /***
481      *  Fix menu for a Field
482      */
483     private void fixFieldMenu()
484     {
485         this.removeAll();
486         // Construct
487         this.add(this.jMenuItemColumnCut);
488         this.add(this.jMenuItemColumnCopy);
489         this.add(this.jMenuItemColumnDelete);
490         this.addSeparator();
491         this.add(this.jMenuItemRename);
492     }
493 }