Statistics
| Revision:

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

History | View | Annotate | Download (14.6 KB)

1 3566 jorpiell
2 3214 jorpiell
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3 4334 jorpiell
 *
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 3214 jorpiell
package es.gva.cit.gazetteer.ui.search;
43 3613 jorpiell
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 3214 jorpiell
import java.awt.Dimension;
52
import java.awt.FlowLayout;
53
import java.awt.event.ActionEvent;
54
import java.awt.event.ActionListener;
55 3566 jorpiell
import java.util.Collection;
56 3214 jorpiell
import javax.swing.BoxLayout;
57
import javax.swing.JButton;
58
import javax.swing.JFrame;
59 4346 jorpiell
import javax.swing.JLabel;
60 3214 jorpiell
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
import javax.swing.event.TreeSelectionEvent;
63
import javax.swing.event.TreeSelectionListener;
64
65 4713 cesar
import org.gvsig.i18n.Messages;
66
67 3214 jorpiell
/**
68 3566 jorpiell
 *
69
 *
70
 *
71 3214 jorpiell
 * @author Jorge Piera Llodra (piera_jor@gva.es)
72
 */
73 3566 jorpiell
public class SearchDialogPanel extends JPanel implements ActionListener, TreeSelectionListener {
74 4334 jorpiell
//        It is needed to close the frame
75
        /**
76
         *
77
         *
78
         */
79
        private JFrame parent;
80 8620 jorpiell
        private Query query = null;
81 4334 jorpiell
        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 3566 jorpiell
136 4334 jorpiell
        /**
137
         *
138
         *
139
         */
140
        protected Collection nodesRecords = new java.util.ArrayList();
141 3566 jorpiell
142 4334 jorpiell
        /**
143
         *
144
         *
145
         */
146
        protected String attribute = null;
147 3566 jorpiell
148 4334 jorpiell
        /**
149
         *
150
         *
151
         */
152
        protected boolean isMinimized = true;
153
        /**
154
         *
155
         *
156
         */
157
        private ThesaurusName currentThesaurus = null;
158 3566 jorpiell
159 4334 jorpiell
        /**
160
         *
161
         *
162
         */
163
        private Collection searchThreads = null;
164 9418 jorpiell
        private String currentServer = null;
165 4334 jorpiell
        /**
166
         * This method initializes
167
         *
168
         *
169
         * @param client
170
         * @param translator
171
         */
172 4713 cesar
        public  SearchDialogPanel(GazetteerClient client, Object serverConnectFrame) {
173 4334 jorpiell
                super();
174
                searchThreads = new java.util.ArrayList();
175
                this.client = client;
176
                this.isMinimized = true;
177
                this.serverConnectFrame = serverConnectFrame;
178
                initialize();
179 3214 jorpiell
180 4334 jorpiell
        }
181 3566 jorpiell
182 4334 jorpiell
        /**
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 4346 jorpiell
                ppalPanel.setPreferredSize(new Dimension(514,91));
190 4334 jorpiell
                ppalPanel.add(getUpperPanel(), null);
191
                ppalPanel.add(getLowerPanel(), null);
192
                ppalPanel.add(getButtonPanel(), null);
193
                add(ppalPanel);
194 3566 jorpiell
195 4334 jorpiell
                getLowerPanel().setVisible(false);
196
                setDefaultButtonListeners();
197
        }
198 3214 jorpiell
199 4334 jorpiell
        /**
200
         * It Gets the upperPanel
201
         *
202
         *
203
         * @return
204
         */
205
        public SearchUpperPanel getUpperPanel() {
206
                if (upperPanel == null){
207 4713 cesar
                        upperPanel = new SearchUpperPanel(
208 4334 jorpiell
                                        client.getProtocol());
209 3214 jorpiell
210 4334 jorpiell
                }
211
                return upperPanel;
212 3214 jorpiell
213 4334 jorpiell
        }
214 3214 jorpiell
215 4334 jorpiell
        /**
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 3613 jorpiell
231 4334 jorpiell
        /**
232
         *
233
         *
234
         *
235
         * @param catCli
236
         */
237
        public void setCatalogClient(GazetteerClient catCli) {
238
                this.client = catCli;
239
        }
240 3214 jorpiell
241 4334 jorpiell
        /**
242
         *
243
         *
244
         *
245
         * @return
246
         */
247
        public JPanel getButtonPanel() {
248
                if (buttonsPanel == null) {
249 4346 jorpiell
                        FlowLayout flowLayout = new FlowLayout();
250
                        flowLayout.setAlignment(FlowLayout.RIGHT);
251
                        buttonsPanel = new JPanel(flowLayout);
252 4334 jorpiell
                        buttonsPanel.add(getSearchButton());
253
                        buttonsPanel.add(getCancelSearchesButton());
254 4346 jorpiell
                        JLabel label = new JLabel();
255
                        label.setPreferredSize(new Dimension(6,0));
256
                        buttonsPanel.add(label);
257 4334 jorpiell
                        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 4713 cesar
                        returnButton = new JButton(Messages.getText("last"));
273 4346 jorpiell
                        returnButton.setPreferredSize(new Dimension(80, 23));
274 4334 jorpiell
                        returnButton.setActionCommand("return");
275
                }
276
                return returnButton;
277
        }
278 3566 jorpiell
279
280 4334 jorpiell
        /**
281
         *
282
         *
283
         *
284
         * @return
285
         */
286
        public JButton getSearchButton() {
287
                if (searchButton == null) {
288 4713 cesar
                        searchButton = new JButton(Messages.getText("searchButton"));
289 4346 jorpiell
                        searchButton.setPreferredSize(new Dimension(80, 23));
290 4334 jorpiell
                        searchButton.setActionCommand("search");
291
                }
292
                return searchButton;
293
        }
294 3566 jorpiell
295 4334 jorpiell
        /**
296
         * It gets the cancel searches button
297
         *
298
         *
299
         * @return
300
         */
301
        public JButton getCancelSearchesButton() {
302
                if (cancelSearchesButton == null) {
303 4713 cesar
                        cancelSearchesButton = new JButton(Messages.getText("cancelSearchButton"));
304 4631 jorpiell
                        cancelSearchesButton.setPreferredSize(new Dimension(90, 23));
305 4334 jorpiell
                        cancelSearchesButton.setActionCommand("cancel");
306
                }
307
                return cancelSearchesButton;
308
        }
309 3566 jorpiell
310 4334 jorpiell
        /**
311
         *
312
         *
313
         *
314
         * @return
315
         */
316
        public JButton getCloseButton() {
317
                if (closeButton == null) {
318 4713 cesar
                        closeButton = new JButton(Messages.getText("close"));
319 4346 jorpiell
                        closeButton.setPreferredSize(new Dimension(80, 23));
320 4334 jorpiell
                        closeButton.setActionCommand("close");
321
                }
322
                return closeButton;
323
        }
324 3566 jorpiell
325 4334 jorpiell
        /**
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 3566 jorpiell
339 4334 jorpiell
        /**
340
         *
341
         *
342
         *
343
         * @return
344
         */
345
        public ThesaurusName getFeatureSelected() {
346
                //return (Feature) controlsPanel.getTipoList().getSelectedValue();
347
                return lowerPanel.getType();
348
        }
349 3613 jorpiell
350 4334 jorpiell
        /**
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 3214 jorpiell
366 4334 jorpiell
        /**
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 3566 jorpiell
390 4334 jorpiell
        }
391 3214 jorpiell
392 4334 jorpiell
        /**
393
         *
394
         *
395
         */
396
        protected void sizeButtonActionPerformed() {
397
                if (isMinimized){
398
                        parent.setSize(525,450);
399 4346 jorpiell
                        ppalPanel.setPreferredSize(new Dimension(514,426));
400 4334 jorpiell
                        getLowerPanel().setVisible(true);
401 4354 jorpiell
                        ppalPanel.setVisible(true);
402 4334 jorpiell
                        getUpperPanel().setUpIcon();
403
                }else{
404
                        parent.setSize(525,115);
405 4346 jorpiell
                        ppalPanel.setPreferredSize(new Dimension(514,91));
406 4334 jorpiell
                        getLowerPanel().setVisible(false);
407 4346 jorpiell
                        ppalPanel.setVisible(true);
408 4334 jorpiell
                        getUpperPanel().setDownIcon();
409
                }
410
                isMinimized = !isMinimized;
411
        }
412 3214 jorpiell
413 4334 jorpiell
        /**
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 4713 cesar
                                                Messages.getText("errorNotThesaurusSelected"),
424 4334 jorpiell
                                                "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 3566 jorpiell
444 4334 jorpiell
445 3566 jorpiell
446 4334 jorpiell
        /**
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 3214 jorpiell
459 4334 jorpiell
        /**
460
         *
461
         *
462
         */
463
        protected void closeButtonActionPerformed() {
464
                parent.setVisible(false);
465
        }
466 3214 jorpiell
467 4334 jorpiell
        /**
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 3613 jorpiell
487 8620 jorpiell
                query = client.createNewQuery();
488 4334 jorpiell
                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 3613 jorpiell
503 4334 jorpiell
        /**
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 3613 jorpiell
513 4334 jorpiell
                Object[] possibilities = new Object[currentThesaurus.getFields().length];
514
                for (int i=0 ; i< possibilities.length ; i++)
515
                        possibilities[i] = currentThesaurus.getFields()[i].getName();
516 3613 jorpiell
517 4334 jorpiell
                String s = (String)JOptionPane.showInputDialog(
518
                                this,
519 4713 cesar
                                Messages.getText("chooseAttribute"),
520 4334 jorpiell
                                "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 4713 cesar
                                        Messages.getText("errorGetRecords"),
542 4334 jorpiell
                                        "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 4713 cesar
                                        Messages.getText("anyResult"),
558
                                        Messages.getText("gazetteer_search"),
559 4334 jorpiell
                                        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 8765 jjdelcerro
677 8620 jorpiell
        public Query getQuery() {
678
                return query;
679
        }
680 9418 jorpiell
681
        public String getCurrentServer() {
682
                return currentServer;
683
        }
684
685
        public void setCurrentServer(String currentServer) {
686
                this.currentServer = currentServer;
687
        }
688 4334 jorpiell
}