Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / GeometryWKTCellEditor.java @ 41610

History | View | Annotate | Download (6.63 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.mapcontrol.dal.feature.swing.table;
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.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.i18n.Messages;
43
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
45

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

    
60
    private static final long serialVersionUID = -2296004227902843851L;
61
    
62
    private int maxRowHeight;
63

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

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

    
86
    @Override
87
    public Object getCellEditorValue() {
88
        String wkt = (String) super.getCellEditorValue();
89
        try {
90
                return geomManager.createFrom(wkt);
91
        } catch (Exception ex) {
92
            throw new WKTToGeometryException(wkt, ex);
93
        }
94
    }
95

    
96
    @Override
97
    public Component getTableCellEditorComponent(JTable table, Object value,
98
            boolean isSelected, int row, int column) {
99
        delegate.setValue(value);
100
        if (((GeometryToWKTDelegate) delegate).isWtkTextTooLong()) {
101
            JOptionPane
102
                    .showMessageDialog(
103
                            table.getParent(),
104
                            Messages
105
                    .getText("Geometria_no_editable_WKT"), Messages
106
                    .getText("Error_editar_geometria"),
107
                            JOptionPane.ERROR_MESSAGE);
108

    
109
            return null;
110
        } else {
111
            int height_wanted = (int) getTextArea().getPreferredSize()
112
                    .getHeight();
113

    
114
            height_wanted = height_wanted > maxRowHeight ? maxRowHeight
115
                    : height_wanted;
116

    
117
            if (height_wanted > table.getRowHeight(row)) {
118
                table.setRowHeight(row, height_wanted);
119
            }
120

    
121
            return editorComponent;
122
        }
123
    }
124

    
125
    @SuppressWarnings("serial")
126
    private class GeometryToWKTDelegate extends
127
            DefaultCellEditor.EditorDelegate {
128
        private DefaultCellEditor.EditorDelegate delegate;
129

    
130
        private boolean wtkTextTooLong = false;
131

    
132
        private int maxWKTLength;
133

    
134
        public GeometryToWKTDelegate(DefaultCellEditor.EditorDelegate delegate,
135
                int maxWKTLength) {
136
            this.delegate = delegate;
137
            this.maxWKTLength = maxWKTLength;
138
        }
139

    
140
        /**
141
         * @return the wtkTextTooLong
142
         */
143
        public boolean isWtkTextTooLong() {
144
            return wtkTextTooLong;
145
        }
146

    
147
        public void setValue(Object value) {
148
            wtkTextTooLong = false;
149
            String strValue = "";
150

    
151
            if (value != null) {
152
                try {
153
                    Geometry geometry = (Geometry) value;
154
                    strValue = geometry.convertToWKT();
155

    
156
                    if (strValue.length() > maxWKTLength) {
157
                        wtkTextTooLong = true;
158
                        delegate.setValue(null);
159
                    } else {
160
                        delegate.setValue(strValue);
161
                    }
162
                } catch (Exception ex) {
163
                    throw new GeometryToWKTException(ex);
164
                }
165
            }
166
        }
167

    
168
        public Object getCellEditorValue() {
169
            if (wtkTextTooLong) {
170
                return null;
171
            } else {
172
                return delegate.getCellEditorValue();
173
            }
174
        }
175

    
176
        public void actionPerformed(ActionEvent e) {
177
            delegate.actionPerformed(e);
178
        }
179

    
180
        public void cancelCellEditing() {
181
            delegate.cancelCellEditing();
182
        }
183

    
184
        public boolean equals(Object obj) {
185
            return delegate.equals(obj);
186
        }
187

    
188
        public int hashCode() {
189
            return delegate.hashCode();
190
        }
191

    
192
        public boolean isCellEditable(EventObject anEvent) {
193
            return delegate.isCellEditable(anEvent);
194
        }
195

    
196
        public void itemStateChanged(ItemEvent e) {
197
            delegate.itemStateChanged(e);
198
        }
199

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

    
204
        public boolean startCellEditing(EventObject anEvent) {
205
            return delegate.startCellEditing(anEvent);
206
        }
207

    
208
        public boolean stopCellEditing() {
209
            return delegate.stopCellEditing();
210
        }
211

    
212
        public String toString() {
213
            return delegate.toString();
214
        }
215
    }
216
}