View Javadoc

1   /*
2   
3   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
4   
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; either version 2 of the License, or (at your
8   option) any later version.
9   
10  This program is distributed in the hope that it will
11  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13  PURPOSE. See the GNU General Public License for more details.
14  
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc., 59
17  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19  */
20  package org.devaki.nextobjects.ui.toolbars;
21  
22  import java.awt.Cursor;
23  import java.awt.Dimension;
24  import java.awt.event.MouseAdapter;
25  import java.awt.event.MouseEvent;
26  import javax.swing.ButtonGroup;
27  import javax.swing.JToolBar;
28  import org.devaki.nextobjects.constants.CstImages;
29  import org.devaki.nextobjects.ui.components.CustomToggleButton;
30  import org.devaki.nextobjects.util.ModelMan;
31  import org.devaki.nextobjects.workspace.models.ConceptualModel;
32  
33  /***
34   * This class is responsible for drawing the draw toolbar 
35   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
36   */
37  public class NOToolBar2 extends JToolBar
38  {
39  
40      /***
41       * TOOL_TABLE
42       */
43      public static final int TOOL_TABLE= 0;
44  
45      /***
46       * TOOL_SELECTION
47       */
48      public static final int TOOL_SELECTION= 1;
49  
50      /***
51       * TOOL_RESIZE
52       */
53      public static final int TOOL_RESIZE= 2;
54  
55      /***
56       * TOOL_ASSOCIATION
57       */
58      public static final int TOOL_ASSOCIATION= 3;
59  
60      /***
61       * TOOL_ASSOCIATION_LINK
62       */
63      public static final int TOOL_ASSOCIATION_LINK= 4;
64  
65      /***
66       * TOOL_CONSTRAINT
67       */
68      public static final int TOOL_CONSTRAINT= 5;
69  
70      /***
71       * TOOL_INHERITANCE
72       */
73      public static final int TOOL_INHERITANCE= 6;
74  
75      /***
76       *  TOOL_LABEL
77       */
78      public static final int TOOL_LABEL= 7;
79  
80      /***
81       *  The current tool
82       */
83      private static int currentTool= TOOL_SELECTION;
84  
85      /***
86       * this button activate drawing table. Clicking on the drawing 
87       * drop a table centered on the point you have just clicked.
88       * This button is activated only on physical model.
89       */
90      private static CustomToggleButton jToggleButtonTable;
91  
92      /***
93       * This button activate the  drawing of an association. Cliking 
94       * on the drawing will drop an association centered on the point 
95       * you have just clicked. This button is activated only on conceptual 
96       * model.
97       */
98      private static CustomToggleButton jToggleButtonAssociation;
99  
100     /***
101      *  This button activate the association link tool , represented by a line and 
102      * a pair of number called cardinaliities. When the tool is activated, dragging 
103      * cause a line to be streched. If the line is between two entites an association 
104      * and two association links are created. If it's between an entity and an 
105      * association then a single association link is created.
106      */
107     private static CustomToggleButton jToggleButtonAssociationLink;
108 
109     /***
110      * This button activate the Inheritancy tool.
111      */
112     private static CustomToggleButton jToggleButtonInheritancy;
113 
114     /***
115      * This button activate the Constraint tool.
116      */
117     private static CustomToggleButton jToggleButtonConstraint;
118 
119     /***
120      * This button activate the Selection tool.
121      */
122     private static CustomToggleButton jToggleButtonSelection;
123 
124     /***
125      *  The button Label
126      */
127     private static CustomToggleButton jToggleButtonLabel;
128 
129     /***
130      * The button group
131      */
132     private static ButtonGroup buttonGroup= new ButtonGroup();
133 
134     /***
135      * Construct a new 'NOToolBar2' object
136      */
137     public NOToolBar2()
138     {
139         //Initialization
140         super(JToolBar.HORIZONTAL);
141         this.setFloatable(true);
142 
143         // Toggle buttons
144 
145         jToggleButtonTable=
146             new CustomToggleButton(
147                 CstImages.ICN_TABLE,
148                 "Draw a table",
149                 "Create a new table or a new entity according to the type of the current model",
150                 false,
151                 false);
152         jToggleButtonAssociation=
153             new CustomToggleButton(
154                 CstImages.ICN_ASSOCIATION,
155                 "Draw an association",
156                 "Create a new association",
157                 false,
158                 false);
159         jToggleButtonAssociationLink=
160             new CustomToggleButton(
161                 CstImages.ICN_ASSOCIATIONLINK,
162                 "Draw an association link",
163                 "Create a new association link",
164                 false,
165                 false);
166         jToggleButtonInheritancy=
167             new CustomToggleButton(
168                 CstImages.ICN_INHERITANCE,
169                 "Draw an inheritance link",
170                 "Create a new inheritance link",
171                 false,
172                 false);
173         jToggleButtonConstraint=
174             new CustomToggleButton(
175                 CstImages.ICN_CONSTRAINT,
176                 "Draw a constraint",
177                 "Create a new constraint",
178                 false,
179                 false);
180         jToggleButtonSelection=
181             new CustomToggleButton(CstImages.ICN_SELECTION, "Select an object", "Select an object.", true, true);
182         jToggleButtonLabel=
183             new CustomToggleButton(
184                 CstImages.ICN_LABEL,
185                 "Add a label",
186                 "Add a label to the current object.",
187                 false,
188                 false);
189         // Button group
190         buttonGroup.add(jToggleButtonTable);
191         buttonGroup.add(jToggleButtonAssociation);
192         buttonGroup.add(jToggleButtonAssociationLink);
193         buttonGroup.add(jToggleButtonInheritancy);
194         buttonGroup.add(jToggleButtonConstraint);
195         buttonGroup.add(jToggleButtonSelection);
196         buttonGroup.add(jToggleButtonLabel);
197 
198         //Listeners
199         jToggleButtonTable.addMouseListener(new MouseAdapter()
200         {
201             public void mousePressed(MouseEvent e)
202             {
203                 setCurrentTool(TOOL_TABLE);
204                 // Set there is no current object
205                 ModelMan.setCurrentObject(null);
206             }
207         });
208         jToggleButtonAssociation.addMouseListener(new MouseAdapter()
209         {
210             public void mousePressed(MouseEvent e)
211             {
212                 setCurrentTool(TOOL_ASSOCIATION);
213                 // Set there is no current object
214                 ModelMan.setCurrentObject(null);
215             }
216         });
217         jToggleButtonAssociationLink.addMouseListener(new MouseAdapter()
218         {
219             public void mousePressed(MouseEvent e)
220             {
221                 setCurrentTool(TOOL_ASSOCIATION_LINK);
222                 if (ModelMan.getCurrentModel() instanceof ConceptualModel)
223                 {
224                     ((ConceptualModel) ModelMan.getCurrentModel()).getModelView().setCursor(
225                         new Cursor(Cursor.CROSSHAIR_CURSOR));
226                 }
227             }
228         });
229         jToggleButtonInheritancy.addMouseListener(new MouseAdapter()
230         {
231             public void mousePressed(MouseEvent e)
232             {
233                 setCurrentTool(TOOL_INHERITANCE);
234             }
235         });
236         jToggleButtonConstraint.addMouseListener(new MouseAdapter()
237         {
238             public void mousePressed(MouseEvent e)
239             {
240                 setCurrentTool(TOOL_CONSTRAINT);
241             }
242         });
243         jToggleButtonSelection.addMouseListener(new MouseAdapter()
244         {
245             public void mousePressed(MouseEvent e)
246             {
247                 setCurrentTool(TOOL_SELECTION);
248             }
249         });
250         jToggleButtonLabel.addMouseListener(new MouseAdapter()
251         {
252             public void mousePressed(MouseEvent e)
253             {
254                 setCurrentTool(TOOL_LABEL);
255             }
256         });
257 
258         //Assembling components
259         this.add(jToggleButtonTable);
260         this.add(new JToolBar.Separator(new Dimension(2, 0)));
261         this.add(jToggleButtonAssociation);
262         this.add(new JToolBar.Separator(new Dimension(2, 0)));
263         this.add(jToggleButtonAssociationLink);
264         // not completly implemented.
265         this.add(new JToolBar.Separator(new Dimension(2, 0)));
266         this.add(jToggleButtonInheritancy);
267         this.add(new JToolBar.Separator(new Dimension(2, 0)));
268         this.add(jToggleButtonConstraint);
269         this.add(new JToolBar.Separator(new Dimension(2, 0)));
270         this.add(jToggleButtonSelection);
271         this.add(new JToolBar.Separator(new Dimension(2, 0)));
272         this.add(jToggleButtonLabel);
273         fixIcons();
274     }
275 
276     /***
277      * Return the current used tool
278      * @return the current tool
279      */
280     static public int getCurrentTool()
281     {
282         return currentTool;
283     }
284 
285     /***
286      * Defines the current button used
287      * @param pCurrentTool the new current tool
288      */
289     static public void setCurrentTool(int pCurrentTool)
290     {
291         currentTool= pCurrentTool;
292         switch (currentTool)
293         {
294             case TOOL_TABLE :
295                 jToggleButtonTable.setSelected(true);
296                 break;
297             case TOOL_ASSOCIATION :
298                 jToggleButtonAssociation.setSelected(true);
299                 break;
300             case TOOL_ASSOCIATION_LINK :
301                 jToggleButtonAssociationLink.setSelected(true);
302                 break;
303             case TOOL_INHERITANCE :
304                 jToggleButtonInheritancy.setSelected(true);
305                 break;
306             case TOOL_CONSTRAINT :
307                 jToggleButtonConstraint.setSelected(true);
308                 break;
309             case TOOL_SELECTION :
310                 jToggleButtonSelection.setSelected(true);
311                 break;
312             case TOOL_LABEL :
313                 jToggleButtonLabel.setSelected(true);
314                 break;
315         }
316     }
317 
318     /***
319      * Enable/Disable icons
320      */
321     static public void fixIcons()
322     {
323         if (ModelMan.getCurrentModel() != null)
324         {
325             jToggleButtonTable.setEnabled(true);
326             jToggleButtonSelection.setEnabled(true);
327             jToggleButtonInheritancy.setEnabled(true);
328             jToggleButtonLabel.setEnabled(true);
329 
330             // CDM
331             if (ModelMan.getCurrentModel() instanceof ConceptualModel)
332             {
333                 jToggleButtonAssociation.setEnabled(true);
334                 jToggleButtonAssociationLink.setEnabled(true);
335                 jToggleButtonConstraint.setEnabled(false);
336             }
337             // PDM
338             else
339             {
340                 jToggleButtonAssociation.setEnabled(false);
341                 jToggleButtonAssociationLink.setEnabled(false);
342                 jToggleButtonConstraint.setEnabled(true);
343             }
344         }
345         // there is no current model    
346         else
347         {
348             jToggleButtonTable.setEnabled(false);
349             jToggleButtonAssociation.setEnabled(false);
350             jToggleButtonAssociationLink.setEnabled(false);
351             jToggleButtonConstraint.setEnabled(false);
352             jToggleButtonInheritancy.setEnabled(false);
353             jToggleButtonSelection.setEnabled(false);
354             jToggleButtonLabel.setEnabled(false);
355         }
356     }
357 
358 }