KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > extensions > LinkHelperManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.extensions;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
21 import org.eclipse.ui.navigator.INavigatorContentService;
22
23 /**
24  * <p>
25  * <strong>EXPERIMENTAL</strong>. This class or interface has been added as
26  * part of a work in progress. There is a guarantee neither that this API will
27  * work nor that it will remain the same. Please do not use this API without
28  * consulting with the Platform/UI team.
29  * </p>
30  *
31  * @since 3.2
32  *
33  */

34 public class LinkHelperManager {
35
36     private static final LinkHelperManager instance = new LinkHelperManager();
37
38     private static final LinkHelperDescriptor[] NO_DESCRIPTORS = new LinkHelperDescriptor[0];
39
40     private List JavaDoc descriptors;
41
42     /**
43      * @return the singleton instance.
44      */

45     public static LinkHelperManager getInstance() {
46         return instance;
47     }
48
49     private LinkHelperManager() {
50         new LinkHelperRegistry().readRegistry();
51     }
52
53     /**
54      * Return the link helper descriptors for a given selection and content
55      * service.
56      *
57      * @param anObject
58      * An object from the viewer.
59      * @param aContentService
60      * The content service to use for visibility filtering. Link
61      * Helpers are filtered by contentExtension elements in
62      * viewerContentBindings.
63      * @return An array of <i>visible</i> and <i>enabled</i> Link Helpers or
64      * an empty array.
65      */

66     public LinkHelperDescriptor[] getLinkHelpersFor(
67             Object JavaDoc anObject,
68             INavigatorContentService aContentService) {
69
70         List JavaDoc helpersList = new ArrayList JavaDoc();
71         LinkHelperDescriptor descriptor = null;
72         for (Iterator JavaDoc itr = getDescriptors().iterator(); itr.hasNext();) {
73             descriptor = (LinkHelperDescriptor) itr.next();
74             if (aContentService.isVisible(descriptor.getId())
75                     && descriptor.isEnabledFor(anObject)) {
76                 helpersList.add(descriptor);
77             }
78         }
79         if (helpersList.size() == 0) {
80             return NO_DESCRIPTORS;
81         }
82         return (LinkHelperDescriptor[]) helpersList
83                 .toArray(new LinkHelperDescriptor[helpersList.size()]);
84
85     }
86
87     /**
88      * Return the link helper descriptors for a given selection and content
89      * service.
90      *
91      * @param anInput
92      * The input of the active viewer.
93      * @param aContentService
94      * The content service to use for visibility filtering. Link
95      * Helpers are filtered by contentExtension elements in
96      * viewerContentBindings.
97      * @return An array of <i>visible</i> and <i>enabled</i> Link Helpers or
98      * an empty array.
99      */

100     public LinkHelperDescriptor[] getLinkHelpersFor(IEditorInput anInput,
101             INavigatorContentService aContentService) {
102
103         List JavaDoc helpersList = new ArrayList JavaDoc();
104         LinkHelperDescriptor descriptor = null;
105         for (Iterator JavaDoc itr = getDescriptors().iterator(); itr.hasNext();) {
106             descriptor = (LinkHelperDescriptor) itr.next();
107             if (aContentService.isVisible(descriptor.getId())
108                     && descriptor.isEnabledFor(anInput)) {
109                 helpersList.add(descriptor);
110             }
111         }
112         if (helpersList.size() == 0) {
113             return NO_DESCRIPTORS;
114         }
115         return (LinkHelperDescriptor[]) helpersList
116                 .toArray(new LinkHelperDescriptor[helpersList.size()]);
117
118     }
119
120     protected List JavaDoc getDescriptors() {
121         if (descriptors == null) {
122             descriptors = new ArrayList JavaDoc();
123         }
124         return descriptors;
125     }
126
127     private class LinkHelperRegistry extends RegistryReader implements
128             ILinkHelperExtPtConstants {
129
130         private LinkHelperRegistry() {
131             super(NavigatorPlugin.PLUGIN_ID, LINK_HELPER);
132         }
133
134         public boolean readElement(IConfigurationElement element) {
135             if (LINK_HELPER.equals(element.getName())) {
136                 try {
137                     getDescriptors().add(new LinkHelperDescriptor(element));
138                 } catch (Throwable JavaDoc e) {
139                     String JavaDoc msg = e.getMessage() != null ? e.getMessage() : e.toString();
140                     NavigatorPlugin.logError(0, msg, e);
141                 }
142                 return true;
143             }
144             return false;
145         }
146     }
147 }
148
Popular Tags