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.Color;
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.GridLayout;
028: import java.awt.HeadlessException;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.KeyEvent;
032: import java.awt.event.MouseEvent;
033: import java.awt.event.MouseListener;
034: import java.io.File;
035:
036: import javax.swing.BorderFactory;
037: import javax.swing.Box;
038: import javax.swing.BoxLayout;
039: import javax.swing.DefaultListModel;
040: import javax.swing.JButton;
041: import javax.swing.JCheckBox;
042: import javax.swing.JComponent;
043: import javax.swing.JDialog;
044: import javax.swing.JFileChooser;
045: import javax.swing.JFrame;
046: import javax.swing.JLabel;
047: import javax.swing.JList;
048: import javax.swing.JOptionPane;
049: import javax.swing.JPanel;
050: import javax.swing.JScrollPane;
051: import javax.swing.KeyStroke;
052: import javax.swing.SwingConstants;
053: import javax.swing.event.ListSelectionEvent;
054: import javax.swing.event.ListSelectionListener;
055:
056: import org.columba.core.gui.base.ButtonWithMnemonic;
057: import org.columba.core.gui.base.CheckBoxWithMnemonic;
058: import org.columba.core.gui.base.SingleSideEtchedBorder;
059: import org.columba.core.help.HelpManager;
060: import org.columba.core.resourceloader.GlobalResourceLoader;
061: import org.columba.core.xml.XmlElement;
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 ProfileChooserDialog extends JDialog implements
074: ActionListener, ListSelectionListener, MouseListener {
075: private static final String RESOURCE_PATH = "org.columba.core.i18n.dialog";
076:
077: protected JButton okButton;
078: protected JButton cancelButton;
079: protected JButton helpButton;
080: protected JButton addButton;
081: protected JButton editButton;
082: //protected JButton defaultButton;
083: private DefaultListModel model;
084: protected JList list;
085: protected String selection;
086: protected JLabel nameLabel;
087: protected JCheckBox checkBox;
088:
089: public ProfileChooserDialog() throws HeadlessException {
090: super ((JFrame) null, GlobalResourceLoader.getString(
091: RESOURCE_PATH, "profiles", "chooser.title"), true);
092:
093: initComponents();
094:
095: layoutComponents();
096:
097: pack();
098: setLocationRelativeTo(null);
099: setVisible(true);
100: }
101:
102: protected void layoutComponents() {
103: JPanel mainPanel = new JPanel(new BorderLayout());
104: mainPanel.setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
105: 12));
106:
107: // top panel
108: JPanel topPanel = new JPanel();
109:
110: topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
111:
112: GridBagLayout gridBagLayout = new GridBagLayout();
113: GridBagConstraints c = new GridBagConstraints();
114:
115: //topPanel.setLayout( );
116: JPanel topBorderPanel = new JPanel();
117: topBorderPanel.setLayout(new BorderLayout());
118:
119: //topBorderPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 5,
120: // 0));
121: topBorderPanel.add(topPanel);
122:
123: //mainPanel.add( topBorderPanel, BorderLayout.NORTH );
124:
125: topPanel.add(nameLabel);
126:
127: topPanel.add(Box.createRigidArea(new Dimension(10, 0)));
128: topPanel.add(Box.createHorizontalGlue());
129:
130: Component glue = Box.createVerticalGlue();
131: c.anchor = GridBagConstraints.EAST;
132: c.gridwidth = GridBagConstraints.REMAINDER;
133:
134: //c.fill = GridBagConstraints.HORIZONTAL;
135: gridBagLayout.setConstraints(glue, c);
136:
137: gridBagLayout = new GridBagLayout();
138: c = new GridBagConstraints();
139:
140: JPanel eastPanel = new JPanel(gridBagLayout);
141: mainPanel.add(eastPanel, BorderLayout.EAST);
142:
143: c.fill = GridBagConstraints.HORIZONTAL;
144: c.weightx = 1.0;
145: c.gridwidth = GridBagConstraints.REMAINDER;
146: gridBagLayout.setConstraints(addButton, c);
147: eastPanel.add(addButton);
148:
149: Component strut1 = Box.createRigidArea(new Dimension(30, 5));
150: gridBagLayout.setConstraints(strut1, c);
151: eastPanel.add(strut1);
152:
153: gridBagLayout.setConstraints(editButton, c);
154: eastPanel.add(editButton);
155:
156: /*
157: * Component strut2 = Box.createRigidArea(new Dimension(30, 5));
158: * gridBagLayout.setConstraints(strut2, c); eastPanel.add(strut2);
159: *
160: * gridBagLayout.setConstraints(defaultButton, c);
161: * eastPanel.add(defaultButton);
162: */
163:
164: glue = Box.createVerticalGlue();
165: c.fill = GridBagConstraints.BOTH;
166: c.weighty = 1.0;
167: gridBagLayout.setConstraints(glue, c);
168: eastPanel.add(glue);
169:
170: // centerpanel
171: JPanel centerPanel = new JPanel(new BorderLayout());
172: centerPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0,
173: 6));
174:
175: JScrollPane scrollPane = new JScrollPane(list);
176: scrollPane.setPreferredSize(new Dimension(250, 150));
177: scrollPane.getViewport().setBackground(Color.white);
178: centerPanel.add(scrollPane, BorderLayout.CENTER);
179:
180: centerPanel.add(checkBox, BorderLayout.SOUTH);
181:
182: mainPanel.add(topPanel, BorderLayout.NORTH);
183:
184: mainPanel.add(centerPanel);
185: getContentPane().add(mainPanel, BorderLayout.CENTER);
186:
187: JPanel bottomPanel = new JPanel(new BorderLayout());
188: bottomPanel.setBorder(new SingleSideEtchedBorder(
189: SwingConstants.TOP));
190:
191: JPanel buttonPanel = new JPanel(new GridLayout(1, 3, 6, 0));
192: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
193: 12, 12));
194:
195: buttonPanel.add(okButton);
196: buttonPanel.add(cancelButton);
197: buttonPanel.add(helpButton);
198: bottomPanel.add(buttonPanel, BorderLayout.EAST);
199: getContentPane().add(bottomPanel, BorderLayout.SOUTH);
200: }
201:
202: protected void initComponents() {
203: addButton = new ButtonWithMnemonic(GlobalResourceLoader
204: .getString(RESOURCE_PATH, "profiles", "add"));
205: addButton.setActionCommand("ADD");
206: addButton.addActionListener(this );
207: addButton.setEnabled(false);
208:
209: editButton = new ButtonWithMnemonic(GlobalResourceLoader
210: .getString(RESOURCE_PATH, "profiles", "edit"));
211: editButton.setActionCommand("EDIT");
212: editButton.addActionListener(this );
213: editButton.setEnabled(false);
214:
215: nameLabel = new JLabel(GlobalResourceLoader.getString(
216: RESOURCE_PATH, "profiles", "label"));
217:
218: checkBox = new CheckBoxWithMnemonic(GlobalResourceLoader
219: .getString(RESOURCE_PATH, "profiles", "dont_ask"));
220:
221: okButton = new ButtonWithMnemonic(GlobalResourceLoader
222: .getString("", "", "ok"));
223: okButton.setActionCommand("OK");
224: okButton.addActionListener(this );
225:
226: cancelButton = new ButtonWithMnemonic(GlobalResourceLoader
227: .getString("", "", "cancel"));
228: cancelButton.setActionCommand("CANCEL");
229: cancelButton.addActionListener(this );
230:
231: helpButton = new ButtonWithMnemonic(GlobalResourceLoader
232: .getString("", "", "help"));
233:
234: // associate with JavaHelp
235: HelpManager.getInstance().enableHelpOnButton(helpButton,
236: "extending_columba_2");
237: HelpManager.getInstance().enableHelpKey(getRootPane(),
238: "extending_columba_2");
239:
240: XmlElement profiles = ProfileManager.getInstance()
241: .getProfiles();
242: model = new DefaultListModel();
243: model.addElement("Default");
244:
245: for (int i = 0; i < profiles.count(); i++) {
246: XmlElement p = profiles.getElement(i);
247: String name = p.getAttribute("name");
248: model.addElement(name);
249: }
250:
251: list = new JList(model);
252: list.addListSelectionListener(this );
253: list.addMouseListener(this );
254:
255: String selected = ProfileManager.getInstance()
256: .getSelectedProfile();
257: if (selected != null) {
258: list.setSelectedValue(selected, true);
259: }
260:
261: getRootPane().setDefaultButton(okButton);
262: getRootPane().registerKeyboardAction(this , "CANCEL",
263: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
264: JComponent.WHEN_IN_FOCUSED_WINDOW);
265: }
266:
267: public void actionPerformed(ActionEvent e) {
268: String action = e.getActionCommand();
269: if (action.equals("OK")) {
270: setVisible(false);
271: } else if (action.equals("CANCEL")) {
272: System.exit(0);
273: } else if (action.equals("ADD")) {
274: JFileChooser fc = new JFileChooser();
275: fc.setMultiSelectionEnabled(true);
276: // bug #996381 (fdietz), directories only!!
277: fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
278: fc.setFileHidingEnabled(false);
279: if (fc.showOpenDialog(this ) == JFileChooser.APPROVE_OPTION) {
280: File location = fc.getSelectedFile();
281: Profile p = new Profile(location.getName(), location);
282: // add profile to profiles.xml
283: ProfileManager.getInstance().addProfile(p);
284:
285: // add to listmodel
286: model.addElement(p.getName());
287: // select new item
288: list.setSelectedValue(p.getName(), true);
289: }
290: } else if (action.equals("EDIT")) {
291: String inputValue = JOptionPane.showInputDialog(
292: GlobalResourceLoader.getString(RESOURCE_PATH,
293: "profiles", "enter_name"), selection);
294:
295: if (inputValue == null) {
296: return;
297: }
298:
299: // rename profile in profiles.xml
300: ProfileManager.getInstance().renameProfile(selection,
301: inputValue);
302:
303: // modify listmodel
304: model.setElementAt(inputValue, model.indexOf(selection));
305: selection = inputValue;
306: }
307: }
308:
309: public void valueChanged(ListSelectionEvent e) {
310: boolean enabled = !list.isSelectionEmpty();
311: addButton.setEnabled(enabled);
312:
313: okButton.setEnabled(enabled);
314: //defaultButton.setEnabled(enabled);
315:
316: selection = (String) list.getSelectedValue();
317:
318: // user's can't edit default account
319: if ((selection != null) && (!selection.equals("Default"))) {
320: editButton.setEnabled(true);
321: } else {
322: editButton.setEnabled(false);
323: }
324: }
325:
326: /**
327: * @return The selection.
328: */
329: public String getSelection() {
330: return selection;
331: }
332:
333: public boolean isDontAskedSelected() {
334: return checkBox.isSelected();
335: }
336:
337: public void mouseClicked(MouseEvent arg0) {
338: if (arg0.getClickCount() >= 2) {
339: actionPerformed(new ActionEvent(list, 0, "OK"));
340: }
341: }
342:
343: public void mouseEntered(MouseEvent arg0) {
344: }
345:
346: public void mouseExited(MouseEvent arg0) {
347: }
348:
349: public void mousePressed(MouseEvent arg0) {
350: }
351:
352: public void mouseReleased(MouseEvent arg0) {
353: }
354:
355: }
|