Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / GeometryWKTCellEditor.java @ 40391

History | View | Annotate | Download (6.91 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.i18n.Messages;
44
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
46

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

    
61
    private static final long serialVersionUID = -2296004227902843851L;
62
    private Geometry nullGeometry = null;
63
    
64
    private int maxRowHeight;
65

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

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

    
93
    @Override
94
    public Object getCellEditorValue() {
95
        String wkt = (String) super.getCellEditorValue();
96
        try {
97
                return geomManager.createFrom(wkt);
98
        } catch (Exception ex) {
99
            throw new WKTToGeometryException(wkt, ex);
100
        }
101
    }
102

    
103
    @Override
104
    public Component getTableCellEditorComponent(JTable table, Object value,
105
            boolean isSelected, int row, int column) {
106
        delegate.setValue(value);
107
        if (((GeometryToWKTDelegate) delegate).isWtkTextTooLong()) {
108
            JOptionPane
109
                    .showMessageDialog(
110
                            table.getParent(),
111
                            Messages
112
                    .getText("Geometria_no_editable_WKT"), Messages
113
                    .getText("Error_editar_geometria"),
114
                            JOptionPane.ERROR_MESSAGE);
115

    
116
            return null;
117
        } else {
118
            int height_wanted = (int) getTextArea().getPreferredSize()
119
                    .getHeight();
120

    
121
            height_wanted = height_wanted > maxRowHeight ? maxRowHeight
122
                    : height_wanted;
123

    
124
            if (height_wanted > table.getRowHeight(row)) {
125
                table.setRowHeight(row, height_wanted);
126
            }
127

    
128
            return editorComponent;
129
        }
130
    }
131

    
132
    @SuppressWarnings("serial")
133
    private class GeometryToWKTDelegate extends
134
            DefaultCellEditor.EditorDelegate {
135
        private DefaultCellEditor.EditorDelegate delegate;
136

    
137
        private boolean wtkTextTooLong = false;
138

    
139
        private int maxWKTLength;
140

    
141
        public GeometryToWKTDelegate(DefaultCellEditor.EditorDelegate delegate,
142
                int maxWKTLength) {
143
            this.delegate = delegate;
144
            this.maxWKTLength = maxWKTLength;
145
        }
146

    
147
        /**
148
         * @return the wtkTextTooLong
149
         */
150
        public boolean isWtkTextTooLong() {
151
            return wtkTextTooLong;
152
        }
153

    
154
        public void setValue(Object value) {
155
            wtkTextTooLong = false;
156
            String strValue = "";
157

    
158
            if (value != null) {
159
                try {
160
                    Geometry geometry = (Geometry) value;
161
                    strValue = geometry.convertToWKT();
162

    
163
                    if (strValue.length() > maxWKTLength) {
164
                        wtkTextTooLong = true;
165
                        delegate.setValue(null);
166
                    } else {
167
                        delegate.setValue(strValue);
168
                    }
169
                } catch (Exception ex) {
170
                    throw new GeometryToWKTException(ex);
171
                }
172
            }
173
        }
174

    
175
        public Object getCellEditorValue() {
176
            if (wtkTextTooLong) {
177
                return null;
178
            } else {
179
                return delegate.getCellEditorValue();
180
            }
181
        }
182

    
183
        public void actionPerformed(ActionEvent e) {
184
            delegate.actionPerformed(e);
185
        }
186

    
187
        public void cancelCellEditing() {
188
            delegate.cancelCellEditing();
189
        }
190

    
191
        public boolean equals(Object obj) {
192
            return delegate.equals(obj);
193
        }
194

    
195
        public int hashCode() {
196
            return delegate.hashCode();
197
        }
198

    
199
        public boolean isCellEditable(EventObject anEvent) {
200
            return delegate.isCellEditable(anEvent);
201
        }
202

    
203
        public void itemStateChanged(ItemEvent e) {
204
            delegate.itemStateChanged(e);
205
        }
206

    
207
        public boolean shouldSelectCell(EventObject anEvent) {
208
            return delegate.shouldSelectCell(anEvent);
209
        }
210

    
211
        public boolean startCellEditing(EventObject anEvent) {
212
            return delegate.startCellEditing(anEvent);
213
        }
214

    
215
        public boolean stopCellEditing() {
216
            return delegate.stopCellEditing();
217
        }
218

    
219
        public String toString() {
220
            return delegate.toString();
221
        }
222
    }
223
}