001: package net.refractions.udig.style.sld.editor.internal;
002:
003: import java.awt.Color;
004:
005: import net.refractions.udig.style.sld.SLDPlugin;
006: import net.refractions.udig.ui.graphics.Glyph;
007: import net.refractions.udig.ui.graphics.SLDs;
008:
009: import org.eclipse.jface.viewers.ILabelProviderListener;
010: import org.eclipse.jface.viewers.ITableLabelProvider;
011: import org.eclipse.jface.viewers.LabelProvider;
012: import org.eclipse.swt.graphics.Image;
013: import org.geotools.brewer.color.StyleGenerator;
014: import org.geotools.styling.Rule;
015: import org.geotools.styling.Style;
016:
017: public class StyleTreeLabelProvider extends LabelProvider implements
018: ITableLabelProvider {
019: public Image getImage(Object element) {
020: if (element instanceof Rule) {
021: //grab the color for the current rule
022: Rule rule = (Rule) element;
023: String[] colors = SLDs.colors(rule);
024: if (colors.length == 0)
025: return null;
026: if (colors.length > 1) {
027: SLDPlugin
028: .log(
029: "Multiple colours received unexpectedly, proceeding with first colour only.", null); //$NON-NLS-1$
030: }
031: final Color color = SLDs.toColor(colors[0]);
032: return Glyph.swatch(color).createImage();
033: }
034: return null;
035: }
036:
037: public String getText(Object element) {
038: if (element instanceof Style) {
039: //shouldn't be called, as the root object is hidden
040: Style style = (Style) element;
041: return style.getTitle();
042: } else if (element instanceof Rule) {
043: Rule rule = (Rule) element;
044: if (rule.getName().startsWith("rule")) //$NON-NLS-1$
045: return StyleGenerator.toStyleExpression(rule
046: .getFilter());
047: else
048: return rule.getTitle();
049: }
050: return super .getText(element); //unknown type
051: }
052:
053: public void removeListener(ILabelProviderListener listener) {
054: }
055:
056: public Image getColumnImage(Object element, int columnIndex) {
057: if (columnIndex == 0) { //image
058: if (element instanceof Rule) {
059: //grab the color for the current rule
060: Rule rule = (Rule) element;
061: String[] colors = SLDs.colors(rule);
062: if (colors.length == 0)
063: return null;
064: Color color = SLDs.toColor(colors[0]);
065: return Glyph.swatch(color).createImage();
066: }
067: }
068: return null;
069: }
070:
071: public String getColumnText(Object element, int columnIndex) {
072: if (columnIndex == 0) { //colour
073: return ""; //$NON-NLS-1$
074: } else if (columnIndex == 1) { //label
075: if (element instanceof String) {
076: SLDPlugin.log("already knew label", null); //$NON-NLS-1$
077: return (String) element;
078: } else if (element instanceof Rule) {
079: Rule rule = (Rule) element;
080: return rule.getTitle();
081: }
082: } else if (columnIndex == 2) { //style expression
083: if (element instanceof Rule) {
084: Rule rule = (Rule) element;
085: if (rule.getName().startsWith("rule")) //$NON-NLS-1$
086: return StyleGenerator.toStyleExpression(rule
087: .getFilter());
088: else
089: return null;
090: }
091: }
092: return null;
093: }
094:
095: public void addListener(ILabelProviderListener listener) {
096: }
097:
098: public void dispose() {
099: }
100:
101: public boolean isLabelProperty(Object element, String property) {
102: return true;
103: }
104:
105: }
|