- Timestamp:
- 2003-05-27T15:40:47+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/util/EmailAddress.java
r4293 r4364 66 66 */ 67 67 public class EmailAddress { 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 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 208 // Add listeners 209 210 209 field1.addKeyListener(new UserListener()); 210 field2.addKeyListener(new HostListener()); 211 211 // Layout 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 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 230 * @param event A <strong>KeyEvent</strong> containing extra information about the key release performed. 231 231 */ 232 233 234 235 236 237 238 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 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 240 * @param event A <strong>KeyEvent</strong> containing extra information about the key release performed. 241 241 */ 242 243 244 245 242 public void keyReleased(KeyEvent event) { 243 user = field1.getText(); 244 } 245 } 246 246 247 247 } 248 248 } 249 250 251 252 253
Note:
See TracChangeset
for help on using the changeset viewer.