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.JColorChooser;
30  import javax.swing.JDialog;
31  import javax.swing.JPanel;
32  import javax.swing.JTabbedPane;
33  import javax.swing.SwingConstants;
34  import javax.swing.WindowConstants;
35  import org.devaki.nextobjects.NextObjects;
36  import org.devaki.nextobjects.constants.CstGraphics;
37  import org.devaki.nextobjects.ui.components.CustomButton;
38  import org.devaki.nextobjects.workspace.models.graphics.AssociationLinkView;
39  import org.devaki.nextobjects.workspace.models.graphics.ConstraintView;
40  import org.devaki.nextobjects.workspace.models.graphics.ObjectView;
41  import org.devaki.nextobjects.workspace.models.styles.LineStyle;
42  
43  /***
44   * This class is responsible for creating the window which manage the style of
45   * all line-like objects
46   * 
47   */
48  public class LineStyleEdit extends JDialog
49  {
50      /***
51       * Reference of the style currently edited
52       */
53      private LineStyle myLineStyle;
54  
55      /***
56       * Reference of the object currently edited
57       */
58      private ObjectView myObjectView;
59  
60      /***
61       * jButtonReset
62       */
63      private CustomButton jButtonReset= new CustomButton("Reset", "Reset to default values", true, false);
64  
65      /***
66       * jButtonOK
67       */
68      private CustomButton jButtonOK= new CustomButton("OK", "", true, false);
69  
70      /***
71       *  jButtonCancel 
72       */
73      private CustomButton jButtonCancel= new CustomButton("Cancel", "", true, false);
74  
75      /***
76       *  jTabbedPane
77       */
78      // Panels
79      private JTabbedPane jTabbedPane= new JTabbedPane(SwingConstants.TOP);
80  
81      /***
82       * l jPanelLine 
83       */
84      private JPanel jPanelLine= new JPanel(new GridBagLayout());
85  
86      /***
87       *  jColorChooserLine
88       */
89      private JColorChooser jColorChooserLine= new JColorChooser();
90  
91      /***
92       * Contruct a new window editor for the style of a line-like object
93       * @param pObjectView the context object view
94       */
95      public LineStyleEdit(ObjectView pObjectView)
96      {
97          /* INITIALIZATION */
98          super(NextObjects.getReference());
99          this.myObjectView= pObjectView;
100         // Define the size of the window
101         this.setSize(new Dimension(475, 500));
102         // Center the window
103         this.setLocationRelativeTo(null);
104         // Specify that the window is modal
105         this.setModal(true);
106         // Specify that the window is not resizable
107         this.setResizable(false);
108         // Define what to do when closing this window using
109         this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
110 
111         /* COMPONENTS PROPERTIES */
112         // Panels
113         this.getContentPane().setLayout(new GridBagLayout());
114 
115         /* LISTENERS */
116         // Action Listeners
117         this.jButtonReset.addActionListener(new ActionListener()
118         {
119             public void actionPerformed(ActionEvent e)
120             {
121                 reset();
122             }
123         });
124         this.jButtonOK.addActionListener(new ActionListener()
125         {
126             public void actionPerformed(ActionEvent e)
127             {
128                 closingWindow(true);
129             }
130         });
131         this.jButtonCancel.addActionListener(new ActionListener()
132         {
133             public void actionPerformed(ActionEvent e)
134             {
135                 closingWindow(false);
136             }
137         });
138 
139         /* ASSEMBLING COMPONENTS */
140         // jTabbedPane
141         this.jTabbedPane.add(this.jPanelLine, "Line");
142         // jPanelLine
143         this.jPanelLine.add(
144             jColorChooserLine,
145             new GridBagConstraints(
146                 0,
147                 0,
148                 1,
149                 1,
150                 1.0,
151                 1.0,
152                 GridBagConstraints.CENTER,
153                 GridBagConstraints.BOTH,
154                 new Insets(5, 5, 5, 5),
155                 0,
156                 0));
157         // Content Panel
158         this.getContentPane().add(
159             this.jTabbedPane,
160             new GridBagConstraints(
161                 0,
162                 0,
163                 3,
164                 1,
165                 1.0,
166                 1.0,
167                 GridBagConstraints.CENTER,
168                 GridBagConstraints.BOTH,
169                 new Insets(5, 5, 0, 5),
170                 0,
171                 0));
172         this.getContentPane().add(
173             this.jButtonReset,
174             new GridBagConstraints(
175                 0,
176                 1,
177                 1,
178                 1,
179                 1.0,
180                 0.0,
181                 GridBagConstraints.WEST,
182                 GridBagConstraints.NONE,
183                 new Insets(5, 5, 5, 5),
184                 0,
185                 0));
186         this.getContentPane().add(
187             this.jButtonOK,
188             new GridBagConstraints(
189                 1,
190                 1,
191                 1,
192                 1,
193                 0.0,
194                 0.0,
195                 GridBagConstraints.CENTER,
196                 GridBagConstraints.NONE,
197                 new Insets(5, 0, 5, 5),
198                 0,
199                 0));
200         this.getContentPane().add(
201             this.jButtonCancel,
202             new GridBagConstraints(
203                 2,
204                 1,
205                 1,
206                 1,
207                 0.0,
208                 0.0,
209                 GridBagConstraints.CENTER,
210                 GridBagConstraints.NONE,
211                 new Insets(5, 0, 5, 5),
212                 0,
213                 0));
214     }
215 
216     /***
217      * What to do before closing the window
218      * @param isOK a boolean
219      */
220     private void closingWindow(boolean isOK)
221     {
222         // Save data if this method follows an action on 'jButtonOK'
223         if (isOK)
224         {
225             this.saveData();
226         }
227         // Dispose window (unallocate resources) - show() must be call to set the
228         // window visible again
229         this.dispose();
230     }
231 
232     /***
233      * Set the informations
234      * @param pLineStyle the context line style
235      */
236     public void setData(LineStyle pLineStyle)
237     {
238         // Define the title of the window
239         this.setTitle(
240             new StringBuffer()
241                 .append("Format of '")
242                 .append(pLineStyle.getMyObjectView().getBaseObject().getName())
243                 .append("' - nextObjects")
244                 .toString());
245         // Initialize the local 'Entity' object with the 'Entity' object to edit
246         this.myLineStyle= pLineStyle;
247         // Initialize components values
248         this.jColorChooserLine.setColor(this.myLineStyle.getLineColor());
249         // Set 'General' the active tab when opening the dialog box
250         // The following condition is useful on the first call of setData when
251         // creating 'EntityEdit'
252         if (this.jTabbedPane.getTabCount() != 0)
253         {
254             this.jTabbedPane.setSelectedIndex(0);
255         }
256 
257         // Set 'jButtonOK' as the default button
258         this.getRootPane().setDefaultButton(this.jButtonOK);
259 
260         // Make the window visible
261         this.show();
262     }
263 
264     /***
265      * Save the information
266      */
267     private void saveData()
268     {
269         this.myLineStyle.setLineColor(this.jColorChooserLine.getColor());
270     }
271 
272     /***
273      * Reset format of the object to its default values.
274      */
275     private void reset()
276     {
277         if (this.myObjectView instanceof AssociationLinkView)
278             this.jColorChooserLine.setColor(CstGraphics.DEFAULT_ASSOCIATIONLINK_COLOR);
279         else if (this.myObjectView instanceof ConstraintView)
280             this.jColorChooserLine.setColor(CstGraphics.DEFAULT_CONSTRAINT_COLOR);
281         else
282             this.jColorChooserLine.setColor(CstGraphics.DEFAULT_INHERITANCELINK_COLOR);
283     }
284 }