KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > model > CategoryModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.core.model;
12
13 import java.net.MalformedURLException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 import org.eclipse.update.core.Site;
18
19 /**
20  * Feature category definition model object.
21  * <p>
22  * This class may be instantiated or subclassed by clients. However, in most
23  * cases clients should instead instantiate or subclass the provided
24  * concrete implementation of this model.
25  * </p>
26  * <p>
27  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
28  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
29  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
30  * (repeatedly) as the API evolves.
31  * </p>
32  * @see org.eclipse.update.core.Category
33  * @since 2.0
34  */

35
36 public class CategoryModel extends ModelObject {
37
38     private String JavaDoc name;
39     private String JavaDoc label;
40     private String JavaDoc localizedLabel;
41     private URLEntryModel description;
42     private static Comparator JavaDoc comp;
43
44     /**
45      * Creates an uninitialized model object.
46      *
47      * @since 2.0
48      */

49     public CategoryModel() {
50         super();
51     }
52
53     /**
54      * Retrieve the name of the category.
55      *
56      * @return category name, or <code>null</code>.
57      * @since 2.0
58      */

59     public String JavaDoc getName() {
60         return name;
61     }
62
63     /**
64      * Retrieve the displayable label for the category. If the model
65      * object has been resolved, the label is localized.
66      *
67      * @return displayable label, or <code>null</code>.
68      * @since 2.0
69      */

70     public String JavaDoc getLabel() {
71         if (localizedLabel != null)
72             return localizedLabel;
73         else
74             return label;
75     }
76
77     /**
78      * Retrieve the non-localized displayable label for the category.
79      *
80      * @return non-localized displayable label, or <code>null</code>.
81      * @since 2.0
82      */

83     public String JavaDoc getLabelNonLocalized() {
84         return label;
85     }
86
87     /**
88      * Retrieve the detailed category description
89      *
90      * @return category description, or <code>null</code>.
91      * @since 2.0
92      */

93     public URLEntryModel getDescriptionModel() {
94         return description;
95     }
96
97     /**
98      * Sets the category displayable label.
99      * Throws a runtime exception if this object is marked read-only.
100      *
101      * @param label displayable label, or resource key
102      * @since 2.0
103      */

104     public void setLabel(String JavaDoc label) {
105         assertIsWriteable();
106         this.label = label;
107         this.localizedLabel = null;
108     }
109
110     /**
111      * Sets the category name.
112      * Throws a runtime exception if this object is marked read-only.
113      *
114      * @param name category name
115      * @since 2.0
116      */

117     public void setName(String JavaDoc name) {
118         assertIsWriteable();
119         this.name = name;
120     }
121
122     /**
123      * Sets the category description.
124      * Throws a runtime exception if this object is marked read-only.
125      *
126      * @param description category description
127      * @since 2.0
128      */

129     public void setDescriptionModel(URLEntryModel description) {
130         assertIsWriteable();
131         this.description = description;
132     }
133
134     /**
135      * Marks the model object as read-only.
136      *
137      * @since 2.0
138      */

139     public void markReadOnly() {
140         super.markReadOnly();
141         markReferenceReadOnly(getDescriptionModel());
142     }
143
144     /**
145      * Resolve the model object.
146      * Any URL strings in the model are resolved relative to the
147      * base URL argument. Any translatable strings in the model that are
148      * specified as translation keys are localized using the supplied
149      * resource bundle.
150      *
151      * @param base URL
152      * @param bundleURL resource bundle URL
153      * @exception MalformedURLException
154      * @since 2.0
155      */

156     public void resolve(URL JavaDoc base,URL JavaDoc bundleURL)
157         throws MalformedURLException JavaDoc {
158         // resolve local elements
159
localizedLabel = resolveNLString(bundleURL, label);
160
161         // delegate to references
162
resolveReference(getDescriptionModel(),base, bundleURL);
163     }
164
165     /**
166      * Compare two category models for equality.
167      *
168      * @see Object#equals(Object)
169      * @since 2.0
170      */

171     public boolean equals(Object JavaDoc obj) {
172         boolean result = false;
173         if (obj instanceof CategoryModel) {
174             CategoryModel otherCategory = (CategoryModel) obj;
175             result = getName().equalsIgnoreCase(otherCategory.getName());
176         }
177         return result;
178     }
179
180     /**
181      * Compute hash code for category model.
182      *
183      * @see Object#hashCode()
184      * @since 2.0
185      */

186     public int hashCode() {
187         return getName().hashCode();
188     }
189
190     /**
191      * Returns a comparator for category models.
192      *
193      * @return comparator
194      * @since 2.0
195      */

196     public static Comparator JavaDoc getComparator() {
197         if (comp == null) {
198             comp = new Comparator JavaDoc() {
199                 /*
200                  * @see Comparator#compare(Object,Object)
201                  * Returns 0 if versions are equal.
202                  * Returns -1 if object1 is after than object2.
203                  * Returns +1 if object1 is before than object2.
204                  */

205                 public int compare(Object JavaDoc o1, Object JavaDoc o2) {
206
207                     CategoryModel cat1 = (CategoryModel) o1;
208                     CategoryModel cat2 = (CategoryModel) o2;
209
210                     if (cat1.equals(cat2))
211                         return 0;
212                     return cat1.getName().compareTo(cat2.getName());
213                 }
214             };
215         }
216         return comp;
217     }
218     
219     /**
220      * @see org.eclipse.update.core.model.ModelObject#getPropertyName()
221      */

222     protected String JavaDoc getPropertyName() {
223         return Site.SITE_FILE;
224     }
225 }
226
Popular Tags