001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.ui;
016:
017: import net.refractions.udig.ui.internal.Messages;
018:
019: import org.eclipse.jface.viewers.IColorProvider;
020: import org.eclipse.jface.viewers.ITableLabelProvider;
021: import org.eclipse.jface.viewers.LabelProvider;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.graphics.Color;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.swt.widgets.Display;
026: import org.geotools.feature.AttributeType;
027: import org.geotools.feature.Feature;
028: import org.geotools.feature.FeatureCollection;
029:
030: import com.vividsolutions.jts.geom.Geometry;
031:
032: class FeatureTableLabelProvider extends LabelProvider implements
033: ITableLabelProvider, IColorProvider {
034: /** FeatureLabelProvider owningFeatureTableControl field */
035: private final FeatureTableControl owningFeatureTableControl;
036:
037: /**
038: * @param control
039: */
040: FeatureTableLabelProvider(FeatureTableControl control) {
041: owningFeatureTableControl = control;
042: }
043:
044: public Image getColumnImage(Object element, int columnIndex) {
045: return null;
046: }
047:
048: /**
049: *
050: * Returns the value of the column / feature attribute.
051: *
052: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
053: * @param element the array of feature attributes.
054: * @param columnIndex the column index / feature attribute.
055: * @return the string representation of the feature attribute, except
056: * for attributes of type Geometry in which a string representing the
057: * type of Geometry is returned.
058: */
059: public String getColumnText(Object element, int columnIndex) {
060: if (element instanceof FeatureCollection) {
061: FeatureCollection fc = (FeatureCollection) element;
062: if (columnIndex == 0) {
063: return fc.getSchema().getTypeName();
064: }
065: return ""; //$NON-NLS-1$
066: } else if (element instanceof Feature) {
067: Feature f = (Feature) element;
068: if (columnIndex == 0)
069: return f.getID();
070: if (owningFeatureTableControl.features == null)
071: return ""; //$NON-NLS-1$
072: String attName = owningFeatureTableControl.getViewer()
073: .getTable().getColumn(columnIndex).getText();
074: AttributeType at = f.getFeatureType().getAttributeType(
075: attName);
076: if (Geometry.class.isAssignableFrom(at.getType())) { //was at.isGeometry()
077: Object att = f.getAttribute(attName);
078: if (att == null)
079: return ""; //$NON-NLS-1$
080: String s = att.getClass().getName();
081: return s.substring(s.lastIndexOf('.') + 1);
082: }
083:
084: Object attribute = f.getAttribute(attName);
085: return attribute == null ? "" : attribute.toString(); //$NON-NLS-1$
086: } else if (element instanceof Throwable) {
087: if (columnIndex == 0) {
088: return ((Throwable) element).getLocalizedMessage();
089: }
090: return null;
091: } else if (element instanceof String) {
092: return (String) element;
093: } else
094: return Messages.FeatureTableControl_loadingMessage;
095: }
096:
097: public Color getBackground(Object element) {
098: Display currentDisplay = Display.getCurrent();
099: if (element == FeatureTableControl.LOADING)
100: return currentDisplay.getSystemColor(SWT.COLOR_YELLOW);
101: if (element instanceof Throwable) {
102: return currentDisplay.getSystemColor(SWT.COLOR_RED);
103: }
104: if (element instanceof Feature) {
105: Feature feature = (Feature) element;
106: if (owningFeatureTableControl.getSelectionProvider()
107: .getSelectionFids().contains(feature.getID())) {
108: return currentDisplay.getSystemColor(SWT.COLOR_YELLOW);
109: }
110: }
111:
112: return null;
113: }
114:
115: public Color getForeground(Object element) {
116: Display currentDisplay = Display.getCurrent();
117: if (element instanceof Throwable) {
118: return currentDisplay.getSystemColor(SWT.COLOR_WHITE);
119: }
120: if (element instanceof Feature) {
121: Feature feature = (Feature) element;
122: if (owningFeatureTableControl.getSelectionProvider()
123: .getSelectionFids().contains(feature.getID())) {
124: return currentDisplay.getSystemColor(SWT.COLOR_BLACK);
125: }
126: }
127:
128: return null;
129: }
130: }
|