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.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: /**
018: * Utility class used to associate BundleInfo's that have the same provider and
019: * image. The algorithm in <code>java.util.zip.CRC32</code> is used to determine
020: * image identity.
021: */
022: public class AboutFeaturesButtonManager {
023: private Map providerMap = new HashMap();
024:
025: private static class Key {
026: public String providerName;
027:
028: public Long crc;
029:
030: /**
031: * @param crc must not be null
032: */
033: public Key(String providerName, Long crc) {
034: this .providerName = providerName;
035: this .crc = crc;
036: }
037:
038: public boolean equals(Object o) {
039: if (!(o instanceof Key)) {
040: return false;
041: }
042: Key other = (Key) o;
043: if (!providerName.equals(other.providerName)) {
044: return false;
045: }
046: return crc.equals(other.crc);
047: }
048:
049: public int hashCode() {
050: return providerName.hashCode();
051: }
052: }
053:
054: /**
055: * @return true if a button should be added (i.e., the argument has an image
056: * and it does not already have a button)
057: */
058: public boolean add(AboutBundleGroupData info) {
059: // no button for features without an image
060: Long crc = info.getFeatureImageCrc();
061: if (crc == null) {
062: return false;
063: }
064:
065: String providerName = info.getProviderName();
066: Key key = new Key(providerName, crc);
067:
068: List infoList = (List) providerMap.get(key);
069: if (infoList != null) {
070: infoList.add(info);
071: return false;
072: }
073:
074: infoList = new ArrayList();
075: infoList.add(info);
076: providerMap.put(key, infoList);
077: return true;
078: }
079:
080: /**
081: * Return an array of all bundle groups that share the argument's provider and
082: * image. Returns an empty array if there isn't any related information.
083: */
084: public AboutBundleGroupData[] getRelatedInfos(
085: AboutBundleGroupData info) {
086: // if there's no image, then there won't be a button
087: Long crc = info.getFeatureImageCrc();
088: if (crc == null) {
089: return new AboutBundleGroupData[0];
090: }
091:
092: String providerName = info.getProviderName();
093: Key key = new Key(providerName, crc);
094:
095: List infoList = (List) providerMap.get(key);
096: if (infoList == null) {
097: return new AboutBundleGroupData[0];
098: }
099:
100: return (AboutBundleGroupData[]) infoList
101: .toArray(new AboutBundleGroupData[0]);
102: }
103: }
|