Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / fmap / dal / swing / impl / featuretable / table / editors / GeometryWKTCellEditor.java @ 42806

History | View | Annotate | Download (7.02 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * AUTHORS (In addition to CIT):
26
 * 2008 {DiSiD Technologies}  {{Task}}
27
 */
28
package org.gvsig.fmap.dal.swing.impl.featuretable.table.editors;
29

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

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

    
39
import org.apache.commons.lang3.StringUtils;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

    
43
import org.gvsig.fmap.dal.swing.impl.featuretable.table.renders.GeometryToWKTException;
44
import org.gvsig.fmap.geom.Geometry;
45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryManager;
47
import org.gvsig.i18n.Messages;
48

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

    
63
    private static final long serialVersionUID = -2296004227902843851L;
64

    
65
    private int maxRowHeight;
66

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

    
74
    /**
75
     * Creates a new editor for Geometries in WKT format.
76
     *
77
     * @param maxWKTLength
78
     *            the maximum WTK length allowed to be edited.
79
     * @param maxRowHeight
80
     *            the maximum row height for the rows with cells rendered with
81
     *            this component
82
     */
83
    public GeometryWKTCellEditor(int maxWKTLength, int maxRowHeight) {
84
        super();
85
        delegate = new GeometryToWKTDelegate(delegate, maxWKTLength);
86
        this.maxRowHeight = maxRowHeight;
87
    }
88

    
89
    @Override
90
    public Object getCellEditorValue() {
91
        String wkt = (String) super.getCellEditorValue();
92
        try {
93
            if( wkt==null || StringUtils.isEmpty(wkt.trim()) ) {
94
                return null;
95
            }
96
            return geomManager.createFrom(wkt);
97
        } catch (Exception ex) {
98
            throw new WKTToGeometryException(wkt, ex);
99
        }
100
    }
101

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

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

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

    
123
            if (height_wanted > table.getRowHeight(row)) {
124
                int previousRowHeight = table.getRowHeight();
125
                table.setRowHeight(row, height_wanted);
126
                table.firePropertyChange("rowheight", previousRowHeight, height_wanted);
127
            }
128

    
129
            return editorComponent;
130
        }
131
    }
132

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

    
138
        private boolean wtkTextTooLong = false;
139

    
140
        private int maxWKTLength;
141

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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