1 11 package org.eclipse.ui.texteditor; 12 13 import java.net.URL ; 14 import com.ibm.icu.text.Collator; 15 import java.util.ArrayList ; 16 import java.util.Collections ; 17 import java.util.Comparator ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 21 import org.osgi.framework.Bundle; 22 23 import org.eclipse.swt.graphics.RGB; 24 25 import org.eclipse.core.runtime.FileLocator; 26 import org.eclipse.core.runtime.IConfigurationElement; 27 import org.eclipse.core.runtime.IExtensionPoint; 28 import org.eclipse.core.runtime.Path; 29 import org.eclipse.core.runtime.Platform; 30 31 import org.eclipse.core.resources.IMarker; 32 33 import org.eclipse.jface.preference.IPreferenceStore; 34 import org.eclipse.jface.preference.PreferenceConverter; 35 import org.eclipse.jface.resource.ImageDescriptor; 36 import org.eclipse.jface.resource.StringConverter; 37 38 import org.eclipse.ui.editors.text.EditorsUI; 39 40 import org.eclipse.ui.internal.editors.text.EditorsPlugin; 41 42 43 49 public class MarkerAnnotationPreferences { 50 51 57 public static void initializeDefaultValues(IPreferenceStore store) { 58 59 boolean ignoreAnnotationsPrefPage= store.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.USE_ANNOTATIONS_PREFERENCE_PAGE); 60 boolean ignoreQuickDiffPrefPage= store.getBoolean(AbstractDecoratedTextEditorPreferenceConstants.USE_QUICK_DIFF_PREFERENCE_PAGE); 61 62 MarkerAnnotationPreferences preferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences(); 63 Iterator e= preferences.getAnnotationPreferences().iterator(); 64 while (e.hasNext()) { 65 AnnotationPreference info= (AnnotationPreference) e.next(); 66 67 if (ignoreAnnotationsPrefPage && info.isIncludeOnPreferencePage() && isComplete(info)) 68 continue; 69 70 if (ignoreQuickDiffPrefPage && (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffChange") || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) )) 74 continue; 75 76 store.setDefault(info.getTextPreferenceKey(), info.getTextPreferenceValue()); 77 store.setDefault(info.getOverviewRulerPreferenceKey(), info.getOverviewRulerPreferenceValue()); 78 if (info.getVerticalRulerPreferenceKey() != null) 79 store.setDefault(info.getVerticalRulerPreferenceKey(), info.getVerticalRulerPreferenceValue()); 80 PreferenceConverter.setDefault(store, info.getColorPreferenceKey(), info.getColorPreferenceValue()); 81 if (info.getShowInNextPrevDropdownToolbarActionKey() != null) 82 store.setDefault(info.getShowInNextPrevDropdownToolbarActionKey(), info.isShowInNextPrevDropdownToolbarAction()); 83 if (info.getIsGoToNextNavigationTargetKey() != null) 84 store.setDefault(info.getIsGoToNextNavigationTargetKey(), info.isGoToNextNavigationTarget()); 85 if (info.getIsGoToPreviousNavigationTargetKey() != null) 86 store.setDefault(info.getIsGoToPreviousNavigationTargetKey(), info.isGoToPreviousNavigationTarget()); 87 if (info.getHighlightPreferenceKey() != null) 88 store.setDefault(info.getHighlightPreferenceKey(), info.getHighlightPreferenceValue()); 89 if (info.getTextStylePreferenceKey() != null) 90 store.setDefault(info.getTextStylePreferenceKey(), info.getTextStyleValue()); 91 } 92 } 93 94 111 public static void useAnnotationsPreferencePage(IPreferenceStore store) throws IllegalStateException { 112 checkAccess(); 113 114 store.putValue(AbstractDecoratedTextEditorPreferenceConstants.USE_ANNOTATIONS_PREFERENCE_PAGE, Boolean.toString(true)); 115 116 MarkerAnnotationPreferences preferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences(); 117 Iterator e= preferences.getAnnotationPreferences().iterator(); 118 while (e.hasNext()) { 119 AnnotationPreference info= (AnnotationPreference) e.next(); 120 121 if (!info.isIncludeOnPreferencePage() || !isComplete(info)) 123 continue; 124 125 store.setToDefault(info.getTextPreferenceKey()); 126 store.setToDefault(info.getOverviewRulerPreferenceKey()); 127 if (info.getVerticalRulerPreferenceKey() != null) 128 store.setToDefault(info.getVerticalRulerPreferenceKey()); 129 store.setToDefault(info.getColorPreferenceKey()); 130 if (info.getShowInNextPrevDropdownToolbarActionKey() != null) 131 store.setToDefault(info.getShowInNextPrevDropdownToolbarActionKey()); 132 if (info.getIsGoToNextNavigationTargetKey() != null) 133 store.setToDefault(info.getIsGoToNextNavigationTargetKey()); 134 if (info.getIsGoToPreviousNavigationTargetKey() != null) 135 store.setToDefault(info.getIsGoToPreviousNavigationTargetKey()); 136 if (info.getHighlightPreferenceKey() != null) 137 store.setToDefault(info.getHighlightPreferenceKey()); 138 if (info.getTextStylePreferenceKey() != null) 139 store.setToDefault(info.getTextStylePreferenceKey()); 140 } 141 } 142 143 160 public static void useQuickDiffPreferencePage(IPreferenceStore store) throws IllegalStateException { 161 checkAccess(); 162 163 store.putValue(AbstractDecoratedTextEditorPreferenceConstants.USE_QUICK_DIFF_PREFERENCE_PAGE, Boolean.toString(true)); 164 165 MarkerAnnotationPreferences preferences= EditorsPlugin.getDefault().getMarkerAnnotationPreferences(); 166 Iterator e= preferences.getAnnotationPreferences().iterator(); 167 while (e.hasNext()) { 168 AnnotationPreference info= (AnnotationPreference) e.next(); 169 170 172 if (!(info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffChange") || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffAddition")) || (info.getAnnotationType().equals("org.eclipse.ui.workbench.texteditor.quickdiffDeletion")) )) 176 continue; 177 178 store.setToDefault(info.getTextPreferenceKey()); 179 store.setToDefault(info.getOverviewRulerPreferenceKey()); 180 if (info.getVerticalRulerPreferenceKey() != null) 181 store.setToDefault(info.getVerticalRulerPreferenceKey()); 182 store.setToDefault(info.getColorPreferenceKey()); 183 if (info.getShowInNextPrevDropdownToolbarActionKey() != null) 184 store.setToDefault(info.getShowInNextPrevDropdownToolbarActionKey()); 185 if (info.getIsGoToNextNavigationTargetKey() != null) 186 store.setToDefault(info.getIsGoToNextNavigationTargetKey()); 187 if (info.getIsGoToPreviousNavigationTargetKey() != null) 188 store.setToDefault(info.getIsGoToPreviousNavigationTargetKey()); 189 if (info.getHighlightPreferenceKey() != null) 190 store.setToDefault(info.getHighlightPreferenceKey()); 191 if (info.getTextStylePreferenceKey() != null) 192 store.setToDefault(info.getTextStylePreferenceKey()); 193 } 194 } 195 196 private static final class AccessChecker extends SecurityManager { 197 public Class [] getClassContext() { 198 return super.getClassContext(); 199 } 200 } 201 207 private static void checkAccess() throws IllegalStateException { 208 Class [] elements = new AccessChecker().getClassContext(); 209 if (!(elements[3].equals(EditorsUI.class) 210 || elements[4].equals(EditorsUI.class))) 211 throw new IllegalStateException (); 212 } 213 214 215 216 private List fFragments; 217 218 private List fPreferences; 219 220 224 public MarkerAnnotationPreferences() { 225 this(false); 226 } 227 228 235 private MarkerAnnotationPreferences(boolean initFromPreferences) { 236 if (initFromPreferences) 237 initializeSharedMakerAnnotationPreferences(); 238 } 239 240 245 public List getAnnotationPreferences() { 246 if (fPreferences == null) 247 initialize(); 248 return fPreferences; 249 } 250 251 260 public List getAnnotationPreferenceFragments() { 261 if (fFragments == null) 262 initialize(); 263 return fFragments; 264 } 265 266 private void initialize() { 267 synchronized (EditorsPlugin.getDefault()) { 268 if (!EditorsPlugin.getDefault().isMarkerAnnotationPreferencesInitialized()) 269 EditorsPlugin.getDefault().setMarkerAnnotationPreferences(new MarkerAnnotationPreferences(true)); 270 } 271 272 MarkerAnnotationPreferences sharedPrefs= EditorsPlugin.getDefault().getMarkerAnnotationPreferences(); 273 274 fFragments= cloneAnnotationPreferences(sharedPrefs.fFragments); 275 fPreferences= cloneAnnotationPreferences(sharedPrefs.fPreferences); 276 } 277 278 282 private void initializeSharedMakerAnnotationPreferences() { 283 284 fFragments= new ArrayList (2); 286 fPreferences= new ArrayList (2); 287 288 IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(EditorsUI.PLUGIN_ID, "markerAnnotationSpecification"); if (extensionPoint != null) { 291 IConfigurationElement[] elements= extensionPoint.getConfigurationElements(); 292 for (int i= 0; i < elements.length; i++) { 293 AnnotationPreference spec= createSpec(elements[i]); 294 if (spec != null) 295 fFragments.add(spec); 296 if (isComplete(spec)) 297 fPreferences.add(spec); 298 } 299 } 300 301 final Collator collator= Collator.getInstance(); 302 Collections.sort(fFragments, new Comparator () { 303 306 public int compare(Object o1, Object o2) { 307 if (o1 == o2) 308 return 0; 309 310 AnnotationPreference ap1= (AnnotationPreference)o1; 311 AnnotationPreference ap2= (AnnotationPreference)o2; 312 313 String label1= ap1.getPreferenceLabel(); 314 String label2= ap2.getPreferenceLabel(); 315 316 if (label1 == null && label2 == null) 317 return 0; 318 319 if (label1 == null) 320 return -1; 321 322 if (label2 == null) 323 return 1; 324 325 return collator.compare(label1, label2); 326 } 327 }); 328 } 329 330 337 private List cloneAnnotationPreferences(List annotationPreferences) { 338 if (annotationPreferences == null) 339 return null; 340 List clone= new ArrayList (annotationPreferences.size()); 341 Iterator iter= annotationPreferences.iterator(); 342 while (iter.hasNext()) 343 clone.add(clone(((AnnotationPreference)iter.next()))); 344 return clone; 345 } 346 347 354 private AnnotationPreference clone(AnnotationPreference annotationPreference) { 355 if (annotationPreference == null) 356 return null; 357 358 AnnotationPreference clone= new AnnotationPreference(); 359 if (annotationPreference.getAnnotationType() != null) { 360 clone.setAnnotationType(annotationPreference.getAnnotationType()); 361 clone.merge(annotationPreference); 362 } 363 364 return clone; 365 } 366 367 376 private static boolean isComplete(AnnotationPreference spec) { 377 return spec.getColorPreferenceKey() != null 378 && spec.getColorPreferenceValue() != null 379 && spec.getTextPreferenceKey() != null 380 && spec.getOverviewRulerPreferenceKey() != null; 381 } 382 383 389 private AnnotationPreference createSpec(IConfigurationElement element) { 390 391 String s; 392 int i; 393 boolean b; 394 395 ReadOnlyAnnotationPreference info= new ReadOnlyAnnotationPreference(); 396 397 s= element.getAttribute("annotationType"); if (s == null || s.trim().length() == 0) return null; 399 info.setAnnotationType(s); 400 401 s= element.getAttribute("label"); if (s != null && s.trim().length() > 0) 403 info.setPreferenceLabel(s); 404 405 s= element.getAttribute("markerType"); if (s != null && s.trim().length() > 0) 407 info.setMarkerType(s); 408 409 s= element.getAttribute("markerSeverity"); if (s != null && s.trim().length() > 0) { 411 i= StringConverter.asInt(s, IMarker.SEVERITY_INFO); 412 info.setSeverity(i); 413 } 414 415 s= element.getAttribute("textPreferenceKey"); if (s != null && s.trim().length() > 0) 417 info.setTextPreferenceKey(s); 418 419 s= element.getAttribute("textPreferenceValue"); if (s != null && s.trim().length() > 0) { 421 b= StringConverter.asBoolean(s, false); 422 info.setTextPreferenceValue(b); 423 } 424 425 s= element.getAttribute("highlightPreferenceKey"); if (s != null && s.trim().length() > 0) 427 info.setHighlightPreferenceKey(s); 428 429 s= element.getAttribute("highlightPreferenceValue"); if (s != null && s.trim().length() > 0) { 431 b= StringConverter.asBoolean(s, false); 432 info.setHighlightPreferenceValue(b); 433 } 434 435 s= element.getAttribute("overviewRulerPreferenceKey"); if (s != null && s.trim().length() > 0) 437 info.setOverviewRulerPreferenceKey(s); 438 439 s= element.getAttribute("overviewRulerPreferenceValue"); if (s != null && s.trim().length() > 0) { 441 b= StringConverter.asBoolean(s, false); 442 info.setOverviewRulerPreferenceValue(b); 443 } 444 445 s= element.getAttribute("verticalRulerPreferenceKey"); if (s != null && s.trim().length() > 0) 447 info.setVerticalRulerPreferenceKey(s); 448 449 s= element.getAttribute("verticalRulerPreferenceValue"); if (s != null && s.trim().length() > 0) { 451 b= StringConverter.asBoolean(s, true); 452 info.setVerticalRulerPreferenceValue(b); 453 } 454 455 s= element.getAttribute("colorPreferenceKey"); if (s != null && s.trim().length() > 0) 457 info.setColorPreferenceKey(s); 458 459 s= element.getAttribute("colorPreferenceValue"); if (s != null && s.trim().length() > 0) { 461 RGB rgb= StringConverter.asRGB(s); 462 info.setColorPreferenceValue(rgb == null ? new RGB(0,0,0) : rgb); 463 } 464 465 s= element.getAttribute("presentationLayer"); if (s != null && s.trim().length() > 0) { 467 i= StringConverter.asInt(s, 0); 468 info.setPresentationLayer(i); 469 } 470 471 s= element.getAttribute("contributesToHeader"); if (s != null && s.trim().length() > 0) { 473 b= StringConverter.asBoolean(s, false); 474 info.setContributesToHeader(b); 475 } 476 477 s= element.getAttribute("showInNextPrevDropdownToolbarActionKey"); if (s != null && s.trim().length() > 0) 479 info.setShowInNextPrevDropdownToolbarActionKey(s); 480 481 s= element.getAttribute("showInNextPrevDropdownToolbarAction"); if (s != null && s.trim().length() > 0) { 483 b= StringConverter.asBoolean(s, false); 484 info.setShowInNextPrevDropdownToolbarAction(b); 485 } 486 487 s= element.getAttribute("isGoToNextNavigationTargetKey"); if (s != null && s.trim().length() > 0) 489 info.setIsGoToNextNavigationTargetKey(s); 490 491 s= element.getAttribute("isGoToNextNavigationTarget"); if (s != null && s.trim().length() > 0) { 493 b= StringConverter.asBoolean(s, false); 494 info.setIsGoToNextNavigationTarget(b); 495 } 496 497 s= element.getAttribute("isGoToPreviousNavigationTargetKey"); if (s != null && s.trim().length() > 0) 499 info.setIsGoToPreviousNavigationTargetKey(s); 500 501 s= element.getAttribute("isGoToPreviousNavigationTarget"); if (s != null && s.trim().length() > 0) { 503 b= StringConverter.asBoolean(s, false); 504 info.setIsGoToPreviousNavigationTarget(b); 505 } 506 507 s= element.getAttribute("symbolicIcon"); if (s != null && s.trim().length() > 0) 509 info.setSymbolicImageName(s); 510 511 s= element.getAttribute("icon"); if (s != null && s.trim().length() > 0) 513 info.setImageDescriptor(getImageDescriptor(s, element)); 514 515 s= element.getAttribute("quickFixIcon"); if (s != null && s.trim().length() > 0) 517 info.setQuickFixImageDescriptor(getImageDescriptor(s, element)); 518 519 s= element.getAttribute("annotationImageProvider"); if (s != null && s.trim().length() > 0) 521 info.setAnnotationImageProviderData(element, "annotationImageProvider"); 523 s= element.getAttribute("textStylePreferenceKey"); if (s != null && s.trim().length() > 0) 525 info.setTextStylePreferenceKey(s); 526 527 s= element.getAttribute("textStylePreferenceValue"); if (s != null && s.trim().length() > 0) { 529 530 if (AnnotationPreference.STYLE_BOX.equals(s) 531 || AnnotationPreference.STYLE_DASHED_BOX.equals(s) 532 || AnnotationPreference.STYLE_IBEAM.equals(s) 533 || AnnotationPreference.STYLE_SQUIGGLES.equals(s) 534 || AnnotationPreference.STYLE_UNDERLINE.equals(s)) 535 info.setTextStyleValue(s); 536 else 537 info.setTextStyleValue(AnnotationPreference.STYLE_NONE); 538 539 } 540 541 s= element.getAttribute("includeOnPreferencePage"); info.setIncludeOnPreferencePage(s == null || StringConverter.asBoolean(s, true)); 543 544 info.markReadOnly(); 545 546 return info; 547 } 548 549 558 private ImageDescriptor getImageDescriptor(String iconPath, IConfigurationElement element) { 559 String pluginId= element.getContributor().getName(); 560 Bundle bundle= Platform.getBundle(pluginId); 561 if (bundle == null) 562 return null; 563 564 URL url= FileLocator.find(bundle, new Path(iconPath), null); 565 if (url != null) 566 return ImageDescriptor.createFromURL(url); 567 568 return ImageDescriptor.getMissingImageDescriptor(); 569 } 570 } 571 | Popular Tags |