File Source: ProfileDetailPanel.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileDetailPanel__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.ui_swing.dialogs.profiles;
24
25 import com.dmdirc.addons.ui_swing.MainFrame;
26 import com.dmdirc.config.prefs.validator.FileNameValidator;
27 import com.dmdirc.config.prefs.validator.IdentValidator;
28 import com.dmdirc.config.prefs.validator.NicknameValidator;
29 import com.dmdirc.config.prefs.validator.NotEmptyValidator;
30 import com.dmdirc.config.prefs.validator.ValidationResponse;
31 import com.dmdirc.addons.ui_swing.components.StandardInputDialog;
32 import com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList;
33 import com.dmdirc.addons.ui_swing.components.validating.ValidatingJTextField;
34
35 import com.dmdirc.config.prefs.validator.ValidatorChain;
36 import java.awt.Dialog.ModalityType;
37 import java.awt.event.ActionEvent;
38 import java.awt.event.ActionListener;
39 import java.util.ArrayList;
40 import java.util.Enumeration;
41
42 import javax.swing.DefaultListModel;
43 import javax.swing.JButton;
44 import javax.swing.JLabel;
45 import javax.swing.JOptionPane;
46 import javax.swing.JPanel;
47 import javax.swing.JScrollPane;
48 import javax.swing.event.ListDataEvent;
49 import javax.swing.event.ListDataListener;
50 import javax.swing.event.ListSelectionEvent;
51 import javax.swing.event.ListSelectionListener;
52
53 import net.miginfocom.swing.MigLayout;
54
55 /** Profile detail panel. */
/*
P/P * Method: Profile access$300(ProfileDetailPanel)
*
* Preconditions:
* x0 != null
* init'ed(x0.profile)
*
* Postconditions:
* return_value == x0.profile
* init'ed(return_value)
*/
56 public final class ProfileDetailPanel extends JPanel implements ActionListener,
57 ListSelectionListener, ListDataListener {
58
59 /**
60 * A version number for this class. It should be changed whenever the class
61 * structure is changed (or anything else that would prevent serialized
62 * objects being unserialized with the new class).
63 */
64 private static final long serialVersionUID = 2;
65 /** Displayed profile. */
66 private Profile profile;
67 /** The profile list model. */
68 private final ProfileListModel model;
69 /** The nickname list model. */
70 private final DefaultListModel nicknameModel;
71 /** Duplicate nickname validator. */
72 private final ValidatorChain<String> validator;
73 /** Name text field. */
74 private ValidatingJTextField name;
75 /** Realname text field. */
76 private ValidatingJTextField realname;
77 /** Ident text field. */
78 private ValidatingJTextField ident;
79 /** Nicknames list. */
80 private ReorderableJList nicknames;
81 /** Add button. */
82 private JButton addButton;
83 /** Delete button. */
84 private JButton delButton;
85 /** Edit button. */
86 private JButton editButton;
87 /** Main frame. */
88 private MainFrame mainFrame;
89
90 /**
91 * Creates a new profile detail panel.
92 *
93 * @param model The list model to use to validate names
94 * @param mainFrame Main frame
95 */
96 @SuppressWarnings("unchecked")
97 public ProfileDetailPanel(final ProfileListModel model,
98 final MainFrame mainFrame) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileDetailPanel(ProfileListModel, MainFrame)
*
* Presumptions:
* java.awt.dnd.DragSource:getDefaultDragSource(...)@99 != null
*
* Postconditions:
* this.addButton == &new JButton(initMainComponents#8)
* this.delButton == &new JButton(initMainComponents#9)
* this.editButton == &new JButton(initMainComponents#10)
* this.ident == &new ValidatingJTextField(initMainComponents#5)
* this.mainFrame == mainFrame
* init'ed(this.mainFrame)
* this.model == model
* init'ed(this.model)
* this.name == &new ValidatingJTextField(initMainComponents#1)
* this.nicknameModel == &new DefaultListModel(ProfileDetailPanel#1)
* ...
*/
99 super();
100 this.mainFrame = mainFrame;
101
102 this.model = model;
103 this.nicknameModel = new DefaultListModel();
104
105 validator = new ValidatorChain(new NoDuplicatesInListValidator(
106 nicknameModel), new NicknameValidator());
107 initMainComponents();
108 layoutComponents();
109
110 clearProfile();
111 }
112
113 /** Initialises the components in the main panel. */
114 private void initMainComponents() {
/*
P/P * Method: void initMainComponents()
*
* Presumptions:
* java.awt.dnd.DragSource:getDefaultDragSource(...)@99 != null
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* this.addButton == &new JButton(initMainComponents#8)
* this.delButton == &new JButton(initMainComponents#9)
* this.editButton == &new JButton(initMainComponents#10)
* this.ident == &new ValidatingJTextField(initMainComponents#5)
* this.name == &new ValidatingJTextField(initMainComponents#1)
* this.nicknames == &new ReorderableJList(initMainComponents#7)
* this.realname == &new ValidatingJTextField(initMainComponents#3)
* new DataFlavor(ReorderableJList#4) num objects <= 1
* new DropTarget(ReorderableJList#3) num objects == 1
* new IdentValidator(initMainComponents#6) num objects == 1
* ...
*/
115 name = new ValidatingJTextField(new ProfileNameValidator());
116 realname = new ValidatingJTextField(new NotEmptyValidator());
117 ident = new ValidatingJTextField(new IdentValidator());
118 nicknames = new ReorderableJList(nicknameModel);
119
120 addButton = new JButton("Add");
121 delButton = new JButton("Delete");
122 editButton = new JButton("Edit");
123
124 nicknames.setVisibleRowCount(4);
125
126 nicknames.addListSelectionListener(this);
127 nicknames.getModel().addListDataListener(this);
128
129 addButton.addActionListener(this);
130 delButton.addActionListener(this);
131 editButton.addActionListener(this);
132 }
133
134 /** Lays out the components in the panel. */
135 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.addButton)
* init'ed(this.delButton)
* init'ed(this.editButton)
* init'ed(this.ident)
* init'ed(this.name)
* init'ed(this.nicknames)
* init'ed(this.realname)
*/
136 setLayout(new MigLayout("fillx"));
137
138 add(new JLabel("Name:"));
139 add(name, "growx, pushx, wrap");
140
141 add(new JLabel("Nicknames:"));
142 add(new JScrollPane(nicknames), "grow, pushx, wrap");
143 add(addButton, "skip 1, split 3, sg button, growx");
144 add(editButton, "sg button, growx");
145 add(delButton, "sg button, growx, wrap");
146
147 add(new JLabel("Realname:"));
148 add(realname, "growx, pushx, wrap");
149
150 add(new JLabel("Ident:"));
151 add(ident, "growx, pushx, wrap");
152 }
153
154 /**
155 * Sets the profile for the detail panel.
156 *
157 * @param profile new Profile for the detail panel
158 */
159 public void setProfile(final Profile profile) {
/*
P/P * Method: void setProfile(Profile)
*
* Preconditions:
* (soft) init'ed(profile.ident)
* (soft) init'ed(profile.name)
* (soft) profile.nicknames != null
* (soft) init'ed(profile.realname)
* (soft) this.addButton != null
* (soft) this.delButton != null
* (soft) this.editButton != null
* (soft) this.ident != null
* (soft) this.ident.errorIcon != null
* (soft) this.ident.textField != null
* ...
*
* Postconditions:
* this.profile == profile
* init'ed(this.profile)
*/
160 this.profile = profile;
161 updateProfile();
162 }
163
164 /** Updates this detail panel. */
165 public void updateProfile() {
/*
P/P * Method: void updateProfile()
*
* Preconditions:
* init'ed(this.profile)
* (soft) this.addButton != null
* (soft) this.delButton != null
* (soft) this.editButton != null
* (soft) this.ident != null
* (soft) this.ident.errorIcon != null
* (soft) this.ident.textField != null
* (soft) this.ident.validator != null
* (soft) this.name != null
* (soft) this.name.errorIcon != null
* ...
*
* Presumptions:
* javax.swing.JList:getModel(...)@118 != null
*
* Test Vectors:
* this.profile: Inverse{null}, Addr_Set{null}
* java.util.Iterator:hasNext(...)@176: {0}, {1}
*/
166 if (profile == null) {
167 clearProfile();
168 return;
169 }
170
171 name.setText(profile.getName());
172 realname.setText(profile.getRealname());
173 ident.setText(profile.getIdent());
174
175 nicknames.getModel().clear();
176 for (String nickname : profile.getNicknames()) {
177 nicknames.getModel().addElement(nickname);
178 }
179
180 name.setEnabled(true);
181 realname.setEnabled(true);
182 ident.setEnabled(true);
183 nicknames.setEnabled(true);
184 addButton.setEnabled(true);
185 }
186
187 /** Clears this detail panel. */
188 public void clearProfile() {
/*
P/P * Method: void clearProfile()
*
* Preconditions:
* this.addButton != null
* this.delButton != null
* this.editButton != null
* this.ident != null
* this.ident.errorIcon != null
* this.ident.textField != null
* this.name != null
* this.name.errorIcon != null
* this.name.textField != null
* this.nicknames != null
* ...
*
* Presumptions:
* javax.swing.JList:getModel(...)@118 != null
*/
189 name.setText("");
190 realname.setText("");
191 ident.setText("");
192 nicknames.getModel().clear();
193
194 name.setEnabled(false);
195 realname.setEnabled(false);
196 ident.setEnabled(false);
197 nicknames.setEnabled(false);
198 addButton.setEnabled(false);
199 delButton.setEnabled(false);
200 editButton.setEnabled(false);
201 }
202
203 /** Saves the detail panels details to the profile. */
204 public void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* init'ed(this.profile)
* (soft) init'ed(this.profile.ident)
* (soft) this.profile.name != null
* (soft) this.profile.nicknames != null
* (soft) this.profile.realname != null
* (soft) this.ident != null
* (soft) this.ident.textField != null
* (soft) this.name != null
* (soft) this.name.textField != null
* (soft) this.nicknames != null
* ...
*
* Presumptions:
* javax.swing.DefaultListModel:elements(...)@213 != null
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* init'ed(this.profile.ident)
* possibly_updated(this.profile.modified)
* init'ed(this.profile.name)
* this.profile.nicknames == One-of{old this.profile.nicknames, &new ArrayList(save#1)}
* this.profile.nicknames != null
* this.profile.oldName == One-of{old this.profile.oldName, old this.profile.name}
* init'ed(this.profile.realname)
* new ArrayList(save#1) num objects <= 1
*
* Test Vectors:
* this.profile: Inverse{null}, Addr_Set{null}
* java.util.Enumeration:hasMoreElements(...)@215: {0}, {1}
*/
205 if (profile == null) {
206 return;
207 }
208
209 profile.setName(name.getText());
210 profile.setRealname(realname.getText());
211 profile.setIdent(ident.getText());
212 profile.setNicknames(new ArrayList<String>());
213 final Enumeration<?> enumeration =
214 nicknames.getModel().elements();
215 while (enumeration.hasMoreElements()) {
216 profile.addNickname((String) enumeration.nextElement());
217 }
218 }
219
220 /**
221 * Validates the current details.
222 *
223 * @return Validation Result
224 */
225 public boolean validateDetails() {
/*
P/P * Method: bool validateDetails()
*
* Preconditions:
* this.ident != null
* this.ident.textField != null
* (soft) this.ident.validator != null
* (soft) this.name != null
* (soft) this.name.textField != null
* (soft) this.name.validator != null
* (soft) this.nicknames != null
* (soft) this.realname != null
* (soft) this.realname.textField != null
* (soft) this.realname.validator != null
*
* Presumptions:
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* javax.swing.DefaultListModel:getSize(...)@226: {1..232-1}, {-231..0}
*/
226 if (!ident.validateText() || !realname.validateText() ||
227 !name.validateText() || nicknames.getModel().getSize() <= 0) {
228 return false;
229 }
230 return true;
231 }
232
233 /**
234 * {@inheritDoc}
235 *
236 * @param e Action event
237 */
238 @Override
239 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* init'ed(this.addButton)
* (soft) init'ed(com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me)
* (soft) init'ed(this.delButton)
* (soft) init'ed(this.editButton)
* (soft) init'ed(this.mainFrame)
* (soft) this.nicknames != null
*
* Presumptions:
* init'ed(java.awt.Dialog$ModalityType.DOCUMENT_MODAL)
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me == One-of{old com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me, &new ProfileManagerDialog(getProfileManagerDialog#1)}
* init'ed(com/dmdirc/addons/ui_swing/dialogs/profiles/ProfileManagerDialog.me)
* new ArrayList(ProfileManagerDialog#1) num objects <= 1
* new ProfileManagerDialog(getProfileManagerDialog#1) num objects <= 1
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).addButton)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).deleteButton)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).deletedProfiles)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).details)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).infoLabel)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).mainFrame)
* ...
*
* Test Vectors:
* javax.swing.JOptionPane:showConfirmDialog(...)@294: {-231..-1, 1..232-1}, {0}
*/
240 if (e.getSource() == addButton) {
241 new StandardInputDialog(ProfileManagerDialog.getProfileManagerDialog(mainFrame),
242 ModalityType.DOCUMENT_MODAL, "New Nickname",
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileDetailPanel$1(ProfileDetailPanel, Window, Dialog$ModalityType, String, String, Validator)
*/
243 "Please enter the new nickname", validator) {
244
245 /**
246 * A version number for this class. It should be changed whenever the class
247 * structure is changed (or anything else that would prevent serialized
248 * objects being unserialized with the new class).
249 */
250 private static final long serialVersionUID = 2;
251
252 /** {@inheritDoc} */
253 @Override
254 public boolean save() {
/*
P/P * Method: bool save()
*
* Preconditions:
* this.nicknames != null
*
* Presumptions:
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* return_value == 1
*/
255 nicknames.getModel().addElement(getText());
256 return true;
257 }
258
259 /** {@inheritDoc} */
260 @Override
261 public void cancelled() {
262 //Ignore
/*
P/P * Method: void cancelled()
*/
263 }
264 }.display();
265 } else if (e.getSource() == editButton) {
266 final StandardInputDialog dialog = new StandardInputDialog(
267 ProfileManagerDialog.getProfileManagerDialog(mainFrame),
268 ModalityType.DOCUMENT_MODAL, "Edit Nickname",
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileDetailPanel$2(ProfileDetailPanel, Window, Dialog$ModalityType, String, String, Validator)
*/
269 "Please enter the new nickname", new NicknameValidator()) {
270
271 /**
272 * A version number for this class. It should be changed whenever the class
273 * structure is changed (or anything else that would prevent serialized
274 * objects being unserialized with the new class).
275 */
276 private static final long serialVersionUID = 2;
277
278 /** {@inheritDoc} */
279 @Override
280 public boolean save() {
/*
P/P * Method: bool save()
*
* Preconditions:
* this.nicknames != null
*
* Presumptions:
* javax.swing.JList:getModel(...)@118 != null
*
* Postconditions:
* return_value == 1
*/
281 nicknames.getModel().setElementAt(
282 getText(), nicknames.getSelectedIndex());
283 return true;
284 }
285
286 /** {@inheritDoc} */
287 @Override
288 public void cancelled() {
289 //Ignore
/*
P/P * Method: void cancelled()
*/
290 }
291 };
292 dialog.setText((String) nicknames.getSelectedValue());
293 dialog.display();
294 } else if (e.getSource() == delButton && JOptionPane.showConfirmDialog(this,
295 "Are you sure you want to delete this nickname?",
296 "Delete Confirmaton", JOptionPane.YES_NO_OPTION) ==
297 JOptionPane.YES_OPTION) {
298 nicknames.getModel().removeElementAt(nicknames.getSelectedIndex());
299 }
300 }
301
302 /** {@inheritDoc} */
303 @Override
304 public void valueChanged(final ListSelectionEvent e) {
/*
P/P * Method: void valueChanged(ListSelectionEvent)
*
* Preconditions:
* e != null
* this.delButton != null
* this.editButton != null
* (soft) this.nicknames != null
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.components.reorderablelist.ReorderableJList:getSelectedIndex(...)@305: {-231..-2, 0..232-1}, {-1}
* javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@305: {1}, {0}
*/
305 if (!e.getValueIsAdjusting() && nicknames.getSelectedIndex() == -1) {
306 editButton.setEnabled(false);
307 delButton.setEnabled(false);
308 } else {
309 editButton.setEnabled(true);
310 delButton.setEnabled(true);
311 }
312 }
313
314 /** {@inheritDoc} */
315 @Override
316 public void intervalAdded(final ListDataEvent e) {
/*
P/P * Method: void intervalAdded(ListDataEvent)
*
* Preconditions:
* this.ident != null
* this.ident.textField != null
* (soft) this.ident.validator != null
* (soft) this.name != null
* (soft) this.name.textField != null
* (soft) this.name.validator != null
* (soft) this.nicknames != null
* (soft) this.realname != null
* (soft) this.realname.textField != null
* (soft) this.realname.validator != null
*/
317 validateDetails();
318 }
319
320 /** {@inheritDoc} */
321 @Override
322 public void intervalRemoved(final ListDataEvent e) {
/*
P/P * Method: void intervalRemoved(ListDataEvent)
*
* Preconditions:
* this.ident != null
* this.ident.textField != null
* (soft) this.ident.validator != null
* (soft) this.name != null
* (soft) this.name.textField != null
* (soft) this.name.validator != null
* (soft) this.nicknames != null
* (soft) this.realname != null
* (soft) this.realname.textField != null
* (soft) this.realname.validator != null
*/
323 validateDetails();
324 }
325
326 /** {@inheritDoc} */
327 @Override
328 public void contentsChanged(final ListDataEvent e) {
/*
P/P * Method: void contentsChanged(ListDataEvent)
*
* Preconditions:
* this.ident != null
* this.ident.textField != null
* (soft) this.ident.validator != null
* (soft) this.name != null
* (soft) this.name.textField != null
* (soft) this.name.validator != null
* (soft) this.nicknames != null
* (soft) this.realname != null
* (soft) this.realname.textField != null
* (soft) this.realname.validator != null
*/
329 validateDetails();
330 }
331
332 /**
333 * Ensures profile names are unique.
334 */
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileDetailPanel$ProfileNameValidator(ProfileDetailPanel, ProfileDetailPanel$1)
*/
335 private class ProfileNameValidator extends FileNameValidator {
336
337 /** {@inheritDoc} */
338 @Override
339 public ValidationResponse validate(final String object) {
/*
P/P * Method: ValidationResponse validate(String)
*
* Preconditions:
* this.model != null
* this.model.profiles != null
* (soft) init'ed(this.profile)
*
* Presumptions:
* java.util.Iterator:next(...)@340 != null
* targetprofile.name@340 != null
*
* Postconditions:
* init'ed(return_value)
* new ValidationResponse(validate#1) num objects <= 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@341: {0}, {1}
* java.util.Iterator:hasNext(...)@340: {0}, {1}
*/
340 for (Profile targetprofile : model) {
341 if (targetprofile != profile && targetprofile.getName().
342 equalsIgnoreCase(object)) {
343 return new ValidationResponse("Profile names must be unique");
344 }
345 }
346
347 return super.validate(object);
348 }
349 }
350 }
SofCheck Inspector Build Version : 2.17854
| ProfileDetailPanel.java |
2009-Jun-25 01:54:24 |
| ProfileDetailPanel.class |
2009-Sep-02 17:04:17 |
| ProfileDetailPanel$1.class |
2009-Sep-02 17:04:17 |
| ProfileDetailPanel$2.class |
2009-Sep-02 17:04:17 |
| ProfileDetailPanel$ProfileNameValidator.class |
2009-Sep-02 17:04:17 |