001: /*******************************************************************************
002: * Copyright (c) 2001, 2007 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.views.properties.tabbed.view;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor;
018:
019: /**
020: * tabbed property registry factory. Caches the tabbed property registry by
021: * tabbed property contributor ID.
022: *
023: * @author Anthony Hunter
024: */
025: public class TabbedPropertyRegistryFactory {
026:
027: class CacheData {
028: TabbedPropertyRegistry registry;
029: List references;
030: }
031:
032: /**
033: * singleton instance of this class
034: */
035: private static TabbedPropertyRegistryFactory INSTANCE = new TabbedPropertyRegistryFactory();
036:
037: /**
038: * get the singleton instance of this class.
039: *
040: * @return the TabbedPropertyRegistryFactory instance.
041: */
042: public static TabbedPropertyRegistryFactory getInstance() {
043: return INSTANCE;
044: }
045:
046: /**
047: * private constructor.
048: */
049: private TabbedPropertyRegistryFactory() {
050: super ();
051: idToCacheData = new HashMap();
052: }
053:
054: protected Map idToCacheData; // cache
055:
056: /**
057: * Creates a registry for the given contributor.
058: *
059: * @param target
060: * the contributor.
061: * @return a registry for the given contributor.
062: */
063: public TabbedPropertyRegistry createRegistry(
064: ITabbedPropertySheetPageContributor target) {
065: /**
066: * Get the contributor id from the ITabbedPropertySheetPageContributor
067: * interface
068: */
069: String key = target.getContributorId();
070: CacheData data = (CacheData) idToCacheData.get(key);
071: if (data == null) {
072: data = new CacheData();
073: data.registry = new TabbedPropertyRegistry(key);
074: data.references = new ArrayList(5);
075: idToCacheData.put(key, data);
076: }
077: data.references.add(target);
078: // keeps track of contributor using the same registry
079: return data.registry;
080: }
081:
082: /**
083: * Indicates that the given contributor no longer needs a registry. The
084: * registry will be disposed when no other contributor of the same type
085: * needs it.
086: *
087: * @param target
088: * the contributor;
089: */
090: public void disposeRegistry(
091: ITabbedPropertySheetPageContributor target) {
092: /**
093: * Get the contributor id from the ITabbedPropertySheetPageContributor
094: * interface
095: */
096: String key = target.getContributorId();
097: CacheData data = (CacheData) idToCacheData.get(key);
098: if (data != null) {
099: data.references.remove(target);
100: if (data.references.isEmpty()) {
101: idToCacheData.remove(key);
102: }
103: }
104: }
105: }
|