svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TransformationEpsgPanel.java @ 8262
History | View | Annotate | Download (14.3 KB)
1 |
package org.gvsig.crs.gui.panels; |
---|---|
2 |
|
3 |
|
4 |
import java.awt.BorderLayout; |
5 |
import java.awt.Component; |
6 |
import java.awt.Dimension; |
7 |
import java.awt.FlowLayout; |
8 |
import java.awt.GridLayout; |
9 |
import java.sql.ResultSet; |
10 |
import java.sql.SQLException; |
11 |
import java.util.ArrayList; |
12 |
|
13 |
import javax.swing.BorderFactory; |
14 |
import javax.swing.JLabel; |
15 |
import javax.swing.JPanel; |
16 |
import javax.swing.JScrollPane; |
17 |
import javax.swing.JTable; |
18 |
import javax.swing.JTextArea; |
19 |
import javax.swing.ListSelectionModel; |
20 |
import javax.swing.event.ListSelectionEvent; |
21 |
import javax.swing.event.ListSelectionListener; |
22 |
import javax.swing.table.DefaultTableModel; |
23 |
import javax.swing.table.TableColumn; |
24 |
|
25 |
import org.cresques.cts.IProjection; |
26 |
import org.gvsig.crs.CrsFactory; |
27 |
import org.gvsig.crs.EpsgConnection; |
28 |
import org.gvsig.crs.ICrs; |
29 |
import org.gvsig.crs.Query; |
30 |
|
31 |
import com.iver.andami.PluginServices; |
32 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
33 |
import com.iver.cit.gvsig.gui.TableSorter; |
34 |
|
35 |
public class TransformationEpsgPanel extends JPanel implements ListSelectionListener { |
36 |
|
37 |
|
38 |
/**
|
39 |
*
|
40 |
*/
|
41 |
private static final long serialVersionUID = 1L; |
42 |
|
43 |
private JPanel panel; |
44 |
private IProjection firstProj;
|
45 |
|
46 |
String[] transformations = {"9603", "9606", "9607", "9613", "9615", "9633"}; |
47 |
|
48 |
private int transformation_code = -1; |
49 |
private String[] values; |
50 |
private String params = "+towgs84="; |
51 |
|
52 |
private JTable transformationTable; |
53 |
private JScrollPane jScrollPane = null; |
54 |
public boolean inverseTranformation = false; |
55 |
|
56 |
public EpsgConnection connect = null;; |
57 |
int crs_target = -1; |
58 |
|
59 |
public DefaultTableModel dtm = null; |
60 |
|
61 |
private int crs_source_code; |
62 |
private String cadWKT = ""; |
63 |
private ListSelectionModel lsm2 = null; |
64 |
public int selectedRowTable = -1; |
65 |
boolean tra = false; |
66 |
|
67 |
private JLabel wkt; |
68 |
private JTextArea info; |
69 |
private JScrollPane areaScrollPane; |
70 |
public TableSorter sorter = null; |
71 |
|
72 |
public TransformationEpsgPanel(int target) { |
73 |
crs_target = target; |
74 |
} |
75 |
|
76 |
public JPanel getJPanel(){ |
77 |
if (panel == null) { |
78 |
panel = new JPanel(); |
79 |
panel.setLayout(new GridLayout(1,2)); |
80 |
panel.setLayout(new FlowLayout(FlowLayout.LEADING,5,10)); |
81 |
panel.setPreferredSize(new Dimension(525, 100)); |
82 |
panel.setBorder( |
83 |
BorderFactory.createCompoundBorder(
|
84 |
BorderFactory.createCompoundBorder(
|
85 |
BorderFactory.createTitledBorder("Transformacion EPSG"), |
86 |
BorderFactory.createEmptyBorder(2,2,2,2)), |
87 |
panel.getBorder())); |
88 |
panel.add(getLabel()); |
89 |
panel.add(getScrollPanelArea(), BorderLayout.NORTH);
|
90 |
panel.add(getJScrollPane()); |
91 |
} |
92 |
return panel;
|
93 |
} |
94 |
|
95 |
private Component getLabel() { |
96 |
if (wkt == null){ |
97 |
wkt = new JLabel(); |
98 |
wkt.setPreferredSize(new Dimension(90, 80)); |
99 |
wkt.setText(PluginServices.getText(this,"Cadena WKT: ")); |
100 |
} |
101 |
return wkt;
|
102 |
} |
103 |
|
104 |
private Component getInfo() { |
105 |
if (info == null){ |
106 |
info = new JTextArea(); |
107 |
info.setLineWrap(true);
|
108 |
info.setWrapStyleWord(true);
|
109 |
info.setPreferredSize(new Dimension(400, 240)); |
110 |
info.setEditable(false);
|
111 |
info.append(getWKT()); |
112 |
} |
113 |
info.setText(getWKT()); |
114 |
return info;
|
115 |
} |
116 |
|
117 |
private JScrollPane getScrollPanelArea() { |
118 |
if(areaScrollPane == null) { |
119 |
areaScrollPane = new JScrollPane(getInfo()); |
120 |
areaScrollPane.setVerticalScrollBarPolicy( |
121 |
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
|
122 |
areaScrollPane.setPreferredSize(new Dimension(420, 90)); |
123 |
areaScrollPane.setBorder( |
124 |
BorderFactory.createCompoundBorder(
|
125 |
BorderFactory.createCompoundBorder(
|
126 |
BorderFactory.createTitledBorder("Wkt"), |
127 |
BorderFactory.createEmptyBorder(2,2,2,2)), |
128 |
areaScrollPane.getBorder())); |
129 |
|
130 |
} |
131 |
return areaScrollPane;
|
132 |
} |
133 |
|
134 |
private JTable getJTable() { |
135 |
if (transformationTable == null) { |
136 |
String[] columnNames= {"Code of Transformation","Name of Transformation","Type of Transformation","Source CRS","Target CRS", "Description of Area"}; |
137 |
Object[][]data = {}; |
138 |
dtm = new DefaultTableModel(data, columnNames) |
139 |
{ |
140 |
public boolean isCellEditable(int row, int column) { |
141 |
return false; |
142 |
} |
143 |
/*
|
144 |
* metodo necesario para cuando utilizamos tablas ordenadas
|
145 |
* ya que sino al ordenar por algun campo no se queda con el orden
|
146 |
* actual al seleccionar una fila (non-Javadoc)
|
147 |
* @see javax.swing.table.TableModel#getColumnClass(int)
|
148 |
*/
|
149 |
public Class getColumnClass(int column) |
150 |
{ |
151 |
return getValueAt(0, column).getClass(); |
152 |
} |
153 |
}; |
154 |
sorter = new TableSorter(dtm);
|
155 |
|
156 |
transformationTable = new JTable(sorter); |
157 |
sorter.setTableHeader(transformationTable.getTableHeader()); |
158 |
transformationTable.setCellSelectionEnabled(false);
|
159 |
transformationTable.setRowSelectionAllowed(true);
|
160 |
transformationTable.setColumnSelectionAllowed(false);
|
161 |
transformationTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
162 |
TableColumn column = null; |
163 |
for (int i = 0; i < columnNames.length; i++) { |
164 |
column = transformationTable.getColumnModel().getColumn(i); |
165 |
if (i == 0 || i == 3 || i == 4) { |
166 |
column.setPreferredWidth(40);
|
167 |
}else if (i == 2) { |
168 |
column.setPreferredWidth(80);
|
169 |
} |
170 |
else {
|
171 |
column.setPreferredWidth(160);
|
172 |
} |
173 |
} |
174 |
ListSelectionModel rowSM2 = transformationTable.getSelectionModel();
|
175 |
rowSM2.addListSelectionListener(this);
|
176 |
} |
177 |
return transformationTable;
|
178 |
} |
179 |
|
180 |
private JScrollPane getJScrollPane() { |
181 |
if (jScrollPane == null) { |
182 |
jScrollPane = new JScrollPane(); |
183 |
jScrollPane.setPreferredSize(new Dimension(525,200)); |
184 |
jScrollPane.setBorder( |
185 |
BorderFactory.createCompoundBorder(
|
186 |
BorderFactory.createCompoundBorder(
|
187 |
BorderFactory.createTitledBorder("Transformations"), |
188 |
BorderFactory.createEmptyBorder(5,5,5,5)), |
189 |
jScrollPane.getBorder())); |
190 |
jScrollPane.setViewportView(getJTable()); |
191 |
} |
192 |
return jScrollPane;
|
193 |
} |
194 |
|
195 |
private void callTransformation(int crsCode){ |
196 |
int numRow = dtm.getRowCount();
|
197 |
|
198 |
while (numRow != 0) { |
199 |
numRow = numRow - 1;
|
200 |
dtm.removeRow(numRow); |
201 |
} |
202 |
|
203 |
String sentence = "SELECT source_geogcrs_code " + |
204 |
"FROM epsg_coordinatereferencesystem " +
|
205 |
"WHERE coord_ref_sys_code = "+ crsCode ;
|
206 |
ResultSet result = Query.select(sentence,connect.getConnection()); |
207 |
int source = 0; |
208 |
try {
|
209 |
result.next(); |
210 |
source = result.getInt("source_geogcrs_code");
|
211 |
} catch (SQLException e1) { |
212 |
e1.printStackTrace(); |
213 |
} |
214 |
|
215 |
ResultSet result2 = null; |
216 |
ResultSet result3 = null; |
217 |
if (source != 0){ |
218 |
crsCode = source; |
219 |
} |
220 |
|
221 |
ArrayList codecs = new ArrayList(); |
222 |
codecs.add(String.valueOf(crsCode));
|
223 |
|
224 |
for (int j=0; j< codecs.size(); j++){ |
225 |
sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
|
226 |
"FROM epsg_coordoperation " +
|
227 |
"WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target; |
228 |
|
229 |
result = Query.select(sentence,connect.getConnection());
|
230 |
|
231 |
try {
|
232 |
while(result.next()) {
|
233 |
Object[]data = new Object[6]; |
234 |
data[0] = String.valueOf(result.getInt("coord_op_code")); |
235 |
data[1] = result.getString("coord_op_name"); |
236 |
data[2] = result.getString("coord_op_type"); |
237 |
data[3] = String.valueOf(result.getInt("source_crs_code")); |
238 |
data[4] = String.valueOf(result.getInt("target_crs_code")); |
239 |
/*codecs.add(data[4]);
|
240 |
codecs = deleteItems(codecs);*/
|
241 |
|
242 |
int aouc = Integer.parseInt(result.getString("area_of_use_code")); |
243 |
|
244 |
sentence = "SELECT area_of_use FROM epsg_area " +
|
245 |
"WHERE area_code = "+ aouc ;
|
246 |
|
247 |
result2 = Query.select(sentence,connect.getConnection());
|
248 |
while (result2.next())
|
249 |
data[5] = result2.getString("area_of_use"); |
250 |
|
251 |
String coord_op_method = result.getString("coord_op_method_code"); |
252 |
|
253 |
sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
|
254 |
"WHERE coord_op_method_code LIKE " + coord_op_method;
|
255 |
result3 = Query.select(sentence,connect.getConnection());
|
256 |
|
257 |
while(result3.next()){
|
258 |
if (Integer.parseInt(result3.getString("reverse_op")) == 1){ |
259 |
for (int i=0; i< transformations.length; i++){ |
260 |
if (coord_op_method.equals(transformations[i])){
|
261 |
dtm.addRow(data); |
262 |
} |
263 |
} |
264 |
} |
265 |
} |
266 |
} |
267 |
} |
268 |
catch (SQLException e1) { |
269 |
e1.printStackTrace(); |
270 |
} |
271 |
} |
272 |
} |
273 |
|
274 |
private ArrayList deleteItems(ArrayList codecs) { |
275 |
ArrayList cod = new ArrayList(); |
276 |
boolean equal = false; |
277 |
for (int i = 0; i< codecs.size(); i++){ |
278 |
String c = (String) codecs.get(i); |
279 |
for (int j = 0; j<cod.size(); j++){ |
280 |
if (cod.get(j).equals(c)){
|
281 |
equal = true;
|
282 |
} |
283 |
} |
284 |
if (!equal){
|
285 |
cod.add(c); |
286 |
} |
287 |
equal = false;
|
288 |
} |
289 |
return cod;
|
290 |
} |
291 |
|
292 |
private void callInverseTransformation(int crsCode){ |
293 |
//inverseTranformation = true;
|
294 |
|
295 |
String sentence = "SELECT source_geogcrs_code " + |
296 |
"FROM epsg_coordinatereferencesystem " +
|
297 |
"WHERE coord_ref_sys_code = "+ crsCode ;
|
298 |
ResultSet result = Query.select(sentence,connect.getConnection()); |
299 |
int source = 0; |
300 |
try {
|
301 |
result.next(); |
302 |
source = result.getInt("source_geogcrs_code");
|
303 |
} catch (SQLException e1) { |
304 |
e1.printStackTrace(); |
305 |
} |
306 |
|
307 |
ResultSet result2 = null; |
308 |
ResultSet result3 = null; |
309 |
if (source != 0){ |
310 |
crsCode = source; |
311 |
} |
312 |
|
313 |
ArrayList codecs = new ArrayList(); |
314 |
codecs.add(String.valueOf(crsCode));
|
315 |
|
316 |
codecs = deleteItems(codecs); |
317 |
|
318 |
for (int j=0; j< codecs.size(); j++){ |
319 |
sentence = "SELECT coord_op_code, coord_op_name, coord_op_type, source_crs_code, target_crs_code, area_of_use_code, coord_op_method_code " +
|
320 |
"FROM epsg_coordoperation " +
|
321 |
"WHERE source_crs_code = " + codecs.get(j) + "AND target_crs_code = " + crs_target; |
322 |
|
323 |
result = Query.select(sentence,connect.getConnection());
|
324 |
|
325 |
try {
|
326 |
while(result.next()) {
|
327 |
Object[]data = new Object[6]; |
328 |
data[0] = String.valueOf(result.getInt("coord_op_code")); |
329 |
data[1] = result.getString("coord_op_name"); |
330 |
data[2] = result.getString("coord_op_type"); |
331 |
data[4] = String.valueOf(result.getInt("source_crs_code")); |
332 |
data[3] = String.valueOf(result.getInt("target_crs_code")); |
333 |
/*codecs.add(data[4]);
|
334 |
codecs = deleteItems(codecs);*/
|
335 |
|
336 |
int aouc = Integer.parseInt(result.getString("area_of_use_code")); |
337 |
|
338 |
sentence = "SELECT area_of_use FROM epsg_area " +
|
339 |
"WHERE area_code = "+ aouc ;
|
340 |
|
341 |
result2 = Query.select(sentence,connect.getConnection());
|
342 |
while (result2.next())
|
343 |
data[5] = result2.getString("area_of_use"); |
344 |
|
345 |
String coord_op_method = result.getString("coord_op_method_code"); |
346 |
|
347 |
sentence = "SELECT reverse_op FROM epsg_coordoperationmethod "+
|
348 |
"WHERE coord_op_method_code LIKE " + coord_op_method;
|
349 |
result3 = Query.select(sentence,connect.getConnection());
|
350 |
|
351 |
while(result3.next()){
|
352 |
if (Integer.parseInt(result3.getString("reverse_op")) == 1){ |
353 |
for (int i=0; i< transformations.length; i++){ |
354 |
if (coord_op_method.equals(transformations[i])){
|
355 |
dtm.addRow(data); |
356 |
} |
357 |
} |
358 |
} |
359 |
} |
360 |
} |
361 |
} |
362 |
catch (SQLException e1) { |
363 |
e1.printStackTrace(); |
364 |
} |
365 |
} |
366 |
} |
367 |
|
368 |
public void setValues(String[] val) { |
369 |
values = val; |
370 |
} |
371 |
|
372 |
public String[] getValues() { |
373 |
return values;
|
374 |
} |
375 |
|
376 |
private void setTrasformation_code(int t_cod) { |
377 |
transformation_code = t_cod; |
378 |
} |
379 |
|
380 |
public int getTransformation_code() { |
381 |
return transformation_code;
|
382 |
} |
383 |
|
384 |
public ICrs getProjection() {
|
385 |
params += values[0];
|
386 |
for(int i = 1; i < values.length; i++) |
387 |
params +=","+values[i];
|
388 |
try {
|
389 |
ICrs crs = new CrsFactory().getCRS(crs_source_code,
|
390 |
cadWKT,params); |
391 |
return crs;
|
392 |
} catch (org.gvsig.crs.CrsException e) {
|
393 |
e.printStackTrace(); |
394 |
} |
395 |
return null; |
396 |
} |
397 |
|
398 |
public void setProjection(IProjection proj) { |
399 |
firstProj = proj; |
400 |
} |
401 |
|
402 |
public void setWKT(String cad){ |
403 |
cadWKT = cad; |
404 |
getInfo(); |
405 |
} |
406 |
|
407 |
public String getWKT(){ |
408 |
return cadWKT;
|
409 |
} |
410 |
|
411 |
public void setSource(int code){ |
412 |
connect = new EpsgConnection();
|
413 |
connect.setConnectionEPSG(); |
414 |
inverseTranformation = false;
|
415 |
crs_source_code = code; |
416 |
callTransformation(crs_source_code); |
417 |
|
418 |
int new_target = crs_target;
|
419 |
crs_target = code; |
420 |
crs_source_code = new_target; |
421 |
callInverseTransformation(crs_source_code); |
422 |
int real_source = crs_target;
|
423 |
crs_target = new_target; |
424 |
crs_source_code = real_source; |
425 |
|
426 |
int numr = dtm.getRowCount();
|
427 |
if (numr > 0 ){ |
428 |
this.getJTable().setRowSelectionInterval(0,0); |
429 |
} |
430 |
} |
431 |
|
432 |
public int getSource(){ |
433 |
return crs_source_code;
|
434 |
} |
435 |
|
436 |
public WindowInfo getWindowInfo() {
|
437 |
WindowInfo m_viewinfo=new WindowInfo(WindowInfo.MODALDIALOG);
|
438 |
m_viewinfo.setTitle("Transformation EPSG");
|
439 |
return m_viewinfo;
|
440 |
} |
441 |
|
442 |
public void valueChanged(ListSelectionEvent e) { |
443 |
// TODO Auto-generated method stub
|
444 |
if (e.getSource() == this.getJTable()){ |
445 |
lsm2 = (ListSelectionModel)e.getSource();
|
446 |
if (lsm2.isSelectionEmpty()) {
|
447 |
selectedRowTable = -1;
|
448 |
tra = false;
|
449 |
} |
450 |
else {
|
451 |
selectedRowTable = lsm2.getMinSelectionIndex(); |
452 |
setTrasformation_code(Integer.parseInt((String)sorter.getValueAt(selectedRowTable,0))); |
453 |
String sentence = "SELECT source_crs_code " + |
454 |
"FROM epsg_coordoperation " +
|
455 |
"WHERE coord_op_code = " + getTransformation_code() ;
|
456 |
ResultSet result = Query.select(sentence,connect.getConnection()); |
457 |
try {
|
458 |
result.next(); |
459 |
int source = result.getInt("source_crs_code"); |
460 |
if (source == getSource()) {
|
461 |
inverseTranformation = false;
|
462 |
} |
463 |
else inverseTranformation = true; |
464 |
} catch (SQLException e1) { |
465 |
e1.printStackTrace(); |
466 |
} |
467 |
} |
468 |
} |
469 |
} |
470 |
|
471 |
} |