1 11 12 package org.eclipse.help.ui.internal; 13 14 import java.util.HashSet ; 15 import java.util.Set ; 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 33 public class HelpActivitySupport implements IHelpActivitySupport { 34 private static final String PREF_KEY_SHOW_DISABLED_ACTIVITIES = "showDisabledActivityTopics"; private static final String SHOW_DISABLED_ACTIVITIES_NEVER = "never"; private static final String SHOW_DISABLED_ACTIVITIES_OFF = "off"; private static final String SHOW_DISABLED_ACTIVITIES_ON = "on"; 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 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"); if (elements.length==1 && elements[0].getName().equals("support")) config = elements[0]; 59 else if (elements.length>0) { 60 IProduct product = Platform.getProduct(); 61 if (product==null) return; 62 String productId = product.getId(); 63 for (int i=0; i<elements.length; i++) { 64 IConfigurationElement element = elements[i]; 65 if (element.getAttribute("productId").equals(productId)) { config = element; 67 break; 68 } 69 } 70 } 71 } 72 private IConfigurationElement getChild(String name) { 73 IConfigurationElement [] children = config.getChildren(name); 74 return children.length==1?children[0]:null; 75 } 76 public String getShowAllMessage() { 77 if (config==null) 78 return null; 79 IConfigurationElement child = getChild("showAllMessage"); if (child!=null) 81 return child.getValue(); 82 return null; 83 } 84 public String getLocalScopeCheckboxLabel() { 85 if (config==null) 86 return null; 87 IConfigurationElement child = getChild("localScopeCheckbox"); 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 getDocumentMessage(boolean embedded) { 97 if (config!=null && documentMessage==null) { 98 IConfigurationElement child = getChild("documentMessage"); if (child!=null) { 100 String value = child.getValue(); 101 String pluginId = child.getAttribute("pluginId"); String className = child.getAttribute("class"); int loc = value.indexOf("ACTIVITY_EDITOR"); if (loc!= -1 && className!=null) { 105 needsLiveHelp=true; 106 StringBuffer buffer = new StringBuffer (); 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 getActivityEditorValue(String pluginId, String className, boolean embedded) { 119 String evalue = embedded?"narrow":""; return "javascript:liveAction(\""+pluginId+"\", \""+className+"\",\""+evalue+"\")"; } 122 } 123 124 125 public HelpActivitySupport(IWorkbench workbench) { 126 activitySupport = workbench.getActivitySupport(); 127 activityDescriptor = new ActivityDescriptor(); 128 pref = HelpBasePlugin.getDefault().getPluginPreferences(); 129 130 String 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 168 public boolean isEnabled(String href) { 169 if (!isFilteringEnabled()) { 170 return true; 171 } 172 return isRoleEnabled(href); 173 } 174 175 178 public boolean isRoleEnabled(String href) { 179 if (href.startsWith("/")) { href = href.substring(1); 181 } 182 183 return activitySupport.getActivityManager().getIdentifier(href) 184 .isEnabled(); 185 } 186 187 197 public boolean isEnabledTopic(String href, String locale) { 198 if (href == null) { 199 return false; 200 } 201 if (!isFilteringEnabled()) { 202 return true; 203 } 204 int ix = href.indexOf("?resultof="); if (ix >= 0) { 206 href = href.substring(0, ix); 207 } 208 Toc[] tocs = HelpPlugin.getTocManager().getTocs(locale); 211 for (int t = 0; t < tocs.length; t++) { 212 String 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 return isInTocSubtree(href, tocs); 223 } 224 231 private boolean isInTocSubtree(String 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 continue; 237 } 238 if (toc.getTopic(href) != null) { 240 return true; 241 } 242 String [] 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 257 public void enableActivities(String href) { 258 if (href.startsWith("/")) { href = href.substring(1); 260 } 261 262 IIdentifier identifier = activitySupport.getActivityManager() 263 .getIdentifier(href); 264 Set activitityIds = identifier.getActivityIds(); 265 if (activitityIds.isEmpty()) { return; 268 } 269 270 Set enabledIds = new HashSet (activitySupport.getActivityManager() 271 .getEnabledActivityIds()); 272 enabledIds.addAll(activitityIds); 273 activitySupport.setEnabledActivityIds(enabledIds); 274 } 275 276 280 private static boolean isWorkbenchFiltering() { 281 return !PlatformUI.getWorkbench().getActivitySupport() 282 .getActivityManager().getDefinedActivityIds().isEmpty(); 283 } 284 public String getShowAllMessage() { 285 return activityDescriptor.getShowAllMessage(); 286 } 287 public String getDocumentMessage(boolean embedded) { 288 return activityDescriptor.getDocumentMessage(embedded); 289 } 290 public boolean getDocumentMessageUsesLiveHelp(boolean embedded) { 291 return activityDescriptor.needsLiveHelp(embedded); 292 } 293 public String getLocalScopeCheckboxLabel() { 294 return activityDescriptor.getLocalScopeCheckboxLabel(); 295 } 296 } 297 | Popular Tags |