View Javadoc

1   package org.devaki.nextobjects.ui.toolbars;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
5   
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  
20  */
21  
22  
23  import java.awt.Dimension;
24  import java.awt.Event;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.KeyEvent;
28  import javax.swing.JToolBar;
29  import javax.swing.KeyStroke;
30  import org.devaki.nextobjects.constants.CstGraphics;
31  import org.devaki.nextobjects.constants.CstImages;
32  import org.devaki.nextobjects.ui.components.CustomButton;
33  import org.devaki.nextobjects.ui.components.CustomButtonMenu;
34  import org.devaki.nextobjects.ui.components.CustomMenuItem;
35  import org.devaki.nextobjects.ui.menus.NOMenuBar;
36  import org.devaki.nextobjects.ui.others.HelpNO;
37  import org.devaki.nextobjects.util.ModelMan;
38  import org.devaki.nextobjects.util.NOFileManager;
39  import org.devaki.nextobjects.util.NOClipboard;
40  import org.devaki.nextobjects.workspace.models.BaseModel;
41  import org.devaki.nextobjects.workspace.models.ConceptualModel;
42  import org.devaki.nextobjects.workspace.models.PhysicalModel;
43  
44  /***
45   *  This is the common toolbar needed every time
46   *  It handle file create and open new model and cut'n'paste
47  * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
48   */
49  public class NOToolBar1 extends JToolBar
50  {
51      /***
52       * Open button
53       */
54      private CustomButton jButtonOpen =
55          new CustomButton(
56              CstImages.ICN_OPEN,
57              "Open a file",
58              "Open an already existing model",
59              true);
60      /***
61       * Save button
62       */
63      private static CustomButton jButtonSave =
64          new CustomButton(
65              CstImages.ICN_SAVE,
66              "Save the model",
67              "Save the current model",
68              false);
69      /***
70       * Cut button
71       */
72      private static CustomButton jButtonCut =
73          new CustomButton(
74              CstImages.ICN_CUT,
75              "Cut the selected object",
76              "Copy to the clipboard and delete the selected object",
77              false);
78  
79      /***
80       * Copy button
81       */
82      private static CustomButton jButtonCopy =
83          new CustomButton(
84              CstImages.ICN_COPY,
85              "Copy the selected object",
86              "Copy to the clipboard the selected object",
87              false);
88  
89      /***
90       * Paste button
91       */
92      private static CustomButton jButtonPaste =
93          new CustomButton(
94              CstImages.ICN_PASTE,
95              "Paste an object",
96              "Paste an object from the clipboard on the current model",
97              false);
98  
99      /***
100      * Help button
101      */
102     private CustomButton jButtonHelp =
103         new CustomButton(
104             CstImages.ICN_HELP,
105             "Help of nextObjects",
106             "Browse the help of nextObjects",
107             true);
108 
109     /***
110      * New CDM button
111      */
112     private CustomButton jButtonNewCDM =
113         new CustomButton(
114             CstImages.ICN_CDM,
115             "Create a new CDM",
116             "Create a new Conceptual Data Model",
117             true);
118     /***
119      * New button
120      */
121     private CustomButtonMenu jButtonMenuNew;
122 
123     /***
124      * New CDM drop down button
125      */
126     private CustomMenuItem jMenuItemNewCDM =
127         new CustomMenuItem(
128             "New CDM",
129             CstImages.ICN_CDM,
130             KeyEvent.VK_C,
131             KeyStroke.getKeyStroke(KeyEvent.VK_N, Event.CTRL_MASK),
132             true);
133 
134     /***
135      * New PDM drop down button
136      */
137     private CustomMenuItem jMenuItemNewPDM =
138         new CustomMenuItem(
139             "New PDM",
140             CstImages.ICN_PDM,
141             KeyEvent.VK_P,
142             KeyStroke.getKeyStroke(
143                 KeyEvent.VK_N,
144                 Event.CTRL_MASK + Event.SHIFT_MASK),
145             true);
146 /***
147  * Toolbar with general utility
148  *
149  */
150     public NOToolBar1()
151     {
152         // Initialization
153         super(JToolBar.HORIZONTAL);
154         this.setFloatable(true);
155         // Components
156         CustomMenuItem[] tmp = { jMenuItemNewCDM, jMenuItemNewPDM };
157         this.jButtonMenuNew = new CustomButtonMenu(this.jButtonNewCDM, tmp);
158         // Action listeners
159         this.jMenuItemNewCDM.addActionListener(new ActionListener()
160         {
161             public final void actionPerformed(final ActionEvent e)
162             {
163                 ModelMan.newConceptualDatamodel(
164                     new ConceptualModel(
165                         new StringBuffer()
166                             .append("Model ")
167                             .append(BaseModel.getCountInstance() + 1)
168                             .toString()));
169             }
170         });
171         this.jMenuItemNewPDM.addActionListener(new ActionListener()
172         {
173             public final void actionPerformed(final ActionEvent e)
174             {
175                 ModelMan.newPhysicalDatamodel(
176                     new PhysicalModel(
177                         new StringBuffer()
178                             .append("Model ")
179                             .append(BaseModel.getCountInstance() + 1)
180                             .toString()));
181             }
182         });
183         this.jButtonNewCDM.addActionListener(new ActionListener()
184         {
185             public final void actionPerformed(final ActionEvent e)
186             {
187                 ModelMan.newConceptualDatamodel(
188                     new ConceptualModel(
189                         new StringBuffer()
190                             .append("Model ")
191                             .append(BaseModel.getCountInstance() + 1)
192                             .toString()));
193             }
194         });
195         this.jButtonOpen.addActionListener(new ActionListener()
196         {
197             public final void actionPerformed(final ActionEvent e)
198             {
199                 NOFileManager.openModel();
200             }
201         });
202         jButtonSave.addActionListener(new ActionListener()
203         {
204             public final void actionPerformed(final ActionEvent e)
205             {
206                 NOFileManager.save(ModelMan.getCurrentModel());
207             }
208         });
209         jButtonCut.addActionListener(new ActionListener()
210         {
211             public final void actionPerformed(final ActionEvent e)
212             {
213                 NOClipboard.cut(ModelMan.getCurrentObjects());
214                 ModelMan.getCurrentModel().getPanel().repaint();
215                 fixIcons();
216                 NOMenuBar.fixEditMenu();
217             }
218         });
219         jButtonCopy.addActionListener(new ActionListener()
220         {
221             public final void actionPerformed(final ActionEvent e)
222             {
223                 NOClipboard.copy(ModelMan.getCurrentObjects());
224                 fixIcons();
225                 NOMenuBar.fixEditMenu();
226             }
227         });
228         jButtonPaste.addActionListener(new ActionListener()
229         {
230             public final void actionPerformed(final ActionEvent e)
231             {
232                 NOClipboard.pasteBaseObject(
233                     (int) CstGraphics.MODEL_INITIAL_POSITION.getX(),
234                     (int) CstGraphics.MODEL_INITIAL_POSITION.getY() / 2);
235             }
236         });
237         jButtonHelp.addActionListener(new ActionListener()
238         {
239             public final void actionPerformed(final ActionEvent e)
240             {
241                 (new HelpNO()).setVisible(true);
242             }
243         });
244 
245         //Assembling components
246         this.add(new JToolBar.Separator(new Dimension(2, 0)));
247         this.add(jButtonMenuNew);
248         this.add(jButtonOpen);
249         this.add(jButtonSave);
250         this.add(new JToolBar.Separator(new Dimension(2, 0)));
251         this.add(jButtonCut);
252         this.add(jButtonCopy);
253         this.add(jButtonPaste);
254         this.add(new JToolBar.Separator(new Dimension(2, 0)));
255         this.add(jButtonHelp);
256 
257     }
258 
259     /***
260      * Fix edit icons
261      */
262     public static void fixIcons()
263     {
264         if (ModelMan.getCurrentModel() != null)
265         {
266             jButtonSave.setEnabled(
267                 ModelMan.getCurrentModel().getFileForSave() != null);
268         }
269         else
270         {
271             jButtonSave.setEnabled(false);
272         }
273         jButtonCut.setEnabled(ModelMan.getCurrentObject() != null);
274         jButtonCopy.setEnabled(ModelMan.getCurrentObject() != null);
275         jButtonPaste.setEnabled(
276             NOClipboard.isBaseObjectClipped()
277                 && ModelMan.getCurrentModel() != null);
278     }
279 
280 }