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  
21  package org.devaki.nextobjects.ui.workspace.models.styles;
22  
23  import java.awt.Dimension;
24  import java.awt.GridBagConstraints;
25  import java.awt.GridBagLayout;
26  import java.awt.Insets;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import javax.swing.JCheckBox;
30  import javax.swing.JColorChooser;
31  import javax.swing.JDialog;
32  import javax.swing.JLabel;
33  import javax.swing.JPanel;
34  import javax.swing.JTabbedPane;
35  import javax.swing.SwingConstants;
36  import javax.swing.WindowConstants;
37  import org.devaki.nextobjects.NextObjects;
38  import org.devaki.nextobjects.constants.CstGraphics;
39  import org.devaki.nextobjects.ui.components.CustomButton;
40  import org.devaki.nextobjects.ui.components.CustomTextField;
41  import org.devaki.nextobjects.workspace.models.graphics.AssociationView;
42  import org.devaki.nextobjects.workspace.models.graphics.EntityView;
43  import org.devaki.nextobjects.workspace.models.graphics.ObjectView;
44  import org.devaki.nextobjects.workspace.models.styles.ClassStyle;
45  
46  /***
47   * This class is responsible for creating the window which manage the style of
48   * all class-like objects
49   */
50  public class ClassStyleEdit extends JDialog
51  {
52  
53      /***
54       * Reference of the style currently edited
55       */
56      private ClassStyle myClassStyle;
57  
58      /***
59       * Reference of the object currently edited
60       */
61      private ObjectView myObjectView;
62  
63      /***
64       *  jButtonReset
65       */
66      private CustomButton jButtonReset= new CustomButton("Reset", "Reset to default values", true, false);
67  
68      /***
69       *  jButtonReset
70       */
71      private CustomButton jButtonOK= new CustomButton("OK", "", true, false);
72  
73      /***
74       *  jButtonReset
75       */
76      private CustomButton jButtonCancel= new CustomButton("Cancel", "", true, false);
77  
78      /***
79       *   jCheckBoxTextAdjusted 
80       */
81      // Check Box
82      private JCheckBox jCheckBoxTextAdjusted= new JCheckBox("Text adjusted");
83  
84      /***
85       *  jLabel
86       */
87      private JLabel jLabelWidth= new JLabel("Width");
88  
89      /***
90       *  jLabel 
91       */
92      private JLabel jLabelHeight= new JLabel("Height");
93  
94      /***
95       *  jTextField 
96       */
97      private CustomTextField jTextFieldWidth= new CustomTextField("", "Adjust the width of the selected object", true);
98  
99      /***
100      *  jTextField 
101      */
102     private CustomTextField jTextFieldHeight= new CustomTextField("", "Adjust the height of the selected object", true);
103 
104     /***
105      * jTabbedPane 
106      */
107     private JTabbedPane jTabbedPane= new JTabbedPane(SwingConstants.TOP);
108 
109     /***
110      * jPanel 
111      */
112     private JPanel jPanelSize= new JPanel(new GridBagLayout());
113 
114     /***
115      * jPanel 
116      */
117     private JPanel jPanelBackground= new JPanel(new GridBagLayout());
118 
119     /***
120      * jPanel 
121      */
122     private JPanel jPanelBorder= new JPanel(new GridBagLayout());
123 
124     /***
125      * jColorChooser 
126      */
127     private JColorChooser jColorChooserBackground= new JColorChooser();
128 
129     /***
130      * jColorChooser 
131      */
132     private JColorChooser jColorChooserBorder= new JColorChooser();
133 
134     /***
135      * Contruct a new window editor for the style of a class-like object
136      * @param pObjectView the context object view
137      */
138     public ClassStyleEdit(ObjectView pObjectView)
139     {
140         /* INITIALIZATION */
141         super(NextObjects.getReference());
142         this.myObjectView= pObjectView;
143         // Define the size of the window
144         this.setSize(new Dimension(475, 500));
145         // Center the window
146         this.setLocationRelativeTo(null);
147         // Specify that the window is modal
148         this.setModal(true);
149         // Specify that the window is not resizable
150         this.setResizable(false);
151         // Define what to do when closing this window using
152         this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
153 
154         /* COMPONENTS PROPERTIES */
155         // Panels
156         this.getContentPane().setLayout(new GridBagLayout());
157 
158         /* LISTENERS */
159         // Action Listeners
160         this.jButtonReset.addActionListener(new ActionListener()
161         {
162             public void actionPerformed(ActionEvent e)
163             {
164                 reset();
165             }
166         });
167         this.jButtonOK.addActionListener(new ActionListener()
168         {
169             public void actionPerformed(ActionEvent e)
170             {
171                 closingWindow(true);
172             }
173         });
174         this.jButtonCancel.addActionListener(new ActionListener()
175         {
176             public void actionPerformed(ActionEvent e)
177             {
178                 closingWindow(false);
179             }
180         });
181         this.jCheckBoxTextAdjusted.addActionListener(new ActionListener()
182         {
183             public void actionPerformed(ActionEvent e)
184             {
185                 if (jCheckBoxTextAdjusted.isSelected())
186                 {
187                     jTextFieldWidth.setEnabled(false);
188                     jTextFieldHeight.setEnabled(false);
189                 }
190                 else
191                 {
192                     jTextFieldWidth.setEnabled(true);
193                     jTextFieldHeight.setEnabled(true);
194                 }
195             }
196         });
197 
198         /* ASSEMBLING COMPONENTS */
199         // jTabbedPane
200         this.jTabbedPane.add(this.jPanelSize, "Size");
201         this.jTabbedPane.add(this.jPanelBackground, "Background");
202         this.jTabbedPane.add(this.jPanelBorder, "Border");
203         // jPanelSize
204         this.jPanelSize.add(
205             this.jLabelWidth,
206             new GridBagConstraints(
207                 0,
208                 0,
209                 1,
210                 1,
211                 0.0,
212                 0.0,
213                 GridBagConstraints.WEST,
214                 GridBagConstraints.NONE,
215                 new Insets(5, 5, 0, 5),
216                 0,
217                 0));
218         this.jPanelSize.add(
219             this.jTextFieldWidth,
220             new GridBagConstraints(
221                 1,
222                 0,
223                 1,
224                 1,
225                 0.5,
226                 0.0,
227                 GridBagConstraints.WEST,
228                 GridBagConstraints.HORIZONTAL,
229                 new Insets(5, 0, 0, 5),
230                 0,
231                 0));
232         this.jPanelSize.add(
233             this.jLabelHeight,
234             new GridBagConstraints(
235                 0,
236                 1,
237                 1,
238                 1,
239                 0.0,
240                 0.0,
241                 GridBagConstraints.WEST,
242                 GridBagConstraints.NONE,
243                 new Insets(5, 5, 0, 5),
244                 0,
245                 0));
246         this.jPanelSize.add(
247             this.jTextFieldHeight,
248             new GridBagConstraints(
249                 1,
250                 1,
251                 1,
252                 1,
253                 0.5,
254                 0.0,
255                 GridBagConstraints.WEST,
256                 GridBagConstraints.HORIZONTAL,
257                 new Insets(5, 0, 0, 5),
258                 0,
259                 0));
260         this.jPanelSize.add(
261             this.jCheckBoxTextAdjusted,
262             new GridBagConstraints(
263                 0,
264                 2,
265                 2,
266                 1,
267                 0.0,
268                 0.0,
269                 GridBagConstraints.NORTHWEST,
270                 GridBagConstraints.NONE,
271                 new Insets(5, 5, 0, 5),
272                 0,
273                 0));
274         this.jPanelSize.add(
275             new JLabel(),
276             new GridBagConstraints(
277                 2,
278                 0,
279                 1,
280                 3,
281                 1.5,
282                 1.0,
283                 GridBagConstraints.CENTER,
284                 GridBagConstraints.BOTH,
285                 new Insets(5, 5, 5, 5),
286                 0,
287                 0));
288         // jPanelBackground
289         this.jPanelBackground.add(
290             this.jColorChooserBackground,
291             new GridBagConstraints(
292                 0,
293                 0,
294                 1,
295                 1,
296                 1.0,
297                 1.0,
298                 GridBagConstraints.CENTER,
299                 GridBagConstraints.BOTH,
300                 new Insets(5, 5, 5, 5),
301                 0,
302                 0));
303         // jPanelBorder
304         this.jPanelBorder.add(
305             this.jColorChooserBorder,
306             new GridBagConstraints(
307                 0,
308                 0,
309                 1,
310                 1,
311                 1.0,
312                 1.0,
313                 GridBagConstraints.CENTER,
314                 GridBagConstraints.BOTH,
315                 new Insets(5, 5, 5, 5),
316                 0,
317                 0));
318         // Content Panel
319         this.getContentPane().add(
320             this.jTabbedPane,
321             new GridBagConstraints(
322                 0,
323                 0,
324                 3,
325                 1,
326                 1.0,
327                 1.0,
328                 GridBagConstraints.CENTER,
329                 GridBagConstraints.BOTH,
330                 new Insets(5, 5, 0, 5),
331                 0,
332                 0));
333         this.getContentPane().add(
334             this.jButtonReset,
335             new GridBagConstraints(
336                 0,
337                 1,
338                 1,
339                 1,
340                 1.0,
341                 0.0,
342                 GridBagConstraints.WEST,
343                 GridBagConstraints.NONE,
344                 new Insets(5, 5, 5, 5),
345                 0,
346                 0));
347         this.getContentPane().add(
348             this.jButtonOK,
349             new GridBagConstraints(
350                 1,
351                 1,
352                 1,
353                 1,
354                 0.0,
355                 0.0,
356                 GridBagConstraints.CENTER,
357                 GridBagConstraints.NONE,
358                 new Insets(5, 0, 5, 5),
359                 0,
360                 0));
361         this.getContentPane().add(
362             this.jButtonCancel,
363             new GridBagConstraints(
364                 2,
365                 1,
366                 1,
367                 1,
368                 0.0,
369                 0.0,
370                 GridBagConstraints.CENTER,
371                 GridBagConstraints.NONE,
372                 new Insets(5, 0, 5, 5),
373                 0,
374                 0));
375     }
376 
377     /***
378      * What to do before closing the window
379      * @param isOK a boolean
380      */
381     private void closingWindow(boolean isOK)
382     {
383         // Save data if this method follows an action on 'jButtonOK'
384         if (isOK)
385         {
386             this.saveData();
387         }
388         // Dispose window (unallocate resources) - show() must be call to set the
389         // window visible again
390         this.dispose();
391     }
392 
393     /***
394      * Set the informations
395      * @param pClassStyle the context class style
396      */
397     public void setData(ClassStyle pClassStyle)
398     {
399         // Define the title of the window
400         this.setTitle(
401             new StringBuffer()
402                 .append("Format of '")
403                 .append(pClassStyle.getMyObjectView().getBaseObject().getName())
404                 .append("' - nextObjects")
405                 .toString());
406         // Initialize the local 'Entity' object with the 'Entity' object to edit
407         this.myClassStyle= pClassStyle;
408         // Initialize components values
409         if (this.myClassStyle.isTextAdjusted())
410         {
411             this.jTextFieldWidth.setEnabled(false);
412             this.jTextFieldHeight.setEnabled(false);
413         }
414         this.jTextFieldWidth.setText(String.valueOf((int) this.myClassStyle.getMyObjectView().getSize().getWidth()));
415         this.jTextFieldHeight.setText(String.valueOf((int) this.myClassStyle.getMyObjectView().getSize().getHeight()));
416         this.jCheckBoxTextAdjusted.setSelected(this.myClassStyle.isTextAdjusted());
417         this.jColorChooserBackground.setColor(this.myClassStyle.getBackgroundColor());
418         this.jColorChooserBorder.setColor(this.myClassStyle.getBorderColor());
419         // Set 'General' the active tab when opening the dialog box
420         // The following condition is useful on the first call of setData when
421         // creating 'EntityEdit'
422         if (this.jTabbedPane.getTabCount() != 0)
423         {
424             this.jTabbedPane.setSelectedIndex(0);
425         }
426 
427         // Set 'jButtonOK' as the default button
428         this.getRootPane().setDefaultButton(this.jButtonOK);
429 
430         // Make the window visible
431         this.show();
432     }
433 
434     /***
435      * Save the information
436      */
437     private void saveData()
438     {
439         // Save components values
440         if (!this.jCheckBoxTextAdjusted.isSelected())
441         {
442             this.myClassStyle.getMyObjectView().setSize(
443                 new Dimension(
444                     Integer.parseInt(this.jTextFieldWidth.getText()),
445                     Integer.parseInt(this.jTextFieldHeight.getText())));
446             this.myClassStyle.setTextAdjusted(false);
447         }
448         else
449         {
450             this.myClassStyle.setTextAdjusted(true);
451         }
452         this.myClassStyle.setBackgroundColor(this.jColorChooserBackground.getColor());
453         this.myClassStyle.setBorderColor(this.jColorChooserBorder.getColor());
454     }
455 
456     /***
457      * Reset format of the object to its default values.
458      */
459     private void reset()
460     {
461         this.jCheckBoxTextAdjusted.setSelected(true);
462         if (this.myObjectView instanceof EntityView)
463         {
464             this.jColorChooserBackground.setColor(CstGraphics.DEFAULT_ENTITY_BACKGROUND_COLOR);
465             this.jColorChooserBorder.setColor(CstGraphics.DEFAULT_ENTITY_BORDER_COLOR);
466         }
467         else if (this.myObjectView instanceof AssociationView)
468         {
469             this.jColorChooserBackground.setColor(CstGraphics.DEFAULT_ASSOCIATION_BACKGROUND_COLOR);
470             this.jColorChooserBorder.setColor(CstGraphics.DEFAULT_ASSOCIATION_BORDER_COLOR);
471         }
472         else
473         {
474             this.jColorChooserBackground.setColor(CstGraphics.DEFAULT_TABLE_BACKGROUND_COLOR);
475             this.jColorChooserBorder.setColor(CstGraphics.DEFAULT_TABLE_BORDER_COLOR);
476         }
477     }
478 }