Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1009 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / ui / search / SearchDialogPanel.java @ 12649

History | View | Annotate | Download (14.6 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42
package es.gva.cit.gazetteer.ui.search;
43
import es.gva.cit.catalogClient.metadataXML.XMLNode;
44
import es.gva.cit.gazetteer.GazetteerClient;
45
import es.gva.cit.gazetteer.querys.Feature;
46
import es.gva.cit.gazetteer.querys.Query;
47
import es.gva.cit.gazetteer.querys.ThesaurusName;
48
import es.gva.cit.gazetteer.ui.showResults.ShowResultsDialog;
49
import es.gva.cit.gazetteer.wfsg.parsers.WfsgDescribeFeatureTypeParser;
50
import java.awt.Cursor;
51
import java.awt.Dimension;
52
import java.awt.FlowLayout;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55
import java.util.Collection;
56
import javax.swing.BoxLayout;
57
import javax.swing.JButton;
58
import javax.swing.JFrame;
59
import javax.swing.JLabel;
60
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
import javax.swing.event.TreeSelectionEvent;
63
import javax.swing.event.TreeSelectionListener;
64

    
65
import org.gvsig.i18n.Messages;
66

    
67
/**
68
 * 
69
 * 
70
 * 
71
 * @author Jorge Piera Llodra (piera_jor@gva.es)
72
 */
73
public class SearchDialogPanel extends JPanel implements ActionListener, TreeSelectionListener {
74
//        It is needed to close the frame
75
        /**
76
         * 
77
         * 
78
         */
79
        private JFrame parent;
80
        private Query query = null;
81
        protected Object serverConnectFrame;
82
//        Panels
83
        /**
84
         * 
85
         * 
86
         */
87
        protected JPanel ppalPanel = null;
88
        /**
89
         * 
90
         * 
91
         */
92
        protected SearchUpperPanel upperPanel = null;
93
        /**
94
         * 
95
         * 
96
         */
97
        protected SearchLowerPanel lowerPanel = null;
98
        /**
99
         * 
100
         * 
101
         */
102
        protected JPanel buttonsPanel = null;
103
//        Buttons
104
        /**
105
         * 
106
         * 
107
         */
108
        private JButton searchButton = null;
109
        /**
110
         * 
111
         * 
112
         */
113
        private JButton closeButton = null;
114
        /**
115
         * 
116
         * 
117
         */
118
        private JButton cancelSearchesButton = null;
119
        /**
120
         * 
121
         * 
122
         */
123
        private JButton returnButton = null;
124
        /**
125
         * 
126
         * 
127
         */
128
        private JButton sizeButton = null;
129
//        Otros
130
        /**
131
         * 
132
         * 
133
         */
134
        protected GazetteerClient client = null;
135

    
136
        /**
137
         * 
138
         * 
139
         */
140
        protected Collection nodesRecords = new java.util.ArrayList();
141

    
142
        /**
143
         * 
144
         * 
145
         */
146
        protected String attribute = null;
147

    
148
        /**
149
         * 
150
         * 
151
         */
152
        protected boolean isMinimized = true;
153
        /**
154
         * 
155
         * 
156
         */
157
        private ThesaurusName currentThesaurus = null;
158

    
159
        /**
160
         * 
161
         * 
162
         */
163
        private Collection searchThreads = null;
164
        private String currentServer = null;
165
        /**
166
         * This method initializes
167
         * 
168
         * 
169
         * @param client 
170
         * @param translator 
171
         */
172
        public  SearchDialogPanel(GazetteerClient client, Object serverConnectFrame) {        
173
                super();
174
                searchThreads = new java.util.ArrayList();
175
                this.client = client;
176
                this.isMinimized = true;
177
                this.serverConnectFrame = serverConnectFrame;
178
                initialize();
179

    
180
        } 
181

    
182
        /**
183
         * This method initializes this
184
         * 
185
         */
186
        private void initialize() {        
187
                ppalPanel = new JPanel();
188
                ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));
189
                ppalPanel.setPreferredSize(new Dimension(514,91));
190
                ppalPanel.add(getUpperPanel(), null);
191
                ppalPanel.add(getLowerPanel(), null);
192
                ppalPanel.add(getButtonPanel(), null);
193
                add(ppalPanel);
194

    
195
                getLowerPanel().setVisible(false);
196
                setDefaultButtonListeners();
197
        } 
198

    
199
        /**
200
         * It Gets the upperPanel
201
         * 
202
         * 
203
         * @return 
204
         */
205
        public SearchUpperPanel getUpperPanel() {        
206
                if (upperPanel == null){
207
                        upperPanel = new SearchUpperPanel(
208
                                        client.getProtocol());  
209

    
210
                }
211
                return upperPanel;
212

    
213
        } 
214

    
215
        /**
216
         * It Gets the lowePanel
217
         * 
218
         * 
219
         * @return 
220
         */
221
        public SearchLowerPanel getLowerPanel() {        
222
                if (lowerPanel == null){
223
                        lowerPanel = new SearchLowerPanel(client.getLnkIGazetteerDriver().getVectorFeatures(),
224
                                        client.getProtocol());  
225
                        if (client.getProtocol().equals("IDEC"))
226
                                lowerPanel.getJScrollThesaurus().setEnabled(false);
227
                }
228
                return lowerPanel;
229
        } 
230

    
231
        /**
232
         * 
233
         * 
234
         * 
235
         * @param catCli 
236
         */
237
        public void setCatalogClient(GazetteerClient catCli) {        
238
                this.client = catCli;
239
        } 
240

    
241
        /**
242
         * 
243
         * 
244
         * 
245
         * @return 
246
         */
247
        public JPanel getButtonPanel() {        
248
                if (buttonsPanel == null) {
249
                        FlowLayout flowLayout = new FlowLayout();
250
                        flowLayout.setAlignment(FlowLayout.RIGHT);
251
                        buttonsPanel = new JPanel(flowLayout);
252
                        buttonsPanel.add(getSearchButton());
253
                        buttonsPanel.add(getCancelSearchesButton());
254
                        JLabel label = new JLabel();
255
                        label.setPreferredSize(new Dimension(6,0));
256
                        buttonsPanel.add(label);
257
                        buttonsPanel.add(getReturnButton());
258
                        buttonsPanel.add(getCloseButton());
259
                        
260
                }
261
                return buttonsPanel;
262
        } 
263
        
264
        /**
265
         * It gets the return button
266
         * 
267
         * 
268
         * @return 
269
         */
270
        public JButton getReturnButton() {        
271
                if (returnButton == null) {
272
                        returnButton = new JButton(Messages.getText("last"));
273
                        returnButton.setPreferredSize(new Dimension(80, 23));
274
                        returnButton.setActionCommand("return");
275
                }
276
                return returnButton;
277
        } 
278

    
279

    
280
        /**
281
         * 
282
         * 
283
         * 
284
         * @return 
285
         */
286
        public JButton getSearchButton() {        
287
                if (searchButton == null) {
288
                        searchButton = new JButton(Messages.getText("searchButton"));
289
                        searchButton.setPreferredSize(new Dimension(80, 23));
290
                        searchButton.setActionCommand("search");
291
                }
292
                return searchButton;
293
        } 
294

    
295
        /**
296
         * It gets the cancel searches button
297
         * 
298
         * 
299
         * @return 
300
         */
301
        public JButton getCancelSearchesButton() {        
302
                if (cancelSearchesButton == null) {
303
                        cancelSearchesButton = new JButton(Messages.getText("cancelSearchButton"));
304
                        cancelSearchesButton.setPreferredSize(new Dimension(90, 23));
305
                        cancelSearchesButton.setActionCommand("cancel");
306
                }
307
                return cancelSearchesButton;
308
        } 
309

    
310
        /**
311
         * 
312
         * 
313
         * 
314
         * @return 
315
         */
316
        public JButton getCloseButton() {        
317
                if (closeButton == null) {
318
                        closeButton = new JButton(Messages.getText("close"));
319
                        closeButton.setPreferredSize(new Dimension(80, 23));
320
                        closeButton.setActionCommand("close");
321
                }
322
                return closeButton;
323
        } 
324

    
325
        /**
326
         * It  gets the change size button
327
         * 
328
         * 
329
         * @return 
330
         */
331
        public JButton getSizeButton() {        
332
                if (sizeButton == null) {
333
                        sizeButton = upperPanel.getSizeButton();
334
                        sizeButton.setActionCommand("size");
335
                }
336
                return sizeButton;
337
        } 
338

    
339
        /**
340
         * 
341
         * 
342
         * 
343
         * @return 
344
         */
345
        public ThesaurusName getFeatureSelected() {        
346
                //return (Feature) controlsPanel.getTipoList().getSelectedValue();
347
                return lowerPanel.getType();
348
        } 
349

    
350
        /**
351
         * 
352
         * 
353
         */
354
        public void setDefaultButtonListeners() {        
355
                getSearchButton().addActionListener(this);
356
                getCloseButton().addActionListener(this);
357
                getSizeButton().addActionListener(this);
358
                getReturnButton().addActionListener(this);
359
                getCancelSearchesButton().addActionListener(this);
360
                lowerPanel.getThesaurusList().addTreeSelectionListener(this);
361
        } 
362
        /* (non-Javadoc)
363
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
364
         */
365

    
366
        /**
367
         * 
368
         * 
369
         * 
370
         * @param e 
371
         */
372
        public void actionPerformed(ActionEvent e) {        
373
                //Buscar
374
                if (e.getActionCommand() == "search") {
375
                        searchButtonActionPerformed();
376
                } 
377
                if (e.getActionCommand() == "close") {
378
                        closeButtonActionPerformed();
379
                }
380
                if (e.getActionCommand() == "size") {
381
                        sizeButtonActionPerformed();
382
                }   
383
                if (e.getActionCommand() == "cancel") {
384
                        cancelSearchesButtonActionPerformed();
385
                }
386
                if (e.getActionCommand() == "return") {
387
                        returnButtonActionPerformed();
388
                }
389

    
390
        } 
391

    
392
        /**
393
         * 
394
         * 
395
         */
396
        protected void sizeButtonActionPerformed() {        
397
                if (isMinimized){
398
                        parent.setSize(525,450);
399
                        ppalPanel.setPreferredSize(new Dimension(514,426));
400
                        getLowerPanel().setVisible(true);
401
                        ppalPanel.setVisible(true);
402
                        getUpperPanel().setUpIcon();
403
                }else{
404
                        parent.setSize(525,115);
405
                        ppalPanel.setPreferredSize(new Dimension(514,91));
406
                        getLowerPanel().setVisible(false);
407
                        ppalPanel.setVisible(true);
408
                        getUpperPanel().setDownIcon();
409
                }
410
                isMinimized = !isMinimized;
411
        } 
412

    
413
        /**
414
         * 
415
         * 
416
         */
417
        protected void searchButtonActionPerformed() {        
418
                if (client.getProtocol().equals("WFS")){
419
                        if ((currentThesaurus == null) ||
420
                                        (currentThesaurus.getName().equals("ThesaurusRoot"))){
421
                                JOptionPane.showMessageDialog(
422
                                                this,
423
                                                Messages.getText("errorNotThesaurusSelected"),
424
                                                "WFS",
425
                                                JOptionPane.ERROR_MESSAGE
426
                                );
427
                                return;
428
                        }
429
                }  
430
                searchThread st =  new searchThread();
431
                searchThreads.add(st);   
432
                setCursor(new Cursor(Cursor.WAIT_CURSOR));       
433
        } 
434
        
435
        /**
436
         * Return button
437
         *
438
         */
439
        protected void returnButtonActionPerformed() {        
440
                ((JFrame)serverConnectFrame).setVisible(true);
441
                parent.setVisible(false);
442
        } 
443

    
444
        
445

    
446
        /**
447
         * 
448
         * 
449
         */
450
        protected void cancelSearchesButtonActionPerformed() {        
451
                for (int i=0 ; i<searchThreads.size() ; i++){
452
                        searchThread st = (searchThread)searchThreads.toArray()[i];
453
                        st.stop();            
454
                }     
455
                searchThreads.clear();
456
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));        
457
        } 
458

    
459
        /**
460
         * 
461
         * 
462
         */
463
        protected void closeButtonActionPerformed() {        
464
                parent.setVisible(false);
465
        } 
466

    
467
        /**
468
         * 
469
         * 
470
         * 
471
         * @return 
472
         */
473
        protected Query doQuery() {        
474
                ThesaurusName[] thesaurus;
475
                if (client.getProtocol().equals("WFS-G")){
476
                        thesaurus = lowerPanel.getAllTypes();
477
                        currentThesaurus = thesaurus[0];
478
                }else{
479
                        if (lowerPanel.getType() == null){
480
                                thesaurus = null;
481
                        }else{
482
                                thesaurus = new ThesaurusName[1];
483
                                thesaurus[0] = lowerPanel.getType();
484
                        }
485
                }        
486

    
487
                query = client.createNewQuery();
488
                query.setName(upperPanel.getName());
489
                query.setNameFilter(lowerPanel.getConcordancia());
490
                query.setFeatures(thesaurus);
491
                query.setFieldAttribute(getFieldAttribute());
492
                query.setRecsByPage(lowerPanel.getNPaginas());
493
                query.setCoordinates(lowerPanel.getCoordenadas());
494
                query.setCoordinatesFilter(lowerPanel.getCoordenadasOpcion());
495
                query.setCoordinatesClicked(upperPanel.isRestrictAreaClicked());
496
                query.getOptions().getAspect().setGoTo(lowerPanel.isGoToClicked());
497
                query.getOptions().getAspect().setKeepOld(lowerPanel.isKeepOldClicked());
498
                query.getOptions().getAspect().setPaintCurrent(lowerPanel.isMarkedPlaceClicked());
499
                query.getOptions().getSearch().setWithAccents(lowerPanel.isAccentsSearchEnabled());
500
                return query;
501
        } 
502

    
503
        /**
504
         * It opens a window with a combo and returns a field attribute name
505
         * 
506
         * 
507
         * @return 
508
         */
509
        private String getFieldAttribute() {        
510
                if (!(client.getProtocol().equals("WFS")))
511
                        return "geographicIdentifier";
512

    
513
                Object[] possibilities = new Object[currentThesaurus.getFields().length];
514
                for (int i=0 ; i< possibilities.length ; i++)
515
                        possibilities[i] = currentThesaurus.getFields()[i].getName();
516

    
517
                String s = (String)JOptionPane.showInputDialog(
518
                                this,
519
                                Messages.getText("chooseAttribute"),
520
                                "WFS",
521
                                JOptionPane.PLAIN_MESSAGE,
522
                                null,
523
                                possibilities,
524
                                currentThesaurus.getFields()[0].getName());
525
                if ((s != null) && (s.length() > 0)) {
526
                        attribute = s;
527
                        return s;
528
                }
529
                return null;
530
        } 
531

    
532
        /**
533
         * It returns the query that the user has selected
534
         * 
535
         */
536
        private void doSearch() {        
537
                nodesRecords = client.getFeature(client.getUrl(),
538
                                doQuery());
539
                if (nodesRecords == null) {
540
                        JOptionPane.showMessageDialog(this,
541
                                        Messages.getText("errorGetRecords"),
542
                                        "Error",
543
                                        JOptionPane.ERROR_MESSAGE);
544
                } 
545

    
546
        } 
547

    
548
        /**
549
         * 
550
         * 
551
         */
552
        private void showResults() {        
553
                Feature[] features = client.getLnkIGazetteerDriver().parseFeatures((XMLNode)nodesRecords.toArray()[0],currentThesaurus,attribute);
554

    
555
                if (features.length == 0){
556
                        JOptionPane.showMessageDialog(this,
557
                                        Messages.getText("anyResult"),
558
                                        Messages.getText("gazetteer_search"),
559
                                        JOptionPane.INFORMATION_MESSAGE);
560
                }else{
561
                        ShowResultsActionPerformed(features);
562
                }
563
        } 
564

    
565
        /**
566
         * 
567
         * 
568
         * 
569
         * @param features 
570
         */
571
        protected void ShowResultsActionPerformed(Feature[] features) {        
572
                new ShowResultsDialog(client,
573
                                features,
574
                                lowerPanel.getNPaginas(),
575
                                doQuery());
576

    
577
        } 
578

    
579
        /**
580
         * It loads the fields for the feature
581
         * 
582
         */
583
        private void loadCurrentFeature() {        
584
                if (client.getProtocol().equals("WFS-G"))
585
                        return;
586

    
587
                Collection fields = client.getLnkIGazetteerDriver().describeFeatureType(client.getUrl(),
588
                                currentThesaurus.getName());
589

    
590
                if (client.getProtocol().equals("WFS")){      
591
                        new WfsgDescribeFeatureTypeParser().parse((XMLNode)fields.toArray()[0],currentThesaurus);
592
                }
593

    
594
        } 
595

    
596
        /**
597
         * 
598
         * 
599
         * 
600
         * @param e 
601
         */
602
        public void valueChanged(TreeSelectionEvent e) {        
603
                if (currentThesaurus == null){
604
                        currentThesaurus = getFeatureSelected();
605
                        loadCurrentFeature();
606
                }else{
607
                        ThesaurusName feature = getFeatureSelected();
608
                        if (!(currentThesaurus.equals(feature))){
609
                                if (feature != null){
610
                                        currentThesaurus = feature;
611
                                        loadCurrentFeature();
612
                                }
613
                        }
614
                }
615

    
616
        } 
617

    
618
        /**
619
         * 
620
         * 
621
         * 
622
         * @param parent The parent to set.
623
         */
624
        public void setParent(JFrame parent) {        
625
                this.parent = parent;
626
        } 
627
        /**
628
         * This class is used to manage the searches.
629
         * It contains method to start and to stop a thread. It is
630
         * necessary to create because "stop" method (for the Thread class)
631
         * is deprecated.
632
         * 
633
         * 
634
         * @author Jorge Piera Llodra (piera_jor@gva.es)
635
         */
636
        private class searchThread implements Runnable {
637

    
638
                /**
639
                 * 
640
                 * 
641
                 */
642
                volatile Thread myThread = null;
643

    
644
                /**
645
                 * 
646
                 * 
647
                 */
648
                public  searchThread() {        
649
                        myThread = new Thread(this);
650
                        myThread.start();
651
                } 
652

    
653
                /**
654
                 * 
655
                 * 
656
                 */
657
                public void stop() {        
658
                        myThread.stop();
659
                } 
660

    
661
                /**
662
                 * 
663
                 * 
664
                 */
665
                public void run() {        
666
                        doSearch();
667
                        if ((nodesRecords != null) && (nodesRecords.size() > 0)) {
668
                                showResults();
669
                        }
670
                        searchThreads.remove(this);
671
                        if (searchThreads.size() == 0){
672
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));       
673
                        }
674
                } 
675
        }
676
        
677
        public Query getQuery() {
678
                return query;
679
        }
680

    
681
        public String getCurrentServer() {
682
                return currentServer;
683
        }
684

    
685
        public void setCurrentServer(String currentServer) {
686
                this.currentServer = currentServer;
687
        }
688
}