KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > about > AboutData


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ui.internal.about;
12
13 import java.io.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import com.ibm.icu.text.Collator;
16 import java.util.Arrays JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.Comparator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import org.eclipse.jface.resource.ImageDescriptor;
23
24 /**
25  * An abstract parent that describes data that can be displayed in a table in one of
26  * the about dialogs.
27  * @since 3.0
28  */

29 public abstract class AboutData {
30     private String JavaDoc providerName;
31
32     private String JavaDoc name;
33
34     private String JavaDoc version;
35
36     private String JavaDoc id;
37
38     private String JavaDoc versionedId = null;
39
40     protected AboutData(String JavaDoc providerName, String JavaDoc name, String JavaDoc version,
41             String JavaDoc id) {
42         this.providerName = providerName == null ? "" : providerName; //$NON-NLS-1$
43
this.name = name == null ? "" : name; //$NON-NLS-1$
44
this.version = version == null ? "" : version; //$NON-NLS-1$
45
this.id = id == null ? "" : id; //$NON-NLS-1$
46
}
47
48     public String JavaDoc getId() {
49         return id;
50     }
51
52     public String JavaDoc getName() {
53         return name;
54     }
55
56     public String JavaDoc getProviderName() {
57         return providerName;
58     }
59
60     public String JavaDoc getVersion() {
61         return version;
62     }
63
64     public String JavaDoc getVersionedId() {
65         if (versionedId == null) {
66             versionedId = getId() + "_" + getVersion(); //$NON-NLS-1$
67
}
68         return versionedId;
69     }
70
71     /**
72      * Modify the argument array to reverse the sort order.
73      * @param infos
74      */

75     private static void reverse(AboutData[] infos) {
76         List JavaDoc infoList = Arrays.asList(infos);
77         Collections.reverse(infoList);
78         for (int i = 0; i < infos.length; ++i) {
79             infos[i] = (AboutData) infoList.get(i);
80         }
81     }
82
83     /**
84      * Modify the argument array to be sorted by provider. If the reverse
85      * boolean is true, the array is assumed to already be sorted and the
86      * direction of sort (ascending vs. descending) is reversed. Entries
87      * with the same name are sorted by name.
88      *
89      * @param reverse
90      * if true then the order of the argument is reversed without
91      * examining the value of the fields
92      * @param infos
93      * the data to be sorted
94      */

95     public static void sortByProvider(boolean reverse, AboutData[] infos) {
96         if (reverse) {
97             reverse(infos);
98             return;
99         }
100
101         Arrays.sort(infos, new Comparator JavaDoc() {
102             Collator collator = Collator.getInstance(Locale.getDefault());
103
104             public int compare(Object JavaDoc a, Object JavaDoc b) {
105                 AboutData info1 = (AboutData) a;
106                 AboutData info2 = (AboutData) b;
107
108                 String JavaDoc provider1 = info1.getProviderName();
109                 String JavaDoc provider2 = info2.getProviderName();
110
111                 if (!provider1.equals(provider2)) {
112                     return collator.compare(provider1, provider2);
113                 }
114
115                 return collator.compare(info1.getName(), info2.getName());
116             }
117         });
118     }
119
120     /**
121      * Modify the argument array to be sorted by name. If the reverse
122      * boolean is true, the array is assumed to already be sorted and the
123      * direction of sort (ascending vs. descending) is reversed.
124      *
125      * @param reverse
126      * if true then the order of the argument is reversed without
127      * examining the value of the fields
128      * @param infos
129      * the data to be sorted
130      */

131     public static void sortByName(boolean reverse, AboutData[] infos) {
132         if (reverse) {
133             reverse(infos);
134             return;
135         }
136
137         Arrays.sort(infos, new Comparator JavaDoc() {
138             Collator collator = Collator.getInstance(Locale.getDefault());
139
140             public int compare(Object JavaDoc a, Object JavaDoc b) {
141                 AboutData info1 = (AboutData) a;
142                 AboutData info2 = (AboutData) b;
143                 return collator.compare(info1.getName(), info2.getName());
144             }
145         });
146     }
147
148     /**
149      * Modify the argument array to be sorted by version. If the reverse
150      * boolean is true, the array is assumed to already be sorted and the
151      * direction of sort (ascending vs. descending) is reversed. Entries
152      * with the same name are sorted by name.
153      *
154      * @param reverse
155      * if true then the order of the argument is reversed without
156      * examining the value of the fields
157      * @param infos
158      * the data to be sorted
159      */

160     public static void sortByVersion(boolean reverse, AboutData[] infos) {
161         if (reverse) {
162             reverse(infos);
163             return;
164         }
165
166         Arrays.sort(infos, new Comparator JavaDoc() {
167             Collator collator = Collator.getInstance(Locale.getDefault());
168
169             public int compare(Object JavaDoc a, Object JavaDoc b) {
170                 AboutData info1 = (AboutData) a;
171                 AboutData info2 = (AboutData) b;
172
173                 String JavaDoc version1 = info1.getVersion();
174                 String JavaDoc version2 = info2.getVersion();
175
176                 if (!version1.equals(version2)) {
177                     return collator.compare(version1, version2);
178                 }
179
180                 return collator.compare(info1.getName(), info2.getName());
181             }
182         });
183     }
184
185     /**
186      * Modify the argument array to be sorted by id. If the reverse
187      * boolean is true, the array is assumed to already be sorted and the
188      * direction of sort (ascending vs. descending) is reversed. Entries
189      * with the same name are sorted by name.
190      *
191      * @param reverse
192      * if true then the order of the argument is reversed without
193      * examining the value of the fields
194      * @param infos
195      * the data to be sorted
196      */

197     public static void sortById(boolean reverse, AboutData[] infos) {
198         if (reverse) {
199             reverse(infos);
200             return;
201         }
202
203         Arrays.sort(infos, new Comparator JavaDoc() {
204             Collator collator = Collator.getInstance(Locale.getDefault());
205
206             public int compare(Object JavaDoc a, Object JavaDoc b) {
207                 AboutData info1 = (AboutData) a;
208                 AboutData info2 = (AboutData) b;
209
210                 String JavaDoc id1 = info1.getId();
211                 String JavaDoc id2 = info2.getId();
212
213                 if (!id1.equals(id2)) {
214                     return collator.compare(id1, id2);
215                 }
216
217                 return collator.compare(info1.getName(), info2.getName());
218             }
219         });
220     }
221
222     protected static URL JavaDoc getURL(String JavaDoc value) {
223         try {
224             if (value != null) {
225                 return new URL JavaDoc(value);
226             }
227         } catch (IOException JavaDoc e) {
228             // do nothing
229
}
230
231         return null;
232     }
233
234     protected static ImageDescriptor getImage(URL JavaDoc url) {
235         return url == null ? null : ImageDescriptor.createFromURL(url);
236     }
237
238     protected static ImageDescriptor getImage(String JavaDoc value) {
239         return getImage(getURL(value));
240     }
241 }
242
Popular Tags