KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > profiles > ProfileManagerDialog


1 //The contents of this file are subject to the Mozilla Public License Version
2
//1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
//Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.gui.profiles;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.GridLayout JavaDoc;
24 import java.awt.HeadlessException JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.MouseEvent JavaDoc;
29 import java.io.File JavaDoc;
30
31 import javax.swing.BorderFactory JavaDoc;
32 import javax.swing.DefaultListModel JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JCheckBox JavaDoc;
35 import javax.swing.JComponent JavaDoc;
36 import javax.swing.JDialog JavaDoc;
37 import javax.swing.JFileChooser JavaDoc;
38 import javax.swing.JLabel JavaDoc;
39 import javax.swing.JList JavaDoc;
40 import javax.swing.JOptionPane JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43 import javax.swing.KeyStroke JavaDoc;
44 import javax.swing.ListSelectionModel JavaDoc;
45 import javax.swing.SwingConstants JavaDoc;
46 import javax.swing.event.ListSelectionEvent JavaDoc;
47 import javax.swing.event.ListSelectionListener JavaDoc;
48
49 import org.columba.api.gui.frame.IFrameMediator;
50 import org.columba.core.gui.base.ButtonWithMnemonic;
51 import org.columba.core.gui.base.DoubleClickListener;
52 import org.columba.core.gui.base.SingleSideEtchedBorder;
53 import org.columba.core.gui.util.DialogHeaderPanel;
54 import org.columba.core.help.HelpManager;
55 import org.columba.core.resourceloader.GlobalResourceLoader;
56 import org.columba.core.resourceloader.IconKeys;
57 import org.columba.core.resourceloader.ImageLoader;
58 import org.columba.core.xml.XmlElement;
59
60 import com.jgoodies.forms.layout.CellConstraints;
61 import com.jgoodies.forms.layout.FormLayout;
62
63 /**
64  * Profile chooser dialog.
65  * <p>
66  * User can choose a profile from a list. Add a new profile or edit and existing
67  * profiles's properties.
68  * <p>
69  * Additionally, the user can choose to hide this dialog on next startup.
70  *
71  * @author fdietz
72  */

73 public class ProfileManagerDialog extends JDialog JavaDoc implements ActionListener JavaDoc,
74         ListSelectionListener JavaDoc {
75     private static final String JavaDoc RESOURCE_PATH = "org.columba.core.i18n.dialog";
76
77     protected JButton JavaDoc okButton;
78
79     protected JButton JavaDoc helpButton;
80
81     protected JButton JavaDoc addButton;
82
83     protected JButton JavaDoc editButton;
84
85     protected JButton JavaDoc removeButton;
86
87     protected JButton JavaDoc importButton;
88
89     protected JButton JavaDoc exportButton;
90
91     // protected JButton defaultButton;
92
private DefaultListModel JavaDoc model;
93
94     protected JList JavaDoc list;
95
96     protected String JavaDoc selection;
97
98     protected JLabel JavaDoc nameLabel;
99
100     protected JCheckBox JavaDoc checkBox;
101
102     public ProfileManagerDialog(IFrameMediator mediator)
103             throws HeadlessException JavaDoc {
104         super(mediator.getView().getFrame(), GlobalResourceLoader.getString(
105                 RESOURCE_PATH, "profiles", "manager.title"), true);
106     
107
108         initComponents();
109
110         layoutComponents();
111
112         pack();
113
114         setLocationRelativeTo(null);
115         setVisible(true);
116     }
117
118     protected void layoutComponents() {
119
120         getContentPane().add(createPanel(), BorderLayout.CENTER);
121
122         JPanel JavaDoc bottomPanel = createBottomPanel();
123         getContentPane().add(bottomPanel, BorderLayout.SOUTH);
124
125         getContentPane().add(
126                 new DialogHeaderPanel(GlobalResourceLoader.getString(
127                         RESOURCE_PATH, "profiles", "header_title"),
128                         GlobalResourceLoader.getString(RESOURCE_PATH,
129                                 "profiles", "header_description"), ImageLoader
130                                 .getIcon(IconKeys.USER)),
131                 BorderLayout.NORTH);
132     }
133
134     private JPanel JavaDoc createPanel() {
135         JPanel JavaDoc jpanel1 = new JPanel JavaDoc();
136         FormLayout formlayout1 = new FormLayout(
137                 "FILL:DEFAULT:GROW(1.0),3DLU,FILL:DEFAULT:NONE",
138                 "CENTER:DEFAULT:NONE,1DLU,FILL:DEFAULT:GROW(1.0),3DLU,CENTER:DEFAULT:NONE");
139         CellConstraints cc = new CellConstraints();
140         jpanel1.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
141         jpanel1.setLayout(formlayout1);
142
143         JLabel JavaDoc jlabel1 = new JLabel JavaDoc();
144         jlabel1.setText("Profiles:");
145         jpanel1.add(jlabel1, cc.xy(1, 1));
146
147         JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(list);
148         scrollPane.setPreferredSize(new Dimension JavaDoc(250, 150));
149         jpanel1.add(scrollPane, cc.xy(1, 3));
150
151         jpanel1.add(checkBox, cc.xy(1, 5));
152
153         jpanel1.add(createPanel1(), new CellConstraints(3, 3, 1, 1,
154                 CellConstraints.DEFAULT, CellConstraints.TOP));
155
156         return jpanel1;
157     }
158
159     private JPanel JavaDoc createPanel1() {
160         JPanel JavaDoc jpanel1 = new JPanel JavaDoc();
161         FormLayout formlayout1 = new FormLayout(
162                 "FILL:DEFAULT:NONE",
163                 "CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU, CENTER:DEFAULT:NONE");
164         CellConstraints cc = new CellConstraints();
165         jpanel1.setLayout(formlayout1);
166
167         jpanel1.add(addButton, cc.xy(1, 1));
168
169         jpanel1.add(editButton, cc.xy(1, 3));
170
171         jpanel1.add(removeButton, cc.xy(1, 5));
172
173         return jpanel1;
174     }
175
176     /**
177      * @return
178      */

179     private JPanel JavaDoc createBottomPanel() {
180         JPanel JavaDoc bottomPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
181         bottomPanel.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
182
183         JPanel JavaDoc buttonPanel = new JPanel JavaDoc(new GridLayout JavaDoc(1, 3, 6, 0));
184         buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
185
186         buttonPanel.add(okButton);
187
188         buttonPanel.add(helpButton);
189         bottomPanel.add(buttonPanel, BorderLayout.EAST);
190         return bottomPanel;
191     }
192
193     protected void initComponents() {
194         addButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
195                 RESOURCE_PATH, "profiles", "add"));
196         addButton.setActionCommand("ADD");
197         addButton.addActionListener(this);
198         addButton.setEnabled(false);
199
200         editButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
201                 RESOURCE_PATH, "profiles", "edit"));
202         editButton.setActionCommand("EDIT");
203         editButton.addActionListener(this);
204         editButton.setEnabled(false);
205
206         removeButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
207                 RESOURCE_PATH, "profiles", "remove"));
208         removeButton.setActionCommand("REMOVE");
209         removeButton.addActionListener(this);
210         removeButton.setEnabled(false);
211
212         importButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
213                 RESOURCE_PATH, "profiles", "import"));
214         importButton.setActionCommand("IMPORT");
215         importButton.addActionListener(this);
216
217         exportButton = new ButtonWithMnemonic(GlobalResourceLoader.getString(
218                 RESOURCE_PATH, "profiles", "export"));
219         exportButton.setActionCommand("EXPORT");
220         exportButton.addActionListener(this);
221         exportButton.setEnabled(false);
222
223         nameLabel = new JLabel JavaDoc("Choose Profile:");
224
225         checkBox = new JCheckBox JavaDoc("Always ask on startup.");
226         checkBox.setSelected(ProfileManager.getInstance().isAlwaysAsk());
227         checkBox.setActionCommand("CHECKBOX");
228         checkBox.addActionListener(this);
229
230         okButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("",
231                 "", "close"));
232         okButton.setActionCommand("CLOSE");
233         okButton.addActionListener(this);
234
235         helpButton = new ButtonWithMnemonic(GlobalResourceLoader.getString("",
236                 "", "help"));
237
238         // associate with JavaHelp
239
HelpManager.getInstance().enableHelpOnButton(helpButton,
240                 "extending_columba_2");
241         HelpManager.getInstance().enableHelpKey(getRootPane(),
242                 "extending_columba_2");
243
244         XmlElement profiles = ProfileManager.getInstance().getProfiles();
245         model = new DefaultListModel JavaDoc();
246         model.addElement("Default");
247
248         for (int i = 0; i < profiles.count(); i++) {
249             XmlElement p = profiles.getElement(i);
250             String JavaDoc name = p.getAttribute("name");
251             model.addElement(name);
252         }
253
254         list = new JList JavaDoc(model);
255         list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
256         list.addListSelectionListener(this);
257         list.addMouseListener(new DoubleClickListener() {
258             public void doubleClick(MouseEvent JavaDoc e) {
259                 actionPerformed(new ActionEvent JavaDoc(list, 0, "EDIT"));
260             }
261         });
262
263         String JavaDoc selected = ProfileManager.getInstance().getSelectedProfile();
264         if (selected != null) {
265             list.setSelectedValue(selected, true);
266         }
267
268         getRootPane().setDefaultButton(okButton);
269         getRootPane().registerKeyboardAction(this, "CLOSE",
270                 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
271                 JComponent.WHEN_IN_FOCUSED_WINDOW);
272     }
273
274     public void actionPerformed(ActionEvent JavaDoc e) {
275         String JavaDoc action = e.getActionCommand();
276         if (action.equals("CLOSE")) {
277             ProfileManager.getInstance().setAlwaysAsk(isAlwaysAskSelected());
278
279             setVisible(false);
280         } else if (action.equals("CHECKBOX")) {
281
282         } else if (action.equals("ADD")) {
283             JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
284             fc.setMultiSelectionEnabled(true);
285             // bug #996381 (fdietz), directories only!!
286
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
287             fc.setFileHidingEnabled(false);
288
289             if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
290                 File JavaDoc location = fc.getSelectedFile();
291                 Profile p = new Profile(location.getName(), location);
292                 // add profile to profiles.xml
293
ProfileManager.getInstance().addProfile(p);
294
295                 // add to listmodel
296
model.addElement(p.getName());
297                 // select new item
298
list.setSelectedValue(p.getName(), true);
299             }
300         } else if (action.equals("EDIT")) {
301             String JavaDoc inputValue = JOptionPane.showInputDialog(
302                     GlobalResourceLoader.getString(RESOURCE_PATH, "profiles",
303                             "enter_name"), selection);
304
305             if (inputValue == null) {
306                 return;
307             }
308
309             // rename profile in profiles.xml
310
ProfileManager.getInstance().renameProfile(selection, inputValue);
311
312             // modify listmodel
313
model.setElementAt(inputValue, model.indexOf(selection));
314
315             selection = inputValue;
316         } else if (action.equals("REMOVE")) {
317             if (ProfileManager.getInstance().getCurrentProfile().getName()
318                     .equals(selection)) {
319                 // can't delete currently running profile
320
JOptionPane.showMessageDialog(this, GlobalResourceLoader
321                         .getString(RESOURCE_PATH, "profiles", "errDelete.msg"),
322                         GlobalResourceLoader.getString(RESOURCE_PATH,
323                                 "profiles", "errDelete.title"),
324                         JOptionPane.ERROR_MESSAGE);
325             } else {
326                 Profile p = ProfileManager.getInstance().getProfileForName(
327                         selection);
328                 if (p != null) {
329                     int n = JOptionPane.showConfirmDialog(this,
330                             GlobalResourceLoader.getString(RESOURCE_PATH,
331                                     "profiles", "confirmDelete.msg"),
332                             GlobalResourceLoader.getString(RESOURCE_PATH,
333                                     "profiles", "confirmDelete.title"),
334                             JOptionPane.YES_NO_OPTION,
335                             JOptionPane.QUESTION_MESSAGE);
336                     if (n == JOptionPane.NO_OPTION) {
337                         return;
338                     }
339
340                     ProfileManager.getInstance().removeProfile(selection);
341                     model.removeElement(selection);
342
343                 }
344             }
345         } else if (action.equals("IMPORT")) {
346             // TODO (@author fdietz): add import feature
347
/*
348              * JFileChooser chooser = new JFileChooser();
349              * chooser.addChoosableFileFilter(new FileFilter() { public boolean
350              * accept(File file) { return file.isDirectory() ||
351              * file.getName().toLowerCase().endsWith(".zip"); }
352              *
353              * public String getDescription() { return "Columba Profile
354              * Archive"; } }); chooser.setAcceptAllFileFilterUsed(false);
355              *
356              * int result = chooser.showOpenDialog(this);
357              *
358              * if (result == JFileChooser.APPROVE_OPTION) { File file =
359              * chooser.getSelectedFile(); }
360              */

361         } else if (action.equals("EXPORT")) {
362             // TODO (@author fdietz): add export feature
363
}
364     }
365
366     public void valueChanged(ListSelectionEvent JavaDoc e) {
367         boolean enabled = !list.isSelectionEmpty();
368         addButton.setEnabled(enabled);
369
370         exportButton.setEnabled(enabled);
371
372         // get current list selection
373
selection = (String JavaDoc) list.getSelectedValue();
374
375         // user's can't delete default account
376
if ((selection != null) && (!selection.equals("Default"))) {
377             removeButton.setEnabled(true);
378             editButton.setEnabled(true);
379         } else {
380             removeButton.setEnabled(false);
381             editButton.setEnabled(false);
382         }
383     }
384
385     /**
386      * @return The selection.
387      */

388     public String JavaDoc getSelection() {
389         return selection;
390     }
391
392     public boolean isAlwaysAskSelected() {
393         return checkBox.isSelected();
394     }
395 }
Popular Tags