KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > config > account > IdentityPanel


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.mail.gui.config.account;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.io.File JavaDoc;
24
25 import javax.swing.JButton JavaDoc;
26 import javax.swing.JCheckBox JavaDoc;
27 import javax.swing.JFileChooser JavaDoc;
28 import javax.swing.JLabel JavaDoc;
29 import javax.swing.JOptionPane JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32
33 import org.columba.core.gui.base.CheckBoxWithMnemonic;
34 import org.columba.core.gui.base.LabelWithMnemonic;
35 import org.columba.mail.config.AccountItem;
36 import org.columba.mail.config.Identity;
37 import org.columba.mail.config.MailConfig;
38 import org.columba.mail.folder.imap.IMAPRootFolder;
39 import org.columba.mail.gui.tree.FolderTreeModel;
40 import org.columba.mail.util.MailResourceLoader;
41 import org.columba.ristretto.message.Address;
42 import org.columba.ristretto.parser.ParserException;
43
44 import com.jgoodies.forms.builder.DefaultFormBuilder;
45 import com.jgoodies.forms.layout.FormLayout;
46
47 public class IdentityPanel extends DefaultPanel implements ActionListener JavaDoc {
48     private JLabel JavaDoc nameLabel;
49     private JTextField JavaDoc nameTextField;
50     private JLabel JavaDoc addressLabel;
51     private JTextField JavaDoc addressTextField;
52     private JLabel JavaDoc organisationLabel;
53     private JTextField JavaDoc organisationTextField;
54     private JLabel JavaDoc replyaddressLabel;
55     private JTextField JavaDoc replyaddressTextField;
56     private JLabel JavaDoc accountnameLabel;
57     private JTextField JavaDoc accountnameTextField;
58     private JCheckBox JavaDoc defaultAccountCheckBox;
59     private JButton JavaDoc selectSignatureButton;
60     private JCheckBox JavaDoc attachsignatureCheckBox;
61     private AccountItem account;
62     private JButton JavaDoc editSignatureButton;
63     
64     //private ConfigFrame frame;
65
public IdentityPanel(AccountItem account) {
66         super();
67
68         //this.frame = frame;
69
this.account = account;
70
71         initComponents();
72         layoutComponents();
73         updateComponents(true);
74     }
75
76     protected void updateComponents(boolean b) {
77         Identity identity = account.getIdentity();
78         if (b) {
79             accountnameTextField.setText(account.getName());
80             Address address = identity.getAddress();
81             nameTextField.setText(address.getDisplayName());
82             addressTextField.setText(address.getMailAddress());
83             address = identity.getReplyToAddress();
84             replyaddressTextField.setText(
85                     address == null ? "" : address.getMailAddress());
86             organisationTextField.setText(identity.getOrganisation());
87             File JavaDoc signature = identity.getSignature();
88             selectSignatureButton.setText(
89                     signature == null ? new File JavaDoc(System.getProperty("user.home"), ".signature").getPath() : signature.getPath());
90
91             attachsignatureCheckBox.setSelected(signature != null);
92
93             defaultAccountCheckBox.setSelected(
94                     MailConfig.getInstance().getAccountList().getDefaultAccountUid()
95                         == account.getInteger("uid"));
96         } else {
97             try {
98                 Address address = Address.parse(addressTextField.getText());
99                 if (nameTextField.getText() != null) {
100                     address.setDisplayName(nameTextField.getText());
101                 }
102                 identity.setAddress(address);
103                 if (replyaddressTextField.getText().length() > 0) {
104                     address = Address.parse(replyaddressTextField.getText());
105                 } else {
106                     address = null;
107                 }
108                 identity.setReplyToAddress(address);
109             } catch (ParserException pe) {} //does not occur
110
identity.setOrganisation(organisationTextField.getText());
111             if (attachsignatureCheckBox.isSelected()) {
112                 identity.setSignature(new File JavaDoc(selectSignatureButton.getText()));
113             } else {
114                 identity.setSignature(null);
115             }
116
117             if( !account.getName().equals(accountnameTextField.getText())) {
118                 account.setName(accountnameTextField.getText());
119                 if( !account.isPopAccount()) {
120                     // Account is an IMAP account -> change root folder name
121

122                     IMAPRootFolder imapRoot = (IMAPRootFolder) FolderTreeModel.getInstance().getImapFolder(account.getUid());
123                     try {
124                         imapRoot.setName(accountnameTextField.getText());
125                         FolderTreeModel.getInstance().nodeStructureChanged(imapRoot);
126                     } catch (Exception JavaDoc e) {
127                     }
128                 }
129             }
130
131             if (defaultAccountCheckBox.isSelected()) {
132                 MailConfig.getInstance().getAccountList().setDefaultAccount(
133                     account.getUid());
134             }
135         }
136     }
137
138     protected void initComponents() {
139         accountnameLabel = new LabelWithMnemonic(MailResourceLoader.getString(
140                     "dialog", "account", "identity_accountname"));
141
142         accountnameTextField = new JTextField JavaDoc();
143         accountnameLabel.setLabelFor(accountnameTextField);
144         defaultAccountCheckBox = new CheckBoxWithMnemonic(MailResourceLoader.getString(
145                     "dialog", "account", "defaultaccount"));
146
147         nameLabel = new LabelWithMnemonic(MailResourceLoader.getString(
148                     "dialog", "account", "yourname"));
149
150         nameTextField = new JTextField JavaDoc();
151         nameLabel.setLabelFor(nameTextField);
152         addressLabel = new LabelWithMnemonic(MailResourceLoader.getString(
153                     "dialog", "account", "address"));
154
155         addressTextField = new JTextField JavaDoc();
156         addressLabel.setLabelFor(addressTextField);
157         replyaddressLabel = new LabelWithMnemonic(MailResourceLoader.getString(
158                     "dialog", "account", "replytoaddress"));
159
160         organisationLabel = new LabelWithMnemonic(MailResourceLoader.getString(
161                     "dialog", "account", "organisation"));
162
163         replyaddressTextField = new JTextField JavaDoc();
164         replyaddressLabel.setLabelFor(replyaddressTextField);
165         organisationTextField = new JTextField JavaDoc();
166         organisationLabel.setLabelFor(organisationTextField);
167
168         attachsignatureCheckBox = new CheckBoxWithMnemonic(MailResourceLoader.getString(
169                     "dialog", "account", "attachthissignature"));
170
171         selectSignatureButton = new JButton JavaDoc("~/.signature");
172         selectSignatureButton.setActionCommand("CHOOSE");
173         selectSignatureButton.addActionListener(this);
174         
175         editSignatureButton = new JButton JavaDoc(new EditSignatureAction(null,account));
176     }
177
178     protected void layoutComponents() {
179         // Create a FormLayout instance.
180
FormLayout layout = new FormLayout("10dlu, max(70dlu;default), 3dlu, fill:max(150dlu;default):grow",
181                 
182             // 2 columns
183
""); // rows are added dynamically (no need to define them here)
184

185         // create a form builder
186
DefaultFormBuilder builder = new DefaultFormBuilder(layout, this);
187
188         // create EmptyBorder between components and dialog-frame
189
builder.setDefaultDialogBorder();
190
191         // skip the first column
192
builder.setLeadingColumnOffset(1);
193
194         // Add components to the panel:
195
builder.appendSeparator(MailResourceLoader.getString("dialog",
196                 "account", "account_information"));
197         builder.nextLine();
198
199         builder.append(accountnameLabel, 1);
200         builder.append(accountnameTextField);
201         builder.nextLine();
202
203         builder.append(defaultAccountCheckBox, 3);
204         builder.nextLine();
205
206         builder.appendSeparator(MailResourceLoader.getString("dialog",
207                 "account", "needed_information"));
208         builder.nextLine();
209
210         builder.append(nameLabel, 1);
211         builder.append(nameTextField);
212         builder.nextLine();
213
214         builder.append(addressLabel, 1);
215         builder.append(addressTextField);
216         builder.nextLine();
217
218         builder.appendSeparator(MailResourceLoader.getString("dialog",
219                 "account", "optional_information"));
220         builder.nextLine();
221
222         builder.append(organisationLabel, 1);
223         builder.append(organisationTextField);
224         builder.nextLine();
225
226         builder.append(replyaddressLabel, 1);
227         builder.append(replyaddressTextField);
228         builder.nextLine(1);
229         
230
231         JPanel JavaDoc panel = new JPanel JavaDoc();
232         FormLayout l = new FormLayout("max(100;default), 3dlu, left:max(50dlu;default), 3dlu, left:max(50dlu;default)",
233                 
234             // 3 columns
235
"fill:default:grow"); // rows are added dynamically (no need to define them here)
236

237         // create a form builder
238
DefaultFormBuilder b = new DefaultFormBuilder(l, panel);
239         
240         b.append(attachsignatureCheckBox, selectSignatureButton, editSignatureButton);
241
242         //b.append(selectSignatureButton);
243
builder.append(panel, 3);
244         builder.nextLine();
245
246         /*
247          * JPanel innerPanel = builder.getPanel();
248          * FormDebugUtils.dumpAll(innerPanel); setLayout(new BorderLayout());
249          * add(innerPanel, BorderLayout.CENTER);
250          */

251         /*
252          * setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
253          *
254          * GridBagLayout mainLayout = new GridBagLayout(); GridBagConstraints
255          * mainConstraints = new GridBagConstraints();
256          *
257          * mainConstraints.anchor = GridBagConstraints.NORTHWEST;
258          * mainConstraints.fill = GridBagConstraints.HORIZONTAL;
259          * mainConstraints.weightx = 1.0;
260          *
261          * setLayout(mainLayout);
262          *
263          * JPanel accountPanel = new JPanel(); Border b1 =
264          * BorderFactory.createEtchedBorder(); Border b2 =
265          * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
266          * "dialog", "account", "account_information"));
267          *
268          * Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
269          * Border border = BorderFactory.createCompoundBorder(b2, emptyBorder);
270          * accountPanel.setBorder(border);
271          *
272          * GridBagLayout layout = new GridBagLayout(); GridBagConstraints c =
273          * new GridBagConstraints(); accountPanel.setLayout(layout);
274          *
275          * //defaultAccountCheckBox.setEnabled(false);
276          *
277          * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
278          * GridBagConstraints.NORTHWEST; c.weightx = 0.1; c.gridwidth =
279          * GridBagConstraints.RELATIVE; layout.setConstraints(accountnameLabel,
280          * c); accountPanel.add(accountnameLabel);
281          *
282          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
283          * layout.setConstraints(accountnameTextField, c);
284          * accountPanel.add(accountnameTextField);
285          *
286          * c.gridwidth = GridBagConstraints.REMAINDER;
287          * layout.setConstraints(defaultAccountCheckBox, c);
288          * accountPanel.add(defaultAccountCheckBox);
289          *
290          * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
291          *
292          * mainLayout.setConstraints(accountPanel, mainConstraints);
293          * add(accountPanel);
294          *
295          * JPanel neededPanel = new JPanel(); b1 =
296          * BorderFactory.createEtchedBorder(); b2 =
297          * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
298          * "dialog", "account", "needed_information"));
299          *
300          * emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5); border =
301          * BorderFactory.createCompoundBorder(b2, emptyBorder);
302          * neededPanel.setBorder(border);
303          *
304          * layout = new GridBagLayout(); c = new GridBagConstraints();
305          * neededPanel.setLayout(layout);
306          *
307          * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
308          * GridBagConstraints.NORTHWEST; c.weightx = 0.1;
309          *
310          * c.gridwidth = GridBagConstraints.RELATIVE;
311          * layout.setConstraints(nameLabel, c); neededPanel.add(nameLabel);
312          *
313          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
314          * layout.setConstraints(nameTextField, c);
315          * neededPanel.add(nameTextField);
316          *
317          * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
318          * layout.setConstraints(addressLabel, c);
319          * neededPanel.add(addressLabel);
320          *
321          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
322          * layout.setConstraints(addressTextField, c);
323          * neededPanel.add(addressTextField);
324          *
325          * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
326          *
327          * mainLayout.setConstraints(neededPanel, mainConstraints);
328          * add(neededPanel);
329          *
330          * JPanel optionalPanel = new JPanel(); b1 =
331          * BorderFactory.createEtchedBorder(); b2 =
332          * BorderFactory.createTitledBorder( b1, MailResourceLoader.getString(
333          * "dialog", "account", "optional_information"));
334          *
335          * emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5); border =
336          * BorderFactory.createCompoundBorder(b2, emptyBorder);
337          *
338          * optionalPanel.setBorder(border);
339          *
340          * layout = new GridBagLayout(); c = new GridBagConstraints();
341          * optionalPanel.setLayout(layout);
342          *
343          * c.fill = GridBagConstraints.HORIZONTAL; c.anchor =
344          * GridBagConstraints.NORTHWEST; c.weightx = 0.1;
345          *
346          * c.gridwidth = GridBagConstraints.RELATIVE;
347          * layout.setConstraints(replyaddressLabel, c);
348          * optionalPanel.add(replyaddressLabel);
349          *
350          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
351          * layout.setConstraints(replyaddressTextField, c);
352          * optionalPanel.add(replyaddressTextField);
353          *
354          * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
355          * layout.setConstraints(organisationLabel, c);
356          * optionalPanel.add(organisationLabel);
357          *
358          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
359          * layout.setConstraints(organisationTextField, c);
360          * optionalPanel.add(organisationTextField);
361          *
362          * c.gridwidth = GridBagConstraints.RELATIVE; c.weightx = 0.1;
363          * layout.setConstraints(attachsignatureCheckBox, c);
364          * optionalPanel.add(attachsignatureCheckBox);
365          *
366          * c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 0.9;
367          * layout.setConstraints(selectSignatureButton, c);
368          * optionalPanel.add(selectSignatureButton);
369          *
370          * mainConstraints.gridwidth = GridBagConstraints.REMAINDER;
371          * mainLayout.setConstraints(optionalPanel, mainConstraints);
372          * add(optionalPanel);
373          *
374          * mainConstraints.gridheight = GridBagConstraints.REMAINDER;
375          * mainConstraints.weighty = 1.0; mainConstraints.fill =
376          * GridBagConstraints.VERTICAL; Component vglue =
377          * Box.createVerticalGlue(); mainLayout.setConstraints(vglue,
378          * mainConstraints); add(vglue);
379          */

380     }
381
382     public boolean isFinished() {
383         String JavaDoc address = addressTextField.getText();
384         if (accountnameTextField.getText().length() == 0) {
385             JOptionPane.showMessageDialog(this,
386                 MailResourceLoader.getString("dialog", "account", "namelabel"));
387             return false;
388         } else if (address.length() == 0) {
389             JOptionPane.showMessageDialog(this,
390                 MailResourceLoader.getString("dialog", "account", "addresslabel"));
391             return false;
392         } else {
393             try {
394                 Address.parse(address);
395             } catch (ParserException pe) {
396                 JOptionPane.showMessageDialog(this,
397                     MailResourceLoader.getString("dialog", "account", "invalidaddress"));
398                 return false;
399             }
400         }
401         return true;
402     }
403
404     public void actionPerformed(ActionEvent JavaDoc e) {
405         String JavaDoc action = e.getActionCommand();
406
407         if (action.equals("CHOOSE")) {
408             JFileChooser JavaDoc fc = new JFileChooser JavaDoc();
409             if( account.getIdentity().getSignature() != null ) {
410                 fc.setSelectedFile(account.getIdentity().getSignature());
411             }
412             
413             int returnVal = fc.showOpenDialog(this);
414
415             if (returnVal == JFileChooser.APPROVE_OPTION) {
416                 File JavaDoc file = fc.getSelectedFile();
417                 selectSignatureButton.setText(file.getPath());
418             }
419         }
420     }
421 }
422
Popular Tags