01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tool.select;
16:
17: import net.refractions.udig.project.ILayer;
18: import net.refractions.udig.project.IMap;
19: import net.refractions.udig.project.command.UndoableMapCommand;
20: import net.refractions.udig.project.command.factory.EditCommandFactory;
21:
22: import org.eclipse.jface.viewers.ICellModifier;
23: import org.eclipse.swt.widgets.Item;
24: import org.geotools.feature.AttributeType;
25: import org.geotools.feature.Feature;
26: import org.geotools.feature.FeatureType;
27:
28: /**
29: * Cell modifier for modifying features by sending commands to the map.
30: *
31: * @author Jesse
32: * @since 1.1.0
33: */
34: public class FeatureTypeCellModifier implements ICellModifier {
35:
36: private final IMap map;
37: private final FeatureType schema;
38: private ILayer layer;
39:
40: public FeatureTypeCellModifier(ILayer layer) {
41: this .map = layer.getMap();
42: this .schema = layer.getSchema();
43: this .layer = layer;
44: }
45:
46: public boolean canModify(Object element, String property) {
47: for (int i = 0; i < schema.getAttributeCount(); i++) {
48: AttributeType attributeType = schema.getAttributeType(i);
49: if (attributeType.getName().equals(property)) {
50: return true;
51: }
52: }
53: return false;
54: }
55:
56: public Object getValue(Object element, String property) {
57: Object attribute = ((Feature) element).getAttribute(property);
58: return attribute;
59: }
60:
61: public void modify(Object element, String property, Object value) {
62: Item item = (Item) element;
63: Feature feature = (Feature) item.getData();
64: Object oldValue = feature.getAttribute(property);
65: if (oldValue == null) {
66: if (value == null)
67: return;
68: else
69: makeModification(feature, layer, property, value, item);
70: } else {
71: if (!oldValue.equals(value))
72: makeModification(feature, layer, property, value, item);
73: }
74:
75: }
76:
77: /**
78: * called to actually make the modification to the feature.
79: *
80: * @param feature feature to modify
81: * @param layer layer feature is has been taken from
82: * @param property name of the attribute to modify
83: * @param value new value
84: * @param item TODO
85: */
86: protected void makeModification(Feature feature, ILayer layer,
87: String property, Object value, Item item) {
88: UndoableMapCommand c = EditCommandFactory.getInstance()
89: .createSetAttributeCommand(feature, layer, property,
90: value);
91: map.sendCommandASync(c);
92: }
93:
94: }
|