/*******************************************************************************
 * Copyright (c) 2014, 2014 Bruno Medeiros and other Contributors.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bruno Medeiros - initial API and implementation
 *******************************************************************************/
package melnorme.lang.ide.ui.views;


import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.StyledString.Styler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.TextStyle;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.navigator.IDescriptionProvider;

public abstract class AbstractLangLabelProvider extends LabelProvider 
		implements IStyledLabelProvider, IDescriptionProvider {
	
	public AbstractLangLabelProvider() {
		super();
	}
	
	@Override
	public String getText(Object element) {
		StyledString styledText = getStyledText(element);
		if(styledText != null) {
			return styledText.getString();
		}
		return null;
	}
	
	@Override
	public abstract StyledString getStyledText(Object element);
	
	/* ----------------- styler helpers ----------------- */
	
	protected static class ForegroundColorStyler extends Styler {
		protected final RGB fgColor;
		
		public ForegroundColorStyler(RGB fgColor) {
			this.fgColor = fgColor;
		}
		
		@Override
		public void applyStyles(TextStyle textStyle) {
			if(fgColor != null) {
				/*FIXME: BUG here Color leak*/
				textStyle.foreground = new Color(Display.getCurrent(), fgColor);
			}
		}
	}
	
	public static ForegroundColorStyler fgColor(RGB rgb) {
		return new ForegroundColorStyler(rgb);
	}
	
	protected static abstract class FontStyler extends Styler {
		
		protected Styler parentStyler;
		
		public FontStyler(Styler parentStyler) {
			this.parentStyler = parentStyler;
		}
		
		@Override
		public void applyStyles(TextStyle textStyle) {
			if(parentStyler != null) {
				parentStyler.applyStyles(textStyle);
			}
			
			Font font = textStyle.font;
			if(font == null) {
				font = JFaceResources.getDefaultFont(); 
			}
			FontDescriptor fontDescriptor = FontDescriptor.createFrom(font);
			fontDescriptor = getModifiedFontDescriptor(fontDescriptor);
			textStyle.font = fontDescriptor.createFont(Display.getCurrent());
		}
		
		protected abstract FontDescriptor getModifiedFontDescriptor(FontDescriptor fontDescriptor);
	}
	
	public static class ItalicStyler extends FontStyler {
		
		public ItalicStyler(Styler parentStyler) {
			super(parentStyler);
		}

		@Override
		protected FontDescriptor getModifiedFontDescriptor(FontDescriptor fontDescriptor) {
			return fontDescriptor.setStyle(SWT.ITALIC);
		}
	}
	
	public static class BoldStyler extends FontStyler {
		
		public BoldStyler(Styler parentStyler) {
			super(parentStyler);
		}
		
		@Override
		protected FontDescriptor getModifiedFontDescriptor(FontDescriptor fontDescriptor) {
			return fontDescriptor.setStyle(SWT.BOLD);
		}
	}
	
	/* ----------------- description ----------------- */
	
	// By default, use full string of styled text  
	@Override
	public String getDescription(Object anElement) {
		
		StyledString styledText = getStyledText(anElement);
		if(styledText != null) {
			return styledText.getString();
		}
		return null;
	}
	
}
@@ -595,16 +595,69 @@
 s;%0D%0A%0D%0A%0D%0A
+import melnorme.util.swt.jface.text.ColorManager;%0D%0A%0D%0A
 import o
@@ -1025,48 +1025,8 @@
 T;%0D%0A
-import org.eclipse.swt.graphics.Color;%0D%0A
 impo
@@ -1765,24 +1765,157 @@
 ---- */%0D%0A%09%0D%0A
+%09/**%0D%0A%09 * Note: this doesn't dispose of created Colors, so to avoid leaking, %0D%0A%09 * it should be used with constant RGBs only %0D%0A%09 */%0D%0A
 %09protected s
@@ -1966,16 +1966,20 @@
 yler %7B%0D%0A
+%09%09%0D%0A
 %09%09protec
@@ -1998,24 +1998,94 @@
 B fgColor;%0D%0A
+%09%09protected final ColorManager registry = ColorManager.getDefault();%0D%0A
 %09%09%0D%0A%09%09public
@@ -2257,99 +2257,49 @@
 %09%09%09%09
-/*FIXME: BUG here Color leak*/%0D%0A%09%09%09%09textStyle.foreground = new Color(Display.getCurrent(), 
+textStyle.foreground = registry.getColor(
 fgCo
@@ -2312,24 +2312,28 @@
 %0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A
+%09%09%0D%0A
 %09%7D%0D%0A%09%0D%0A%09publ

fix Color leak.