Statistics
| Revision:

svn-document-layout / trunk / org.gvsig.app.document.layout2.app / org.gvsig.app.document.layout2.app.mainplugin / src / main / java / org / gvsig / app / project / documents / layout / fframes / gui / EnvelopePanel.java @ 250

History | View | Annotate | Download (5 KB)

1
package org.gvsig.app.project.documents.layout.fframes.gui;
2

    
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.Insets;
6
import java.text.DecimalFormat;
7
import java.text.NumberFormat;
8
import java.text.ParseException;
9

    
10
import javax.swing.JLabel;
11
import javax.swing.JPanel;
12
import javax.swing.JTextField;
13
import javax.swing.text.NumberFormatter;
14

    
15
import org.gvsig.fmap.geom.Geometry;
16
import org.gvsig.fmap.geom.GeometryLocator;
17
import org.gvsig.fmap.geom.GeometryManager;
18
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20
import org.gvsig.i18n.Messages;
21
import org.slf4j.LoggerFactory;
22

    
23

    
24
public class EnvelopePanel extends JPanel {
25
        private static final long serialVersionUID = -1828624765045725544L;
26
        private JTextField fldTop = null;
27
        private JTextField fldLeft = null;
28
        private JTextField fldRight = null;
29
        private JTextField fldBottom = null;
30
        private static final GeometryManager geoMgr = GeometryLocator.getGeometryManager();
31
        private int fieldWidth;
32
        private DecimalFormat formatLatLon = new DecimalFormat("0.######");
33
        private DecimalFormat formatMetres = new DecimalFormat("0.##");
34
        
35
        public EnvelopePanel() {
36
                this(8);
37
        }
38
        
39
        public EnvelopePanel(int fieldWidth) {
40
                super(new GridBagLayout());
41
                this.fieldWidth = fieldWidth;
42
                initComponents();
43
        }
44
        
45
        protected void initComponents() {
46
                int row = 0;
47
                GridBagConstraints c = new GridBagConstraints();
48
                c.gridy = row++;
49
                c.gridx = 1;
50
                c.anchor = GridBagConstraints.LINE_END;
51
                c.insets = new Insets(0, 5, 5, 5);
52
                JLabel label = new JLabel(Messages.getText("Top_extent_lbl"));
53
                add(label, c);
54
                c.gridx = 2;
55
                c.anchor = GridBagConstraints.LINE_START;
56
                add(getFldTop(), c);
57
                
58
                c.gridy = row++;
59
                c.gridx = 0;
60
                c.insets = new Insets(5, 0, 5, 5);
61
                c.anchor = GridBagConstraints.LINE_END;
62
                label = new JLabel(Messages.getText("Left_extent_lbl"));
63
                add(label, c);
64
                c.gridx = 1;
65
                c.insets = new Insets(5, 5, 5, 5);
66
                c.anchor = GridBagConstraints.LINE_START;
67
                add(getFldLeft(), c);                
68

    
69
                c.gridx = 3;
70
                c.anchor = GridBagConstraints.LINE_END;
71
                label = new JLabel(Messages.getText("Right_extent_lbl"));
72
                add(label, c);
73
                c.gridx = 4;
74
                c.anchor = GridBagConstraints.LINE_START;
75
                c.insets = new Insets(5, 5, 5, 0);
76
                add(getFldRight(), c);
77
                
78
                c.gridy = row++;
79
                c.gridx = 1;
80
                c.anchor = GridBagConstraints.LINE_END;
81
                label = new JLabel(Messages.getText("Bottom_extent_lbl"));
82
                c.insets = new Insets(5, 5, 0, 5);
83
                add(label, c);
84
                c.gridx = 2;
85
                c.anchor = GridBagConstraints.LINE_START;
86
                add(getFldBottom(), c);
87
        }
88
        
89
        private JTextField getFldTop() {
90
                if (fldTop==null) {
91
                        fldTop  = new JTextField(fieldWidth);
92
                }
93
                return fldTop;
94
        }
95
        
96
        private JTextField getFldLeft() {
97
                if (fldLeft==null) {
98
                        fldLeft = new JTextField(fieldWidth);
99
                }
100
                return fldLeft;
101
        }
102
        
103
        private JTextField getFldRight() {
104
                if (fldRight==null) {
105
                        fldRight  = new JTextField(fieldWidth);
106
                }
107
                return fldRight;
108
        }
109
        
110
        private JTextField getFldBottom() {
111
                if (fldBottom==null) {
112
                        fldBottom  = new JTextField(fieldWidth);
113
                }
114
                return fldBottom;
115
        }
116
        
117
        public Envelope getEnvelope() {
118
                double minX = 0.0, minY=0.0, maxX=0.0, maxY=0.0;
119
                try {
120
                        maxY = formatMetres.parse(getFldTop().getText()).doubleValue();
121
                        maxX = formatMetres.parse(getFldRight().getText()).doubleValue();
122
                        minY = formatMetres.parse(getFldBottom().getText()).doubleValue();
123
                        minX = formatMetres.parse(getFldLeft().getText()).doubleValue();
124
                } catch (ParseException e1) {
125
                        try {
126
                                maxY = Double.parseDouble(getFldTop().getText().replaceAll(",", "."));
127
                                maxX = Double.parseDouble(getFldRight().getText().replaceAll(",", "."));
128
                                minY = Double.parseDouble(getFldBottom().getText().replaceAll(",", "."));
129
                                minX = Double.parseDouble(getFldLeft().getText().replaceAll(",", "."));
130
                        } catch (NumberFormatException e2) {
131
                                LoggerFactory.getLogger(EnvelopePanel.class).error("Error parsing envelope", e1);
132
                                return null;
133
                        }
134
                }
135
                try {
136
                        return geoMgr.createEnvelope(minX, minY, maxX, maxY, Geometry.SUBTYPES.GEOM2D);
137
                } catch (CreateEnvelopeException e) {
138
                        LoggerFactory.getLogger(EnvelopePanel.class).error("Error creating envelope", e);
139
                        return null;
140
                }
141
        }
142
        
143
        public void setEnvelope(Envelope envelope) {
144
                double top = envelope.getMaximum(1);
145
                double left = envelope.getMinimum(0);
146
                double right = envelope.getMaximum(0);
147
                double bottom = envelope.getMinimum(1);
148
                DecimalFormat format;
149
                if (top>Math.abs(90.0d) || left>Math.abs(180.0d) ||
150
                                right>Math.abs(180.0d) || bottom>Math.abs(90.0d)) {
151
                        format = formatMetres;
152
                }
153
                else {
154
                        format = formatLatLon;
155
                }
156
                String topStr = format.format(top);
157
                getFldTop().setText(topStr);
158
                String leftStr = format.format(left);
159
                getFldLeft().setText(leftStr);
160
                String rightStr = format.format(right);
161
                getFldRight().setText(rightStr);
162
                String bottomStr = format.format(bottom);
163
                getFldBottom().setText(bottomStr);
164
        }
165
        
166
        public void setEditable(boolean editable) {
167
                getFldTop().setEditable(editable);
168
                getFldLeft().setEditable(editable);
169
                getFldRight().setEditable(editable);
170
                getFldBottom().setEditable(editable);
171
        }
172
}