001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package net.refractions.udig.project.ui.internal.properties;
017:
018: import java.math.BigDecimal;
019: import java.math.BigInteger;
020:
021: import net.refractions.udig.project.internal.ProjectPlugin;
022: import net.refractions.udig.project.ui.internal.Messages;
023: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
024: import net.refractions.udig.ui.AttributeValidator;
025: import net.refractions.udig.ui.BasicTypeCellEditor;
026:
027: import org.eclipse.jface.viewers.CellEditor;
028: import org.eclipse.jface.viewers.ComboBoxCellEditor;
029: import org.eclipse.jface.viewers.ICellEditorValidator;
030: import org.eclipse.jface.viewers.ILabelProvider;
031: import org.eclipse.jface.viewers.LabelProvider;
032: import org.eclipse.jface.viewers.TextCellEditor;
033: import org.eclipse.swt.widgets.Composite;
034: import org.eclipse.ui.views.properties.PropertyDescriptor;
035: import org.geotools.feature.AttributeType;
036: import org.geotools.feature.FeatureType;
037: import org.opengis.util.CodeList;
038:
039: /**
040: * A PropertyDescriptor for attributes of a Feature.
041: *
042: * @author jeichar
043: * @since 0.3
044: */
045: public class AttributePropertyDescriptor extends PropertyDescriptor {
046: protected AttributeType type;
047: AttributeValidator validator;
048: private String[] comboBoxList;
049:
050: /** Is the property editable and should return a cell editor. */
051: private boolean editable = false;
052:
053: /**
054: * Creates a new instance of AttributePropertyDescriptor
055: *
056: * @param id The object used to identify the value
057: * @param displayName The Property display name
058: * @param type The Attribute type that describes the attribute
059: * @param featureType the featureType that contains the attributeType.
060: */
061: public AttributePropertyDescriptor(Object id, String displayName,
062: AttributeType type, FeatureType schema) {
063: this (id, displayName, type, schema, false);
064: }
065:
066: /**
067: *
068: * @param id The object used to identify the value
069: * @param displayName The Property display name
070: * @param type The Attribute type that describes the attribute
071: * @param featureType the featureType that contains the attributeType.
072: * @param editable
073: */
074: public AttributePropertyDescriptor(Object id, String displayName,
075: AttributeType type, FeatureType schema, boolean editable) {
076: super (id, displayName);
077: this .type = type;
078: validator = new AttributeValidator(type, schema);
079: comboBoxList = createComboList();
080: this .editable = editable;
081: }
082:
083: /**
084: * @see org.eclipse.ui.views.properties.PropertyDescriptor#createPropertyEditor(org.eclipse.swt.widgets.Composite)
085: */
086: public CellEditor createPropertyEditor(Composite parent) {
087: if (!editable)
088: return null;
089:
090: try {
091: if (Boolean.class.isAssignableFrom(type.getType())
092: || boolean.class.isAssignableFrom(type.getType()))
093: return new ComboBoxCellEditor(parent, new String[] {
094: Messages.AttributePropertyDescriptor_true,
095: Messages.AttributePropertyDescriptor_false });
096: if (String.class.isAssignableFrom(type.getType()))
097: return new TextCellEditor(parent);
098: if (Integer.class.isAssignableFrom(type.getType()))
099: return new BasicTypeCellEditor(parent, Integer.class);
100: if (Double.class.isAssignableFrom(type.getType()))
101: return new BasicTypeCellEditor(parent, Double.class);
102: if (Float.class.isAssignableFrom(type.getType()))
103: return new BasicTypeCellEditor(parent, Float.class);
104: if (Long.class.isAssignableFrom(type.getType()))
105: return new BasicTypeCellEditor(parent, Long.class);
106: if (BigInteger.class.isAssignableFrom(type.getType()))
107: return new BasicTypeCellEditor(parent, BigInteger.class);
108: if (BigDecimal.class.isAssignableFrom(type.getType()))
109: return new BasicTypeCellEditor(parent, BigDecimal.class);
110: if (Long.class.isAssignableFrom(type.getType()))
111: return new BasicTypeCellEditor(parent, Long.class);
112: if (CodeList.class.isAssignableFrom(type.getType())) {
113: return new ComboBoxCellEditor(parent, comboBoxList);
114: }
115: return super .createPropertyEditor(parent);
116: } catch (Throwable t) {
117: ProjectUIPlugin.log("error converting attribute to string",
118: t);
119: return null;
120: }
121: }
122:
123: String[] createComboList() {
124: if (!(CodeList.class.isAssignableFrom(type.getType())))
125: return null;
126: CodeList list = (CodeList) type.createDefaultValue();
127: CodeList[] family = list.family();
128: String[] names = new String[family.length];
129: for (int i = 0; i < names.length; i++) {
130: names[i] = family[i].name();
131: }
132:
133: return names;
134: }
135:
136: /**
137: * @see org.eclipse.ui.views.properties.PropertyDescriptor#getValidator()
138: */
139: protected ICellEditorValidator getValidator() {
140: return validator;
141: }
142:
143: /**
144: * @see org.eclipse.ui.views.properties.PropertyDescriptor#getLabelProvider()
145: */
146: public ILabelProvider getLabelProvider() {
147: return new LabelProvider() {
148: /**
149: * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
150: */
151: public String getText(Object element) {
152: if (element == null)
153: return "null"; //$NON-NLS-1$
154: if (Boolean.class.isAssignableFrom(type.getType())
155: && element instanceof Integer) {
156: int intValue = ((Integer) element).intValue();
157: return intValue == 1 ? "true" : "false"; //$NON-NLS-1$ //$NON-NLS-2$
158: }
159: return element.toString();
160: }
161: };
162: }
163:
164: /**
165: * @return Returns the comboBoxList.
166: */
167: public String[] getComboBoxList() {
168:
169: String[] c = new String[comboBoxList.length];
170: System.arraycopy(comboBoxList, 0, c, 0, c.length);
171: return c;
172: }
173: }
|