001: /*******************************************************************************
002: * Copyright (c) 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.navigator.sorters;
011:
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Set;
019:
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
023: import org.eclipse.ui.internal.navigator.NavigatorContentService;
024: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
025: import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptor;
026: import org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader;
027: import org.eclipse.ui.navigator.INavigatorContentDescriptor;
028:
029: /**
030: * @since 3.2
031: *
032: */
033: public class CommonSorterDescriptorManager {
034:
035: private static final CommonSorterDescriptorManager INSTANCE = new CommonSorterDescriptorManager();
036:
037: private static final CommonSorterDescriptor[] NO_SORTER_DESCRIPTORS = new CommonSorterDescriptor[0];
038:
039: private final Map sortersMap = new HashMap();
040:
041: /**
042: *
043: * @return An initialized singleton instance of the
044: * CommonSorterDescriptorManager.
045: */
046: public static CommonSorterDescriptorManager getInstance() {
047: return INSTANCE;
048: }
049:
050: private CommonSorterDescriptorManager() {
051: new CommonSorterDescriptorRegistry().readRegistry();
052: }
053:
054: /**
055: *
056: * @param contentService
057: * A content service to filter the visible filters.
058: * @param aParent
059: * The parent used to search for a sorter.
060: * @return The set of filters that are 'visible' to the given viewer
061: * descriptor.
062: */
063: public CommonSorterDescriptor[] findApplicableSorters(
064: NavigatorContentService contentService, Object aParent) {
065:
066: INavigatorContentDescriptor sourceOfContribution = contentService
067: .getSourceOfContribution(aParent);
068:
069: List applicableSorters = new ArrayList();
070:
071: CommonSorterDescriptor descriptor;
072: Set sorters = getCommonSorters(sourceOfContribution);
073: for (Iterator sortersItr = sorters.iterator(); sortersItr
074: .hasNext();) {
075: descriptor = (CommonSorterDescriptor) sortersItr.next();
076: if (descriptor.isEnabledForParent(aParent)) {
077: applicableSorters.add(descriptor);
078: }
079: }
080: if (applicableSorters.size() == 0) {
081: return NO_SORTER_DESCRIPTORS;
082: }
083: return (CommonSorterDescriptor[]) applicableSorters
084: .toArray(new CommonSorterDescriptor[applicableSorters
085: .size()]);
086: }
087:
088: /**
089: *
090: * @param aContentService
091: * A content service to filter the visible filters.
092: * @param theSource
093: * The source of each *value.
094: * @param aParent
095: * The parent used to search for a sorter.
096: * @param anLvalue
097: * The left operand to compare.
098: * @param anRvalue
099: * The right operand to compare.
100: * @return The set of filters that are 'visible' to the given viewer
101: * descriptor.
102: */
103: public CommonSorterDescriptor[] findApplicableSorters(
104: NavigatorContentService aContentService,
105: INavigatorContentDescriptor theSource, Object aParent,
106: Object anLvalue, Object anRvalue) {
107:
108: List applicableSorters = new ArrayList();
109:
110: CommonSorterDescriptor descriptor;
111: Set sorters = getCommonSorters(theSource);
112: for (Iterator sortersItr = sorters.iterator(); sortersItr
113: .hasNext();) {
114: descriptor = (CommonSorterDescriptor) sortersItr.next();
115: if (descriptor.isEnabledForParent(aParent)) {
116: applicableSorters.add(descriptor);
117: }
118: }
119: if (applicableSorters.size() == 0) {
120: return NO_SORTER_DESCRIPTORS;
121: }
122: return (CommonSorterDescriptor[]) applicableSorters
123: .toArray(new CommonSorterDescriptor[applicableSorters
124: .size()]);
125: }
126:
127: /**
128: *
129: * @param aContentService
130: * A content service to filter the visible filters.
131: * @param theSource
132: * The source of each *value.
133: * @return The set of filters that are 'visible' to the given viewer
134: * descriptor.
135: */
136: public CommonSorterDescriptor[] findApplicableSorters(
137: INavigatorContentDescriptor theSource) {
138:
139: Set sorters = getCommonSorters(theSource);
140: if (sorters.size() == 0) {
141: return NO_SORTER_DESCRIPTORS;
142: }
143: return (CommonSorterDescriptor[]) sorters
144: .toArray(new CommonSorterDescriptor[sorters.size()]);
145: }
146:
147: private class CommonSorterDescriptorRegistry extends
148: NavigatorContentRegistryReader {
149:
150: private CommonSorterDescriptorRegistry() {
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
157: */
158: protected boolean readElement(IConfigurationElement element) {
159:
160: if (TAG_NAVIGATOR_CONTENT.equals(element.getName())) {
161: String id = element.getAttribute(ATT_ID);
162: if (id != null) {
163: NavigatorContentDescriptor contentDescriptor = CONTENT_DESCRIPTOR_MANAGER
164: .getContentDescriptor(id);
165: if (contentDescriptor != null) {
166: IConfigurationElement[] children = element
167: .getChildren(TAG_COMMON_SORTER);
168:
169: if (children.length > 0) {
170: Set localSorters = getCommonSorters(contentDescriptor);
171: for (int i = 0; i < children.length; i++) {
172: localSorters
173: .add(new CommonSorterDescriptor(
174: children[i]));
175: }
176: return true;
177: }
178: } else {
179: NavigatorPlugin
180: .logError(
181: 0,
182: NLS
183: .bind(
184: CommonNavigatorMessages.CommonSorterDescriptorManager_A_navigatorContent_extension_does_n_,
185: new Object[] {
186: id,
187: element
188: .getDeclaringExtension()
189: .getNamespaceIdentifier() }),
190: null);
191: }
192: } else {
193: NavigatorPlugin
194: .logError(
195: 0,
196: NLS
197: .bind(
198: CommonNavigatorMessages.CommonSorterDescriptorManager_A_navigatorContent_extesnion_in_0_,
199: element
200: .getNamespaceIdentifier()),
201: null);
202: }
203: }
204: return super .readElement(element);
205: }
206:
207: }
208:
209: private Set getCommonSorters(
210: INavigatorContentDescriptor contentDescriptor) {
211: Set descriptors = null;
212: synchronized (sortersMap) {
213: descriptors = (Set) sortersMap.get(contentDescriptor);
214: if (descriptors == null) {
215: sortersMap.put(contentDescriptor,
216: descriptors = new HashSet());
217: }
218: }
219: return descriptors;
220: }
221:
222: }
|