File Source: ProfileManagerDialog.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog__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.addons.ui_swing.components.ListScroller;
27 import com.dmdirc.addons.ui_swing.components.StandardDialog;
28 import com.dmdirc.addons.ui_swing.components.renderers.ProfileListCellRenderer;
29 import com.dmdirc.addons.ui_swing.components.text.TextLabel;
30 import com.dmdirc.addons.ui_swing.dialogs.NewServerDialog;
31 import com.dmdirc.config.Identity;
32 import com.dmdirc.config.IdentityManager;
33
34 import java.awt.Dialog.ModalityType;
35 import java.awt.event.ActionEvent;
36 import java.awt.event.ActionListener;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40
41 import javax.swing.JButton;
42 import javax.swing.JList;
43 import javax.swing.JOptionPane;
44 import javax.swing.JScrollPane;
45 import javax.swing.ListSelectionModel;
46 import javax.swing.WindowConstants;
47 import javax.swing.event.ListSelectionEvent;
48 import javax.swing.event.ListSelectionListener;
49
50 import net.miginfocom.swing.MigLayout;
51
52 /** Profile editing dialog. */
53 public final class ProfileManagerDialog extends StandardDialog implements ActionListener,
54 ListSelectionListener {
55
56 /**
57 * A version number for this class. It should be changed whenever the class
58 * structure is changed (or anything else that would prevent serialized
59 * objects being unserialized with the new class).
60 */
61 private static final long serialVersionUID = 2;
62 /** Previously created instance of ProfileEditorDialog. */
63 private static volatile ProfileManagerDialog me;
64 /** Profile list. */
65 private JList profileList;
66 /** Profile list mode. */
67 private ProfileListModel model;
68 /** Profile detail panel. */
69 private ProfileDetailPanel details;
70 /** Info label. */
71 private TextLabel infoLabel;
72 /** Add button. */
73 private JButton addButton;
74 /** Delete button. */
75 private JButton deleteButton;
76 /** Selected index. */
77 private int selectedIndex;
78 /** Deleted profiles. */
79 private final List<Profile> deletedProfiles;
80 /** main frame. */
81 private MainFrame mainFrame;
82
83 /**
84 * Creates a new instance of ProfileEditorDialog.
85 *
86 * @param parentWindow main frame
87 */
88 private ProfileManagerDialog(final MainFrame mainFrame) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog(MainFrame)
*
* Presumptions:
* init'ed(java.awt.Dialog$ModalityType.MODELESS)
* this.addButton != null
* this.deleteButton != null
* this.model != null
* this.model.profiles != null
* ...
*
* Postconditions:
* init'ed(this.addButton)
* init'ed(this.deleteButton)
* this.deletedProfiles == &new ArrayList(ProfileManagerDialog#1)
* init'ed(this.details)
* init'ed(this.infoLabel)
* this.mainFrame == mainFrame
* init'ed(this.mainFrame)
* this.model != null
* this.profileList != null
* init'ed(this.selectedIndex)
* ...
*
* Test Vectors:
* java.util.List:size(...)@60: {-231..0}, {1..232-1}
*/
89 super(mainFrame, ModalityType.MODELESS);
90 this.mainFrame = mainFrame;
91 deletedProfiles = new ArrayList<Profile>();
92
93 initComponents();
94
95 layoutComponents();
96
97 addListeners();
98
99 if (model.getSize() > 0) {
100 profileList.setSelectedIndex(0);
101 } else {
102 selectedIndex = -1;
103 }
104 }
105
106 /**
107 * Creates the dialog if one doesn't exist, and displays it.
108 *
109 * @param mainFrame Main frame
110 */
111 public static void showProfileManagerDialog(final MainFrame mainFrame) {
/*
P/P * Method: void showProfileManagerDialog(MainFrame)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ProfileManagerDialog(getProfileManagerDialog#1)}
* me != null
* new ArrayList(ProfileManagerDialog#1) num objects <= 1
* new ProfileManagerDialog(getProfileManagerDialog#1) num objects == new ArrayList(ProfileManagerDialog#1) num objects
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).addButton)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).deleteButton)
* new ProfileManagerDialog(getProfileManagerDialog#1).deletedProfiles == &new ArrayList(ProfileManagerDialog#1)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).details)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).infoLabel)
* new ProfileManagerDialog(getProfileManagerDialog#1).mainFrame == mainFrame
* ...
*/
112 me = getProfileManagerDialog(mainFrame);
113
114 me.pack();
115 me.setLocationRelativeTo(mainFrame);
116 me.setVisible(true);
117 me.requestFocusInWindow();
118 }
119
120 /**
121 * Returns the current instance of the ProfileManagerDialog.
122 *
123 * @param mainFrame Main frame
124 *
125 * @return The current ProfileManagerDialog instance
126 */
127 public static ProfileManagerDialog getProfileManagerDialog(final MainFrame mainFrame) {
/*
P/P * Method: ProfileManagerDialog getProfileManagerDialog(MainFrame)
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == One-of{old me, &new ProfileManagerDialog(getProfileManagerDialog#1)}
* me != null
* return_value == One-of{old me, &new ProfileManagerDialog(getProfileManagerDialog#1)}
* return_value != null
* 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)
* new ProfileManagerDialog(getProfileManagerDialog#1).deletedProfiles == &new ArrayList(ProfileManagerDialog#1)
* init'ed(new ProfileManagerDialog(getProfileManagerDialog#1).details)
* ...
*/
128 synchronized (ProfileManagerDialog.class) {
129 if (me == null) {
130 me = new ProfileManagerDialog(mainFrame);
131 }
132 }
133
134 return me;
135 }
136
137 /** Initialises the components. */
138 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Preconditions:
* init'ed(this.mainFrame)
*
* Presumptions:
* java.awt.dnd.DragSource:getDefaultDragSource(...)@99 != null
*
* Postconditions:
* this.addButton == &new JButton(initComponents#6)
* this.deleteButton == &new JButton(initComponents#7)
* this.details == &new ProfileDetailPanel(initComponents#5)
* this.infoLabel == &new TextLabel(initComponents#8)
* this.model == &new ProfileListModel(initComponents#3)
* this.details.model == &new ProfileListModel(initComponents#3)
* this.profileList == &new JList(initComponents#4)
* new ArrayList(ProfileListModel#1) num objects == 1
* new DefaultListModel(ProfileDetailPanel#1) num objects == 1
* new DropTarget(ReorderableJList#3) num objects == 1
* ...
*/
139 setTitle("Profile Editor");
140 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
141 setResizable(false);
142
143 orderButtons(new JButton(), new JButton());
144
145 model = new ProfileListModel();
146 profileList = new JList(model);
147 details = new ProfileDetailPanel(model, mainFrame);
148 addButton = new JButton("Add");
149 deleteButton = new JButton("Delete");
150 infoLabel =
151 new TextLabel("Profiles describe information needed to " +
152 "connect to a server. You can use a different profile for " +
153 "each connection.");
154
155 profileList.setCellRenderer(new ProfileListCellRenderer());
156 profileList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
157 new ListScroller(profileList);
158
159 populateList();
160 }
161
162 /** Lays out the dialog. */
163 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.addButton)
* init'ed(this.deleteButton)
* init'ed(this.details)
* init'ed(this.infoLabel)
* init'ed(this.profileList)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getContentPane(...)@164 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getContentPane(...)@166 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getContentPane(...)@167 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getContentPane(...)@169 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getContentPane(...)@170 != null
* ...
*/
164 getContentPane().setLayout(new MigLayout("fill, wmin 700, wmax 700"));
165
166 getContentPane().add(infoLabel, "wrap, spanx 2");
167 getContentPane().add(new JScrollPane(profileList),
168 "growy, wmin 200, wmax 200");
169 getContentPane().add(details, "grow, wrap");
170 getContentPane().add(addButton, "wrap, wmin 200, wmax 200");
171 getContentPane().add(deleteButton, "left, wmin 200, wmax 200");
172 getContentPane().add(getLeftButton(), "split, right, sg button");
173 getContentPane().add(getRightButton(), "right, sg button");
174
175 pack();
176 }
177
178 /** Adds listeners to the components. */
179 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.addButton != null
* this.deleteButton != null
* this.profileList != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getCancelButton(...)@181 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getOkButton(...)@180 != null
*/
180 getOkButton().addActionListener(this);
181 getCancelButton().addActionListener(this);
182
183 addButton.addActionListener(this);
184 deleteButton.addActionListener(this);
185
186 profileList.addListSelectionListener(this);
187 }
188
189 /** Populates the profile list. */
190 public void populateList() {
/*
P/P * Method: void populateList()
*
* Preconditions:
* this.model != null
* this.model.profiles != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getProfiles(...)@193 != null
* java.util.Iterator:next(...)@194 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@194: {0}, {1}
*/
191 final String profileString = "profile";
192 model.clear();
193 final List<Identity> profiles = IdentityManager.getProfiles();
194 for (Identity profile : profiles) {
195 model.add(new Profile(profile.getName(),
196 profile.getOptionList(profileString, "nicknames"),
197 profile.getOption(profileString, "realname"),
198 profile.getOption(profileString, "ident"),
199 false));
200 }
201 }
202
203 /** Saves the profile list. */
204 private void save() {
/*
P/P * Method: void save()
*
* Preconditions:
* init'ed(com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.me)
* this.details != null
* this.details.ident != null
* this.details.ident.textField != null
* (soft) init'ed(me)
* (soft) init'ed(this.details.profile.ident)
* (soft) this.details.profile.name != null
* (soft) this.details.profile.nicknames != null
* (soft) this.details.profile.realname != null
* (soft) this.deletedProfiles != null
* ...
*
* Presumptions:
* getNewServerDialog(...).identityField != null
* getNewServerDialog(...)@220 init'ed
* java.util.Iterator:next(...)@210 != null
* java.util.Iterator:next(...)@213 != null
*
* Postconditions:
* com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.me == One-of{old com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.me, &new NewServerDialog(getNewServerDialog#1)}
* init'ed(com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.me)
* me == One-of{old me, null}
* init'ed(me)
* init'ed(this.details.profile.ident)
* possibly_updated(this.details.profile.modified)
* init'ed(this.details.profile.name)
* this.details.profile.nicknames == One-of{old this.details.profile.nicknames, &new ArrayList(save#1*)}
* this.details.profile.nicknames != null
* possibly_updated(this.details.profile.oldName)
* ...
*
* Test Vectors:
* com/dmdirc/addons/ui_swing/dialogs/NewServerDialog.me: Addr_Set{null}, Inverse{null}
* java.util.Iterator:hasNext(...)@209: {0}, {1}
* java.util.Iterator:hasNext(...)@213: {0}, {1}
*/
205 if (details.validateDetails()) {
206 details.save();
207 final Iterator<Profile> it = model.iterator();
208
209 while (it.hasNext()) {
210 it.next().save();
211 }
212
213 for (Profile profile : deletedProfiles) {
214 profile.delete();
215 }
216
217 dispose();
218 }
219 if (NewServerDialog.isNewServerDialogShowing()) {
220 NewServerDialog.getNewServerDialog(mainFrame).populateProfiles();
221 }
222 }
223
224 /**
225 * {@inheritDoc}
226 *
227 * @param e Action event
228 */
229 @Override
230 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* (soft) init'ed(me)
* (soft) init'ed(this.details.profile.ident)
* (soft) init'ed(this.details.profile.name)
* (soft) init'ed(this.details.profile.nicknames)
* (soft) init'ed(this.details.profile.realname)
* (soft) init'ed(this.addButton)
* (soft) init'ed(this.deleteButton)
* (soft) this.deletedProfiles != null
* (soft) this.model != null
* ...
*
* Presumptions:
* java.awt.event.ActionEvent:getSource(...)@231 != null
* java.awt.event.ActionEvent:getSource(...)@233 != null
* java.awt.event.ActionEvent:getSource(...)@235 != null
* java.awt.event.ActionEvent:getSource(...)@237 != null
* javax.swing.JList:getModel(...)@245 != null
* ...
*
* Postconditions:
* me == One-of{old me, null}
* init'ed(me)
* this.details.profile.ident == old this.details.profile.ident
* this.details.profile.modified == old this.details.profile.modified
* this.details.profile.name == old this.details.profile.name
* this.details.profile.nicknames == old this.details.profile.nicknames
* this.details.profile.oldName == old this.details.profile.oldName
* this.details.profile.realname == old this.details.profile.realname
* new ArrayList(save#1*) num objects == undefined
* new ArrayList(save#1*) num objects == 0, if init'ed
*
* Test Vectors:
* java.lang.Object:equals(...)@231: {0}, {1}
* java.lang.Object:equals(...)@233: {0}, {1}
* java.lang.Object:equals(...)@235: {0}, {1}
* java.lang.Object:equals(...)@237: {0}, {1}
* javax.swing.JList:getSelectedIndex(...)@242: {1..232-2}, {-231..0}
* javax.swing.JOptionPane:showConfirmDialog(...)@237: {-231..-1, 1..232-1}, {0}
* javax.swing.ListModel:getSize(...)@245: {2..232-1}, {0}
*/
231 if (e.getSource().equals(getOkButton())) {
232 save();
233 } else if (e.getSource().equals(getCancelButton())) {
234 dispose();
235 } else if (e.getSource().equals(addButton)) {
236 addProfile();
237 } else if (e.getSource().equals(deleteButton) && JOptionPane.showConfirmDialog(this,
238 "Are you sure you want to delete this profile?",
239 "Delete Confirmaton", JOptionPane.YES_NO_OPTION) ==
240 JOptionPane.YES_OPTION) {
241 final Profile selectedProfile = (Profile) profileList.getSelectedValue();
242 int selected = profileList.getSelectedIndex();
243 deletedProfiles.add(selectedProfile);
244 model.remove(selectedProfile);
245 final int profilesSize = profileList.getModel().getSize();
246 if (profilesSize == 0) {
247 selected = -1;
248 } else if (selected >= profilesSize) {
249 selected = profilesSize - 1;
250 } else if (selected <= 0) {
251 selected = 0;
252 }
253 profileList.setSelectedIndex(selected);
254 }
255 }
256
257 /**
258 * Prompts and adds a new profile
259 */
260 private void addProfile() {
/*
P/P * Method: void addProfile()
*
* Preconditions:
* this.model != null
* this.model.profiles != null
* this.profileList != null
*
* Presumptions:
* java.lang.System:getProperty(...)@261 != null
*/
261 final String nick = System.getProperty("user.name").replace(' ', '_');
262
263 String name = "New Profile";
264 int i = 1;
265
266 while (model.contains(name)) {
267 name = "New Profile " + ++i;
268 }
269
270 final Profile profile = new Profile(name, nick, nick);
271 model.add(profile);
272 profileList.setSelectedIndex(model.indexOf(profile));
273 }
274
275 /** {@inheritDoc} */
276 @Override
277 public void valueChanged(final ListSelectionEvent e) {
/*
P/P * Method: void valueChanged(ListSelectionEvent)
*
* Preconditions:
* e != null
* this.model != null
* this.model.profiles != null
* this.profileList != null
* (soft) init'ed(this.details.profile)
* (soft) init'ed(this.details.profile.ident)
* (soft) this.details.profile.name != null
* (soft) this.details.profile.nicknames != null
* (soft) this.details.profile.realname != null
* (soft) init'ed(this.selectedIndex)
* ...
*
* Presumptions:
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getOkButton(...)@291 != null
* com.dmdirc.addons.ui_swing.dialogs.profiles.ProfileManagerDialog:getOkButton(...)@293 != null
* javax.swing.JList:getSelectedValue(...).nicknames@283 != null
* this.details.profile.name != null
* this.details.profile.nicknames != null
* ...
*
* Postconditions:
* init'ed(this.details.profile)
* init'ed(this.details.profile.ident)
* possibly_updated(this.details.profile.modified)
* init'ed(this.details.profile.name)
* this.details.profile.nicknames != null
* possibly_updated(this.details.profile.oldName)
* init'ed(this.details.profile.realname)
* init'ed(this.selectedIndex)
* new ArrayList(save#1) num objects <= 1
*
* Test Vectors:
* java.util.List:size(...)@60: {-231..-1, 1..232-1}, {0}
* javax.swing.JList:getSelectedIndex(...)@284: {-231..-2, 0..232-1}, {-1}
* javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@278: {0}, {1}
* javax.swing.event.ListSelectionEvent:getValueIsAdjusting(...)@281: {1}, {0}
*/
278 if (e.getValueIsAdjusting() && !details.validateDetails()) {
279 profileList.setSelectedIndex(selectedIndex);
280 }
281 if (!e.getValueIsAdjusting()) {
282 details.save();
283 details.setProfile((Profile) profileList.getSelectedValue());
284 if (profileList.getSelectedIndex() == -1) {
285 deleteButton.setEnabled(false);
286 } else {
287 deleteButton.setEnabled(true);
288 }
289 }
290 if (model.getSize() == 0) {
291 getOkButton().setEnabled(false);
292 } else {
293 getOkButton().setEnabled(true);
294 }
295 selectedIndex = profileList.getSelectedIndex();
296 }
297
298 /** {@inheritDoc} */
299 @Override
300 public void dispose() {
/*
P/P * Method: void dispose()
*
* Preconditions:
* init'ed(me)
*
* Postconditions:
* me == null
*
* Test Vectors:
* me: Inverse{null}, Addr_Set{null}
*/
301 if (me == null) {
302 return;
303 }
304 synchronized (me) {
305 super.dispose();
306 me = null;
307 }
308 }
309 }
SofCheck Inspector Build Version : 2.17854
| ProfileManagerDialog.java |
2009-Jun-25 01:54:24 |
| ProfileManagerDialog.class |
2009-Sep-02 17:04:16 |