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 java.math.BigDecimal;
018: import java.math.BigInteger;
019:
020: import org.eclipse.jface.viewers.TextCellEditor;
021: import org.eclipse.swt.widgets.Composite;
022:
023: /**
024: * A Cell editor
025: * @author Jesse
026: * @since 1.1.0
027: */
028: public class BasicTypeCellEditor extends TextCellEditor {
029:
030: private Class<? extends Object> type;
031: private Object lastLegalValue;
032:
033: public BasicTypeCellEditor(Composite composite,
034: Class<? extends Object> type) {
035: super (composite);
036: if (!isLegalType(type))
037: throw new IllegalArgumentException(type
038: + " is not a supported type by this editor"); //$NON-NLS-1$
039: this .type = type;
040: }
041:
042: @Override
043: protected boolean isCorrect(Object value) {
044: if (value == null)
045: return super .isCorrect(value);
046: try {
047: return super .isCorrect(convertToType(value.toString()));
048: } catch (NumberFormatException e) {
049: return super .isCorrect(value);
050: }
051: }
052:
053: @Override
054: protected Object doGetValue() {
055: return safeConvertToType(super .doGetValue());
056: }
057:
058: private Object safeConvertToType(Object value2) {
059: String value = (String) value2;
060: value = value.trim();
061: try {
062: Object convertToType = convertToType(value);
063: lastLegalValue = convertToType;
064: return convertToType;
065: } catch (NumberFormatException e) {
066: if (lastLegalValue != null)
067: return lastLegalValue;
068: else
069: return 0;
070: }
071: }
072:
073: @Override
074: protected void doSetValue(Object value) {
075: if (value != null && !isLegalType(value.getClass())) {
076: throw new IllegalArgumentException(type
077: + " is not a supported type by this editor"); //$NON-NLS-1$
078: }
079: String stringValue;
080: if (value == null)
081: stringValue = ""; //$NON-NLS-1$
082: else {
083: stringValue = value.toString();
084: lastLegalValue = value;
085: }
086: super .doSetValue(stringValue);
087: }
088:
089: private boolean isLegalType(Class<? extends Object> type) {
090: if (type == Short.class) {
091: return true;
092: } else if (type == String.class) {
093: return true;
094: } else if (type == Integer.class) {
095: return true;
096: } else if (type == Byte.class) {
097: return true;
098: } else if (type == Character.class) {
099: return true;
100: } else if (type == Long.class) {
101: return true;
102: } else if (type == Double.class) {
103: return true;
104: } else if (type == Float.class) {
105: return true;
106: } else if (type == BigDecimal.class) {
107: return true;
108: } else if (type == BigInteger.class) {
109: return true;
110: }
111: return false;
112: }
113:
114: private Object convertToType(String value) {
115: if (type == Short.class) {
116: return Short.valueOf(value);
117: } else if (type == String.class) {
118: return value;
119: } else if (type == Integer.class) {
120: return Integer.valueOf(value);
121: } else if (type == Byte.class) {
122: return Byte.valueOf(value);
123: } else if (type == Character.class) {
124: return value.charAt(0);
125: } else if (type == Long.class) {
126: return Long.valueOf(value);
127: } else if (type == Double.class) {
128: return Double.valueOf(value);
129: } else if (type == Float.class) {
130: return Float.valueOf(value);
131: } else if (type == BigDecimal.class) {
132: return new BigDecimal(value);
133: } else if (type == BigInteger.class) {
134: return new BigInteger(value);
135: }
136: return null;
137: }
138:
139: }
|