KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > sorters > CommonSorterDescriptorManager


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.navigator.sorters;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
25 import org.eclipse.ui.internal.navigator.NavigatorContentService;
26 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
27 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentDescriptor;
28 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader;
29 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
30
31 /**
32  * @since 3.2
33  *
34  */

35 public class CommonSorterDescriptorManager {
36
37     private static final CommonSorterDescriptorManager INSTANCE = new CommonSorterDescriptorManager();
38
39     private static final CommonSorterDescriptor[] NO_SORTER_DESCRIPTORS = new CommonSorterDescriptor[0];
40
41     private final Map JavaDoc sortersMap = new HashMap JavaDoc();
42
43     /**
44      *
45      * @return An initialized singleton instance of the
46      * CommonSorterDescriptorManager.
47      */

48     public static CommonSorterDescriptorManager getInstance() {
49         return INSTANCE;
50     }
51
52     private CommonSorterDescriptorManager() {
53         new CommonSorterDescriptorRegistry().readRegistry();
54     }
55
56     /**
57      *
58      * @param contentService
59      * A content service to filter the visible filters.
60      * @param aParent
61      * The parent used to search for a sorter.
62      * @return The set of filters that are 'visible' to the given viewer
63      * descriptor.
64      */

65     public CommonSorterDescriptor[] findApplicableSorters(
66             NavigatorContentService contentService, Object JavaDoc aParent) {
67
68         INavigatorContentDescriptor sourceOfContribution = contentService
69                 .getSourceOfContribution(aParent);
70
71         List JavaDoc applicableSorters = new ArrayList JavaDoc();
72
73         CommonSorterDescriptor descriptor;
74         Set JavaDoc sorters = getCommonSorters(sourceOfContribution);
75         for (Iterator JavaDoc sortersItr = sorters.iterator(); sortersItr.hasNext();) {
76             descriptor = (CommonSorterDescriptor) sortersItr.next();
77             if (descriptor.isEnabledForParent(aParent)) {
78                 applicableSorters.add(descriptor);
79             }
80         }
81         if (applicableSorters.size() == 0) {
82             return NO_SORTER_DESCRIPTORS;
83         }
84         return (CommonSorterDescriptor[]) applicableSorters
85                 .toArray(new CommonSorterDescriptor[applicableSorters.size()]);
86     }
87
88     /**
89      *
90      * @param aContentService
91      * A content service to filter the visible filters.
92      * @param theSource
93      * The source of each *value.
94      * @param aParent
95      * The parent used to search for a sorter.
96      * @param anLvalue
97      * The left operand to compare.
98      * @param anRvalue
99      * 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 JavaDoc aParent,
106             Object JavaDoc anLvalue, Object JavaDoc anRvalue) {
107
108         List JavaDoc applicableSorters = new ArrayList JavaDoc();
109
110         CommonSorterDescriptor descriptor;
111         Set JavaDoc sorters = getCommonSorters(theSource);
112         for (Iterator JavaDoc sortersItr = sorters.iterator(); sortersItr.hasNext();) {
113             descriptor = (CommonSorterDescriptor) sortersItr.next();
114             if (descriptor.isEnabledForParent(aParent)) {
115                 applicableSorters.add(descriptor);
116             }
117         }
118         if (applicableSorters.size() == 0) {
119             return NO_SORTER_DESCRIPTORS;
120         }
121         return (CommonSorterDescriptor[]) applicableSorters
122                 .toArray(new CommonSorterDescriptor[applicableSorters.size()]);
123     }
124     
125
126     /**
127      *
128      * @param aContentService
129      * A content service to filter the visible filters.
130      * @param theSource
131      * The source of each *value.
132      * @return The set of filters that are 'visible' to the given viewer
133      * descriptor.
134      */

135     public CommonSorterDescriptor[] findApplicableSorters(INavigatorContentDescriptor theSource) {
136   
137         Set JavaDoc sorters = getCommonSorters(theSource);
138         if (sorters.size() == 0) {
139             return NO_SORTER_DESCRIPTORS;
140         }
141         return (CommonSorterDescriptor[]) sorters
142                 .toArray(new CommonSorterDescriptor[sorters.size()]);
143     }
144
145     private class CommonSorterDescriptorRegistry extends
146             NavigatorContentRegistryReader {
147
148         private CommonSorterDescriptorRegistry() {
149         }
150
151         /*
152          * (non-Javadoc)
153          *
154          * @see org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
155          */

156         protected boolean readElement(IConfigurationElement element) {
157
158             if (TAG_NAVIGATOR_CONTENT.equals(element.getName())) {
159                 String JavaDoc id = element.getAttribute(ATT_ID);
160                 if (id != null) {
161                     NavigatorContentDescriptor contentDescriptor = CONTENT_DESCRIPTOR_MANAGER
162                             .getContentDescriptor(id);
163                     if (contentDescriptor != null) {
164                         IConfigurationElement[] children = element
165                                 .getChildren(TAG_COMMON_SORTER);
166
167                         if (children.length > 0) {
168                             Set JavaDoc localSorters = getCommonSorters(contentDescriptor);
169                             for (int i = 0; i < children.length; i++) {
170                                 localSorters.add(new CommonSorterDescriptor(
171                                         children[i]));
172                             }
173                             return true;
174                         }
175                     } else {
176                         NavigatorPlugin
177                                 .logError(
178                                         0,
179                                         NLS
180                                                 .bind(
181                                                         CommonNavigatorMessages.CommonSorterDescriptorManager_A_navigatorContent_extension_does_n_,
182                                                         new Object JavaDoc[] {
183                                                                 id,
184                                                                 element
185                                                                         .getDeclaringExtension()
186                                                                         .getNamespaceIdentifier() }),
187                                         null);
188                     }
189                 } else {
190                     NavigatorPlugin
191                             .logError(
192                                     0,
193                                     NLS
194                                             .bind(
195                                                     CommonNavigatorMessages.CommonSorterDescriptorManager_A_navigatorContent_extesnion_in_0_,
196                                                     element.getNamespaceIdentifier()),
197                                     null);
198                 }
199             }
200             return super.readElement(element);
201         }
202
203     }
204
205     private Set JavaDoc getCommonSorters(INavigatorContentDescriptor contentDescriptor) {
206         Set JavaDoc descriptors = null;
207         synchronized (sortersMap) {
208             descriptors = (Set JavaDoc) sortersMap.get(contentDescriptor);
209             if (descriptors == null) {
210                 sortersMap.put(contentDescriptor, descriptors = new HashSet JavaDoc());
211             }
212         }
213         return descriptors;
214     }
215
216 }
217
Popular Tags