1 11 package org.eclipse.ui.texteditor.rulers; 12 13 import java.net.URL ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.ILog; 19 import org.eclipse.core.runtime.InvalidRegistryObjectException; 20 import org.eclipse.core.runtime.content.IContentType; 21 22 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.ui.IEditorInput; 25 import org.eclipse.ui.IWorkbenchPartSite; 26 import org.eclipse.ui.internal.texteditor.TextEditorPlugin; 27 import org.eclipse.ui.internal.texteditor.rulers.ExtensionPointHelper; 28 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnMessages; 29 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnPlacement; 30 import org.eclipse.ui.internal.texteditor.rulers.RulerColumnTarget; 31 import org.eclipse.ui.texteditor.IDocumentProvider; 32 import org.eclipse.ui.texteditor.IDocumentProviderExtension4; 33 import org.eclipse.ui.texteditor.ITextEditor; 34 35 42 public final class RulerColumnDescriptor { 43 44 private static final String CLASS= "class"; 46 private static final String ID= "id"; 48 private static final String NAME= "name"; 50 private static final String ENABLED= "enabled"; 52 private static final String ICON= "icon"; 54 private static final String GLOBAL= "global"; 56 private static final String INCLUDE_IN_MENU= "includeInMenu"; 58 private static final String TARGET_EDITOR= "targetEditor"; 60 private static final String TARGET_CONTENT_TYPE= "targetContentType"; 62 private static final String TARGET_CLASS= "targetClass"; 64 private static final String PLACEMENT= "placement"; 66 67 private final String fId; 68 69 private final String fName; 70 71 private final ImageDescriptor fIcon; 72 73 private final IConfigurationElement fElement; 74 75 private final RulerColumnTarget fTarget; 76 77 private final RulerColumnPlacement fRulerColumnPlacement; 78 79 private final boolean fDefaultEnablement; 80 81 private final boolean fIsGlobal; 82 83 private final boolean fIncludeInMenu; 84 85 93 RulerColumnDescriptor(IConfigurationElement element, RulerColumnRegistry registry) throws InvalidRegistryObjectException { 94 Assert.isLegal(registry != null); 95 Assert.isLegal(element != null); 96 fElement= element; 97 98 ILog log= TextEditorPlugin.getDefault().getLog(); 99 ExtensionPointHelper helper= new ExtensionPointHelper(element, log); 100 101 fId= helper.getNonNullAttribute(ID); 102 fName= helper.getDefaultAttribute(NAME, fId); 103 helper.getNonNullAttribute(CLASS); URL iconURL= helper.getDefaultResourceURL(ICON, null); 105 fIcon= iconURL == null ? null : ImageDescriptor.createFromURL(iconURL); 106 fDefaultEnablement= helper.getDefaultAttribute(ENABLED, true); 107 fIsGlobal= helper.getDefaultAttribute(GLOBAL, true); 108 fIncludeInMenu= helper.getDefaultAttribute(INCLUDE_IN_MENU, true); 109 110 IConfigurationElement[] targetEditors= element.getChildren(TARGET_EDITOR); 111 IConfigurationElement[] targetContentTypes= element.getChildren(TARGET_CONTENT_TYPE); 112 IConfigurationElement[] targetClasses= element.getChildren(TARGET_CLASS); 113 114 if (targetContentTypes.length + targetEditors.length + targetClasses.length == 0) { 115 helper.fail(RulerColumnMessages.RulerColumnDescriptor_missing_target_msg); 116 fTarget= null; } else { 118 RulerColumnTarget combined= null; 119 for (int i= 0; i < targetEditors.length; i++) { 120 IConfigurationElement targetEditor= targetEditors[i]; 121 RulerColumnTarget target= RulerColumnTarget.createEditorIdTarget(new ExtensionPointHelper(targetEditor, log).getNonNullAttribute(ID)); 122 combined= RulerColumnTarget.createOrTarget(combined, target); 123 } 124 for (int i= 0; i < targetContentTypes.length; i++) { 125 IConfigurationElement targetContentType= targetContentTypes[i]; 126 RulerColumnTarget target= RulerColumnTarget.createContentTypeTarget(new ExtensionPointHelper(targetContentType, log).getNonNullAttribute(ID)); 127 combined= RulerColumnTarget.createOrTarget(combined, target); 128 } 129 for (int i= 0; i < targetClasses.length; i++) { 130 IConfigurationElement targetClass= targetClasses[i]; 131 RulerColumnTarget target= RulerColumnTarget.createClassTarget(new ExtensionPointHelper(targetClass, log).getNonNullAttribute(CLASS)); 132 combined= RulerColumnTarget.createOrTarget(combined, target); 133 } 134 fTarget= combined; 135 } 136 137 IConfigurationElement[] placements= element.getChildren(PLACEMENT); 138 switch (placements.length) { 139 case 0: 140 fRulerColumnPlacement= new RulerColumnPlacement(); 141 break; 142 case 1: 143 fRulerColumnPlacement= new RulerColumnPlacement(placements[0]); 144 break; 145 default: 146 helper.fail(RulerColumnMessages.RulerColumnDescriptor_invalid_placement_msg); 147 fRulerColumnPlacement= null; break; 149 } 150 151 Assert.isTrue(fTarget != null); 152 Assert.isTrue(fRulerColumnPlacement != null); 153 } 154 155 160 public String getId() { 161 return fId; 162 } 163 164 169 public String getName() { 170 return fName; 171 } 172 173 179 public ImageDescriptor getIcon() { 180 return fIcon; 181 } 182 183 RulerColumnTarget getTarget() { 184 return fTarget; 185 } 186 187 RulerColumnPlacement getPlacement() { 188 return fRulerColumnPlacement; 189 } 190 191 197 public boolean getDefaultEnablement() { 198 return fDefaultEnablement; 199 } 200 201 209 public boolean isGlobal() { 210 return fIsGlobal; 211 } 212 213 219 public boolean isIncludedInMenu() { 220 return fIncludeInMenu; 221 } 222 223 229 public boolean matchesEditor(ITextEditor editor) { 230 Assert.isLegal(editor != null); 231 RulerColumnTarget target= getTarget(); 232 233 IWorkbenchPartSite site= editor.getSite(); 234 if (site != null && target.matchesEditorId(site.getId())) 235 return true; 236 237 if (target.matchesClass(editor.getClass())) 238 return true; 239 240 IContentType contentType= getContentType(editor); 241 return contentType != null && target.matchesContentType(contentType); 242 243 } 244 245 253 public IContributedRulerColumn createColumn(ITextEditor editor) throws CoreException, InvalidRegistryObjectException { 254 Assert.isLegal(editor != null); 255 IContributedRulerColumn column= (IContributedRulerColumn)fElement.createExecutableExtension(CLASS); 256 column.setDescriptor(this); 257 column.setEditor(editor); 258 column.columnCreated(); 259 return column; 260 } 261 262 266 public String toString() { 267 return "RulerColumnDescriptor[name=" + getName() + "]"; } 269 270 IConfigurationElement getConfigurationElement() { 271 return fElement; 272 } 273 274 277 public int hashCode() { 278 final int prime= 31; 279 int result= 1; 280 result= prime * result + ((fId == null) ? 0 : fId.hashCode()); 281 return result; 282 } 283 284 287 public boolean equals(Object obj) { 288 if (this == obj) 289 return true; 290 if (obj == null) 291 return false; 292 if (getClass() != obj.getClass()) 293 return false; 294 final RulerColumnDescriptor other= (RulerColumnDescriptor) obj; 295 if (fId == null) { 296 if (other.fId != null) 297 return false; 298 } else if (!fId.equals(other.fId)) 299 return false; 300 return true; 301 } 302 303 311 private IContentType getContentType(ITextEditor editor) { 312 IEditorInput input= editor.getEditorInput(); 313 if (input == null) 314 return null; 315 IDocumentProvider provider= editor.getDocumentProvider(); 316 if (provider instanceof IDocumentProviderExtension4) { 317 IDocumentProviderExtension4 ext= (IDocumentProviderExtension4) provider; 318 try { 319 return ext.getContentType(input); 320 } catch (CoreException x) { 321 } 323 } 324 return null; 325 } 326 327 String getContributor() { 328 try { 329 return fElement.getContributor().getName(); 330 } catch (InvalidRegistryObjectException e) { 331 return "unknown"; } 333 } 334 } 335 | Popular Tags |