001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.config.template;
017:
018: import java.awt.BorderLayout;
019: import java.awt.Dimension;
020: import java.awt.GridLayout;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.KeyEvent;
024:
025: import javax.swing.BorderFactory;
026: import javax.swing.JButton;
027: import javax.swing.JComponent;
028: import javax.swing.JDialog;
029: import javax.swing.JFrame;
030: import javax.swing.JList;
031: import javax.swing.JPanel;
032: import javax.swing.JScrollPane;
033: import javax.swing.KeyStroke;
034: import javax.swing.SwingConstants;
035: import javax.swing.event.ListSelectionEvent;
036: import javax.swing.event.ListSelectionListener;
037:
038: import org.columba.core.gui.base.ButtonWithMnemonic;
039: import org.columba.core.gui.base.SingleSideEtchedBorder;
040: import org.columba.core.help.HelpManager;
041: import org.columba.mail.message.IHeaderList;
042: import org.columba.mail.util.MailResourceLoader;
043:
044: /**
045: * Asks the user to choose a template from a list.
046: *
047: * @author fdietz
048: */
049:
050: public class ChooseTemplateDialog extends JDialog implements
051: ActionListener, ListSelectionListener {
052: boolean result;
053: JList list;
054: Object uid;
055: IHeaderList headerList;
056: JButton okButton;
057:
058: public ChooseTemplateDialog(JFrame parent, IHeaderList list) {
059: super (parent, true);
060:
061: this .headerList = list;
062:
063: initComponents();
064:
065: updateComponents(true);
066:
067: pack();
068: setLocationRelativeTo(null);
069: setVisible(true);
070: }
071:
072: protected JPanel createButtonPanel() {
073: JPanel bottom = new JPanel();
074: bottom.setLayout(new BorderLayout());
075: bottom
076: .setBorder(new SingleSideEtchedBorder(
077: SwingConstants.TOP));
078:
079: //bottom.setLayout( new BoxLayout( bottom, BoxLayout.X_AXIS ) );
080: //bottom.add( Box.createHorizontalStrut());
081: ButtonWithMnemonic cancelButton = new ButtonWithMnemonic(
082: MailResourceLoader.getString("global", "cancel"));
083:
084: //$NON-NLS-1$ //$NON-NLS-2$
085: cancelButton.addActionListener(this );
086: cancelButton.setActionCommand("CANCEL"); //$NON-NLS-1$
087:
088: okButton = new ButtonWithMnemonic(MailResourceLoader.getString(
089: "global", "ok"));
090:
091: //$NON-NLS-1$ //$NON-NLS-2$
092: okButton.addActionListener(this );
093: okButton.setActionCommand("OK"); //$NON-NLS-1$
094: okButton.setDefaultCapable(true);
095: okButton.setEnabled(false);
096: getRootPane().setDefaultButton(okButton);
097:
098: ButtonWithMnemonic helpButton = new ButtonWithMnemonic(
099: MailResourceLoader.getString("global", "help"));
100:
101: // associate with JavaHelp
102: HelpManager.getInstance().enableHelpOnButton(helpButton,
103: "template_dialog");
104: HelpManager.getInstance().enableHelpKey(getRootPane(),
105: "template_dialog");
106:
107: JPanel buttonPanel = new JPanel();
108: buttonPanel.setBorder(BorderFactory.createEmptyBorder(12, 12,
109: 12, 12));
110: buttonPanel.setLayout(new GridLayout(1, 3, 6, 0));
111: buttonPanel.add(okButton);
112: buttonPanel.add(cancelButton);
113: buttonPanel.add(helpButton);
114:
115: //bottom.add( Box.createHorizontalGlue() );
116: bottom.add(buttonPanel, BorderLayout.EAST);
117:
118: return bottom;
119: }
120:
121: protected void initComponents() {
122: list = new JList(headerList.getUids());
123: list.addListSelectionListener(this );
124: list.setPreferredSize(new Dimension(200, 300));
125: list.setCellRenderer(new HeaderCellRenderer(headerList));
126:
127: JPanel panel = new JPanel();
128: panel
129: .setBorder(BorderFactory.createEmptyBorder(12, 12, 12,
130: 12));
131: panel.setLayout(new BorderLayout());
132:
133: JScrollPane scrollPane = new JScrollPane(list);
134: panel.add(scrollPane, BorderLayout.CENTER);
135:
136: getContentPane().setLayout(new BorderLayout());
137:
138: getContentPane().add(panel, BorderLayout.CENTER);
139:
140: getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
141: getRootPane().registerKeyboardAction(this , "CANCEL",
142: KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
143: JComponent.WHEN_IN_FOCUSED_WINDOW);
144: }
145:
146: protected void updateComponents(boolean b) {
147: if (b) {
148: // model -> view
149: } else {
150: // view -> model
151: }
152: }
153:
154: public void actionPerformed(ActionEvent arg0) {
155: String action = arg0.getActionCommand();
156:
157: if (action.equals("CANCEL")) {
158: result = false;
159: setVisible(false);
160: } else if (action.equals("OK")) {
161: result = true;
162: uid = list.getSelectedValue();
163: setVisible(false);
164: }
165: }
166:
167: /**
168: * @return
169: */
170: public boolean isResult() {
171: return result;
172: }
173:
174: /**
175: * @return
176: */
177: public Object getUid() {
178: return uid;
179: }
180:
181: /* (non-Javadoc)
182: * @see javax.swing.event.ListSelectionListener#valueChanged(javax.swing.event.ListSelectionEvent)
183: */
184: public void valueChanged(ListSelectionEvent ev) {
185: Object selection = list.getSelectedValue();
186:
187: if (selection != null) {
188: okButton.setEnabled(true);
189: } else {
190: okButton.setEnabled(false);
191: }
192: }
193: }
|