bug 514043 - [Dark Theme] range indicator color should be
configurable via preferences

Change-Id: I6586f77f3e200677afc251bfc84a42f6d6513887
Signed-off-by: Fabian Pfaff <fabian.pfaff@vogella.com>
diff --git a/org.eclipse.ui.workbench.texteditor/plugin.properties b/org.eclipse.ui.workbench.texteditor/plugin.properties
index a87812f..4f21843 100644
--- a/org.eclipse.ui.workbench.texteditor/plugin.properties
+++ b/org.eclipse.ui.workbench.texteditor/plugin.properties
@@ -186,6 +186,9 @@
 command.openHyperlink.name= Open Hyperlink
 command.openHyperlink.description= Opens the hyperlink at the caret location or opens a chooser if more than one hyperlink is available
 
+Color.rangeIndicator= Range indicator color
+Color.rangeIndicatorDesc= The color that is used for the ruler that marks the range of the current selected text block.
+
 SpellingEngine= Spelling Engine
 
 blockSelectionModeFont.label= Text Editor Block Selection Font
diff --git a/org.eclipse.ui.workbench.texteditor/plugin.xml b/org.eclipse.ui.workbench.texteditor/plugin.xml
index 558c966..d51f0cc 100644
--- a/org.eclipse.ui.workbench.texteditor/plugin.xml
+++ b/org.eclipse.ui.workbench.texteditor/plugin.xml
@@ -1302,6 +1302,15 @@
                value="Monaco-regular-11">
          </fontValue>
       </fontDefinition>
+      <colorDefinition
+            categoryId="org.eclipse.ui.workbenchMisc"
+            id="org.eclipse.ui.editors.rangeIndicatorColor"
+            label="%Color.rangeIndicator"
+            value="COLOR_LIST_SELECTION">
+         <description>
+            %Color.rangeIndicatorDesc
+         </description>
+      </colorDefinition>
    </extension>
    
    <extension
diff --git a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/DefaultRangeIndicator.java b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/DefaultRangeIndicator.java
index 429f6df..f6cda66 100644
--- a/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/DefaultRangeIndicator.java
+++ b/org.eclipse.ui.workbench.texteditor/src/org/eclipse/ui/texteditor/DefaultRangeIndicator.java
@@ -27,6 +27,8 @@
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 
+import org.eclipse.jface.resource.JFaceResources;
+
 import org.eclipse.jface.text.source.Annotation;
 import org.eclipse.jface.text.source.IAnnotationPresentation;
 
@@ -41,10 +43,11 @@
  */
 public class DefaultRangeIndicator extends Annotation implements IAnnotationPresentation {
 
-	 /** The color palette data of this range indicator */
-	private static PaletteData fgPaletteData;
+	private static final String RANGE_INDICATOR_COLOR= "org.eclipse.ui.editors.rangeIndicatorColor"; //$NON-NLS-1$
 	/** The image of this range indicator */
 	private Image fImage;
+	/** The color used to draw the range indicator during the last paint action. */
+	private Color fLastRangeIndicatorColor;
 
 	/**
 	 * Creates a new range indicator.
@@ -54,7 +57,6 @@
 
 	@Override
 	public void paint(GC gc, Canvas canvas, Rectangle bounds) {
-
 		Point canvasSize= canvas.getSize();
 
 		int x= 0;
@@ -74,12 +76,15 @@
 		if (h <= 0)
 			return;
 
-		Image image = getImage(canvas);
+		Color currentRangeIndicatorColor= JFaceResources.getColorRegistry().get(RANGE_INDICATOR_COLOR);
+		Image image= getImage(canvas, currentRangeIndicatorColor);
 		gc.drawImage(image, 0, 0, w, h, x, y, w, h);
 
-		gc.setBackground(canvas.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
+		gc.setBackground(currentRangeIndicatorColor);
 		gc.fillRectangle(x, bounds.y, w, b);
 		gc.fillRectangle(x, bounds.y + bounds.height - b, w, b);
+
+		fLastRangeIndicatorColor= currentRangeIndicatorColor;
 	}
 
 	@Override
@@ -91,28 +96,30 @@
 	 * Returns the image of this range indicator.
 	 *
 	 * @param control the control
+	 * @param rangeIndicatorColor the color to be used to paint the range indicator
 	 * @return an image
 	 */
-	private Image getImage(Control control) {
+	private Image getImage(Control control, Color rangeIndicatorColor) {
 		if (fImage == null) {
-				fImage= createImage(control.getDisplay(), control.getSize());
+			fImage= createImage(control.getDisplay(), control.getSize(), rangeIndicatorColor);
 
-				control.addDisposeListener(new DisposeListener() {
-					@Override
-					public void widgetDisposed(DisposeEvent e) {
-						if (fImage != null && !fImage.isDisposed()) {
-							fImage.dispose();
-							fImage= null;
-						}
+			control.addDisposeListener(new DisposeListener() {
+				@Override
+				public void widgetDisposed(DisposeEvent e) {
+					if (fImage != null && !fImage.isDisposed()) {
+						fImage.dispose();
+						fImage = null;
 					}
-				});
+				}
+			});
 		} else {
 			Rectangle imageRectangle= fImage.getBounds();
 			Point controlSize= control.getSize();
 
-			if (imageRectangle.width < controlSize.x || imageRectangle.height < controlSize.y) {
+			if (imageRectangle.width < controlSize.x || imageRectangle.height < controlSize.y
+					|| !rangeIndicatorColor.equals(fLastRangeIndicatorColor)) {
 				fImage.dispose();
-				fImage= createImage(control.getDisplay(), controlSize);
+				fImage= createImage(control.getDisplay(), controlSize, rangeIndicatorColor);
 			}
 		}
 
@@ -125,22 +132,24 @@
 	 *
 	 * @param display the display on which to create the image
 	 * @param size the image size
+	 * @param rangeIndicatorColor the color to be used to paint the range indicator
 	 * @return a new image
- 	 */
-	private static Image createImage(Display display, Point size) {
+	 */
+	private static Image createImage(Display display, Point size, Color rangeIndicatorColor) {
 
 		int width= size.x;
 		int height= size.y;
 
-		if (fgPaletteData == null)
-			fgPaletteData= createPalette(display);
 
-		ImageData imageData= new ImageData(width, height, 1, fgPaletteData);
+		ImageData imageData= new ImageData(width, height, 1, createPalette(display, rangeIndicatorColor));
 
 		for (int y= 0; y < height; y++)
 			for (int x= 0; x < width; x++)
 				imageData.setPixel(x, y, (x + y) % 2);
 
+		imageData.transparentPixel= imageData.palette.getPixel(imageData.getRGBs()[1]);
+
+
 		return new Image(display, imageData);
 	}
 
@@ -148,19 +157,11 @@
 	 * Creates and returns a new color palette data.
 	 *
 	 * @param display the display
+	 * @param rangeIndicatorColor the color to be used to paint the range indicator
 	 * @return the new color palette data
 	 */
-	private static PaletteData createPalette(Display display) {
-		Color c1;
-		Color c2;
-
-		c1= display.getSystemColor(SWT.COLOR_LIST_SELECTION);
-		c2= display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
-
-		RGB rgbs[]= new RGB[] {
-			new RGB(c1.getRed(), c1.getGreen(), c1.getBlue()),
-			new RGB(c2.getRed(), c2.getGreen(), c2.getBlue())};
-
-		return new PaletteData(rgbs);
+	private static PaletteData createPalette(Display display, Color rangeIndicatorColor) {
+		return new PaletteData(new RGB[] { rangeIndicatorColor.getRGB(),
+				display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND).getRGB() });
 	}
 }