source: trunk/gli/src/org/greenstone/gatherer/util/EmailAddress.java@ 4364

Last change on this file since 4364 was 4364, checked in by mdewsnip, 21 years ago

Fixed tabbing.

  • Property svn:keywords set to Author Date Id Revision
File size: 10.0 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.util;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 03/05/02
50 * Revised: 04/10/02 - Commented
51 **************************************************************************************/
52import java.awt.BorderLayout;
53import java.awt.Color;
54import java.awt.GridLayout;
55import java.awt.event.KeyAdapter;
56import java.awt.event.KeyEvent;
57import java.util.StringTokenizer;
58import javax.swing.BorderFactory;
59import javax.swing.JLabel;
60import javax.swing.JPanel;
61import javax.swing.JTextField;
62import org.greenstone.gatherer.Gatherer;
63/** This class wraps an email address, separating address and host, and providing a pretty printer.
64 * @author John Thompson
65 * @version 2.3
66 */
67public class EmailAddress {
68 /** The controls for editing this email. */
69 private Control controls = null;
70 /** A reference to the Gatherer. */
71 private Gatherer gatherer = null;
72 /** The type of this email, in collection configuration terms. */
73 private int type = -1;
74 /** The host part of the email (after @). */
75 public String host = null;
76 /** The user part of the email (before @). */
77 public String user = null;
78 /** The name given to or associated with this particular email (maybe the persons name, or it this email belongs to a certain variable i.e Collection Author). */
79 public String name = null;
80 /** Enumeration of important email address types - in this case the creator of a collection. */
81 static final public int CREATOR = 0;
82 /** Enumeration of important email address types - in this case the maintainer of a collection. */
83 static final public int MAINTAINER = 1;
84 /** Enumeration of important email address types - in this case any other email address other than the two noted above. */
85 static final public int OTHER = 2;
86 /** Constructor.
87 * @param gatherer A reference to the <strong>Gatherer</strong> for messaging purposes.
88 */
89 public EmailAddress(Gatherer gatherer) {
90 this.gatherer = gatherer;
91 }
92 /** Constructor.
93 * @param gatherer A reference to the <strong>Gatherer</strong> for messaging purposes.
94 * @param raw A <strong>String</strong> containing both the name and email separated by whitespace.
95 */
96 public EmailAddress(Gatherer gatherer, String raw) {
97 this(gatherer);
98 StringTokenizer tokenizer = new StringTokenizer(raw);
99 if(tokenizer.countTokens() >= 2) {
100 this.name = tokenizer.nextToken();
101 if(name.equals("creator")) {
102 type = CREATOR;
103 }
104 else if(name.equals("maintainer")) {
105 type = MAINTAINER;
106 }
107 else {
108 type = OTHER;
109 }
110 String email = tokenizer.nextToken();
111 if(email.indexOf("@") != -1) {
112 this.user = email.substring(0, email.indexOf("@"));
113 this.host = email.substring(email.indexOf("@") + 1);
114 }
115 }
116 else {
117 this.name = "Invalid Email";
118 this.user = "";
119 this.host = "";
120 }
121 }
122 /** Retrieve the controls associated with editing this email address.
123 * @return A <strong>JPanel</strong> containing those controls.
124 */
125 public JPanel getControls() {
126 if(controls == null) {
127 controls = new Control(name);
128 }
129 return controls;
130 }
131 /** Retrieve the controls associated with editing this email address, but ensure they have a certain title label.
132 * @param label The <strong>String</strong> to use as the label.
133 * @return A <strong>JPanel</strong> containing those controls.
134 */
135 public JPanel getControls(String label) {
136 if(controls == null) {
137 controls = new Control(label);
138 }
139 return controls;
140 }
141 /** Method to produce this email as a string.
142 * @return A <strong>String</string> representing this email.
143 */
144 public String toString() {
145 return name + " " + user + "@" + host;
146 }
147 /** Determines if this is a valid email.
148 * @return <i>true</i> if it is current valid, <i>false</i> otherwise.
149 */
150 public boolean isValid() {
151 return (user != null && user.length() > 0) && (host != null && host.length() > 0);
152 }
153 /** Overloaded to call get with both a key and an empty argument array.
154 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
155 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatiically populated with formatting Strings of with argument String provided in the get call.
156 */
157 private String get(String key) {
158 return get(key, null);
159 }
160 /** Used to retrieve a property value from the Locale specific ResourceBundle, based upon the key and arguments supplied. If the key cannot be found or if some other part of the call fails a default (English) error message is returned. <BR>
161 * Here the get recieves a second argument which is an array of Strings used to populate argument fields, denoted {<I>n</I>}, within the value String returned. Note that argument numbers greater than or equal to 32 are automatically mapped to the formatting String named Farg<I>n</I>.
162 * @param key A <strong>String</strong> which is mapped to a initial String within the ResourceBundle.
163 * @param args A <strong>String[]</strong> used to populate argument fields within the complete String.
164 * @return A <strong>String</strong> which has been referenced by the key String and that either contains no argument fields, or has had the argument fields automatically populated with formatting Strings of with argument String provided in the get call.
165 * @see org.greenstone.gatherer.Dictionary
166 * @see org.greenstone.gatherer.Gatherer
167 */
168 private String get(String key, String args[]) {
169 if(key.indexOf('.') == -1) {
170 key = "CDM.General.Email." + key;
171 }
172 return gatherer.dictionary.get(key, args);
173 }
174 /** The controls used for editing an email address. Basically two text fields with an '@' label in the middle. You also have a title label for this control, which is known for the major email address types, or can be supplied for any other purpose. */
175 private class Control
176 extends JPanel {
177 /** The at symbol label. */
178 private JLabel at = null;
179 /** The title label. */
180 private JLabel label = null;
181 /** The first text field for the user part of an address. */
182 private JTextField field1 = null;
183 /** The second text field for the host part of an address. */
184 private JTextField field2 = null;
185 /** The panel on to which the text fields and the at label are placed. */
186 private JPanel middle = null;
187 /** Constructor.
188 * @param name The <strong>String</strong> to use for the title label.
189 * @see org.greenstone.gatherer.util.EmailAddress.Control.HostListener
190 * @see org.greenstone.gatherer.util.EmailAddress.Control.UserListener
191 */
192 public Control(String name) {
193 super();
194 switch(type) {
195 case CREATOR:
196 label = new JLabel(get("Creator"));
197 break;
198 case MAINTAINER:
199 label = new JLabel(get("Maintainer"));
200 break;
201 default:
202 label = new JLabel(name);
203 }
204 middle = new JPanel();
205 field1 = new JTextField(user);
206 at = new JLabel("@");
207 field2 = new JTextField(host);
208 // Add listeners
209 field1.addKeyListener(new UserListener());
210 field2.addKeyListener(new HostListener());
211 // Layout
212 label.setBorder(BorderFactory.createEmptyBorder(2,5,2,10));
213
214 at.setBorder(BorderFactory.createEmptyBorder(2,5,2,5));
215
216 middle.setLayout(new BorderLayout());
217 middle.add(field1, BorderLayout.CENTER);
218 middle.add(at, BorderLayout.EAST);
219
220 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
221 setLayout(new GridLayout(1,3));
222 add(label);
223 add(middle);
224 add(field2);
225 }
226 /** Listens for changes of the host part of the email address and updates the email object accordingly. */
227 private class HostListener
228 extends KeyAdapter {
229 /** Whenever a key is released (ie after a key typed event has been fired) when typing in the host field, this method updates the host field of the email object.
230 * @param event A <strong>KeyEvent</strong> containing extra information about the key release performed.
231 */
232 public void keyReleased(KeyEvent event) {
233 host = field2.getText();
234 }
235 }
236 /** Listens for changes of the user part of the email address and updates the email object accordingly. */
237 private class UserListener
238 extends KeyAdapter {
239 /** Whenever a key is released (ie after a key typed event has been fired) when typing in the user field, this method updates the user field of the email object.
240 * @param event A <strong>KeyEvent</strong> containing extra information about the key release performed.
241 */
242 public void keyReleased(KeyEvent event) {
243 user = field1.getText();
244 }
245 }
246
247 }
248}
Note: See TracBrowser for help on using the repository browser.