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.mail.gui.config.account;
020:
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.io.File;
024:
025: import javax.swing.JButton;
026: import javax.swing.JCheckBox;
027: import javax.swing.JFileChooser;
028: import javax.swing.JLabel;
029: import javax.swing.JOptionPane;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032:
033: import org.columba.core.gui.base.CheckBoxWithMnemonic;
034: import org.columba.core.gui.base.LabelWithMnemonic;
035: import org.columba.mail.config.AccountItem;
036: import org.columba.mail.config.Identity;
037: import org.columba.mail.config.MailConfig;
038: import org.columba.mail.folder.imap.IMAPRootFolder;
039: import org.columba.mail.gui.tree.FolderTreeModel;
040: import org.columba.mail.util.MailResourceLoader;
041: import org.columba.ristretto.message.Address;
042: import org.columba.ristretto.parser.ParserException;
043:
044: import com.jgoodies.forms.builder.DefaultFormBuilder;
045: import com.jgoodies.forms.layout.FormLayout;
046:
047: public class IdentityPanel extends DefaultPanel implements
048: ActionListener {
049: private JLabel nameLabel;
050: private JTextField nameTextField;
051: private JLabel addressLabel;
052: private JTextField addressTextField;
053: private JLabel organisationLabel;
054: private JTextField organisationTextField;
055: private JLabel replyaddressLabel;
056: private JTextField replyaddressTextField;
057: private JLabel accountnameLabel;
058: private JTextField accountnameTextField;
059: private JCheckBox defaultAccountCheckBox;
060: private JButton selectSignatureButton;
061: private JCheckBox attachsignatureCheckBox;
062: private AccountItem account;
063: private JButton editSignatureButton;
064:
065: //private ConfigFrame frame;
066: public IdentityPanel(AccountItem account) {
067: super ();
068:
069: //this.frame = frame;
070: this .account = account;
071:
072: initComponents();
073: layoutComponents();
074: updateComponents(true);
075: }
076:
077: protected void updateComponents(boolean b) {
078: Identity identity = account.getIdentity();
079: if (b) {
080: accountnameTextField.setText(account.getName());
081: Address address = identity.getAddress();
082: nameTextField.setText(address.getDisplayName());
083: addressTextField.setText(address.getMailAddress());
084: address = identity.getReplyToAddress();
085: replyaddressTextField.setText(address == null ? ""
086: : address.getMailAddress());
087: organisationTextField.setText(identity.getOrganisation());
088: File signature = identity.getSignature();
089: selectSignatureButton.setText(signature == null ? new File(
090: System.getProperty("user.home"), ".signature")
091: .getPath() : signature.getPath());
092:
093: attachsignatureCheckBox.setSelected(signature != null);
094:
095: defaultAccountCheckBox.setSelected(MailConfig.getInstance()
096: .getAccountList().getDefaultAccountUid() == account
097: .getInteger("uid"));
098: } else {
099: try {
100: Address address = Address.parse(addressTextField
101: .getText());
102: if (nameTextField.getText() != null) {
103: address.setDisplayName(nameTextField.getText());
104: }
105: identity.setAddress(address);
106: if (replyaddressTextField.getText().length() > 0) {
107: address = Address.parse(replyaddressTextField
108: .getText());
109: } else {
110: address = null;
111: }
112: identity.setReplyToAddress(address);
113: } catch (ParserException pe) {
114: } //does not occur
115: identity.setOrganisation(organisationTextField.getText());
116: if (attachsignatureCheckBox.isSelected()) {
117: identity.setSignature(new File(selectSignatureButton
118: .getText()));
119: } else {
120: identity.setSignature(null);
121: }
122:
123: if (!account.getName().equals(
124: accountnameTextField.getText())) {
125: account.setName(accountnameTextField.getText());
126: if (!account.isPopAccount()) {
127: // Account is an IMAP account -> change root folder name
128:
129: IMAPRootFolder imapRoot = (IMAPRootFolder) FolderTreeModel
130: .getInstance().getImapFolder(
131: account.getUid());
132: try {
133: imapRoot
134: .setName(accountnameTextField.getText());
135: FolderTreeModel.getInstance()
136: .nodeStructureChanged(imapRoot);
137: } catch (Exception e) {
138: }
139: }
140: }
141:
142: if (defaultAccountCheckBox.isSelected()) {
143: MailConfig.getInstance().getAccountList()
144: .setDefaultAccount(account.getUid());
145: }
146: }
147: }
148:
149: protected void initComponents() {
150: accountnameLabel = new LabelWithMnemonic(MailResourceLoader
151: .getString("dialog", "account", "identity_accountname"));
152:
153: accountnameTextField = new JTextField();
154: accountnameLabel.setLabelFor(accountnameTextField);
155: defaultAccountCheckBox = new CheckBoxWithMnemonic(
156: MailResourceLoader.getString("dialog", "account",
157: "defaultaccount"));
158:
159: nameLabel = new LabelWithMnemonic(MailResourceLoader.getString(
160: "dialog", "account", "yourname"));
161:
162: nameTextField = new JTextField();
163: nameLabel.setLabelFor(nameTextField);
164: addressLabel = new LabelWithMnemonic(MailResourceLoader
165: .getString("dialog", "account", "address"));
166:
167: addressTextField = new JTextField();
168: addressLabel.setLabelFor(addressTextField);
169: replyaddressLabel = new LabelWithMnemonic(MailResourceLoader
170: .getString("dialog", "account", "replytoaddress"));
171:
172: organisationLabel = new LabelWithMnemonic(MailResourceLoader
173: .getString("dialog", "account", "organisation"));
174:
175: replyaddressTextField = new JTextField();
176: replyaddressLabel.setLabelFor(replyaddressTextField);
177: organisationTextField = new JTextField();
178: organisationLabel.setLabelFor(organisationTextField);
179:
180: attachsignatureCheckBox = new CheckBoxWithMnemonic(
181: MailResourceLoader.getString("dialog", "account",
182: "attachthissignature"));
183:
184: selectSignatureButton = new JButton("~/.signature");
185: selectSignatureButton.setActionCommand("CHOOSE");
186: selectSignatureButton.addActionListener(this );
187:
188: editSignatureButton = new JButton(new EditSignatureAction(null,
189: account));
190: }
191:
192: protected void layoutComponents() {
193: // Create a FormLayout instance.
194: FormLayout layout = new FormLayout(
195: "10dlu, max(70dlu;default), 3dlu, fill:max(150dlu;default):grow",
196:
197: // 2 columns
198: ""); // rows are added dynamically (no need to define them here)
199:
200: // create a form builder
201: DefaultFormBuilder builder = new DefaultFormBuilder(layout,
202: this );
203:
204: // create EmptyBorder between components and dialog-frame
205: builder.setDefaultDialogBorder();
206:
207: // skip the first column
208: builder.setLeadingColumnOffset(1);
209:
210: // Add components to the panel:
211: builder.appendSeparator(MailResourceLoader.getString("dialog",
212: "account", "account_information"));
213: builder.nextLine();
214:
215: builder.append(accountnameLabel, 1);
216: builder.append(accountnameTextField);
217: builder.nextLine();
218:
219: builder.append(defaultAccountCheckBox, 3);
220: builder.nextLine();
221:
222: builder.appendSeparator(MailResourceLoader.getString("dialog",
223: "account", "needed_information"));
224: builder.nextLine();
225:
226: builder.append(nameLabel, 1);
227: builder.append(nameTextField);
228: builder.nextLine();
229:
230: builder.append(addressLabel, 1);
231: builder.append(addressTextField);
232: builder.nextLine();
233:
234: builder.appendSeparator(MailResourceLoader.getString("dialog",
235: "account", "optional_information"));
236: builder.nextLine();
237:
238: builder.append(organisationLabel, 1);
239: builder.append(organisationTextField);
240: builder.nextLine();
241:
242: builder.append(replyaddressLabel, 1);
243: builder.append(replyaddressTextField);
244: builder.nextLine(1);
245:
246: JPanel panel = new JPanel();
247: FormLayout l = new FormLayout(
248: "max(100;default), 3dlu, left:max(50dlu;default), 3dlu, left:max(50dlu;default)",
249:
250: // 3 columns
251: "fill:default:grow"); // rows are added dynamically (no need to define them here)
252:
253: // create a form builder
254: DefaultFormBuilder b = new DefaultFormBuilder(l, panel);
255:
256: b.append(attachsignatureCheckBox, selectSignatureButton,
257: editSignatureButton);
258:
259: //b.append(selectSignatureButton);
260: builder.append(panel, 3);
261: builder.nextLine();
262:
263: /*
264: * JPanel innerPanel = builder.getPanel();
265: * FormDebugUtils.dumpAll(innerPanel); setLayout(new BorderLayout());
266: * add(innerPanel, BorderLayout.CENTER);
267: */
268: /*
269: * setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
270: *
271: * GridBagLayout mainLayout = new GridBagLayout(); GridBagConstraints
272: * mainConstraints = new GridBagConstraints();
273: *
274: * mainConstraints.anchor = GridBagConstraints.NORTHWEST;
275: * mainConstraints.fill = GridBagConstraints.HORIZONTAL;
276: * mainConstraints.weightx = 1.0;
277: *
278: * setLayout(mainLayout);
279: *
280: * JPanel accountPanel = new JPanel(); Border b1 =
281: * BorderFactory.createEtchedBorder(); Border b2 =
282: * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
283: * "dialog", "account", "account_information"));
284: *
285: * Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
286: * Border border = BorderFactory.createCompoundBorder(b2, emptyBorder);
287: * accountPanel.setBorder(border);
288: *
289: * GridBagLayout layout = new GridBagLayout(); GridBagConstraints c =
290: * new GridBagConstraints(); accountPanel.setLayout(layout);
291: *
292: * //defaultAccountCheckBox.setEnabled(false);
293: *
294: * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
295: * GridBagConstraints.NORTHWEST; c.weightx = 0.1; c.gridwidth =
296: * GridBagConstraints.RELATIVE; layout.setConstraints(accountnameLabel,
297: * c); accountPanel.add(accountnameLabel);
298: *
299: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
300: * layout.setConstraints(accountnameTextField, c);
301: * accountPanel.add(accountnameTextField);
302: *
303: * c.gridwidth = GridBagConstraints.REMAINDER;
304: * layout.setConstraints(defaultAccountCheckBox, c);
305: * accountPanel.add(defaultAccountCheckBox);
306: *
307: * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
308: *
309: * mainLayout.setConstraints(accountPanel, mainConstraints);
310: * add(accountPanel);
311: *
312: * JPanel neededPanel = new JPanel(); b1 =
313: * BorderFactory.createEtchedBorder(); b2 =
314: * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
315: * "dialog", "account", "needed_information"));
316: *
317: * emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5); border =
318: * BorderFactory.createCompoundBorder(b2, emptyBorder);
319: * neededPanel.setBorder(border);
320: *
321: * layout = new GridBagLayout(); c = new GridBagConstraints();
322: * neededPanel.setLayout(layout);
323: *
324: * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
325: * GridBagConstraints.NORTHWEST; c.weightx = 0.1;
326: *
327: * c.gridwidth = GridBagConstraints.RELATIVE;
328: * layout.setConstraints(nameLabel, c); neededPanel.add(nameLabel);
329: *
330: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
331: * layout.setConstraints(nameTextField, c);
332: * neededPanel.add(nameTextField);
333: *
334: * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
335: * layout.setConstraints(addressLabel, c);
336: * neededPanel.add(addressLabel);
337: *
338: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
339: * layout.setConstraints(addressTextField, c);
340: * neededPanel.add(addressTextField);
341: *
342: * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
343: *
344: * mainLayout.setConstraints(neededPanel, mainConstraints);
345: * add(neededPanel);
346: *
347: * JPanel optionalPanel = new JPanel(); b1 =
348: * BorderFactory.createEtchedBorder(); b2 =
349: * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
350: * "dialog", "account", "optional_information"));
351: *
352: * emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5); border =
353: * BorderFactory.createCompoundBorder(b2, emptyBorder);
354: *
355: * optionalPanel.setBorder(border);
356: *
357: * layout = new GridBagLayout(); c = new GridBagConstraints();
358: * optionalPanel.setLayout(layout);
359: *
360: * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
361: * GridBagConstraints.NORTHWEST; c.weightx = 0.1;
362: *
363: * c.gridwidth = GridBagConstraints.RELATIVE;
364: * layout.setConstraints(replyaddressLabel, c);
365: * optionalPanel.add(replyaddressLabel);
366: *
367: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
368: * layout.setConstraints(replyaddressTextField, c);
369: * optionalPanel.add(replyaddressTextField);
370: *
371: * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
372: * layout.setConstraints(organisationLabel, c);
373: * optionalPanel.add(organisationLabel);
374: *
375: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
376: * layout.setConstraints(organisationTextField, c);
377: * optionalPanel.add(organisationTextField);
378: *
379: * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
380: * layout.setConstraints(attachsignatureCheckBox, c);
381: * optionalPanel.add(attachsignatureCheckBox);
382: *
383: * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
384: * layout.setConstraints(selectSignatureButton, c);
385: * optionalPanel.add(selectSignatureButton);
386: *
387: * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
388: * mainLayout.setConstraints(optionalPanel, mainConstraints);
389: * add(optionalPanel);
390: *
391: * mainConstraints.gridheight = GridBagConstraints.REMAINDER;
392: * mainConstraints.weighty = 1.0; mainConstraints.fill =
393: * GridBagConstraints.VERTICAL; Component vglue =
394: * Box.createVerticalGlue(); mainLayout.setConstraints(vglue,
395: * mainConstraints); add(vglue);
396: */
397: }
398:
399: public boolean isFinished() {
400: String address = addressTextField.getText();
401: if (accountnameTextField.getText().length() == 0) {
402: JOptionPane.showMessageDialog(this , MailResourceLoader
403: .getString("dialog", "account", "namelabel"));
404: return false;
405: } else if (address.length() == 0) {
406: JOptionPane.showMessageDialog(this , MailResourceLoader
407: .getString("dialog", "account", "addresslabel"));
408: return false;
409: } else {
410: try {
411: Address.parse(address);
412: } catch (ParserException pe) {
413: JOptionPane.showMessageDialog(this , MailResourceLoader
414: .getString("dialog", "account",
415: "invalidaddress"));
416: return false;
417: }
418: }
419: return true;
420: }
421:
422: public void actionPerformed(ActionEvent e) {
423: String action = e.getActionCommand();
424:
425: if (action.equals("CHOOSE")) {
426: JFileChooser fc = new JFileChooser();
427: if (account.getIdentity().getSignature() != null) {
428: fc
429: .setSelectedFile(account.getIdentity()
430: .getSignature());
431: }
432:
433: int returnVal = fc.showOpenDialog(this );
434:
435: if (returnVal == JFileChooser.APPROVE_OPTION) {
436: File file = fc.getSelectedFile();
437: selectSignatureButton.setText(file.getPath());
438: }
439: }
440: }
441: }
|