KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 /**
19  * Utility class used to associate BundleInfo's that have the same provider and
20  * image. The algorithm in <code>java.util.zip.CRC32</code> is used to determine
21  * image identity.
22  */

23 public class AboutFeaturesButtonManager {
24     private Map JavaDoc providerMap = new HashMap JavaDoc();
25
26     private static class Key {
27         public String JavaDoc providerName;
28
29         public Long JavaDoc crc;
30
31         /**
32          * @param crc must not be null
33          */

34         public Key(String JavaDoc providerName, Long JavaDoc crc) {
35             this.providerName = providerName;
36             this.crc = crc;
37         }
38
39         public boolean equals(Object JavaDoc o) {
40             if (!(o instanceof Key)) {
41                 return false;
42             }
43             Key other = (Key) o;
44             if (!providerName.equals(other.providerName)) {
45                 return false;
46             }
47             return crc.equals(other.crc);
48         }
49
50         public int hashCode() {
51             return providerName.hashCode();
52         }
53     }
54
55     /**
56      * @return true if a button should be added (i.e., the argument has an image
57      * and it does not already have a button)
58      */

59     public boolean add(AboutBundleGroupData info) {
60         // no button for features without an image
61
Long JavaDoc crc = info.getFeatureImageCrc();
62         if (crc == null) {
63             return false;
64         }
65
66         String JavaDoc providerName = info.getProviderName();
67         Key key = new Key(providerName, crc);
68
69         List JavaDoc infoList = (List JavaDoc) providerMap.get(key);
70         if (infoList != null) {
71             infoList.add(info);
72             return false;
73         }
74
75         infoList = new ArrayList JavaDoc();
76         infoList.add(info);
77         providerMap.put(key, infoList);
78         return true;
79     }
80
81     /**
82      * Return an array of all bundle groups that share the argument's provider and
83      * image. Returns an empty array if there isn't any related information.
84      */

85     public AboutBundleGroupData[] getRelatedInfos(AboutBundleGroupData info) {
86         // if there's no image, then there won't be a button
87
Long JavaDoc crc = info.getFeatureImageCrc();
88         if (crc == null) {
89             return new AboutBundleGroupData[0];
90         }
91
92         String JavaDoc providerName = info.getProviderName();
93         Key key = new Key(providerName, crc);
94
95         List JavaDoc infoList = (List JavaDoc) providerMap.get(key);
96         if (infoList == null) {
97             return new AboutBundleGroupData[0];
98         }
99
100         return (AboutBundleGroupData[]) infoList
101                 .toArray(new AboutBundleGroupData[0]);
102     }
103 }
104
Popular Tags