View Javadoc

1   package org.devaki.nextobjects.ui.workspace.models.objects;
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  import java.awt.Dimension;
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.awt.event.ActionEvent;
26  import java.awt.event.ActionListener;
27  import java.awt.event.FocusAdapter;
28  import java.awt.event.FocusEvent;
29  import javax.swing.BorderFactory;
30  import javax.swing.JDialog;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JTabbedPane;
35  import javax.swing.SwingConstants;
36  import javax.swing.WindowConstants;
37  import javax.swing.event.DocumentEvent;
38  import javax.swing.event.DocumentListener;
39  import org.devaki.nextobjects.NextObjects;
40  import org.devaki.nextobjects.ui.components.CustomButton;
41  import org.devaki.nextobjects.ui.components.CustomComboBox;
42  import org.devaki.nextobjects.ui.components.CustomTextArea;
43  import org.devaki.nextobjects.ui.components.CustomTextField;
44  import org.devaki.nextobjects.workspace.models.ConceptualModel;
45  import org.devaki.nextobjects.workspace.models.objects.InheritanceLink;
46  /***
47   * This is the editor window of an inheritance link
48   * @author <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
49   */
50  public class InheritanceLinkEdit extends JDialog
51  {
52      /***
53       *  Local reference of the edited object
54       */
55      private InheritanceLink myInheritanceLink;
56      /***
57       * jLabel
58       */
59      private JLabel jLabelName = new JLabel("Name");
60      /***
61       * jLabel
62       */
63      private JLabel jLabelDescription = new JLabel("Description");
64      /***
65       * jLabel
66       */
67      private JLabel jLabelParent = new JLabel("Parent Entity");
68      /***
69       * jLabel
70       */
71      private JLabel jLabelChild = new JLabel("Child Entity");
72      /***
73       * jTextAea
74       */
75      private CustomTextField jTextFieldName =
76          new CustomTextField(
77              "",
78              "Name of the inheritance link in the schema",
79              true);
80      /***
81       * jLabel
82       */
83      private CustomTextArea jTextAreaDescription =
84          new CustomTextArea(
85              "",
86              "Description of the inheritance link",
87              true,
88              true);
89      /***
90       * jLabel
91       */
92      private CustomTextArea jTextAreaNotes =
93          new CustomTextArea("", "Notes about the inheritance link", true, true);
94      /***
95       * jButton
96       */
97      private CustomButton jButtonOK = new CustomButton("OK", "", true, false);
98      /***
99       * jButton
100      */
101     private CustomButton jButtonCancel =
102         new CustomButton("Cancel", "", true, false);
103     /***
104      * jComboBox
105      */
106     private CustomComboBox jComboBoxParent =
107         new CustomComboBox("Change parent entity", true);
108     /***
109      * jComboBox
110      */
111     private CustomComboBox jComboBoxChild =
112         new CustomComboBox("Change child entity", true);
113     /***
114      * jTabbedPane
115      */
116     private JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.TOP);
117     /***
118      * jPanel
119      */
120     private JPanel jPanelGeneral = new JPanel(new GridBagLayout());
121     /***
122      * jPanel
123      */
124     private JPanel jPanelGeneralProperties = new JPanel(new GridBagLayout());
125     /***
126      * jPanel
127      */
128     private JPanel jPanelGeneralInheritance = new JPanel(new GridBagLayout());
129     /***
130      * jPanel
131      */
132     private JPanel jPanelNotes = new JPanel(new GridBagLayout());
133     /***
134      * Construct a new 'InheritanceLinkEdit' object
135      */
136     public InheritanceLinkEdit()
137     {
138         super(NextObjects.getReference());
139         // Define the size of the window
140         this.setSize(new Dimension(457, 500));
141         // Center the window
142         this.setLocationRelativeTo(null);
143         // Specify that the window is modal
144         this.setModal(true);
145         // Specify that the window is not resizable
146         this.setResizable(false);
147         // Define what to do when closing this window
148         this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
149         // PANELS
150         // Set a GridBagLayout for the content panel
151         this.getContentPane().setLayout(new GridBagLayout());
152         // Create titled borders for some panels
153         this.jPanelGeneralProperties.setBorder(
154             BorderFactory.createTitledBorder("Properties"));
155         this.jPanelGeneralInheritance.setBorder(
156             BorderFactory.createTitledBorder("Inheritance"));
157         // ACTION
158         this.jButtonOK.addActionListener(new ActionListener()
159         {
160             // When calling 'jButtonOK', save data and close the window
161             public final void actionPerformed(final ActionEvent e)
162             {
163                 closingWindow(true);
164             }
165         });
166         jButtonCancel.addActionListener(new ActionListener()
167         {
168             // When calling 'jButtonCancel', close the dialog without saving
169             public final void actionPerformed(final ActionEvent e)
170             {
171                 closingWindow(false);
172             }
173         });
174         // FOCUS
175         this.jTextFieldName.addFocusListener(new FocusAdapter()
176         {
177             // Select the content of 'jTextFieldName' when it gained focus
178             public final void focusGained(final FocusEvent e)
179             {
180                 jTextFieldName.selectAll();
181             }
182         });
183         // DOCUMENT
184         this
185             .jTextFieldName
186             .getDocument()
187             .addDocumentListener(new DocumentListener()
188         {
189             // Not implemented
190             public final void changedUpdate(final DocumentEvent e)
191             {
192             }
193             // When inserting text, call 'enablingOKButton'
194             public final void insertUpdate(final DocumentEvent e)
195             {
196                 enablingOKButton();
197             }
198             // When removing text, call 'enablingOKButton'
199             public final void removeUpdate(final DocumentEvent e)
200             {
201                 enablingOKButton();
202             }
203         });
204         // 'jPanelGeneralProperties'
205         this.jPanelGeneralProperties.add(
206             this.jLabelName,
207             new GridBagConstraints(
208                 0,
209                 0,
210                 1,
211                 1,
212                 0.0,
213                 0.0,
214                 GridBagConstraints.WEST,
215                 GridBagConstraints.NONE,
216                 new Insets(5, 5, 0, 5),
217                 0,
218                 0));
219         this.jPanelGeneralProperties.add(
220             this.jTextFieldName,
221             new GridBagConstraints(
222                 1,
223                 0,
224                 1,
225                 1,
226                 0.0,
227                 0.0,
228                 GridBagConstraints.CENTER,
229                 GridBagConstraints.HORIZONTAL,
230                 new Insets(5, 0, 0, 5),
231                 0,
232                 0));
233         this.jPanelGeneralProperties.add(
234             this.jLabelDescription,
235             new GridBagConstraints(
236                 0,
237                 1,
238                 1,
239                 1,
240                 0.0,
241                 0.0,
242                 GridBagConstraints.NORTHWEST,
243                 GridBagConstraints.NONE,
244                 new Insets(5, 5, 0, 5),
245                 0,
246                 0));
247         this.jPanelGeneralProperties.add(
248             new JScrollPane(this.jTextAreaDescription),
249             new GridBagConstraints(
250                 1,
251                 1,
252                 1,
253                 1,
254                 1.0,
255                 1.0,
256                 GridBagConstraints.CENTER,
257                 GridBagConstraints.BOTH,
258                 new Insets(5, 0, 5, 5),
259                 0,
260                 0));
261         // 'jPanelGeneralInheritance'
262         this.jPanelGeneralInheritance.add(
263             this.jLabelParent,
264             new GridBagConstraints(
265                 0,
266                 0,
267                 1,
268                 1,
269                 0.0,
270                 0.0,
271                 GridBagConstraints.WEST,
272                 GridBagConstraints.NONE,
273                 new Insets(5, 5, 0, 5),
274                 0,
275                 0));
276         this.jPanelGeneralInheritance.add(
277             this.jComboBoxParent,
278             new GridBagConstraints(
279                 1,
280                 0,
281                 1,
282                 1,
283                 1.0,
284                 0.0,
285                 GridBagConstraints.CENTER,
286                 GridBagConstraints.HORIZONTAL,
287                 new Insets(5, 0, 0, 5),
288                 0,
289                 0));
290         this.jPanelGeneralInheritance.add(
291             this.jLabelChild,
292             new GridBagConstraints(
293                 0,
294                 1,
295                 1,
296                 1,
297                 0.0,
298                 0.0,
299                 GridBagConstraints.WEST,
300                 GridBagConstraints.NONE,
301                 new Insets(5, 5, 0, 5),
302                 0,
303                 0));
304         this.jPanelGeneralInheritance.add(
305             this.jComboBoxChild,
306             new GridBagConstraints(
307                 1,
308                 1,
309                 1,
310                 1,
311                 1.0,
312                 0.0,
313                 GridBagConstraints.CENTER,
314                 GridBagConstraints.HORIZONTAL,
315                 new Insets(5, 0, 5, 5),
316                 0,
317                 0));
318         // 'jPanelGeneral'
319         this.jPanelGeneral.add(
320             this.jPanelGeneralProperties,
321             new GridBagConstraints(
322                 0,
323                 0,
324                 1,
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.jPanelGeneral.add(
334             this.jPanelGeneralInheritance,
335             new GridBagConstraints(
336                 0,
337                 1,
338                 1,
339                 1,
340                 0.0,
341                 0.0,
342                 GridBagConstraints.CENTER,
343                 GridBagConstraints.BOTH,
344                 new Insets(5, 5, 5, 5),
345                 0,
346                 0));
347         // 'jPanelNotes'
348         this.jPanelNotes.add(
349             new JScrollPane(this.jTextAreaNotes),
350             new GridBagConstraints(
351                 0,
352                 0,
353                 1,
354                 1,
355                 1.0,
356                 1.0,
357                 GridBagConstraints.CENTER,
358                 GridBagConstraints.BOTH,
359                 new Insets(5, 5, 5, 5),
360                 0,
361                 0));
362         // 'jTabbedPane'
363         this.jTabbedPane.add(this.jPanelGeneral, "General");
364         this.jTabbedPane.add(this.jPanelNotes, "Notes");
365         // Content Panel
366         this.getContentPane().add(
367             this.jTabbedPane,
368             new GridBagConstraints(
369                 0,
370                 0,
371                 2,
372                 1,
373                 1.0,
374                 1.0,
375                 GridBagConstraints.CENTER,
376                 GridBagConstraints.BOTH,
377                 new Insets(5, 5, 0, 5),
378                 0,
379                 0));
380         this.getContentPane().add(
381             this.jButtonOK,
382             new GridBagConstraints(
383                 0,
384                 1,
385                 1,
386                 1,
387                 1.0,
388                 0.0,
389                 GridBagConstraints.EAST,
390                 GridBagConstraints.NONE,
391                 new Insets(5, 5, 5, 5),
392                 0,
393                 0));
394         this.getContentPane().add(
395             this.jButtonCancel,
396             new GridBagConstraints(
397                 1,
398                 1,
399                 1,
400                 1,
401                 0.0,
402                 0.0,
403                 GridBagConstraints.CENTER,
404                 GridBagConstraints.NONE,
405                 new Insets(5, 0, 5, 5),
406                 0,
407                 0));
408     } // End of 'InheritanceLinkEdit'
409     /***
410      * What to do before closing the window
411      * @param isOK is ok
412      */
413     private void closingWindow(final boolean isOK)
414     {
415         // Save data if this method follows an action on 'jButtonOK'
416         if (isOK)
417         {
418             this.saveData();
419         }
420         // Dispose window (unallocate resources) - show() must be
421         // call to set the window visible again
422         this.dispose();
423     }
424     /***
425      * Enable or disable the 'OK' button depending on the length of
426      * 'jTextFieldName'
427      */
428     private void enablingOKButton()
429     {
430         if (this.jTextFieldName.getText().length() != 0)
431         {
432             // Both text fields length are different from zero,
433             // so enable 'jButtonOK'
434             this.jButtonOK.setEnabled(true);
435         }
436         // One or both text fields length are equal to zero,
437         // so diable 'jButtonOK'
438         else
439         {
440             this.jButtonOK.setEnabled(false);
441         }
442     }
443     /***
444      * Get the informations from the selected InheritanceLink
445      * @param pInheritanceLink inheritance link
446      */
447     public final void setData(final InheritanceLink pInheritanceLink)
448     {
449         // Define title of the window
450         this.setTitle(
451             new StringBuffer()
452                 .append("Properties of '")
453                 .append(pInheritanceLink.getName())
454                 .append("' - nextObjects")
455                 .toString());
456         // Initialize the local object with the object to edit
457         this.myInheritanceLink = pInheritanceLink;
458         // Initialize components values
459         this.jTextFieldName.setText(this.myInheritanceLink.getName());
460         this.jTextAreaDescription.setText(
461             this.myInheritanceLink.getDescription());
462         this.jComboBoxParent.setData(
463             ((ConceptualModel) this.myInheritanceLink.getMyModel())
464                 .getAssociations());
465         this.jComboBoxParent.setSelectedItem(
466             this.myInheritanceLink.getParentClass());
467         this.jComboBoxChild.setData(
468             ((ConceptualModel) this.myInheritanceLink.getMyModel())
469                 .getEntities());
470         this.jComboBoxChild.setSelectedItem(
471             this.myInheritanceLink.getChildClass());
472         this.jTextAreaNotes.setText(this.myInheritanceLink.getNotes());
473         // Set 'jButtonOK' as the default button
474         this.getRootPane().setDefaultButton(this.jButtonOK);
475         // Make the window visible
476         this.show();
477     }
478     /***
479      * Save the informations into the edited object
480      */
481     public final void saveData()
482     {
483         this.myInheritanceLink.setName(this.jTextFieldName.getText());
484         this.myInheritanceLink.setDescription(
485             this.jTextAreaDescription.getText());
486         this.myInheritanceLink.setNotes(this.jTextAreaNotes.getText());
487         //repaint
488         myInheritanceLink.getMyModel().getModelView().setFullRefresh(true);
489         myInheritanceLink.getMyModel().getModelView().drawingArea.repaint();
490     }
491 } // End of class 'InheritanceLinkEdit'