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:
017: package org.columba.mail.gui.config.accountwizard;
018:
019: import java.lang.reflect.Method;
020:
021: import javax.swing.Box;
022: import javax.swing.BoxLayout;
023: import javax.swing.JComponent;
024: import javax.swing.JLabel;
025: import javax.swing.JPanel;
026: import javax.swing.JTextField;
027: import javax.swing.SwingUtilities;
028: import javax.swing.event.DocumentEvent;
029: import javax.swing.event.DocumentListener;
030:
031: import net.javaprog.ui.wizard.AbstractStep;
032: import net.javaprog.ui.wizard.DataLookup;
033: import net.javaprog.ui.wizard.DataModel;
034: import net.javaprog.ui.wizard.DefaultDataLookup;
035:
036: import org.columba.core.gui.base.LabelWithMnemonic;
037: import org.columba.core.gui.base.MultiLineLabel;
038: import org.columba.core.gui.base.WizardTextField;
039: import org.columba.mail.util.MailResourceLoader;
040: import org.columba.ristretto.message.Address;
041: import org.columba.ristretto.parser.ParserException;
042:
043: class IdentityStep extends AbstractStep {
044: protected DataModel data;
045: protected JTextField nameTextField;
046: protected JTextField addressTextField;
047: protected JTextField accountNameTextField;
048:
049: public IdentityStep(DataModel data) {
050: super (MailResourceLoader.getString("dialog", "accountwizard",
051: "identity"), MailResourceLoader.getString("dialog",
052: "accountwizard", "identity_description"));
053: this .data = data;
054: setCanGoNext(false);
055: }
056:
057: protected JComponent createComponent() {
058: JComponent component = new JPanel();
059: component.setLayout(new BoxLayout(component, BoxLayout.Y_AXIS));
060: component.add(new MultiLineLabel(MailResourceLoader.getString(
061: "dialog", "accountwizard", "identity_text")));
062: component.add(Box.createVerticalStrut(40));
063:
064: WizardTextField middlePanel = new WizardTextField();
065:
066: LabelWithMnemonic nameLabel = new LabelWithMnemonic(
067: MailResourceLoader.getString("dialog", "accountwizard",
068: "name"));
069: middlePanel.addLabel(nameLabel);
070: nameTextField = new JTextField();
071:
072: DocumentListener fieldListener = new DocumentListener() {
073: public void removeUpdate(DocumentEvent e) {
074: checkFields();
075: }
076:
077: public void insertUpdate(DocumentEvent e) {
078: checkFields();
079: }
080:
081: protected void checkFields() {
082: String s = addressTextField.getText();
083: setCanGoNext((nameTextField.getDocument().getLength() > 0)
084: && (s.length() > 0)
085: && isEmailAddress(s)
086: && (accountNameTextField.getDocument()
087: .getLength() > 0));
088: }
089:
090: protected boolean isEmailAddress(String s) {
091: int at = s.indexOf('@');
092: int lastDot = s.lastIndexOf('.');
093:
094: return (at > 0) && (lastDot > (at + 1))
095: && (lastDot < (s.length() - 1));
096: }
097:
098: public void changedUpdate(DocumentEvent e) {
099: }
100: };
101:
102: nameTextField.getDocument().addDocumentListener(fieldListener);
103: nameLabel.setLabelFor(nameTextField);
104: middlePanel.addTextField(nameTextField);
105:
106: JLabel exampleLabel = new JLabel(MailResourceLoader.getString(
107: "dialog", "accountwizard", "example")
108: + "Bill Gates");
109: middlePanel.addExample(exampleLabel);
110:
111: LabelWithMnemonic addressLabel = new LabelWithMnemonic(
112: MailResourceLoader.getString("dialog", "accountwizard",
113: "address"));
114: middlePanel.addLabel(addressLabel);
115: addressTextField = new JTextField();
116: addressTextField.getDocument().addDocumentListener(
117: fieldListener);
118: addressLabel.setLabelFor(addressTextField);
119: middlePanel.addTextField(addressTextField);
120: middlePanel.addExample(new JLabel(MailResourceLoader.getString(
121: "dialog", "accountwizard", "example")
122: + "BillGates@microsoft.com"));
123:
124: LabelWithMnemonic accountNameLabel = new LabelWithMnemonic(
125: MailResourceLoader.getString("dialog", "accountwizard",
126: "accountname"));
127: middlePanel.addLabel(accountNameLabel);
128: accountNameTextField = new JTextField();
129: Method method = null;
130: try {
131: method = accountNameTextField.getClass().getMethod(
132: "getText", null);
133: } catch (NoSuchMethodException nsme) {
134: }
135: data.registerDataLookup("Identity.accountName",
136: new DefaultDataLookup(accountNameTextField, method,
137: null));
138: accountNameTextField.getDocument().addDocumentListener(
139: fieldListener);
140: accountNameLabel.setLabelFor(accountNameTextField);
141: middlePanel.addTextField(accountNameTextField);
142: middlePanel.addExample(new JLabel(MailResourceLoader.getString(
143: "dialog", "accountwizard", "example")
144: + "Bill's private mail"));
145: component.add(middlePanel);
146:
147: data.registerDataLookup("Identity.address", new DataLookup() {
148: public Object lookupData() {
149: try {
150: Address address = Address.parse(addressTextField
151: .getText());
152: address.setDisplayName(nameTextField.getText());
153: return address;
154: } catch (ParserException pe) {
155: return null;
156: }
157: }
158: });
159: return component;
160: }
161:
162: public void prepareRendering() {
163: SwingUtilities.invokeLater(new Runnable() {
164: public void run() {
165: nameTextField.requestFocus();
166: }
167: });
168: }
169: }
|