Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libJCRS / src / org / gvsig / crs / gui / panels / TransformationManualPanel.java @ 9241

History | View | Annotate | Download (17.7 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package org.gvsig.crs.gui.panels;
42

    
43
import java.awt.Color;
44
import java.awt.Component;
45
import java.awt.Dimension;
46
import java.awt.FlowLayout;
47
import java.awt.GridLayout;
48

    
49
import javax.swing.BorderFactory;
50
import javax.swing.JLabel;
51
import javax.swing.JPanel;
52
import javax.swing.JTextArea;
53
import javax.swing.JTextField;
54

    
55
import org.gvsig.crs.CrsFactory;
56
import org.gvsig.crs.CrsWkt;
57
import org.gvsig.crs.ICrs;
58

    
59
import com.iver.andami.PluginServices;
60

    
61
/**
62
 * Clase que define el panel de tranformaciones manuales, as? como su
63
 * funcionamiento.
64
 * 
65
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
66
 *
67
 */
68
public class TransformationManualPanel extends JPanel {
69
        
70
        private static final long serialVersionUID = 1L;
71
        
72
        private static boolean pressed = true;
73
        
74
        private JLabel x_Translation;
75
        private JLabel y_Translation;
76
        private JLabel z_Translation;
77
        private JLabel x_Rotation;
78
        private JLabel y_Rotation;
79
        private JLabel z_Rotation;
80
        private JLabel scale;
81
        
82
        private JTextField tx_Translation;
83
        private JTextField ty_Translation;
84
        private JTextField tz_Translation;
85
        private JTextField tx_Rotation;
86
        private JTextField ty_Rotation;
87
        private JTextField tz_Rotation;
88
        private JTextField tScale;
89
        
90
        private JLabel domainTranslation;
91
        private JLabel domainRotation;
92
        private JLabel domainScale;
93
        
94
        private JLabel wkt;
95
        private JTextArea info;        
96
        int codeEpsg;
97
        String targetAbrev = "";
98
        String sourceAbrev = "";
99
        String[] targetAuthority;
100
        
101
        private String cadWKT = "";        
102
        
103
        public TransformationManualPanel() {                
104
                initialize();        
105
        }
106
        
107
        private void initialize(){
108
                        setLayout(new GridLayout(0,3));
109
                        setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
110
                        setBorder(
111
                                    BorderFactory.createCompoundBorder(
112
                                                        BorderFactory.createCompoundBorder(
113
                                                                        BorderFactory.createTitledBorder(PluginServices.getText(this,"transformacion_manual")),
114
                                                                        BorderFactory.createEmptyBorder(2,2,2,2)),
115
                                                                        getBorder()));
116
                        setPreferredSize(new Dimension(525,400));
117
                        add(getX_Translation(),null);
118
                        add(getTx_Translation(),null);
119
                        add(getDomainTranslation(), null);                        
120
                        add(getY_Translation(),null);
121
                        add(getTy_Translation(),null);
122
                        add(getDomainTranslation(), null);
123
                        add(getZ_Translation(),null);
124
                        add(getTz_Translation(),null);
125
                        add(getDomainTranslation(), null);
126
                        add(getX_Rotation(),null);
127
                        add(getTx_Rotation(),null);
128
                        add(getDomainRotation(), null);
129
                        add(getY_Rotation(),null);
130
                        add(getTy_Rotation(),null);
131
                        add(getDomainRotation(), null);                        
132
                        add(getZ_Rotation(),null);
133
                        add(getTz_Rotation(),null);
134
                        add(getDomainRotation(), null);
135
                        add(getScale(),null);
136
                        add(getTscale(),null);
137
                        add(getDomainScale(), null);
138
                        //panel.add(getJPanelParams(), null);
139
                        //panel.add(getButtonPane(), null);
140
        }
141
        
142
        private JLabel getDomainTranslation(){
143
                domainTranslation = new JLabel();
144
                domainTranslation.setPreferredSize(new Dimension(150, 20));
145
                domainTranslation.setText("[-1000.0, 1000.0]");
146
                return domainTranslation;
147
        }
148
        
149
        private JLabel getDomainRotation(){
150
                domainRotation = new JLabel();
151
                domainRotation.setPreferredSize(new Dimension(150, 20));
152
                domainRotation.setText("[-60.0, 60.0]");
153
                return domainRotation;
154
        }
155
        
156
        private JLabel getDomainScale(){
157
                domainScale = new JLabel();
158
                domainScale.setPreferredSize(new Dimension(150, 20));
159
                domainScale.setText("[-20.0, 20.0]");
160
                return domainScale;
161
        }
162

    
163
        private JLabel getX_Translation() {
164
                if(x_Translation == null ) {
165
                        x_Translation = new JLabel();
166
                        x_Translation.setPreferredSize(new Dimension(120,20));
167
                        //x_Translation.setFont(new Font("x_Translation:",Font.BOLD,15));
168
                        x_Translation.setText(PluginServices.getText(this,"x_Translation")+":");
169
                        
170
                }
171
                return x_Translation;
172
        }
173
        
174
        private JLabel getY_Translation() {
175
                if(y_Translation == null ) {
176
                        y_Translation = new JLabel();
177
                        y_Translation.setPreferredSize(new Dimension(120,20));
178
                        //y_Translation.setFont(new Font("y_Translation:",Font.BOLD,15));
179
                        y_Translation.setText(PluginServices.getText(this,"y_Translation")+":");
180
                        
181
                }
182
                return y_Translation;
183
        }
184
        
185
        private JLabel getZ_Translation() {
186
                if(z_Translation == null ) {
187
                        z_Translation = new JLabel();
188
                        z_Translation.setPreferredSize(new Dimension(120,20));
189
                        //z_Translation.setFont(new Font("z_Translation:",Font.BOLD,15));
190
                        z_Translation.setText(PluginServices.getText(this,"z_Translation")+":");
191
                        
192
                }
193
                return z_Translation;
194
        }
195
        
196
        private JLabel getX_Rotation() {
197
                if(x_Rotation == null ) {
198
                        x_Rotation = new JLabel();
199
                        x_Rotation.setPreferredSize(new Dimension(120,20));
200
                        //x_Rotation.setFont(new Font("x_Rotation:",Font.BOLD,15));
201
                        x_Rotation.setText(PluginServices.getText(this,"x_Rotation")+":");
202
                        
203
                }
204
                return x_Rotation;
205
        }
206
        
207
        private JLabel getY_Rotation() {
208
                if(y_Rotation == null ) {
209
                        y_Rotation = new JLabel();
210
                        y_Rotation.setPreferredSize(new Dimension(120,20));
211
                        //y_Rotation.setFont(new Font("x_Rotation:",Font.BOLD,15));
212
                        y_Rotation.setText(PluginServices.getText(this,"y_Rotation")+":");
213
                        
214
                }
215
                return y_Rotation;
216
        }
217
        
218
        private JLabel getZ_Rotation() {
219
                if(z_Rotation == null ) {
220
                        z_Rotation = new JLabel();
221
                        z_Rotation.setPreferredSize(new Dimension(120,20));
222
                        //z_Rotation.setFont(new Font("x_Translation:",Font.BOLD,15));
223
                        z_Rotation.setText(PluginServices.getText(this,"z_Rotation")+":");
224
                        
225
                }
226
                return z_Rotation;
227
        }
228
        
229
        private JLabel getScale() {
230
                if(scale == null ) {
231
                        scale = new JLabel();
232
                        scale.setPreferredSize(new Dimension(120,20));
233
                        //scale.setFont(new Font("scale:",Font.BOLD,15));
234
                        scale.setText(PluginServices.getText(this,"scale")+":");
235
                        
236
                }
237
                return scale;
238
        }
239
        
240
        public JTextField getTx_Translation() {
241
                if(tx_Translation == null ) {
242
                        tx_Translation = new JTextField();
243
                        tx_Translation.setPreferredSize(new Dimension(120,20));
244
                        //tx_Translation.setFont(new Font("",Font.ITALIC,10));
245
                        tx_Translation.setText("0");
246
                        tx_Translation.setEditable(true);
247
                        //tx_Translation.addKeyListener(this);
248
                }
249
                return tx_Translation;
250
        }
251
        
252
        public JTextField getTy_Translation() {
253
                if(ty_Translation == null ) {
254
                        ty_Translation = new JTextField();
255
                        ty_Translation.setPreferredSize(new Dimension(120,20));
256
                        //ty_Translation.setFont(new Font("",Font.ITALIC,10));
257
                        ty_Translation.setText("0");
258
                        ty_Translation.setEditable(true);
259
                        //ty_Translation.addKeyListener(this);
260
                }
261
                return ty_Translation;
262
        }
263
        
264
        public JTextField getTz_Translation() {
265
                if(tz_Translation == null ) {
266
                        tz_Translation = new JTextField();
267
                        tz_Translation.setPreferredSize(new Dimension(120,20));
268
                        //tz_Translation.setFont(new Font("",Font.ITALIC,10));
269
                        tz_Translation.setText("0");
270
                        tz_Translation.setEditable(true);
271
                        //tz_Translation.addKeyListener(this);
272
                }
273
                return tz_Translation;
274
        }
275
        
276
        public JTextField getTx_Rotation() {
277
                if(tx_Rotation == null ) {
278
                        tx_Rotation = new JTextField();
279
                        tx_Rotation.setPreferredSize(new Dimension(120,20));
280
                        //tx_Rotation.setFont(new Font("",Font.ITALIC,10));
281
                        tx_Rotation.setText("0");
282
                        tx_Rotation.setEditable(true);
283
                        //tx_Rotation.addKeyListener(this);
284
                }
285
                return tx_Rotation;
286
        }
287
        
288
        public JTextField getTy_Rotation() {
289
                if(ty_Rotation == null ) {
290
                        ty_Rotation = new JTextField();
291
                        ty_Rotation.setPreferredSize(new Dimension(120,20));
292
                        //ty_Rotation.setFont(new Font("",Font.ITALIC,10));
293
                        ty_Rotation.setText("0");
294
                        ty_Rotation.setEditable(true);
295
                        //ty_Rotation.addKeyListener(this);
296
                }
297
                return ty_Rotation;
298
        }
299
        
300
        public JTextField getTz_Rotation() {
301
                if(tz_Rotation == null ) {
302
                        tz_Rotation = new JTextField();
303
                        tz_Rotation.setPreferredSize(new Dimension(120,20));
304
                        //tz_Rotation.setFont(new Font("",Font.ITALIC,10));
305
                        tz_Rotation.setText("0");
306
                        tz_Rotation.setEditable(true);
307
                        //tz_Rotation.addKeyListener(this);
308
                }
309
                return tz_Rotation;
310
        }
311
        
312
        public JTextField getTscale() {
313
                if(tScale == null ) {
314
                        tScale = new JTextField();
315
                        tScale.setPreferredSize(new Dimension(120,20));
316
                        //tScale.setFont(new Font("",Font.ITALIC,10));
317
                        tScale.setText("0");
318
                        tScale.setEditable(true);
319
                        //tScale.addKeyListener(this);
320
                }
321
                return tScale;
322
        }
323
                
324
        private Component getInfo() {
325
                if (info == null){
326
                        info = new JTextArea();
327
                        info.setLineWrap(true);
328
                        info.setWrapStyleWord(true);
329
                        info.setPreferredSize(new Dimension(400, 240));
330
                        info.setEditable(false);
331
                        info.setText(getWKT());
332
                }
333
                info.setText(getWKT());
334
                return info;
335
        }
336
        
337
        /**
338
         * 
339
         * @return
340
         */
341
        public ICrs getProjection() {
342
                if(tx_Translation.getText().equals("")){
343
                        tx_Translation.setText("0");
344
                }
345
                else if (ty_Translation.getText().equals("")){
346
                        ty_Translation.setText("0");
347
                }
348
                else if (tz_Translation.getText().equals("")){
349
                        tz_Translation.setText("0");
350
                }
351
                else if (tx_Rotation.getText().equals("")){
352
                        tx_Rotation.setText("0");
353
                }
354
                else if (ty_Rotation.getText().equals("")){
355
                        ty_Rotation.setText("0");
356
                }
357
                else if (tz_Rotation.getText().equals("")){
358
                        tz_Rotation.setText("0");
359
                }
360
                else if (tScale.getText().equals("")){
361
                        tScale.setText("0");
362
                }                
363
                String param = "+towgs84="+ tx_Translation.getText()+","+
364
                                           ty_Translation.getText()+","+
365
                                           tz_Translation.getText()+","+
366
                                           tx_Rotation.getText()+","+
367
                                           ty_Rotation.getText()+","+
368
                                           tz_Rotation.getText()+","+
369
                                           tScale.getText() + " ";
370
                
371
                try {
372
                        ICrs crs = new CrsFactory().getCRS(getCode(),
373
                                        getWKT(),param);
374
                        return crs;
375
                } catch (org.gvsig.crs.CrsException e) {
376
                        e.printStackTrace();
377
                }
378
                return null;
379
        }
380
        
381
        /**
382
         * 
383
         * @param cod
384
         */
385
        public void setCode(int cod){
386
                codeEpsg = cod;
387
        }
388
        
389
        /**
390
         * 
391
         * @return
392
         */
393
        public int getCode(){
394
                return codeEpsg;
395
        }
396
        
397
        /**
398
         * 
399
         * @param cad
400
         */
401
        public void setWKT(String cad){
402
                cadWKT = cad;
403
                CrsWkt parser = new CrsWkt(cad);
404
                setSourceAbrev(parser.getAuthority()[0], parser.getAuthority()[1]);
405
                getInfo();
406
        }        
407
        
408
        /**
409
         * 
410
         * @return
411
         */
412
        public String getWKT(){
413
                return cadWKT;
414
        }
415
        
416
        /**
417
         * 
418
         * @return
419
         */
420
        public static boolean isPressed() { return pressed; }
421
        
422
        
423
        /**
424
         * metodo para comprobar si los JTextfile estan escritos 
425
         * correctamente los campos de la transformacion manual
426
         */
427
        public boolean correctJTextField(){
428
                boolean correct = true;
429
                
430
                if ((tx_Translation.getText().length()==0) || (tx_Translation.getText().length()!=verify(tx_Translation.getText()).length())){
431
                        tx_Translation.setText("0");
432
                        tx_Translation.setBackground(new Color(255,204,204));
433
                        correct = false;
434
                }
435
                else tx_Translation.setBackground(new Color(255,255,255));
436
                if ((ty_Translation.getText().length()==0) || (ty_Translation.getText().length()!=verify(ty_Translation.getText()).length())){
437
                        ty_Translation.setText("0");
438
                        ty_Translation.setBackground(new Color(255,204,204));
439
                        correct = false;
440
                }
441
                else ty_Translation.setBackground(new Color(255,255,255));
442
                if ((tz_Translation.getText().length()==0) || (tz_Translation.getText().length()!=verify(tz_Translation.getText()).length())){
443
                        tz_Translation.setText("0");
444
                        tz_Translation.setBackground(new Color(255,204,204));
445
                        correct = false;
446
                }
447
                else tz_Translation.setBackground(new Color(255,255,255));
448
                if ((tx_Rotation.getText().length() == 0) || (tx_Rotation.getText().length()!=verify(tx_Rotation.getText()).length())){
449
                        tx_Rotation.setText("0");
450
                        tx_Rotation.setBackground(new Color(255,204,204));
451
                        correct = false;
452
                }
453
                else tx_Rotation.setBackground(new Color(255,255,255));
454
                if ((ty_Rotation.getText().length() == 0) || (ty_Rotation.getText().length()!=verify(ty_Rotation.getText()).length())){
455
                        ty_Rotation.setText("0");
456
                        ty_Rotation.setBackground(new Color(255,204,204));
457
                        correct = false;
458
                }
459
                else ty_Rotation.setBackground(new Color(255,255,255));
460
                if ((tz_Rotation.getText().length() == 0) || (tz_Rotation.getText().length()!=verify(tz_Rotation.getText()).length())){
461
                        tz_Rotation.setText("0");
462
                        tz_Rotation.setBackground(new Color(255,204,204));
463
                        correct = false;
464
                }
465
                else tz_Rotation.setBackground(new Color(255,255,255));
466
                if ((tScale.getText().length() == 0) || (tScale.getText().length()!=verify(tScale.getText()).length())){
467
                        tScale.setText("0");
468
                        tScale.setBackground(new Color(255,204,204));
469
                        correct = false;
470
                }
471
                else tScale.setBackground(new Color(255,255,255));
472
                return correct;
473
        }
474
        
475
        /**
476
         * Verifica que el valor insertado en el campo JTextField correspondiente
477
         * se corresponde con un valor n?merico y de tipo double
478
         * 
479
         * @param cad
480
         * @return
481
         */
482
        private String verify(String cad) {
483
                String num_cad = "";
484
                int pto = 0;
485
                char[] nums = {'0','1','2','3','4','5','6','7','8','9'};
486
                if (cad.startsWith("-")) num_cad += "-";
487
                for (int i=0; i< cad.length(); i++){                                                
488
                        if (cad.charAt(i) == '.'){                                
489
                                if (cad.startsWith(".") || cad.endsWith(".") || pto>0);
490
                                else {
491
                                        pto++;
492
                                        num_cad += cad.charAt(i);
493
                                }
494
                        }                        
495
                        else
496
                                for (int j=0; j< nums.length; j++)
497
                                        if (cad.charAt(i) == nums[j])
498
                                                num_cad += cad.charAt(i);                                
499
                }                
500
                return num_cad;
501
        }
502
        
503
        /**
504
         * Mira si los JTextField tienen dominios correctos
505
         * @return
506
         */
507
        public boolean correctDomain(){
508
                boolean correctDomain = true;
509
                double tx = Double.parseDouble(tx_Translation.getText());
510
                double ty = Double.parseDouble(ty_Translation.getText());
511
                double tz = Double.parseDouble(tz_Translation.getText());
512
                double rx = Double.parseDouble(tx_Rotation.getText());
513
                double ry = Double.parseDouble(ty_Rotation.getText());
514
                double rz = Double.parseDouble(tz_Rotation.getText());
515
                double sc = Double.parseDouble(tScale.getText());
516
                
517
                if (tx < -1000.0 || tx > 1000.0) {
518
                        tx_Translation.setText("0");
519
                        tx_Translation.setBackground(new Color(255,204,204));
520
                        correctDomain = false;
521
                }
522
                else tx_Translation.setBackground(new Color(255,255,255));
523
                if (ty < -1000.0 || ty > 1000.0) {
524
                        ty_Translation.setText("0");
525
                        ty_Translation.setBackground(new Color(255,204,204));
526
                        correctDomain = false;
527
                }
528
                else ty_Translation.setBackground(new Color(255,255,255));
529
                if (tz < -1000.0 || tz > 1000.0) {
530
                        tz_Translation.setText("0");
531
                        tz_Translation.setBackground(new Color(255,204,204));
532
                        correctDomain = false;
533
                }
534
                else tz_Translation.setBackground(new Color(255,255,255));
535
                if (rx < -60.0 || rx > 60.0) {
536
                        tx_Rotation.setText("0");
537
                        tx_Rotation.setBackground(new Color(255,204,204));
538
                        correctDomain = false;
539
                }
540
                else tx_Rotation.setBackground(new Color(255,255,255));
541
                if (ry < -60.0 || ry > 60.0) {
542
                        ty_Rotation.setText("0");
543
                        ty_Rotation.setBackground(new Color(255,204,204));
544
                        correctDomain = false;
545
                }
546
                else ty_Rotation.setBackground(new Color(255,255,255));
547
                if (rz < -60.0 || rz > 60.0) {
548
                        tz_Rotation.setText("0");
549
                        tz_Rotation.setBackground(new Color(255,204,204));
550
                        correctDomain = false;
551
                }
552
                else tz_Rotation.setBackground(new Color(255,255,255));
553
                if (sc < -20.0 || sc > 20.0) {
554
                        tScale.setText("0");
555
                        tScale.setBackground(new Color(255,204,204));
556
                        correctDomain = false;
557
                }
558
                else tScale.setBackground(new Color(255,255,255));
559
                return correctDomain;
560
        }
561
        
562
        /**
563
         * Consigue el estado actual de los par?metros insertados. En caso
564
         * de que todos los par?metros sean 0, o haya par?metros sin valor
565
         * deshabilitar? el bot?n de aceptar
566
         * @return
567
         */
568
        public boolean getStatus() {
569
                if ((tx_Translation.getText().equals("0") || tx_Translation.getText().equals("")) &&
570
                                (ty_Translation.getText().equals("0")  || ty_Translation.getText().equals(""))&&
571
                                (tz_Translation.getText().equals("0")  || tz_Translation.getText().equals(""))&&
572
                                (tx_Rotation.getText().equals("0")  || tx_Rotation.getText().equals(""))&&
573
                                (ty_Rotation.getText().equals("0")  || ty_Rotation.getText().equals(""))&&
574
                                (tz_Rotation.getText().equals("0")  || tz_Rotation.getText().equals(""))&&
575
                                (tScale.getText().equals("0")  || tScale.getText().equals("")))
576
                        return false;
577
                else if (tx_Translation.getText().equals("") ||
578
                                (ty_Translation.getText().equals(""))||
579
                                (tz_Translation.getText().equals(""))||
580
                                (tx_Rotation.getText().equals(""))||
581
                                (ty_Rotation.getText().equals(""))||
582
                                (tz_Rotation.getText().equals(""))||
583
                                (tScale.getText().equals("")))
584
                        return false;
585
                return true;
586
        }
587
        
588
        public void setTargetAuthority(String[] authority){
589
                targetAuthority = authority;
590
                setTargetAbrev(targetAuthority[0], targetAuthority[1]);                
591
        }
592
        
593
        public void setTargetAbrev(String fuente, String codigo){
594
                targetAbrev = fuente + ":" + codigo;
595
        }
596
        
597
        public String getTargetAbrev() {
598
                return targetAbrev;
599
        }
600
        
601
        public void setSourceAbrev(String fuente, String codigo){
602
                sourceAbrev = fuente + ":" + codigo;
603
        }
604
        
605
        public String getSourceAbrev(){
606
                return sourceAbrev;
607
        }
608
        
609
        public String getValues(){
610
                return "["+tx_Translation.getText()+", "+ty_Translation.getText()+
611
                ", "+tz_Translation.getText()+", "+tx_Rotation.getText()+", "+ty_Rotation.getText()+", "
612
                +tz_Rotation.getText()+", "+tScale.getText()+"]";
613
        }
614

    
615
}