1 package com.explosion.utilities.preferences.groups.dialogs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Dimension;
24 import java.awt.Frame;
25 import java.awt.GridBagConstraints;
26 import java.awt.GridBagLayout;
27 import java.awt.Insets;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.MouseEvent;
30 import java.util.Vector;
31
32 import javax.swing.BorderFactory;
33 import javax.swing.JButton;
34 import javax.swing.JDialog;
35 import javax.swing.JList;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollPane;
39
40 import com.explosion.utilities.GeneralUtils;
41 import com.explosion.utilities.exception.ExceptionManagerFactory;
42 import com.explosion.utilities.preferences.groups.PreferenceGroup;
43 import com.explosion.utilities.preferences.groups.PreferenceGroupManager;
44
45 /***
46 * @author Stephen Cowx Date created:@25-Jan-2003
47 * This class provides a list of all of the preferenceGroups for a particular
48 * PreferenceGroupManager.
49 *
50 * The dialog procides facilities for adding new elements to the group,
51 * deleting, editing and existing elements.
52 *
53 */
54 public class PreferenceGroupsEditorDialog extends JDialog
55 {
56
57 private Frame owner;
58
59 private JPanel buttonPanel = new JPanel();
60
61 private JList persistentObjectList;
62
63 private JScrollPane scrollPane = new JScrollPane();
64
65 private JButton removeButton = new JButton("Delete");
66
67 private JButton addButton = new JButton("New");
68
69 private JButton renameButton = new JButton("Rename");
70
71 private JButton editButton = new JButton("Edit");
72
73 private JButton duplicateButton = new JButton("Duplicate");
74
75 private JButton exitButton = new JButton("Exit");
76
77 private JButton okButton = new JButton("Done");
78
79 private PreferenceGroupManager objectManager;
80
81 private String initialSelection = null;
82
83 private Vector trackedButtons = new Vector();
84
85 private Vector buttons = new Vector();
86
87 /***
88 * Constructor
89 */
90 public PreferenceGroupsEditorDialog(Frame owner, String title, boolean modal, PreferenceGroupManager objectManager) throws Exception
91 {
92 super(owner, title, modal);
93 this.owner = owner;
94 this.objectManager = objectManager;
95 init();
96 }
97
98 public PreferenceGroupsEditorDialog(Frame owner, String title, boolean modal, PreferenceGroupManager objectManager, String initialSelection) throws Exception
99 {
100 super(owner, title, modal);
101 this.owner = owner;
102 this.objectManager = objectManager;
103 this.initialSelection = initialSelection;
104 init();
105 }
106
107 /***
108 * This method sets out the look and feel for this component
109 */
110 public void init() throws Exception
111 {
112 this.setSize(new Dimension(300, 200));
113 this.getContentPane().setLayout(new GridBagLayout());
114
115 addButton.addActionListener(new java.awt.event.ActionListener()
116 {
117
118 public void actionPerformed(ActionEvent e)
119 {
120 addButton_actionPerformed(e);
121 }
122 });
123 editButton.addActionListener(new java.awt.event.ActionListener()
124 {
125
126 public void actionPerformed(ActionEvent e)
127 {
128 editButton_actionPerformed(e);
129 }
130 });
131 removeButton.addActionListener(new java.awt.event.ActionListener()
132 {
133
134 public void actionPerformed(ActionEvent e)
135 {
136 removeButton_actionPerformed(e);
137 }
138 });
139 renameButton.addActionListener(new java.awt.event.ActionListener()
140 {
141
142 public void actionPerformed(ActionEvent e)
143 {
144 renameButton_actionPerformed(e);
145 }
146 });
147 okButton.addActionListener(new java.awt.event.ActionListener()
148 {
149
150 public void actionPerformed(ActionEvent e)
151 {
152 okButton_actionPerformed(e);
153 }
154 });
155 duplicateButton.addActionListener(new java.awt.event.ActionListener()
156 {
157
158 public void actionPerformed(ActionEvent e)
159 {
160 duplicateButton_actionPerformed(e);
161 }
162 });
163
164 trackedButtons.addElement(removeButton);
165 trackedButtons.addElement(duplicateButton);
166
167 buttons.addElement(addButton);
168 buttons.addElement(editButton);
169 buttons.addElement(duplicateButton);
170 buttons.addElement(renameButton);
171 buttons.addElement(removeButton);
172 buttons.addElement(okButton);
173
174 buttonPanel.setLayout(new GridBagLayout());
175 buttonPanel.setBorder(BorderFactory.createEtchedBorder());
176 for (int i = 0; i < buttons.size(); i++)
177 buttonPanel.add(((JButton) buttons.elementAt(i)), new GridBagConstraints(0, i, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
178
179 objectManager.sort();
180 persistentObjectList = new JList(objectManager.getIdentifiers());
181 persistentObjectList.setBorder(BorderFactory.createEtchedBorder());
182 persistentObjectList.addMouseListener(new java.awt.event.MouseAdapter()
183 {
184
185 public void mouseClicked(MouseEvent e)
186 {
187 connectionList_mouseClicked(e);
188 }
189 });
190 scrollPane.getViewport().add(persistentObjectList);
191
192 if (persistentObjectList == null)
193 persistentObjectList = new JList();
194 else
195 persistentObjectList.setSelectedIndex(0);
196
197 this.getContentPane().add(scrollPane, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 10, 10));
198 this.getContentPane().add(buttonPanel, new GridBagConstraints(1, 0, 1, 1, 0.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 10, 10));
199
200 if (persistentObjectList.getModel().getSize() > 0)
201 {
202 if (initialSelection == null)
203 persistentObjectList.setSelectedIndex(0);
204 else
205 persistentObjectList.setSelectedValue(initialSelection, true);
206 }
207
208 checkEnabled();
209
210 }
211
212 protected void duplicateButton_actionPerformed(ActionEvent e)
213 {
214 try
215 {
216 objectManager.duplicateGroup(persistentObjectList.getSelectedValue().toString(), "Copy of " + persistentObjectList.getSelectedValue().toString());
217 refresh();
218 } catch (Exception e1)
219 {
220 ExceptionManagerFactory.getExceptionManager().manageException(e1, "Exception caught while duplicating descriptor.");
221 }
222 }
223
224 void addButton_actionPerformed(ActionEvent e)
225 {
226 try
227 {
228 PreferenceGroupNameEditDialog dialog = new PreferenceGroupNameEditDialog(this, "Enter the name", null, objectManager);
229 dialog.setSize(PreferenceGroupNameEditDialog.nameEditorDimension);
230 GeneralUtils.centreWindowInParent(dialog);
231 dialog.setVisible(true);
232
233 refresh();
234
235 if (dialog.getObject() != null)
236 {
237 persistentObjectList.setSelectedValue(dialog.getObject().getIdentifier(), true);
238 editButton_actionPerformed(e);
239 }
240 } catch (Exception ex)
241 {
242 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
243 }
244 }
245
246 void okButton_actionPerformed(ActionEvent e)
247 {
248 try
249 {
250 objectManager.saveAll();
251 this.setVisible(false);
252 } catch (Exception ex)
253 {
254 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
255 }
256 }
257
258 void editButton_actionPerformed(ActionEvent e)
259 {
260 String selectedObject = persistentObjectList.getSelectedValue().toString();
261 PreferenceGroup preferenceGroup = objectManager.getGroup(selectedObject);
262 boolean isEditable = preferenceGroup.isEditable();
263
264 PreferenceGroupEditDialog dialog = new PreferenceGroupEditDialog(this, selectedObject, isEditable);
265 dialog.loadPreferences(objectManager.getGroup(persistentObjectList.getSelectedValue().toString()));
266 dialog.setSize(PreferenceGroupEditDialog.editorDimension);
267 try
268 {
269 GeneralUtils.centreWindowInParent(dialog);
270 } catch (Exception ex)
271 {
272 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
273 }
274 dialog.setVisible(true);
275 }
276
277 void connectionList_mouseClicked(MouseEvent e)
278 {
279 checkEnabled();
280 }
281
282 void removeButton_actionPerformed(ActionEvent e)
283 {
284 try
285 {
286 int result = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete " + persistentObjectList.getSelectedValue().toString() + " ?", "Delete", JOptionPane.YES_NO_OPTION,
287 JOptionPane.QUESTION_MESSAGE);
288 if (result == JOptionPane.YES_OPTION)
289 {
290 objectManager.deleteGroup(persistentObjectList.getSelectedValue().toString());
291 refresh();
292 }
293 } catch (Exception ex)
294 {
295 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
296 }
297 }
298
299 void renameButton_actionPerformed(ActionEvent e)
300 {
301 try
302 {
303 PreferenceGroup object = objectManager.getGroup(persistentObjectList.getSelectedValue().toString());
304 PreferenceGroupNameEditDialog dialog = new PreferenceGroupNameEditDialog(this, "Rename " + object.getIdentifier(), object, objectManager);
305 dialog.setSize(PreferenceGroupNameEditDialog.nameEditorDimension);
306 GeneralUtils.centreWindowInParent(dialog);
307 dialog.setVisible(true);
308 refresh();
309 } catch (Exception ex)
310 {
311 com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(ex, null);
312 }
313 }
314
315 public void refresh()
316 {
317 objectManager.sort();
318 Vector names = objectManager.getIdentifiers();
319 persistentObjectList.setListData(names);
320
321 if (names.size() > 0) persistentObjectList.setSelectedIndex(0);
322
323 checkEnabled();
324 }
325
326 private void checkEnabled()
327 {
328 if (persistentObjectList.getModel().getSize() < 1)
329 {
330 this.removeButton.setEnabled(false);
331 this.renameButton.setEnabled(false);
332 this.editButton.setEnabled(false);
333
334 for (int t = 0; t < trackedButtons.size(); t++)
335 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
336
337 return;
338 }
339
340 if (objectManager.getGroup(persistentObjectList.getSelectedValue().toString()).isEditable())
341 {
342 this.removeButton.setEnabled(true);
343 this.renameButton.setEnabled(true);
344 this.editButton.setText("Edit");
345
346 for (int t = 0; t < trackedButtons.size(); t++)
347 ((JButton) trackedButtons.elementAt(t)).setEnabled(true);
348
349 }
350 else
351 {
352 this.removeButton.setEnabled(false);
353 this.renameButton.setEnabled(false);
354 this.editButton.setText("View");
355
356 for (int t = 0; t < trackedButtons.size(); t++)
357 ((JButton) trackedButtons.elementAt(t)).setEnabled(false);
358
359 }
360 }
361
362 public String getSelectedValue()
363 {
364 if (persistentObjectList.getSelectedValue() == null)
365 return "";
366 else
367 return (String) persistentObjectList.getSelectedValue();
368 }
369
370 public void addButton(JButton button, boolean addToTrackedButtons)
371 {
372 buttons.insertElementAt(button, buttons.size() - 1);
373
374 if (addToTrackedButtons) trackedButtons.addElement(button);
375
376 buttonPanel.removeAll();
377 for (int i = 0; i < buttons.size(); i++)
378 buttonPanel.add(((JButton) buttons.elementAt(i)), new GridBagConstraints(0, i, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0));
379 buttonPanel.revalidate();
380
381 refresh();
382 }
383
384 /***
385 * Returns the persistentObjectList.
386 *
387 * @return JList
388 */
389 public JList getPersistentObjectList()
390 {
391 return persistentObjectList;
392 }
393
394 }