gvsig-projects-pool / org.gvsig.lidar.app / org.gvsig.lidar.app.mainplugin / src / main / java / org / gvsig / exportto / swing / panel / SelectEnvelopePanel.java @ 281
History | View | Annotate | Download (6.26 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2016 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
|
24 |
package org.gvsig.exportto.swing.panel; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.awt.GridBagConstraints; |
28 |
import java.awt.GridBagLayout; |
29 |
import java.awt.Insets; |
30 |
|
31 |
import javax.swing.BorderFactory; |
32 |
import javax.swing.ButtonGroup; |
33 |
import javax.swing.JPanel; |
34 |
import javax.swing.JRadioButton; |
35 |
|
36 |
import org.cresques.cts.ICoordTrans; |
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.GeometryLocator; |
39 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
40 |
import org.gvsig.fmap.geom.primitive.Envelope; |
41 |
import org.gvsig.fmap.mapcontext.MapContext; |
42 |
import org.gvsig.gui.beans.coordinatespanel.CoordinatesPanel; |
43 |
import org.gvsig.tools.ToolsLocator; |
44 |
import org.gvsig.tools.i18n.I18nManager; |
45 |
import org.gvsig.tools.locator.LocatorException; |
46 |
|
47 |
/**
|
48 |
* A panel to select an envelope from different options:
|
49 |
* - original data source envelope
|
50 |
* - visible extent of the provided View (MapContext)
|
51 |
* - user defined Envelope
|
52 |
*/
|
53 |
public class SelectEnvelopePanel extends JPanel { |
54 |
I18nManager trans = ToolsLocator.getI18nManager(); |
55 |
|
56 |
private JRadioButton btnOriginalEnvelope = null; |
57 |
private JRadioButton btnViewEnvelope = null; |
58 |
private JRadioButton btnUserDefinedEnvelope = null; |
59 |
private ButtonGroup group = null; |
60 |
|
61 |
private CoordinatesPanel btnPnlDefinedEnvelope;
|
62 |
|
63 |
private MapContext mapContext = null; |
64 |
private ICoordTrans viewTolayerCT = null; |
65 |
|
66 |
private static final long serialVersionUID = 4179360502906000062L; |
67 |
|
68 |
/**
|
69 |
* When using this constructor, the View extent will not be
|
70 |
* available.
|
71 |
*/
|
72 |
public SelectEnvelopePanel() {
|
73 |
initComponents(); |
74 |
} |
75 |
|
76 |
/**
|
77 |
* Preferred constructor.
|
78 |
*
|
79 |
* @param mapcontext The MapContext to get the View envelope
|
80 |
* @param viewTolayerCT A coordinate transform to transform the
|
81 |
* view envelope from view projection coordinates to layer
|
82 |
* projection coordinates. Can be set to null if layer and view
|
83 |
* have the same projection.
|
84 |
*/
|
85 |
public SelectEnvelopePanel(MapContext mapContext, ICoordTrans viewTolayerCT) {
|
86 |
this.mapContext = mapContext;
|
87 |
this.viewTolayerCT = viewTolayerCT;
|
88 |
initComponents(); |
89 |
} |
90 |
|
91 |
private void initComponents() { |
92 |
this.setLayout(new GridBagLayout()); |
93 |
group = new ButtonGroup(); |
94 |
GridBagConstraints constraints = new GridBagConstraints(); |
95 |
constraints.gridx = 0;
|
96 |
constraints.gridy = 0;
|
97 |
constraints.anchor = GridBagConstraints.WEST;
|
98 |
constraints.fill = GridBagConstraints.HORIZONTAL;
|
99 |
constraints.insets = new Insets(5, 5, 5, 5); |
100 |
this.add(getBtnOriginalEnvelope(), constraints);
|
101 |
getBtnOriginalEnvelope().setSelected(true);
|
102 |
if (mapContext!=null) { |
103 |
constraints.gridy++; |
104 |
this.add(getBtnViewEnvelope(), constraints);
|
105 |
} |
106 |
constraints.gridy++; |
107 |
this.add(getBtnUserDefinedEnvelope(), constraints);
|
108 |
constraints.gridy++; |
109 |
constraints.ipadx = 30;
|
110 |
this.add(getPnlUserDefinedEnvelope(), constraints);
|
111 |
} |
112 |
|
113 |
private JRadioButton getBtnOriginalEnvelope() { |
114 |
if (btnOriginalEnvelope==null) { |
115 |
String text = trans.getTranslation("Do_not_filter_by_envelope"); |
116 |
btnOriginalEnvelope = new JRadioButton(text); |
117 |
group.add(btnOriginalEnvelope); |
118 |
} |
119 |
return btnOriginalEnvelope;
|
120 |
} |
121 |
|
122 |
private JRadioButton getBtnViewEnvelope() { |
123 |
if (btnViewEnvelope==null) { |
124 |
String text = trans.getTranslation("Use_View_envelope"); |
125 |
btnViewEnvelope = new JRadioButton(text); |
126 |
group.add(btnViewEnvelope); |
127 |
} |
128 |
return btnViewEnvelope;
|
129 |
} |
130 |
|
131 |
private JRadioButton getBtnUserDefinedEnvelope() { |
132 |
if (btnUserDefinedEnvelope==null) { |
133 |
String text = trans.getTranslation("Use_custom_envelope_(coordinates_using_data_source_projection)"); |
134 |
btnUserDefinedEnvelope = new JRadioButton(text); |
135 |
group.add(btnUserDefinedEnvelope); |
136 |
} |
137 |
return btnUserDefinedEnvelope;
|
138 |
} |
139 |
|
140 |
private CoordinatesPanel getPnlUserDefinedEnvelope() {
|
141 |
if (btnPnlDefinedEnvelope==null) { |
142 |
btnPnlDefinedEnvelope = new CoordinatesPanel();
|
143 |
} |
144 |
return btnPnlDefinedEnvelope;
|
145 |
} |
146 |
|
147 |
|
148 |
public Envelope getEnvelope() {
|
149 |
if (getBtnViewEnvelope().isSelected()) {
|
150 |
if (this.mapContext!=null) { |
151 |
Envelope envelope = this.mapContext.getViewPort().getEnvelope();
|
152 |
if (viewTolayerCT!=null) { |
153 |
envelope = envelope.convert(viewTolayerCT); |
154 |
} |
155 |
return envelope;
|
156 |
} |
157 |
} |
158 |
else if (getBtnUserDefinedEnvelope().isSelected()){ |
159 |
double minX = string2double(getPnlUserDefinedEnvelope().getValue11());
|
160 |
double minY = string2double(getPnlUserDefinedEnvelope().getValue22());
|
161 |
double maxX = string2double(getPnlUserDefinedEnvelope().getValue21());
|
162 |
double maxY = string2double(getPnlUserDefinedEnvelope().getValue12());
|
163 |
try {
|
164 |
return GeometryLocator.getGeometryManager().createEnvelope(minX, minY, maxX, maxY, Geometry.SUBTYPES.GEOM2D);
|
165 |
} catch (LocatorException e) {
|
166 |
// TODO Auto-generated catch block
|
167 |
e.printStackTrace(); |
168 |
} catch (CreateEnvelopeException e) {
|
169 |
// TODO Auto-generated catch block
|
170 |
e.printStackTrace(); |
171 |
} |
172 |
} |
173 |
return null; |
174 |
} |
175 |
|
176 |
/**
|
177 |
* Tries to parse a string as a double
|
178 |
* @return
|
179 |
*/
|
180 |
public double string2double(String strDouble) { |
181 |
try {
|
182 |
return Double.valueOf(strDouble).doubleValue(); |
183 |
} |
184 |
catch (Exception e) {} |
185 |
// assume comma was used as decimal separator
|
186 |
String replaced = strDouble.replaceAll(".", ""); |
187 |
replaced = replaced.replaceAll(",", "."); |
188 |
try {
|
189 |
return Double.valueOf(strDouble).doubleValue(); |
190 |
} |
191 |
catch (Exception e) {} |
192 |
|
193 |
// return 0.0 if no valid number could be decoded
|
194 |
return 0.0d; |
195 |
} |
196 |
|
197 |
|
198 |
|
199 |
} |