Statistics
| Revision:

root / tags / v2_0_0_Build_2050 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / swing / dynfield / GeometryDynFieldComponent.java @ 38672

History | View | Annotate | Download (4.95 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2012 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program 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
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.mapcontrol.swing.dynfield;
24

    
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27

    
28
import javax.swing.BoxLayout;
29
import javax.swing.JCheckBox;
30
import javax.swing.JComponent;
31
import javax.swing.JPanel;
32
import javax.swing.JTextField;
33

    
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
import org.gvsig.fmap.geom.Geometry;
38
import org.gvsig.i18n.Messages;
39
import org.gvsig.tools.dynobject.DynField;
40
import org.gvsig.tools.service.ServiceException;
41
import org.gvsig.tools.swing.api.dynobject.dynfield.ValueField;
42
import org.gvsig.tools.swing.spi.AbstractJDynFieldComponent;
43

    
44

    
45
/**
46
 * 
47
 * This class provides a swing component
48
 * to represent {@link Geometry} objects in dialogs/forms
49
 * 
50
 * 
51
 * @author jldominguez
52
 *
53
 */
54
public class GeometryDynFieldComponent extends
55
AbstractJDynFieldComponent implements ActionListener {
56
    
57
    private static Logger logger =
58
        LoggerFactory.getLogger(GeometryDynFieldComponent.class);
59
    
60
    private JCheckBox wktCheckBox = null; 
61
    private JTextField txtField = null; 
62
    private JPanel panel = null;
63

    
64
    public GeometryDynFieldComponent(
65
        DynField definition,
66
        ValueField value,
67
        boolean writeable) throws ServiceException {
68
        
69
        super(definition, value, false /* cannot edit geometry. writeable*/);
70
        init();
71
    }
72

    
73

    
74

    
75

    
76

    
77
    public void setEnabled(boolean isEnabled) {
78
    }
79

    
80
    public void requestFocus() {
81
    }
82

    
83
    public JComponent asJComponent() {
84
        return panel;
85
    }
86

    
87
    public Object getValue() {
88
        return this.getFieldValue();
89
    }
90

    
91
    protected void setJDynFieldComponentListeners() {
92
    }
93

    
94
    protected void afterUI() {
95
    }
96

    
97
    protected void initData() {
98
    }
99
    
100
    private String getValueText(boolean wkt) {
101
        Object val = this.getFieldValue();
102
        String txt = null;
103
        
104
        if (val == null || (!(val instanceof Geometry))) {
105
            txt = "[" + Messages.getText("_Invalid_value") + "]";
106
        } else {
107
            Geometry geom = (Geometry) val;
108
            if (wkt) {
109
                try {
110
                    txt = geom.convertToWKT();
111
                } catch (Exception e) {
112
                    logger.info("Error while getting WKT: " + e.getMessage(), e);
113
                    txt = "[" + Messages.getText("_Unable_to_show_value") + "]";
114
                }
115
            } else {
116
                txt = geom.getGeometryType().getName();
117
            }
118
        }
119
        return txt;
120
    }
121

    
122
    protected void initUI() {
123
        
124
        wktCheckBox =
125
            new JCheckBox(Messages.getText("_Show_as_text") + "  ");
126
        wktCheckBox.setSelected(false);
127
        wktCheckBox.addActionListener(this);
128
        txtField = new JTextField();
129
        // txtField.setEditable(false);
130
        txtField.setText(this.getValueText(wktCheckBox.isSelected()));
131
        txtField.setHorizontalAlignment(JTextField.LEFT);
132
        
133
        panel = new JPanel(); // new GridBagLayout());
134
        panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
135
        
136
        /*
137
        GridBagConstraints constr = new GridBagConstraints();
138
        constr.insets = new Insets(3, 3, 3, 3);
139
        constr.gridx = 0;
140
        constr.gridy = 0;
141
        constr.fill = GridBagConstraints.NONE;
142
        */
143
        
144
        panel.add(wktCheckBox); // , constr);
145
        /*
146
        constr.gridx = 1;
147
        constr.fill = GridBagConstraints.HORIZONTAL;
148
        */
149
        panel.add(txtField); // , constr);
150
    }
151

    
152
    protected void setNonNullValue(Object value) {
153
    }
154

    
155
    protected void setNullValue() {
156
    }
157

    
158
    protected void setReadOnly() {
159
    }
160

    
161

    
162
    public void actionPerformed(ActionEvent e) {
163
        // update txt field when check box state changes
164
        if (txtField != null && wktCheckBox != null) {
165
            txtField.setText(this.getValueText(wktCheckBox.isSelected()));
166
            txtField.setHorizontalAlignment(JTextField.LEFT);
167
        }
168
    }
169

    
170
}