/net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/editors/haskell/text/ColorProvider.java

https://github.com/serras/eclipsefp · Java · 156 lines · 120 code · 20 blank · 16 comment · 13 complexity · 13f5f9dceb4dbda830aa8d1fec144234 MD5 · raw file

  1. // Copyright (c) 2003-2008 by Leif Frenzel - see http://leiffrenzel.de
  2. // This code is made available under the terms of the Eclipse Public License,
  3. // version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
  4. package net.sf.eclipsefp.haskell.ui.internal.editors.haskell.text;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.Map;
  8. import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
  9. import net.sf.eclipsefp.haskell.ui.internal.preferences.editor.IEditorPreferenceNames;
  10. import org.eclipse.core.runtime.Assert;
  11. import org.eclipse.jface.preference.IPreferenceStore;
  12. import org.eclipse.jface.preference.PreferenceConverter;
  13. import org.eclipse.jface.resource.StringConverter;
  14. import org.eclipse.swt.graphics.Color;
  15. import org.eclipse.swt.graphics.RGB;
  16. import org.eclipse.swt.widgets.Display;
  17. /** Provides colors for syntax coloring in the editor.
  18. *
  19. * This is implemented as singleton to make it accessible from
  20. * everywhere and also to reduce resource management to one single place.
  21. *
  22. * @author Leif Frenzel
  23. */
  24. public class ColorProvider implements IEditorPreferenceNames {
  25. public static final RGB DEFAULT_COMMENT = new RGB( 63, 127, 95 );
  26. public static final RGB DEFAULT_LITERATE_COMMENT = new RGB( 63, 95, 191 );
  27. public static final RGB DEFAULT_DOC = new RGB( 63, 95, 191 );
  28. public static final RGB DEFAULT_PRAGMA = new RGB( 63, 95, 191 );
  29. public static final RGB DEFAULT_KEYWORD = new RGB( 0, 0, 196 );
  30. public static final RGB DEFAULT_FUNCTION = new RGB( 64, 192, 192 );
  31. public static final RGB DEFAULT_STRING = new RGB( 128, 64, 64 );
  32. public static final RGB DEFAULT_CHAR = new RGB( 96, 96, 96 );
  33. public static final RGB DEFAULT_NUMBER = new RGB( 0, 0, 0 );
  34. public static final RGB DEFAULT_VAR = new RGB( 0, 0, 0 );
  35. public static final RGB DEFAULT_VARSYM = new RGB( 155, 0, 0 );
  36. public static final RGB DEFAULT_CON = new RGB( 200, 0, 0 );
  37. public static final RGB DEFAULT_CPP = new RGB( 63, 95, 191 );
  38. public static final RGB DEFAULT_TH = new RGB( 0, 0, 0 );
  39. public static final RGB DEFAULT_SYMBOL = new RGB( 128, 128, 0 );
  40. public static final RGB DEFAULT_OTHER = new RGB( 0, 0, 0 );
  41. /** The internal singleton reference to ColorProvider. */
  42. private static class SingletonHolder {
  43. private static final ColorProvider theInstance = new ColorProvider();
  44. }
  45. private final Map<RGB, Color> colors;
  46. private final Map<String, RGB> rgbs;
  47. private IPreferenceStore prefStore;
  48. /** <p>constructs the singleton instance of ColorProvider. Private in order
  49. * to ensure the singleton pattern.</p> */
  50. private ColorProvider() {
  51. colors = new HashMap<RGB, Color>( 10 );
  52. rgbs = new HashMap<String, RGB>( 10 );
  53. initializeRgbs();
  54. }
  55. public static final ColorProvider getInstance() {
  56. return SingletonHolder.theInstance;
  57. }
  58. public ColorProvider(final IPreferenceStore store){
  59. prefStore=store;
  60. colors = new HashMap<RGB, Color>( 10 );
  61. rgbs = new HashMap<String, RGB>( 10 );
  62. initializeRgbs();
  63. }
  64. /** <p>releases all of the color resources held by this ColorProvider.</p> */
  65. public void dispose() {
  66. Iterator<Color> it = colors.values().iterator();
  67. while( it.hasNext() ) {
  68. it.next().dispose();
  69. it.remove();
  70. }
  71. }
  72. public Color getColor( final String key ) {
  73. RGB rgb = rgbs.get( key );
  74. Assert.isNotNull( rgb );
  75. return getColor( rgb );
  76. }
  77. void changeColor( final String key, final Object newValue ) {
  78. RGB oldRgb = rgbs.get( key );
  79. if( oldRgb != null ) {
  80. RGB newRgb = getNewRgb( newValue );
  81. if( newRgb != null ) {
  82. rgbs.put( key, newRgb );
  83. }
  84. }
  85. }
  86. // helping methods
  87. //////////////////
  88. private RGB getNewRgb( final Object value ) {
  89. RGB result = null;
  90. if( value instanceof RGB ) {
  91. result = ( RGB )value;
  92. } else if( value instanceof String ) {
  93. result = StringConverter.asRGB( ( String )value );
  94. }
  95. return result;
  96. }
  97. private Color getColor( final RGB rgb ) {
  98. Color color = colors.get( rgb );
  99. if( color == null || color.isDisposed()) {
  100. color = new Color( Display.getCurrent(), rgb );
  101. colors.put( rgb, color );
  102. }
  103. return color;
  104. }
  105. private void initializeRgbs() {
  106. putRgb( EDITOR_COMMENT_COLOR, DEFAULT_COMMENT );
  107. putRgb( EDITOR_DOC_COLOR, DEFAULT_DOC);
  108. putRgb( EDITOR_PRAGMA_COLOR, DEFAULT_PRAGMA );
  109. putRgb( EDITOR_LITERATE_COMMENT_COLOR, DEFAULT_LITERATE_COMMENT );
  110. putRgb( EDITOR_FUNCTION_COLOR, DEFAULT_FUNCTION );
  111. putRgb( EDITOR_KEYWORD_COLOR, DEFAULT_KEYWORD );
  112. putRgb( EDITOR_DEFAULT_COLOR, DEFAULT_OTHER );
  113. putRgb( EDITOR_STRING_COLOR, DEFAULT_STRING );
  114. putRgb( EDITOR_CHAR_COLOR, DEFAULT_CHAR );
  115. putRgb( EDITOR_NUMBER_COLOR, DEFAULT_NUMBER );
  116. putRgb( EDITOR_VAR_COLOR, DEFAULT_VAR );
  117. putRgb( EDITOR_VARSYM_COLOR, DEFAULT_VARSYM );
  118. putRgb( EDITOR_CON_COLOR, DEFAULT_CON );
  119. putRgb( EDITOR_CPP_COLOR, DEFAULT_CPP );
  120. putRgb( EDITOR_TH_COLOR, DEFAULT_TH );
  121. putRgb( EDITOR_SYMBOL_COLOR, DEFAULT_SYMBOL );
  122. }
  123. private void putRgb( final String key, final RGB defaultRgb ) {
  124. RGB rgb = PreferenceConverter.getColor( getPreferenceStore(), key );
  125. if( rgb == null ) {
  126. rgb = defaultRgb;
  127. }
  128. rgbs.put( key, rgb );
  129. }
  130. private IPreferenceStore getPreferenceStore() {
  131. if (prefStore!=null){
  132. return prefStore;
  133. }
  134. return HaskellUIPlugin.getDefault().getPreferenceStore();
  135. }
  136. }