KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > filetypes > Association


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 package org.jdesktop.jdic.filetypes;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import org.jdesktop.jdic.filetypes.internal.AppUtility;
28
29
30 /**
31  * This class represents a file type association.
32  * <P>
33  * A file type association contains a description, the MIME type, file extensions,
34  * icon file, actions which are represented by <code>Action</code> objects, and
35  * stored MIME file name for Gnome/UNIX platforms.
36  * <p>
37  * An association could be registered into or unregistered from the system using
38  * certain methods of <code>AssociationService</code>. An association for a particular
39  * file type could be returned by certain methods of <code>AssociationService</code>.
40  * <p>
41  *
42  * @see Action
43  * @see AssociationService
44  */

45 public class Association {
46   
47     /**
48      * The name of the generated or removed MIME files on Gnome while
49      * registering/unregistering an association.
50      */

51     private String JavaDoc name;
52
53     /**
54      * Description of the association.
55      */

56     private String JavaDoc description;
57   
58     /**
59      * Mime type of the association.
60      */

61     private String JavaDoc mimeType;
62   
63     /**
64      * File extension list of the association.
65      * <P>
66      * For Gnome/Unix platforms, all the file extentions in the list will be used.
67      * For Microsoft Windows platforms, only the first file extension in the list is used.
68      *
69      */

70     private List JavaDoc fileExtensionList;
71   
72     /**
73      * Icon file name of the association.
74      */

75     private String JavaDoc iconFileName;
76   
77     /**
78      * Action list of the association.
79      */

80     private List JavaDoc actionList;
81     
82     /**
83      * Hashcode for this association
84      */

85     private int hashcode;
86   
87     /**
88      * Returns the name of the MIME files the association is stored in
89      * for Gnome/Unix platforms.
90      * <p>
91      * For Gnome/Unix platforms, the association is stored in plain text files:
92      * <code>name</code>.mime, <code>name</code>.keys and <code>name</code>.applications.
93      * While registering or unregistering an association using certain methods
94      * of <code>AssociationService</code>, the MIME files with the given name
95      * are created or removed.
96      * <p>
97      * For Microsoft Windows platforms, the association is stored in the registry,
98      * this name is not used.
99      *
100      * @return the MIME file name.
101      */

102     public String JavaDoc getName() {
103         return name;
104     }
105   
106     /**
107      * Returns the name of the MIME files the association is stored in
108      * for Gnome/Unix platforms.
109      *
110      * @param name a given name value.
111      */

112     public void setName(String JavaDoc name) {
113         if (name == null) {
114             throw new IllegalArgumentException JavaDoc("The given MIME file name is null.");
115         }
116         
117         this.name = name;
118     }
119
120     /**
121      * Returns the description string of this <code>Association</code>.
122      *
123      * @return the description of this association.
124      */

125     public String JavaDoc getDescription() {
126         return description;
127     }
128   
129     /**
130      * Sets the description string of this <code>Association</code>.
131      *
132      * @param description a given description value.
133      */

134     public void setDescription(String JavaDoc description) {
135         if (description == null) {
136             throw new IllegalArgumentException JavaDoc("The given description is null.");
137         }
138
139         this.description = description;
140     }
141   
142     /**
143      * Returns the MIME type of this <code>Association</code>.
144      *
145      * @return the MIME type.
146      */

147     public String JavaDoc getMimeType() {
148         return mimeType;
149     }
150   
151     /**
152      * Sets the MIME type of this <code>Association</code>.
153      *
154      * @param mimeType a given MIME type.
155      */

156     public void setMimeType(String JavaDoc mimeType) {
157         if (mimeType == null) {
158             throw new IllegalArgumentException JavaDoc("The given MIME type is null.");
159         }
160         
161         this.mimeType = mimeType;
162     }
163   
164     /**
165      * Adds one file extension to the file extension list of this <code>Association</code>.
166      * If the given file extension already exists in the file extension
167      * list, no changes are made to the file extension list.
168      * <P>
169      * The specified file extension could have a leading '.' character or not.
170      * <P>
171      * For Microsoft Windows platforms, only the first file extension is used during
172      * registeration.
173      *
174      * @param fileExt a given file extension.
175      * @return <code>true</code> if the given file extension is added successfully
176      * to the file extension list; <code>false</code> otherwise.
177      */

178     public boolean addFileExtension(String JavaDoc fileExt) {
179         if (fileExt == null) {
180             throw new IllegalArgumentException JavaDoc("The given file extension is null.");
181         }
182     
183         // Add the leading '.' character to the given file extension if not exists.
184
fileExt = AppUtility.addDotToFileExtension(fileExt);
185         
186         if (fileExtensionList == null) {
187             fileExtensionList = new ArrayList JavaDoc();
188         }
189         
190         return fileExtensionList.add(fileExt);
191     }
192   
193     /**
194      * Removes the given file extension from the file extension list of this
195      * <code>Association</code>. If the file extension is not contained in the file
196      * extension list, no changes are made to the file extension list.
197      * <P>
198      * The specified file extension may have a leading '.' character or not.
199      *
200      * @param fileExt a given file extension.
201      * @return <code>true</code> if the given file extension is removed successfully
202      * from the file extension list; <code>false</code> otherwise.
203      */

204     public boolean removeFileExtension(String JavaDoc fileExt) {
205         if (fileExt == null) {
206             throw new IllegalArgumentException JavaDoc("The given file extension is null.");
207         }
208         
209         // Add the leading '.' character to the given file extension if not exists.
210
fileExt = AppUtility.addDotToFileExtension(fileExt);
211         if (fileExtensionList != null) {
212             return fileExtensionList.remove(fileExt);
213         }
214         
215         return false;
216     }
217   
218     /**
219      * Returns the file extension list of this <code>Association</code>.
220      *
221      * @return the file extension list of the association.
222      */

223     public List JavaDoc getFileExtList() {
224         // Make defensive copy
225
if (fileExtensionList == null) {
226             return null;
227         } else {
228             List JavaDoc retList = new ArrayList JavaDoc();
229             
230             Iterator JavaDoc iter = fileExtensionList.iterator();
231             while (iter.hasNext()) {
232                 retList.add(iter.next());
233             }
234             
235             return retList;
236         }
237     }
238   
239     /**
240      * Returns the icon file name representing this <code>Association</code>.
241      *
242      * @return the icon file name for this association.
243      */

244     public String JavaDoc getIconFileName() {
245         return iconFileName;
246     }
247   
248     /**
249      * Sets the icon file name representing this <code>Association</code>.
250      * <P>
251      * For Microsoft Windows platforms, the given icon file will be registered
252      * only if the given file extension list is not empty.
253      *
254      * @param fileName a given icon file name.
255      */

256     public void setIconFileName(String JavaDoc fileName) {
257         if (fileName == null) {
258             throw new IllegalArgumentException JavaDoc("The given icon file name is null.");
259         }
260
261         this.iconFileName = fileName;
262     }
263   
264     /**
265      * Adds a given action to the action list of this <code>Association</code>.
266      * If the given action already exists in the action list, no changes are
267      * made to the action list.
268      * <P>
269      * A valid action should not have null verb or command field.
270      * <P>
271      * For Microsoft Windows platforms, an association with non-empty action list
272      * would be valid for registration when the file extension list is not empty.
273      *
274      * @param action a given action.
275      * @return <code>true</code> if the given action is added successfully
276      * to the action list; <code>false</code> otherwise.
277      */

278     public boolean addAction(Action action) {
279         if (action == null) {
280             throw new IllegalArgumentException JavaDoc("The given action is null.");
281         }
282         
283         // Check the specified action object has no null verb and command field.
284
if (action.getVerb() == null) {
285             throw new IllegalArgumentException JavaDoc("the given action object has null verb field.");
286         } else if (action.getCommand() == null) {
287             throw new IllegalArgumentException JavaDoc("the given action object has null command field.");
288         }
289     
290         if (actionList == null) {
291             actionList = new ArrayList JavaDoc();
292         }
293         
294         return actionList.add(new Action(action.getVerb(), action.getCommand(),
295                         action.getDescription()));
296     }
297   
298     /**
299      * Removes a given action from the action list of this <code>Association</code>.
300      * If the action is not contained in the action list, no changes are made to
301      * the action list.
302      * <P>
303      * A valid action should not have null verb or command field.
304      *
305      * @param action a given action.
306      * @return <code>true</code> if the given action is removed successfully
307      * from the action list; <code>false</code> otherwise.
308      */

309     public boolean removeAction(Action action) {
310         if (action == null) {
311             throw new IllegalArgumentException JavaDoc("The given action is null.");
312         }
313
314         // Check the specified action object has no null verb and command field.
315
if ((action.getVerb() == null) || (action.getCommand() == null)) {
316             throw new IllegalArgumentException JavaDoc("the given action object has null verb field or command field.");
317         }
318         if (actionList != null) {
319             return actionList.remove(action);
320         }
321         
322         return false;
323     }
324   
325     /**
326      * Returns the action list of this <code>Association</code>.
327      *
328      * @return the action list of the association.
329      */

330     public List JavaDoc getActionList() {
331         // Make defensive copy
332
if (actionList == null) {
333             return null;
334         } else {
335             List JavaDoc retList = new ArrayList JavaDoc();
336             
337             Iterator JavaDoc iter = actionList.iterator();
338             while (iter.hasNext()) {
339                 retList.add(iter.next());
340             }
341             
342             return retList;
343         }
344     }
345
346     /**
347      * Returns the action, whose verb field is the same with the
348      * specified verb.
349      *
350      * @param verb the specified verb.
351      * @return the action with the specified verb; <code>null</code> if no
352      * approprate action is found.
353      */

354     public Action getActionByVerb(String JavaDoc verb) {
355         Iterator JavaDoc iter;
356     
357         if (actionList != null) {
358             iter = actionList.iterator();
359             if (iter != null) {
360                 while (iter.hasNext()) {
361                     Action temAction = (Action) iter.next();
362                     String JavaDoc temVerb = temAction.getVerb();
363
364                     if (temVerb.equalsIgnoreCase(verb)) {
365                         return temAction;
366                     }
367                 }
368             }
369         }
370
371         return null;
372     }
373   
374     /**
375      * Overrides the same method of <code>java.lang.Object</code>.
376      * <p>
377      * Determines whether or not two associations are equal. Two instances
378      * of <code>Association</code> are equal if the values of all the fields
379      * are the same.
380      *
381      * @param other an object to be compared with this <code>Association</code>.
382      * @return <code>true</code> if the object to be compared is an instance of
383      * <code>Association</code> and has the same values;
384      * <code>false</code> otherwise.
385      */

386     public boolean equals(Object JavaDoc other) {
387         if (!(other instanceof Association)) {
388             return false;
389         }
390         Association otherAssoc = (Association) other;
391     
392         /*
393          * Compares if the basic part of the association (description, iconfile, mimetype)
394          * equals
395          */

396         boolean isBasicEquals, isActionListEquals, isFileExtListEquals;
397         String JavaDoc otherDesc = otherAssoc.getDescription();
398         String JavaDoc otherIconFileName = otherAssoc.getIconFileName();
399         String JavaDoc otherMimeType = otherAssoc.getMimeType();
400
401         isBasicEquals = ((description == null
402                         ? otherDesc == null
403                         : description.equals(otherDesc))
404                 && (iconFileName == null
405                         ? otherIconFileName == null
406                         : iconFileName.equals(otherIconFileName))
407                 && (mimeType == null
408                         ? otherMimeType == null
409                         : mimeType.equals(otherMimeType)));
410                      
411         if (!isBasicEquals) {
412             return false;
413         }
414         
415         //Compare if the file extension list equals
416
List JavaDoc otherFileExtList = otherAssoc.getFileExtList();
417         isFileExtListEquals = false;
418         //fileExtlistEqulas when
419
//1. both file extension lists are null
420
//2. neither file extension lists is null and they have same elements
421
if ((fileExtensionList == null) && (otherFileExtList == null)) {
422             isFileExtListEquals = true;
423         } else if ((fileExtensionList != null) && (otherFileExtList != null)) {
424             if ((fileExtensionList.containsAll(otherFileExtList)) &&
425                 (otherFileExtList.containsAll(fileExtensionList))) {
426                 isFileExtListEquals = true;
427             }
428         }
429         if (!isFileExtListEquals) {
430             return false;
431         }
432
433         //Compare if the action list equals
434
List JavaDoc otherActionList = otherAssoc.getActionList();
435         isActionListEquals = false;
436         //action list Equlas when
437
//1. both action lists are null
438
//2. neither action lists is null and they have same elements
439
if ((actionList == null) && (otherActionList != null)) {
440             isActionListEquals = true;
441         } else if ((actionList != null) && (otherActionList != null)) {
442             if ((actionList.containsAll(otherActionList)) &&
443                 (otherActionList.containsAll(actionList))) {
444                 isActionListEquals = true;
445             }
446         }
447         
448         return isActionListEquals;
449     }
450     
451     /**
452      * Overrides the same method of <code>java.lang.Object</code>.
453      * <p>
454      * Returns the hashcode for this <code>Association</code>.
455      *
456      * @return a hash code for this <code>Association<code>.
457      */

458     public int hashCode() {
459         if (hashcode != 0) {
460             int result = 17;
461             if (this.name != null) {
462                 result = result * 37 + this.name.hashCode();
463             }
464             if (this.description != null) {
465                 result = result * 37 + this.description.hashCode();
466             }
467             if (this.mimeType != null) {
468                 result = result * 37 + this.mimeType.hashCode();
469             }
470             if (this.iconFileName != null) {
471                 result = result * 37 + this.iconFileName.hashCode();
472             }
473             if (this.fileExtensionList != null) {
474                 result = result * 37 + this.fileExtensionList.hashCode();
475             }
476             if (this.actionList != null) {
477                 result = result * 37 + this.actionList.hashCode();
478             }
479             hashcode = result;
480         }
481         return hashcode;
482     }
483     
484   
485     /**
486      * Overrides the same method of <code>java.lang.Object</code>.
487      * <p>
488      * Returns a <code>String</code> that represents the value of this
489      * <code>Association</code>.
490      *
491      * <PRE>
492      * The output of this object as a string would be like:
493      * MIME File Name:
494      * Description:
495      * MIME Type:
496      * Icon File:
497      * File Extension:
498      * Action List:
499      * Description:
500      * Verb:
501      * Command:
502      * </PRE>
503      * @return a string representation of this <code>Association</code>.
504      */

505     public String JavaDoc toString() {
506         String JavaDoc crlfString = "\r\n";
507         String JavaDoc content = "";
508         Iterator JavaDoc temIter;
509
510         content = content.concat("MIME File Name: ");
511         if (this.name != null) {
512             content = content.concat(name);
513         }
514         content = content.concat(crlfString);
515
516         content = content.concat("Description: ");
517         if (this.description != null) {
518             content = content.concat(description);
519         }
520         content = content.concat(crlfString);
521     
522         content = content.concat("MIME Type: ");
523         if (this.mimeType != null) {
524             content = content.concat(mimeType);
525         }
526         content = content.concat(crlfString);
527
528         
529         content = content.concat("Icon File: ");
530         if (this.iconFileName != null) {
531             content = content.concat(iconFileName);
532         }
533         content = content.concat(crlfString);
534     
535         content = content.concat("File Extension: ");
536         if (fileExtensionList != null) {
537             temIter = fileExtensionList.iterator();
538             if (temIter != null) {
539                 while (temIter.hasNext()) {
540                     content = content.concat((String JavaDoc) temIter.next());
541                     if (temIter.hasNext()) {
542                         content = content.concat(" ");
543                     }
544                 }
545             }
546         }
547         content = content.concat(crlfString);
548     
549         content = content.concat("Action List: ");
550         if (actionList != null) {
551             temIter = actionList.iterator();
552             if (temIter != null) {
553                 content = content.concat(crlfString);
554                 while (temIter.hasNext()) {
555                     Action temAction = (Action) temIter.next();
556                     content = content.concat(temAction.toString());
557                 }
558             }
559         }
560         content = content.concat(crlfString);
561     
562         return content;
563     }
564 }
565
Popular Tags