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

Last change on this file since 5331 was 4933, checked in by jmt12, 21 years ago

Major CDM rewrite so it uses DOM.

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