KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > HelpActivitySupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.help.ui.internal;
13
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.IConfigurationElement;
18 import org.eclipse.core.runtime.IProduct;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Preferences;
21 import org.eclipse.help.internal.HelpPlugin;
22 import org.eclipse.help.internal.base.HelpBasePlugin;
23 import org.eclipse.help.internal.base.IHelpActivitySupport;
24 import org.eclipse.help.internal.toc.Toc;
25 import org.eclipse.ui.IWorkbench;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.activities.IIdentifier;
28 import org.eclipse.ui.activities.IWorkbenchActivitySupport;
29
30 /**
31  * Wrapper for eclipse ui activity support
32  */

33 public class HelpActivitySupport implements IHelpActivitySupport {
34     private static final String JavaDoc PREF_KEY_SHOW_DISABLED_ACTIVITIES = "showDisabledActivityTopics"; //$NON-NLS-1$
35
private static final String JavaDoc SHOW_DISABLED_ACTIVITIES_NEVER = "never"; //$NON-NLS-1$
36
private static final String JavaDoc SHOW_DISABLED_ACTIVITIES_OFF = "off"; //$NON-NLS-1$
37
private static final String JavaDoc SHOW_DISABLED_ACTIVITIES_ON = "on"; //$NON-NLS-1$
38
// private static final String SHOW_DISABLED_ACTIVITIES_ALWAYS = "always"; //$NON-NLS-1$
39

40     private Preferences pref;
41     private IWorkbenchActivitySupport activitySupport;
42     private boolean userCanToggleFiltering;
43     private boolean filteringEnabled;
44     private ActivityDescriptor activityDescriptor;
45     
46     class ActivityDescriptor {
47         private IConfigurationElement config;
48         private String JavaDoc documentMessage;
49         private boolean needsLiveHelp;
50         
51         public ActivityDescriptor() {
52             load();
53         }
54         
55         private void load() {
56             IConfigurationElement [] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.eclipse.help.base.activitySupport");//$NON-NLS-1$
57
if (elements.length==1 && elements[0].getName().equals("support")) //$NON-NLS-1$
58
config = elements[0];
59             else if (elements.length>0) {
60                 IProduct product = Platform.getProduct();
61                 if (product==null) return;
62                 String JavaDoc productId = product.getId();
63                 for (int i=0; i<elements.length; i++) {
64                     IConfigurationElement element = elements[i];
65                     if (element.getAttribute("productId").equals(productId)) { //$NON-NLS-1$
66
config = element;
67                         break;
68                     }
69                 }
70             }
71         }
72         private IConfigurationElement getChild(String JavaDoc name) {
73             IConfigurationElement [] children = config.getChildren(name);
74             return children.length==1?children[0]:null;
75         }
76         public String JavaDoc getShowAllMessage() {
77             if (config==null)
78                 return null;
79             IConfigurationElement child = getChild("showAllMessage"); //$NON-NLS-1$
80
if (child!=null)
81                 return child.getValue();
82             return null;
83         }
84         public String JavaDoc getLocalScopeCheckboxLabel() {
85             if (config==null)
86                 return null;
87             IConfigurationElement child = getChild("localScopeCheckbox"); //$NON-NLS-1$
88
if (child!=null)
89                 return child.getValue();
90             return null;
91         }
92         public boolean needsLiveHelp(boolean embedded) {
93             getDocumentMessage(embedded);
94             return needsLiveHelp;
95         }
96         public String JavaDoc getDocumentMessage(boolean embedded) {
97             if (config!=null && documentMessage==null) {
98                 IConfigurationElement child = getChild("documentMessage"); //$NON-NLS-1$
99
if (child!=null) {
100                     String JavaDoc value = child.getValue();
101                     String JavaDoc pluginId = child.getAttribute("pluginId"); //$NON-NLS-1$
102
String JavaDoc className = child.getAttribute("class"); //$NON-NLS-1$
103
int loc = value.indexOf("ACTIVITY_EDITOR"); //$NON-NLS-1$
104
if (loc!= -1 && className!=null) {
105                         needsLiveHelp=true;
106                         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
107                         buffer.append(value.substring(0, loc));
108                         buffer.append(getActivityEditorValue(pluginId, className, embedded));
109                         buffer.append(value.substring(loc+15));
110                         documentMessage = buffer.toString();
111                     }
112                     else
113                         documentMessage = value;
114                 }
115             }
116             return documentMessage;
117         }
118         private String JavaDoc getActivityEditorValue(String JavaDoc pluginId, String JavaDoc className, boolean embedded) {
119             String JavaDoc evalue = embedded?"narrow":""; //$NON-NLS-1$ //$NON-NLS-2$
120
return "javascript:liveAction(\""+pluginId+"\", \""+className+"\",\""+evalue+"\")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
121
}
122     }
123     
124
125     public HelpActivitySupport(IWorkbench workbench) {
126         activitySupport = workbench.getActivitySupport();
127         activityDescriptor = new ActivityDescriptor();
128         pref = HelpBasePlugin.getDefault().getPluginPreferences();
129
130         String JavaDoc showDisabledActivities = pref
131                 .getString(PREF_KEY_SHOW_DISABLED_ACTIVITIES);
132         userCanToggleFiltering = SHOW_DISABLED_ACTIVITIES_OFF
133                 .equalsIgnoreCase(showDisabledActivities)
134                 || SHOW_DISABLED_ACTIVITIES_ON
135                         .equalsIgnoreCase(showDisabledActivities);
136         userCanToggleFiltering = userCanToggleFiltering
137                 && isWorkbenchFiltering();
138
139         filteringEnabled = SHOW_DISABLED_ACTIVITIES_OFF
140                 .equalsIgnoreCase(showDisabledActivities)
141                 || SHOW_DISABLED_ACTIVITIES_NEVER
142                         .equalsIgnoreCase(showDisabledActivities);
143         filteringEnabled = filteringEnabled && isWorkbenchFiltering();
144     }
145     public boolean isFilteringEnabled() {
146         return filteringEnabled;
147     }
148     public void setFilteringEnabled(boolean enabled) {
149         if (userCanToggleFiltering) {
150             filteringEnabled = enabled;
151             if (enabled) {
152                 pref.setValue(PREF_KEY_SHOW_DISABLED_ACTIVITIES,
153                         SHOW_DISABLED_ACTIVITIES_OFF);
154             } else {
155                 pref.setValue(PREF_KEY_SHOW_DISABLED_ACTIVITIES,
156                         SHOW_DISABLED_ACTIVITIES_ON);
157             }
158         }
159     }
160     public boolean isUserCanToggleFiltering() {
161         return userCanToggleFiltering;
162     }
163     /*
164      * (non-Javadoc)
165      *
166      * @see org.eclipse.help.internal.base.IHelpActivitySupport#isEnabled()
167      */

168     public boolean isEnabled(String JavaDoc href) {
169         if (!isFilteringEnabled()) {
170             return true;
171         }
172         return isRoleEnabled(href);
173     }
174
175     /* (non-Javadoc)
176      * @see org.eclipse.help.internal.base.IHelpActivitySupport#isRoleEnabled(java.lang.String)
177      */

178     public boolean isRoleEnabled(String JavaDoc href) {
179         if (href.startsWith("/")) { //$NON-NLS-1$
180
href = href.substring(1);
181         }
182
183         return activitySupport.getActivityManager().getIdentifier(href)
184                 .isEnabled();
185     }
186
187     /**
188      * Checks whether topic belongs to a TOC that mathes enabled activity.
189      * Enabled children TOCs are searched if linked by also enabled TOCs.
190      * Additionally topic may match description topic of a root TOC.
191      *
192      * @return true if topic belongs to an enabled TOC
193      * @param href
194      * @param locale
195      * locale for which TOCs are checked
196      */

197     public boolean isEnabledTopic(String JavaDoc href, String JavaDoc locale) {
198         if (href == null) {
199             return false;
200         }
201         if (!isFilteringEnabled()) {
202             return true;
203         }
204         int ix = href.indexOf("?resultof="); //$NON-NLS-1$
205
if (ix >= 0) {
206             href = href.substring(0, ix);
207         }
208         // Find out if description topic for enabled top level TOCs matches the
209
// topic
210
Toc[] tocs = HelpPlugin.getTocManager().getTocs(locale);
211         for (int t = 0; t < tocs.length; t++) {
212             String JavaDoc descriptionHref = tocs[t].getTopic(null).getHref();
213             if (descriptionHref != null
214                     && descriptionHref.length() > 0
215                     && descriptionHref.equals(href)
216                     && HelpBasePlugin.getActivitySupport().isEnabled(
217                             tocs[t].getHref())) {
218                 return true;
219             }
220         }
221         // Find out if any contributed toc that is enabled contains the topic
222
return isInTocSubtree(href, tocs);
223     }
224     /**
225      * @param href
226      * href of a topic
227      * @param tocList
228      * List of IToc
229      * @return true if given topic belongs to one of enabled ITocs
230      */

231     private boolean isInTocSubtree(String JavaDoc href, Toc[] tocList) {
232         for (int i=0;i<tocList.length;++i) {
233             Toc toc = tocList[i];
234             if (!HelpBasePlugin.getActivitySupport().isEnabled(toc.getHref())) {
235                 // TOC is not enabled, check other TOCs
236
continue;
237             }
238             // Check topics in navigation
239
if (toc.getTopic(href) != null) {
240                 return true;
241             }
242             // Check extra docs
243
String JavaDoc[] extraDocs = toc.getTocContribution().getExtraDocuments();
244             for (int j=0;j<extraDocs.length;++j) {
245                 if (extraDocs[j].equals(href)) {
246                     return true;
247                 }
248             }
249         }
250         return false;
251     }
252     /*
253      * (non-Javadoc)
254      *
255      * @see org.eclipse.help.internal.base.IHelpActivitySupport#enableActivities(java.lang.String)
256      */

257     public void enableActivities(String JavaDoc href) {
258         if (href.startsWith("/")) { //$NON-NLS-1$
259
href = href.substring(1);
260         }
261
262         IIdentifier identifier = activitySupport.getActivityManager()
263                 .getIdentifier(href);
264         Set JavaDoc activitityIds = identifier.getActivityIds();
265         if (activitityIds.isEmpty()) { // if there are no activities that match
266
// this identifier, do nothing.
267
return;
268         }
269
270         Set JavaDoc enabledIds = new HashSet JavaDoc(activitySupport.getActivityManager()
271                 .getEnabledActivityIds());
272         enabledIds.addAll(activitityIds);
273         activitySupport.setEnabledActivityIds(enabledIds);
274     }
275
276     /**
277      * @return whether the UI is set up to filter contributions (has defined
278      * activity categories).
279      */

280     private static boolean isWorkbenchFiltering() {
281         return !PlatformUI.getWorkbench().getActivitySupport()
282                 .getActivityManager().getDefinedActivityIds().isEmpty();
283     }
284     public String JavaDoc getShowAllMessage() {
285         return activityDescriptor.getShowAllMessage();
286     }
287     public String JavaDoc getDocumentMessage(boolean embedded) {
288         return activityDescriptor.getDocumentMessage(embedded);
289     }
290     public boolean getDocumentMessageUsesLiveHelp(boolean embedded) {
291         return activityDescriptor.needsLiveHelp(embedded);
292     }
293     public String JavaDoc getLocalScopeCheckboxLabel() {
294         return activityDescriptor.getLocalScopeCheckboxLabel();
295     }
296 }
297
Popular Tags