001: //The contents of this file are subject to the Mozilla Public License Version
002: //1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: //Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018:
019: package org.columba.core.gui.profiles;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.GridLayout;
024: import java.awt.HeadlessException;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.MouseEvent;
029: import java.io.File;
030:
031: import javax.swing.BorderFactory;
032: import javax.swing.DefaultListModel;
033: import javax.swing.JButton;
034: import javax.swing.JCheckBox;
035: import javax.swing.JComponent;
036: import javax.swing.JDialog;
037: import javax.swing.JFileChooser;
038: import javax.swing.JLabel;
039: import javax.swing.JList;
040: import javax.swing.JOptionPane;
041: import javax.swing.JPanel;
042: import javax.swing.JScrollPane;
043: import javax.swing.KeyStroke;
044: import javax.swing.ListSelectionModel;
045: import javax.swing.SwingConstants;
046: import javax.swing.event.ListSelectionEvent;
047: import javax.swing.event.ListSelectionListener;
048:
049: import org.columba.api.gui.frame.IFrameMediator;
050: import org.columba.core.gui.base.ButtonWithMnemonic;
051: import org.columba.core.gui.base.DoubleClickListener;
052: import org.columba.core.gui.base.SingleSideEtchedBorder;
053: import org.columba.core.gui.util.DialogHeaderPanel;
054: import org.columba.core.help.HelpManager;
055: import org.columba.core.resourceloader.GlobalResourceLoader;
056: import org.columba.core.resourceloader.IconKeys;
057: import org.columba.core.resourceloader.ImageLoader;
058: import org.columba.core.xml.XmlElement;
059:
060: import com.jgoodies.forms.layout.CellConstraints;
061: import com.jgoodies.forms.layout.FormLayout;
062:
063: /**
064: * Profile chooser dialog.
065: * <p>
066: * User can choose a profile from a list. Add a new profile or edit and existing
067: * profiles's properties.
068: * <p>
069: * Additionally, the user can choose to hide this dialog on next startup.
070: *
071: * @author fdietz
072: */
073: public class ProfileManagerDialog extends JDialog implements
074: ActionListener, ListSelectionListener {
075: private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";
076:
077: protected JButton okButton;
078:
079: protected JButton helpButton;
080:
081: protected JButton addButton;
082:
083: protected JButton editButton;
084:
085: protected JButton removeButton;
086:
087: protected JButton importButton;
088:
089: protected JButton exportButton;
090:
091: // protected JButton defaultButton;
092: private DefaultListModel model;
093:
094: protected JList list;
095:
096: protected String selection;
097:
098: protected JLabel nameLabel;
099:
100: protected JCheckBox checkBox;
101:
102: public ProfileManagerDialog(IFrameMediator mediator)
103: throws HeadlessException {
104: super (mediator.getView().getFrame(), GlobalResourceLoader
105: .getString(RESOURCE_PATH, "profiles", "manager.title"),
106: true);
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 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"),
130: ImageLoader.getIcon(IconKeys.USER)),
131: BorderLayout.NORTH);
132: }
133:
134: private JPanel createPanel() {
135: JPanel jpanel1 = new JPanel();
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,
141: 12));
142: jpanel1.setLayout(formlayout1);
143:
144: JLabel jlabel1 = new JLabel();
145: jlabel1.setText("Profiles:");
146: jpanel1.add(jlabel1, cc.xy(1, 1));
147:
148: JScrollPane scrollPane = new JScrollPane(list);
149: scrollPane.setPreferredSize(new Dimension(250, 150));
150: jpanel1.add(scrollPane, cc.xy(1, 3));
151:
152: jpanel1.add(checkBox, cc.xy(1, 5));
153:
154: jpanel1.add(createPanel1(), new CellConstraints(3, 3, 1, 1,
155: CellConstraints.DEFAULT, CellConstraints.TOP));
156:
157: return jpanel1;
158: }
159:
160: private JPanel createPanel1() {
161: JPanel jpanel1 = new JPanel();
162: FormLayout formlayout1 = new FormLayout(
163: "FILL:DEFAULT:NONE",
164: "CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU,CENTER:DEFAULT:NONE,3DLU, CENTER:DEFAULT:NONE");
165: CellConstraints cc = new CellConstraints();
166: jpanel1.setLayout(formlayout1);
167:
168: jpanel1.add(addButton, cc.xy(1, 1));
169:
170: jpanel1.add(editButton, cc.xy(1, 3));
171:
172: jpanel1.add(removeButton, cc.xy(1, 5));
173:
174: return jpanel1;
175: }
176:
177: /**
178: * @return
179: */
180: private JPanel createBottomPanel() {
181: JPanel bottomPanel = new JPanel(new BorderLayout());
182: bottomPanel.setBorder(new SingleSideEtchedBorder(
183: SwingConstants.TOP));
184:
185: JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 6, 0));
186: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
187: 12, 12));
188:
189: buttonPanel.add(okButton);
190:
191: buttonPanel.add(helpButton);
192: bottomPanel.add(buttonPanel, BorderLayout.EAST);
193: return bottomPanel;
194: }
195:
196: protected void initComponents() {
197: addButton = new ButtonWithMnemonic(GlobalResourceLoader
198: .getString(RESOURCE_PATH, "profiles", "add"));
199: addButton.setActionCommand("ADD");
200: addButton.addActionListener(this );
201: addButton.setEnabled(false);
202:
203: editButton = new ButtonWithMnemonic(GlobalResourceLoader
204: .getString(RESOURCE_PATH, "profiles", "edit"));
205: editButton.setActionCommand("EDIT");
206: editButton.addActionListener(this );
207: editButton.setEnabled(false);
208:
209: removeButton = new ButtonWithMnemonic(GlobalResourceLoader
210: .getString(RESOURCE_PATH, "profiles", "remove"));
211: removeButton.setActionCommand("REMOVE");
212: removeButton.addActionListener(this );
213: removeButton.setEnabled(false);
214:
215: importButton = new ButtonWithMnemonic(GlobalResourceLoader
216: .getString(RESOURCE_PATH, "profiles", "import"));
217: importButton.setActionCommand("IMPORT");
218: importButton.addActionListener(this );
219:
220: exportButton = new ButtonWithMnemonic(GlobalResourceLoader
221: .getString(RESOURCE_PATH, "profiles", "export"));
222: exportButton.setActionCommand("EXPORT");
223: exportButton.addActionListener(this );
224: exportButton.setEnabled(false);
225:
226: nameLabel = new JLabel("Choose Profile:");
227:
228: checkBox = new JCheckBox("Always ask on startup.");
229: checkBox
230: .setSelected(ProfileManager.getInstance().isAlwaysAsk());
231: checkBox.setActionCommand("CHECKBOX");
232: checkBox.addActionListener(this );
233:
234: okButton = new ButtonWithMnemonic(GlobalResourceLoader
235: .getString("", "", "close"));
236: okButton.setActionCommand("CLOSE");
237: okButton.addActionListener(this );
238:
239: helpButton = new ButtonWithMnemonic(GlobalResourceLoader
240: .getString("", "", "help"));
241:
242: // associate with JavaHelp
243: HelpManager.getInstance().enableHelpOnButton(helpButton,
244: "extending_columba_2");
245: HelpManager.getInstance().enableHelpKey(getRootPane(),
246: "extending_columba_2");
247:
248: XmlElement profiles = ProfileManager.getInstance()
249: .getProfiles();
250: model = new DefaultListModel();
251: model.addElement("Default");
252:
253: for (int i = 0; i < profiles.count(); i++) {
254: XmlElement p = profiles.getElement(i);
255: String name = p.getAttribute("name");
256: model.addElement(name);
257: }
258:
259: list = new JList(model);
260: list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
261: list.addListSelectionListener(this );
262: list.addMouseListener(new DoubleClickListener() {
263: public void doubleClick(MouseEvent e) {
264: actionPerformed(new ActionEvent(list, 0, "EDIT"));
265: }
266: });
267:
268: String selected = ProfileManager.getInstance()
269: .getSelectedProfile();
270: if (selected != null) {
271: list.setSelectedValue(selected, true);
272: }
273:
274: getRootPane().setDefaultButton(okButton);
275: getRootPane().registerKeyboardAction(this , "CLOSE",
276: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
277: JComponent.WHEN_IN_FOCUSED_WINDOW);
278: }
279:
280: public void actionPerformed(ActionEvent e) {
281: String action = e.getActionCommand();
282: if (action.equals("CLOSE")) {
283: ProfileManager.getInstance().setAlwaysAsk(
284: isAlwaysAskSelected());
285:
286: setVisible(false);
287: } else if (action.equals("CHECKBOX")) {
288:
289: } else if (action.equals("ADD")) {
290: JFileChooser fc = new JFileChooser();
291: fc.setMultiSelectionEnabled(true);
292: // bug #996381 (fdietz), directories only!!
293: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
294: fc.setFileHidingEnabled(false);
295:
296: if (fc.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
297: File location = fc.getSelectedFile();
298: Profile p = new Profile(location.getName(), location);
299: // add profile to profiles.xml
300: ProfileManager.getInstance().addProfile(p);
301:
302: // add to listmodel
303: model.addElement(p.getName());
304: // select new item
305: list.setSelectedValue(p.getName(), true);
306: }
307: } else if (action.equals("EDIT")) {
308: String inputValue = JOptionPane.showInputDialog(
309: GlobalResourceLoader.getString(RESOURCE_PATH,
310: "profiles", "enter_name"), selection);
311:
312: if (inputValue == null) {
313: return;
314: }
315:
316: // rename profile in profiles.xml
317: ProfileManager.getInstance().renameProfile(selection,
318: inputValue);
319:
320: // modify listmodel
321: model.setElementAt(inputValue, model.indexOf(selection));
322:
323: selection = inputValue;
324: } else if (action.equals("REMOVE")) {
325: if (ProfileManager.getInstance().getCurrentProfile()
326: .getName().equals(selection)) {
327: // can't delete currently running profile
328: JOptionPane.showMessageDialog(this ,
329: GlobalResourceLoader.getString(RESOURCE_PATH,
330: "profiles", "errDelete.msg"),
331: GlobalResourceLoader.getString(RESOURCE_PATH,
332: "profiles", "errDelete.title"),
333: JOptionPane.ERROR_MESSAGE);
334: } else {
335: Profile p = ProfileManager.getInstance()
336: .getProfileForName(selection);
337: if (p != null) {
338: int n = JOptionPane.showConfirmDialog(this ,
339: GlobalResourceLoader.getString(
340: RESOURCE_PATH, "profiles",
341: "confirmDelete.msg"),
342: GlobalResourceLoader.getString(
343: RESOURCE_PATH, "profiles",
344: "confirmDelete.title"),
345: JOptionPane.YES_NO_OPTION,
346: JOptionPane.QUESTION_MESSAGE);
347: if (n == JOptionPane.NO_OPTION) {
348: return;
349: }
350:
351: ProfileManager.getInstance().removeProfile(
352: selection);
353: model.removeElement(selection);
354:
355: }
356: }
357: } else if (action.equals("IMPORT")) {
358: // TODO (@author fdietz): add import feature
359: /*
360: * JFileChooser chooser = new JFileChooser();
361: * chooser.addChoosableFileFilter(new FileFilter() { public boolean
362: * accept(File file) { return file.isDirectory() ||
363: * file.getName().toLowerCase().endsWith(".zip"); }
364: *
365: * public String getDescription() { return "Columba Profile
366: * Archive"; } }); chooser.setAcceptAllFileFilterUsed(false);
367: *
368: * int result = chooser.showOpenDialog(this);
369: *
370: * if (result == JFileChooser.APPROVE_OPTION) { File file =
371: * chooser.getSelectedFile(); }
372: */
373: } else if (action.equals("EXPORT")) {
374: // TODO (@author fdietz): add export feature
375: }
376: }
377:
378: public void valueChanged(ListSelectionEvent e) {
379: boolean enabled = !list.isSelectionEmpty();
380: addButton.setEnabled(enabled);
381:
382: exportButton.setEnabled(enabled);
383:
384: // get current list selection
385: selection = (String) list.getSelectedValue();
386:
387: // user's can't delete default account
388: if ((selection != null) && (!selection.equals("Default"))) {
389: removeButton.setEnabled(true);
390: editButton.setEnabled(true);
391: } else {
392: removeButton.setEnabled(false);
393: editButton.setEnabled(false);
394: }
395: }
396:
397: /**
398: * @return The selection.
399: */
400: public String getSelection() {
401: return selection;
402: }
403:
404: public boolean isAlwaysAskSelected() {
405: return checkBox.isSelected();
406: }
407: }
|