1 11 package org.eclipse.ui.internal.registry; 12 13 import java.util.StringTokenizer ; 14 15 import org.eclipse.core.commands.IHandler; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Status; 21 import org.eclipse.jface.resource.ImageDescriptor; 22 import org.eclipse.ui.IPageLayout; 23 import org.eclipse.ui.IPluginContribution; 24 import org.eclipse.ui.ISharedImages; 25 import org.eclipse.ui.IViewPart; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.handlers.IHandlerActivation; 28 import org.eclipse.ui.handlers.IHandlerService; 29 import org.eclipse.ui.internal.WorkbenchPlugin; 30 import org.eclipse.ui.plugin.AbstractUIPlugin; 31 import org.eclipse.ui.views.IViewDescriptor; 32 33 36 public class ViewDescriptor implements IViewDescriptor, IPluginContribution { 37 private String id; 38 39 private ImageDescriptor imageDescriptor; 40 41 private IConfigurationElement configElement; 42 43 private String [] categoryPath; 44 45 private float fastViewWidthRatio; 46 47 51 private IHandlerActivation handlerActivation; 52 53 59 public ViewDescriptor(IConfigurationElement e) 60 throws CoreException { 61 configElement = e; 62 loadFromExtension(); 63 } 64 65 68 public IViewPart createView() throws CoreException { 69 Object extension = WorkbenchPlugin.createExtension( 70 getConfigurationElement(), 71 IWorkbenchRegistryConstants.ATT_CLASS); 72 return (IViewPart) extension; 73 } 74 75 78 public String [] getCategoryPath() { 79 return categoryPath; 80 } 81 82 87 public IConfigurationElement getConfigurationElement() { 88 return configElement; 89 } 90 91 94 public String getDescription() { 95 return RegistryReader.getDescription(configElement); 96 } 97 98 101 public String getId() { 102 return id; 103 } 104 105 108 public ImageDescriptor getImageDescriptor() { 109 if (imageDescriptor != null) { 110 return imageDescriptor; 111 } 112 String iconName = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 113 if (iconName == null) { 115 return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( 116 ISharedImages.IMG_DEF_VIEW); 117 } 118 IExtension extension = configElement.getDeclaringExtension(); 119 String extendingPluginId = extension.getNamespace(); 120 imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin( 121 extendingPluginId, iconName); 122 if (imageDescriptor == null) { 124 imageDescriptor = ImageDescriptor.getMissingImageDescriptor(); 125 } 126 127 return imageDescriptor; 128 } 129 130 133 public String getLabel() { 134 return configElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 135 } 136 137 142 public String getAccelerator() { 143 return configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ACCELERATOR); 144 } 145 146 149 public float getFastViewWidthRatio() { 150 configElement.getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO); return fastViewWidthRatio; 152 } 153 154 157 private void loadFromExtension() throws CoreException { 158 id = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 159 160 String category = configElement.getAttribute(IWorkbenchRegistryConstants.TAG_CATEGORY); 161 162 if ((configElement.getAttribute(IWorkbenchRegistryConstants.ATT_NAME) == null) 164 || (RegistryReader.getClassValue(configElement, 165 IWorkbenchRegistryConstants.ATT_CLASS) == null)) { 166 throw new CoreException(new Status(IStatus.ERROR, configElement 167 .getNamespace(), 0, 168 "Invalid extension (missing label or class name): " + id, null)); 170 } 171 172 if (category != null) { 173 StringTokenizer stok = new StringTokenizer (category, "/"); categoryPath = new String [stok.countTokens()]; 175 for (int i = 0; stok.hasMoreTokens(); i++) { 177 categoryPath[i] = stok.nextToken(); 178 } 179 } 180 181 String ratio = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_FAST_VIEW_WIDTH_RATIO); 182 if (ratio != null) { 183 try { 184 fastViewWidthRatio = new Float (ratio).floatValue(); 185 if (fastViewWidthRatio > IPageLayout.RATIO_MAX) { 186 fastViewWidthRatio = IPageLayout.RATIO_MAX; 187 } 188 if (fastViewWidthRatio < IPageLayout.RATIO_MIN) { 189 fastViewWidthRatio = IPageLayout.RATIO_MIN; 190 } 191 } catch (NumberFormatException e) { 192 fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO; 193 } 194 } else { 195 fastViewWidthRatio = IPageLayout.DEFAULT_FASTVIEW_RATIO; 196 } 197 } 198 199 203 public String toString() { 204 return "View(" + getId() + ")"; } 206 207 212 public String getPluginId() { 213 String pluginId = configElement.getNamespace(); 214 return pluginId == null ? "" : pluginId; } 216 217 222 public String getLocalId() { 223 return getId() == null ? "" : getId(); } 225 226 229 public boolean getAllowMultiple() { 230 String string = configElement.getAttribute(IWorkbenchRegistryConstants.ATT_ALLOW_MULTIPLE); 231 return string == null ? false : Boolean.valueOf(string).booleanValue(); 232 } 233 234 237 public Object getAdapter(Class adapter) { 238 if (adapter.equals(IConfigurationElement.class)) { 239 return getConfigurationElement(); 240 } 241 return null; 242 } 243 244 251 public final void activateHandler() { 252 if (handlerActivation == null) { 253 final IHandler handler = new ShowViewHandler(getId()); 254 final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); 255 handlerActivation = handlerService 256 .activateHandler(getId(), handler); 257 } 258 } 259 260 267 public final void deactivateHandler() { 268 if (handlerActivation != null) { 269 final IHandlerService handlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class); 270 handlerService.deactivateHandler(handlerActivation); 271 handlerActivation = null; 272 } 273 } 274 } 275 | Popular Tags |