001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.about;
011:
012: import java.io.IOException;
013: import java.net.URL;
014: import com.ibm.icu.text.Collator;
015: import java.util.Arrays;
016: import java.util.Collections;
017: import java.util.Comparator;
018: import java.util.List;
019: import java.util.Locale;
020:
021: import org.eclipse.jface.resource.ImageDescriptor;
022:
023: /**
024: * An abstract parent that describes data that can be displayed in a table in one of
025: * the about dialogs.
026: * @since 3.0
027: */
028: public abstract class AboutData {
029: private String providerName;
030:
031: private String name;
032:
033: private String version;
034:
035: private String id;
036:
037: private String versionedId = null;
038:
039: protected AboutData(String providerName, String name,
040: String version, String id) {
041: this .providerName = providerName == null ? "" : providerName; //$NON-NLS-1$
042: this .name = name == null ? "" : name; //$NON-NLS-1$
043: this .version = version == null ? "" : version; //$NON-NLS-1$
044: this .id = id == null ? "" : id; //$NON-NLS-1$
045: }
046:
047: public String getId() {
048: return id;
049: }
050:
051: public String getName() {
052: return name;
053: }
054:
055: public String getProviderName() {
056: return providerName;
057: }
058:
059: public String getVersion() {
060: return version;
061: }
062:
063: public String getVersionedId() {
064: if (versionedId == null) {
065: versionedId = getId() + "_" + getVersion(); //$NON-NLS-1$
066: }
067: return versionedId;
068: }
069:
070: /**
071: * Modify the argument array to reverse the sort order.
072: * @param infos
073: */
074: private static void reverse(AboutData[] infos) {
075: List infoList = Arrays.asList(infos);
076: Collections.reverse(infoList);
077: for (int i = 0; i < infos.length; ++i) {
078: infos[i] = (AboutData) infoList.get(i);
079: }
080: }
081:
082: /**
083: * Modify the argument array to be sorted by provider. If the reverse
084: * boolean is true, the array is assumed to already be sorted and the
085: * direction of sort (ascending vs. descending) is reversed. Entries
086: * with the same name are sorted by name.
087: *
088: * @param reverse
089: * if true then the order of the argument is reversed without
090: * examining the value of the fields
091: * @param infos
092: * the data to be sorted
093: */
094: public static void sortByProvider(boolean reverse, AboutData[] infos) {
095: if (reverse) {
096: reverse(infos);
097: return;
098: }
099:
100: Arrays.sort(infos, new Comparator() {
101: Collator collator = Collator.getInstance(Locale
102: .getDefault());
103:
104: public int compare(Object a, Object b) {
105: AboutData info1 = (AboutData) a;
106: AboutData info2 = (AboutData) b;
107:
108: String provider1 = info1.getProviderName();
109: String provider2 = info2.getProviderName();
110:
111: if (!provider1.equals(provider2)) {
112: return collator.compare(provider1, provider2);
113: }
114:
115: return collator.compare(info1.getName(), info2
116: .getName());
117: }
118: });
119: }
120:
121: /**
122: * Modify the argument array to be sorted by name. If the reverse
123: * boolean is true, the array is assumed to already be sorted and the
124: * direction of sort (ascending vs. descending) is reversed.
125: *
126: * @param reverse
127: * if true then the order of the argument is reversed without
128: * examining the value of the fields
129: * @param infos
130: * the data to be sorted
131: */
132: public static void sortByName(boolean reverse, AboutData[] infos) {
133: if (reverse) {
134: reverse(infos);
135: return;
136: }
137:
138: Arrays.sort(infos, new Comparator() {
139: Collator collator = Collator.getInstance(Locale
140: .getDefault());
141:
142: public int compare(Object a, Object b) {
143: AboutData info1 = (AboutData) a;
144: AboutData info2 = (AboutData) b;
145: return collator.compare(info1.getName(), info2
146: .getName());
147: }
148: });
149: }
150:
151: /**
152: * Modify the argument array to be sorted by version. If the reverse
153: * boolean is true, the array is assumed to already be sorted and the
154: * direction of sort (ascending vs. descending) is reversed. Entries
155: * with the same name are sorted by name.
156: *
157: * @param reverse
158: * if true then the order of the argument is reversed without
159: * examining the value of the fields
160: * @param infos
161: * the data to be sorted
162: */
163: public static void sortByVersion(boolean reverse, AboutData[] infos) {
164: if (reverse) {
165: reverse(infos);
166: return;
167: }
168:
169: Arrays.sort(infos, new Comparator() {
170: Collator collator = Collator.getInstance(Locale
171: .getDefault());
172:
173: public int compare(Object a, Object b) {
174: AboutData info1 = (AboutData) a;
175: AboutData info2 = (AboutData) b;
176:
177: String version1 = info1.getVersion();
178: String version2 = info2.getVersion();
179:
180: if (!version1.equals(version2)) {
181: return collator.compare(version1, version2);
182: }
183:
184: return collator.compare(info1.getName(), info2
185: .getName());
186: }
187: });
188: }
189:
190: /**
191: * Modify the argument array to be sorted by id. If the reverse
192: * boolean is true, the array is assumed to already be sorted and the
193: * direction of sort (ascending vs. descending) is reversed. Entries
194: * with the same name are sorted by name.
195: *
196: * @param reverse
197: * if true then the order of the argument is reversed without
198: * examining the value of the fields
199: * @param infos
200: * the data to be sorted
201: */
202: public static void sortById(boolean reverse, AboutData[] infos) {
203: if (reverse) {
204: reverse(infos);
205: return;
206: }
207:
208: Arrays.sort(infos, new Comparator() {
209: Collator collator = Collator.getInstance(Locale
210: .getDefault());
211:
212: public int compare(Object a, Object b) {
213: AboutData info1 = (AboutData) a;
214: AboutData info2 = (AboutData) b;
215:
216: String id1 = info1.getId();
217: String id2 = info2.getId();
218:
219: if (!id1.equals(id2)) {
220: return collator.compare(id1, id2);
221: }
222:
223: return collator.compare(info1.getName(), info2
224: .getName());
225: }
226: });
227: }
228:
229: protected static URL getURL(String value) {
230: try {
231: if (value != null) {
232: return new URL(value);
233: }
234: } catch (IOException e) {
235: // do nothing
236: }
237:
238: return null;
239: }
240:
241: protected static ImageDescriptor getImage(URL url) {
242: return url == null ? null : ImageDescriptor.createFromURL(url);
243: }
244:
245: protected static ImageDescriptor getImage(String value) {
246: return getImage(getURL(value));
247: }
248: }
|