View Javadoc

1   package com.explosion.utilities.preferences.groups.dialogs;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.awt.BorderLayout;
24  import java.awt.Dialog;
25  import java.awt.Dimension;
26  import java.awt.Frame;
27  import java.awt.event.ActionEvent;
28  
29  import javax.swing.JButton;
30  import javax.swing.JDialog;
31  import javax.swing.JLabel;
32  import javax.swing.JOptionPane;
33  import javax.swing.JPanel;
34  import javax.swing.JTextField;
35  import javax.swing.event.DocumentEvent;
36  import javax.swing.event.DocumentListener;
37  
38  import com.explosion.utilities.preferences.groups.PreferenceGroup;
39  import com.explosion.utilities.preferences.groups.PreferenceGroupManager;
40  
41  /***
42   * @author Stephen Cowx Date created:@26-Jan-2003
43   */
44  public class PreferenceGroupNameEditDialog extends JDialog implements DocumentListener
45  {
46  
47      private PreferenceGroup object;
48  
49      private PreferenceGroupManager manager;
50  
51      private JPanel configPanel = new JPanel();
52  
53      private JPanel okCancelApplyPanel = new JPanel();
54  
55      private JLabel nameLabel = new JLabel("Name: ");
56  
57      private JTextField field;
58  
59      private JButton cancelButton = new JButton("Cancel");
60  
61      private JButton okButton = new JButton("Ok");
62  
63      public static Dimension nameEditorDimension = new Dimension(300, 85);
64  
65      public PreferenceGroupNameEditDialog(Dialog owner, String title, PreferenceGroup object, PreferenceGroupManager manager)
66      {
67          super(owner, title, true);
68          this.object = object;
69          this.manager = manager;
70          init();
71      }
72  
73      public PreferenceGroupNameEditDialog(Frame owner, String title, PreferenceGroup object, PreferenceGroupManager manager)
74      {
75          super(owner, title, true);
76          this.object = object;
77          this.manager = manager;
78          init();
79      }
80  
81      public void init()
82      {
83          if (object == null)
84              field = new JTextField("<Type a name here>");
85          else
86          {
87              field = new JTextField(object.getIdentifier());
88  
89              if (!object.isEditable()) field.setEditable(false);
90          }
91  
92          field.getDocument().addDocumentListener(this);
93  
94          okButton.setMnemonic('o');
95          cancelButton.setMnemonic('c');
96  
97          okCancelApplyPanel.add(okButton, null);
98          okCancelApplyPanel.add(cancelButton, null);
99  
100         okButton.addActionListener(new java.awt.event.ActionListener()
101         {
102 
103             public void actionPerformed(ActionEvent e)
104             {
105                 okButton_actionPerformed(e);
106             }
107         });
108         cancelButton.addActionListener(new java.awt.event.ActionListener()
109         {
110 
111             public void actionPerformed(ActionEvent e)
112             {
113                 cancelButton_actionPerformed(e);
114             }
115         });
116 
117         configPanel.setLayout(new BorderLayout());
118         configPanel.add(nameLabel, BorderLayout.WEST);
119         configPanel.add(field, BorderLayout.CENTER);
120 
121         this.getContentPane().setLayout(new BorderLayout());
122         this.getContentPane().add(configPanel, BorderLayout.CENTER);
123         this.getContentPane().add(okCancelApplyPanel, BorderLayout.SOUTH);
124 
125         this.okButton.setEnabled(false);
126     }
127 
128     void okButton_actionPerformed(ActionEvent e)
129     {
130         try
131         {
132             if (field.getText().trim().length() < 1)
133             {
134                 JOptionPane.showMessageDialog(this, "The name you have given is invalid as it has a length of zero. PLease type a name which has one or more letter in it.");
135                 return;
136             }
137 
138             if (manager.getGroup(field.getText().trim()) != null)
139             {
140                 JOptionPane.showMessageDialog(this, "The name '" + field.getText().trim() + "' is already in use. Please choose another name.");
141                 return;
142             }
143 
144             if (object == null)
145             {
146                 manager.addGroup(field.getText().trim(), manager.createGroup(field.getText().trim()));
147                 this.object = manager.getGroup(field.getText().trim());
148             } else
149             {
150                 manager.renameGroup(object.getIdentifier(), field.getText().trim());
151             }
152         } catch (Exception ex)
153         {
154             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
155         }
156         this.dispose();
157     }
158 
159     void cancelButton_actionPerformed(ActionEvent e)
160     {
161         this.dispose();
162     }
163 
164     public void changedUpdate(DocumentEvent e)
165     {
166         if (object == null || !this.field.getText().equals(object.getIdentifier())) this.okButton.setEnabled(true);
167     }
168 
169     public void insertUpdate(DocumentEvent e)
170     {
171         if (object == null || !this.field.getText().equals(object.getIdentifier())) this.okButton.setEnabled(true);
172     }
173 
174     public void removeUpdate(DocumentEvent e)
175     {
176         if (object == null || !this.field.getText().equals(object.getIdentifier())) this.okButton.setEnabled(true);
177     }
178 
179     /***
180      * Returns the object.
181      * 
182      * @return PreferenceGroup
183      */
184     public PreferenceGroup getObject()
185     {
186         return object;
187     }
188 
189 }