Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / GeometryWKTCellEditor.java @ 38564

History | View | Annotate | Download (7.28 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 {DiSiD Technologies}  {{Task}}
26
 */
27
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table;
28

    
29
import java.awt.Component;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ItemEvent;
32
import java.util.EventObject;
33

    
34
import javax.swing.DefaultCellEditor;
35
import javax.swing.JOptionPane;
36
import javax.swing.JTable;
37

    
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.exception.CreateGeometryException;
43
import org.gvsig.fmap.geom.operation.fromwkt.FromWKT;
44
import org.gvsig.fmap.geom.operation.fromwkt.FromWKTGeometryOperationContext;
45
import org.gvsig.fmap.geom.operation.towkt.ToWKT;
46
import org.gvsig.i18n.Messages;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
/**
51
 * Editor for cells of type Geometry in WKT format.
52
 * <p>
53
 * If the WKT to represent a Geometry is too big, editing is not allowed, as the
54
 * rendering of that big text is too slow.
55
 * </p>
56
 * 
57
 * @author <a href="mailto:cordin@disid.com">C?sar Ordi?ana</a>
58
 */
59
public class GeometryWKTCellEditor extends TextAreaCellEditor {
60
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
61
        private static final Logger logger = LoggerFactory.getLogger(GeometryWKTCellEditor.class);
62
    public static final int DEFAULT_MAX_WKT_LENGTH = 10000;
63

    
64
    private static final long serialVersionUID = -2296004227902843851L;
65
    private Geometry nullGeometry = null;
66
    
67
    private int maxRowHeight;
68

    
69
    /**
70
     * Creates a new editor for Geometries in WKT format.
71
     */
72
    public GeometryWKTCellEditor() {
73
        this(DEFAULT_MAX_WKT_LENGTH, 160);
74
    }
75

    
76
    /**
77
     * Creates a new editor for Geometries in WKT format.
78
     * 
79
     * @param maxWKTLength
80
     *            the maximum WTK length allowed to be edited.
81
     * @param maxRowHeight
82
     *            the maximum row height for the rows with cells rendered with
83
     *            this component
84
     */
85
    public GeometryWKTCellEditor(int maxWKTLength, int maxRowHeight) {
86
        super();
87
        delegate = new GeometryToWKTDelegate(delegate, maxWKTLength);
88
        this.maxRowHeight = maxRowHeight;
89
        try {
90
                        nullGeometry = geomManager.createNullGeometry(SUBTYPES.GEOM2D);
91
                } catch (CreateGeometryException e) {
92
                        logger.error("Error creating a null geometry", e);
93
                }
94
    }
95

    
96
    @Override
97
    public Object getCellEditorValue() {
98
        String wkt = (String) super.getCellEditorValue();
99
        FromWKTGeometryOperationContext context = new FromWKTGeometryOperationContext(
100
                wkt, null);
101

    
102
        try {
103
            return nullGeometry.invokeOperation(FromWKT.CODE, context);
104
        } catch (Exception ex) {
105
            throw new WKTToGeometryException(wkt, ex);
106
        }
107
    }
108

    
109
    @Override
110
    public Component getTableCellEditorComponent(JTable table, Object value,
111
            boolean isSelected, int row, int column) {
112
        delegate.setValue(value);
113
        if (((GeometryToWKTDelegate) delegate).isWtkTextTooLong()) {
114
            JOptionPane
115
                    .showMessageDialog(
116
                            table.getParent(),
117
                            Messages
118
                    .getText("Geometria_no_editable_WKT"), Messages
119
                    .getText("Error_editar_geometria"),
120
                            JOptionPane.ERROR_MESSAGE);
121

    
122
            return null;
123
        } else {
124
            int height_wanted = (int) getTextArea().getPreferredSize()
125
                    .getHeight();
126

    
127
            height_wanted = height_wanted > maxRowHeight ? maxRowHeight
128
                    : height_wanted;
129

    
130
            if (height_wanted > table.getRowHeight(row)) {
131
                table.setRowHeight(row, height_wanted);
132
            }
133

    
134
            return editorComponent;
135
        }
136
    }
137

    
138
    @SuppressWarnings("serial")
139
    private class GeometryToWKTDelegate extends
140
            DefaultCellEditor.EditorDelegate {
141
        private DefaultCellEditor.EditorDelegate delegate;
142

    
143
        private boolean wtkTextTooLong = false;
144

    
145
        private int maxWKTLength;
146

    
147
        public GeometryToWKTDelegate(DefaultCellEditor.EditorDelegate delegate,
148
                int maxWKTLength) {
149
            this.delegate = delegate;
150
            this.maxWKTLength = maxWKTLength;
151
        }
152

    
153
        /**
154
         * @return the wtkTextTooLong
155
         */
156
        public boolean isWtkTextTooLong() {
157
            return wtkTextTooLong;
158
        }
159

    
160
        public void setValue(Object value) {
161
            wtkTextTooLong = false;
162
            String strValue = "";
163

    
164
            if (value != null) {
165
                try {
166
                    Geometry geometry = (Geometry) value;
167
                    strValue = (String) geometry.invokeOperation(ToWKT.CODE,
168
                            null);
169

    
170
                    if (strValue.length() > maxWKTLength) {
171
                        wtkTextTooLong = true;
172
                        delegate.setValue(null);
173
                    } else {
174
                        delegate.setValue(strValue);
175
                    }
176
                } catch (Exception ex) {
177
                    throw new GeometryToWKTException(ex);
178
                }
179
            }
180
        }
181

    
182
        public Object getCellEditorValue() {
183
            if (wtkTextTooLong) {
184
                return null;
185
            } else {
186
                return delegate.getCellEditorValue();
187
            }
188
        }
189

    
190
        public void actionPerformed(ActionEvent e) {
191
            delegate.actionPerformed(e);
192
        }
193

    
194
        public void cancelCellEditing() {
195
            delegate.cancelCellEditing();
196
        }
197

    
198
        public boolean equals(Object obj) {
199
            return delegate.equals(obj);
200
        }
201

    
202
        public int hashCode() {
203
            return delegate.hashCode();
204
        }
205

    
206
        public boolean isCellEditable(EventObject anEvent) {
207
            return delegate.isCellEditable(anEvent);
208
        }
209

    
210
        public void itemStateChanged(ItemEvent e) {
211
            delegate.itemStateChanged(e);
212
        }
213

    
214
        public boolean shouldSelectCell(EventObject anEvent) {
215
            return delegate.shouldSelectCell(anEvent);
216
        }
217

    
218
        public boolean startCellEditing(EventObject anEvent) {
219
            return delegate.startCellEditing(anEvent);
220
        }
221

    
222
        public boolean stopCellEditing() {
223
            return delegate.stopCellEditing();
224
        }
225

    
226
        public String toString() {
227
            return delegate.toString();
228
        }
229
    }
230
}