View Javadoc

1   package org.devaki.nextobjects.ui.workspace.models.objects;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Laurent Thevenet,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.JDialog;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JScrollPane;
33  import javax.swing.JTabbedPane;
34  import javax.swing.SwingConstants;
35  import javax.swing.WindowConstants;
36  import org.devaki.nextobjects.NextObjects;
37  import org.devaki.nextobjects.ui.components.CustomButton;
38  import org.devaki.nextobjects.ui.components.CustomTextArea;
39  import org.devaki.nextobjects.ui.components.CustomTextField;
40  import org.devaki.nextobjects.workspace.models.objects.Label;
41  /***
42   * Edit a note on the models
43   * @author     <a href="mailto:eflorent@devaki.org">Emmanuel Florent</a>
44   */
45  public class LabelEdit extends JDialog
46  {
47      /***
48       * the label
49       */
50      private Label myLabel;
51      /***
52       * the ok button
53       */
54      private CustomButton jButtonOK = new CustomButton("OK", "", true, false);
55      /***
56       * the cancel button
57       */
58      private CustomButton jButtonCancel =
59          new CustomButton("Cancel", "", true, false);
60      /***
61       * jlabel name
62       */
63      private JLabel jLabelName = new JLabel("Name");
64      /***
65       * jlabel description
66       */
67      private JLabel jLabelDescription = new JLabel("Description");
68      /***
69       * jtextfield name
70       */
71      private CustomTextField jTextFieldName =
72          new CustomTextField("", "Name of the association in the schema", true);
73      /***
74       * jtextarea description
75       */
76      private CustomTextArea jTextAreaDescription =
77          new CustomTextArea("", "Description of the association", true, true);
78      /***
79       * the tablbed pane
80       */
81      private JTabbedPane jTabbedPane = new JTabbedPane(SwingConstants.TOP);
82      /***
83       * the unique panel
84       */
85      private JPanel jPanelNotes = new JPanel(new GridBagLayout());
86      /***
87       * Create a new 'LabelEdit' object
88       */
89      public LabelEdit()
90      {
91          super(NextObjects.getReference());
92          this.jLabelDescription.setBorder(null);
93          this.jLabelName.setBorder(null);
94          // Initialization
95          // Define the size of the window
96          this.setSize(new Dimension(475, 500));
97          // Center the window
98          this.setLocationRelativeTo(null);
99          // Specify that the window is modal
100         this.setModal(true);
101         // Specify that the window is not resizable
102         this.setResizable(false);
103         // Define what to do when closing this window using the cross button
104         this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
105         // Components properties
106         // PANELS
107         // Set a GridBagLayout for the content panel
108         this.getContentPane().setLayout(new GridBagLayout());
109         // Create titled borders for some panels
110         // Listeners
111         // FOCUS
112         this.jTextFieldName.addFocusListener(new FocusAdapter()
113         {
114             // Select the content of 'jTextFieldName' when it gained focus
115             public final void focusGained(final FocusEvent e)
116             {
117                 jTextFieldName.selectAll();
118             }
119         });
120         this.jButtonOK.addActionListener(new ActionListener()
121         {
122             // When calling 'jButtonOK', save data and close the window
123             public final void actionPerformed(final ActionEvent e)
124             {
125                 closingWindow(true);
126             }
127         });
128         this.jButtonCancel.addActionListener(new ActionListener()
129         {
130             // When calling 'jButtonCancel', close the dialog without saving
131             public final void actionPerformed(final ActionEvent e)
132             {
133                 closingWindow(false);
134             }
135         });
136         // 'jPanelNotes'
137         this.jPanelNotes.add(
138             this.jLabelName,
139             new GridBagConstraints(
140                 0,
141                 0,
142                 1,
143                 1,
144                 0.0,
145                 0.0,
146                 GridBagConstraints.NORTH,
147                 GridBagConstraints.BOTH,
148                 new Insets(5, 5, 5, 5),
149                 0,
150                 0));
151         this.jPanelNotes.add(
152             this.jTextFieldName,
153             new GridBagConstraints(
154                 1,
155                 0,
156                 1,
157                 1,
158                 0.0,
159                 0.0,
160                 GridBagConstraints.NORTH,
161                 GridBagConstraints.BOTH,
162                 new Insets(5, 5, 5, 5),
163                 0,
164                 0));
165         // 'jPanelNotes'
166         jTextAreaDescription.setLineWrap(true);
167         jTextAreaDescription.setWrapStyleWord(true);
168         this.jPanelNotes.add(
169             new JScrollPane(
170                 this.jTextAreaDescription,
171                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
172                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER),
173             new GridBagConstraints(
174                 0,
175                 1,
176                 2,
177                 2,
178                 1.0,
179                 1.0,
180                 GridBagConstraints.CENTER,
181                 GridBagConstraints.BOTH,
182                 new Insets(5, 5, 5, 5),
183                 0,
184                 0));
185         this.jTabbedPane.add(this.jPanelNotes, "Notes");
186         // Content Panel
187         this.getContentPane().add(
188             this.jTabbedPane,
189             new GridBagConstraints(
190                 0,
191                 0,
192                 2,
193                 1,
194                 1.0,
195                 1.0,
196                 GridBagConstraints.CENTER,
197                 GridBagConstraints.BOTH,
198                 new Insets(5, 5, 0, 5),
199                 0,
200                 0));
201         this.getContentPane().add(
202             this.jButtonOK,
203             new GridBagConstraints(
204                 0,
205                 1,
206                 1,
207                 1,
208                 1.0,
209                 0.0,
210                 GridBagConstraints.EAST,
211                 GridBagConstraints.NONE,
212                 new Insets(5, 5, 5, 5),
213                 0,
214                 0));
215         this.getContentPane().add(
216             this.jButtonCancel,
217             new GridBagConstraints(
218                 1,
219                 1,
220                 1,
221                 1,
222                 0.0,
223                 0.0,
224                 GridBagConstraints.CENTER,
225                 GridBagConstraints.NONE,
226                 new Insets(5, 0, 5, 5),
227                 0,
228                 0));
229     } // End of 'AssociationEdit()'
230     /***
231      * What to do before closing the window
232      * @param isOK can save
233      */
234     private void closingWindow(final boolean isOK)
235     {
236         // Save data if this method follows an action on 'jButtonOK'
237         if (isOK)
238         {
239             this.saveData();
240         }
241         // Dispose window (unallocate resources)
242         // show() must be call to set the window visible again
243         this.dispose();
244     }
245     /***
246      * Initialize window components with the data of the object given as
247      * parameter of the constructor
248      * @param pLabel the context label
249      */
250     public final void setData(final Label pLabel)
251     {
252         // Define title of the window
253         this.setTitle(
254             new StringBuffer()
255                 .append("Properties of '")
256                 .append(pLabel.getName())
257                 .append("' - devaki-nextobjects")
258                 .toString());
259         // Initialize the local reference of the edited object
260         this.myLabel = pLabel;
261         this.jTextFieldName.setText(pLabel.getName());
262         this.jTextAreaDescription.setText(pLabel.getNotes());
263         // Set 'General', the active tab when opening the dialog box
264         this.jTabbedPane.setSelectedIndex(0);
265         // Set 'jButtonOK' as the default button
266         this.getRootPane().setDefaultButton(this.jButtonOK);
267         // Make the window visible
268         this.show();
269     }
270     /***
271      * Save the informations into the edited object
272      */
273     private void saveData()
274     {
275         this.myLabel.setName(this.jTextFieldName.getText());
276         this.myLabel.setNotes(this.jTextAreaDescription.getText());
277         //repaint
278         myLabel.getMyModel().getModelView().setFullRefresh(true);
279         myLabel.getMyModel().getModelView().drawingArea.repaint();
280     }
281 }