Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appGazetteer / src / org / gvsig / gazetteer / ui / search / SearchDialogPanel.java @ 28434

History | View | Annotate | Download (11.4 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 org.gvsig.gazetteer.ui.search;
43
import java.awt.BorderLayout;
44
import java.awt.Cursor;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.util.Collection;
48

    
49
import javax.swing.Icon;
50
import javax.swing.ImageIcon;
51
import javax.swing.JFrame;
52
import javax.swing.JOptionPane;
53
import javax.swing.JPanel;
54

    
55
import org.gvsig.catalog.ui.search.SearchButtonPanel;
56
import org.gvsig.catalog.utils.CatalogConstants;
57
import org.gvsig.gazetteer.GazetteerClient;
58
import org.gvsig.gazetteer.querys.Feature;
59
import org.gvsig.gazetteer.querys.FeatureType;
60
import org.gvsig.gazetteer.querys.FeatureTypeAttribute;
61
import org.gvsig.gazetteer.querys.GazetteerQuery;
62
import org.gvsig.gazetteer.ui.showresults.ShowResultsDialog;
63
import org.gvsig.i18n.Messages;
64

    
65

    
66
/**
67
 * @author Jorge Piera Llodra (piera_jor@gva.es)
68
 */
69
public class SearchDialogPanel extends JPanel implements ActionListener {
70
        private JFrame parent;
71
        private GazetteerQuery query = null;
72
        protected Object serverConnectFrame;
73
        protected SearchUpperPanel upperPanel = null;
74
        protected SearchLowerPanel lowerPanel = null;
75
        protected SearchButtonPanel buttonsPanel = null;
76
        protected GazetteerClient client = null;
77
        protected boolean isMinimized = true;
78
        protected Feature[] features = null;
79
        private Collection searchThreads = null;
80
        private String currentServer = null;
81
        private String featureAttribute = null;
82

    
83
        /**
84
         * This method initializes
85
         * 
86
         * 
87
         * @param client 
88
         * @param translator 
89
         */
90
        public  SearchDialogPanel(GazetteerClient client, Object serverConnectFrame) {        
91
                super();
92
                searchThreads = new java.util.ArrayList();
93
                this.client = client;
94
                this.isMinimized = true;
95
                this.serverConnectFrame = serverConnectFrame;
96
                initialize();
97
                getUpperPanel().setIcon(getDownIcon());
98
        } 
99

    
100
        /**
101
         * This method initializes this
102
         */
103
        private void initialize() {        
104
                setLayout(new BorderLayout());
105
                add(getUpperPanel(),BorderLayout.NORTH);
106
                add(getLowerPanel(),BorderLayout.CENTER);
107
                add(getButtonPanel(),BorderLayout.SOUTH);
108
                getLowerPanel().setVisible(false);
109
        } 
110

    
111
        /**
112
         * It Gets the upperPanel
113
         * @return 
114
         */
115
        public SearchUpperPanel getUpperPanel() {        
116
                if (upperPanel == null){
117
                        upperPanel = new SearchUpperPanel(); 
118
                        upperPanel.addActionListener(this);
119
                }
120
                return upperPanel;
121

    
122
        } 
123

    
124
        /**
125
         * It Gets the lowePanel
126
         * @return 
127
         */
128
        public SearchLowerPanel getLowerPanel() {        
129
                if (lowerPanel == null){
130
                        FeatureType[] types = null;                        
131
                        try {
132
                                types = ((GazetteerClient)client).getFeatureTypes();
133
                        } catch (Exception e) {
134
                                //The thesaurus will not loaded
135
                        }                        
136
                        lowerPanel = new SearchLowerPanel(types,
137
                                        client.getAditionalSearchPanel());
138
                        lowerPanel.addResultsByPageNumber(10);
139
                        lowerPanel.addResultsByPageNumber(25);
140
                        lowerPanel.addResultsByPageNumber(50);
141
                        lowerPanel.addCoordinatesRelationship(
142
                                        Messages.getText("coordinatesContains"));
143
                        lowerPanel.addCoordinatesRelationship(
144
                                        Messages.getText("coordinatesFullyOutsideOf"));
145
                }
146
                return lowerPanel;
147
        } 
148

    
149
        /**
150
         * Set the gazetteer client
151
         * @param 
152
         * Gazetteer client to set
153
         */
154
        public void setGazetteerClient(GazetteerClient gazetteerClient) {        
155
                this.client = gazetteerClient;
156
        } 
157

    
158
        /**
159
         * @return the buttons panel
160
         */
161
        public JPanel getButtonPanel() {        
162
                if (buttonsPanel == null) {
163
                        buttonsPanel = new SearchButtonPanel();
164
                        buttonsPanel.addActionListener(this);                        
165
                }
166
                return buttonsPanel;
167
        }         
168

    
169
        /**
170
         * @return 
171
         */
172
        public FeatureType getFeatureSelected() {        
173
                return lowerPanel.getType();
174
        } 
175

    
176
        /*
177
         * (non-Javadoc)
178
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
179
         */        
180
        public void actionPerformed(ActionEvent e) {        
181
                if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
182
                        searchButtonActionPerformed();
183
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0){
184
                        closeButtonActionPerformed();
185
                }else if(e.getActionCommand().compareTo(CatalogConstants.RESIZE_BUTTON_ACTION_COMMAND)==0){
186
                        resizeButtonActionPerformed();
187
                }else if (e.getActionCommand().compareTo(CatalogConstants.CANCEL_BUTTON_ACTION_COMMAND)==0){
188
                        cancelSearchesButtonActionPerformed();
189
                }else if (e.getActionCommand().compareTo(CatalogConstants.LAST_BUTTON_ACTION_COMMAND)==0){
190
                        lastButtonActionPerformed();
191
                }
192
        } 
193

    
194
        /**
195
         * thrown when the resize button is clicked
196
         */
197
        protected void resizeButtonActionPerformed() {        
198
                if (isMinimized){
199
                        parent.setSize(parent.getWidth(),450);
200
                        parent.doLayout();
201
                        getLowerPanel().setVisible(true);
202
                        getUpperPanel().setIcon(getUpIcon());
203
                }else{
204
                        parent.setSize(parent.getWidth(),115);
205
                        getLowerPanel().setVisible(false);                        
206
                        getUpperPanel().setIcon(getDownIcon());
207
                }
208
                isMinimized = !isMinimized;
209
        } 
210
        
211
        
212
        protected Icon getUpIcon(){
213
                return new ImageIcon("./gvSIG/extensiones/org.gvsig.catalog/images/up.png");
214
        }
215
        
216
        protected Icon getDownIcon(){
217
                return new ImageIcon("./gvSIG/extensiones/org.gvsig.catalog/images/down.png");
218
        }
219

    
220

    
221
        /**
222
         * thrown when the search button is clicked
223
         * @throws Exception 
224
         */
225
        protected void searchButtonActionPerformed() {        
226
                FeatureType featureType = lowerPanel.getType();
227
                if (client.isDescribeFeatureTypeNeeded()){
228
                        try {
229
                                FeatureTypeAttribute atribute = getAttribute(featureType);
230
                                if (atribute == null){
231
                                        return;
232
                                }else{
233
                                        featureAttribute = atribute.getName();
234
                                }
235
                        } catch (Exception e) {
236
                                e.printStackTrace();
237
                                return;
238
                        }
239
                }        
240
                searchThread st =  new searchThread();
241
                searchThreads.add(st);   
242
                setCursor(new Cursor(Cursor.WAIT_CURSOR));       
243
        } 
244

    
245
        /**
246
         * This method open a Jdialog panel and shows a list of
247
         * attributes to choose one of them.
248
         * @param featureType
249
         * Feature 
250
         * @return
251
         * The selected attribute
252
         * @throws Exception
253
         */
254
        private FeatureTypeAttribute getAttribute(FeatureType featureType) throws Exception{
255
                if ((featureType == null) || 
256
                                (lowerPanel.getType().getName().equals(Messages.getText("ThesaurusRoot")))){
257
                        JOptionPane.showMessageDialog(
258
                                        this,
259
                                        Messages.getText("errorNotThesaurusSelected"),
260
                                        "WFS",
261
                                        JOptionPane.ERROR_MESSAGE
262
                        );
263
                        return null;
264
                }        
265
                FeatureTypeAttribute[] atributes = 
266
                        client.describeFeatureType(featureType.getName());
267
                FeatureTypeAttribute attribute = (FeatureTypeAttribute)JOptionPane.showInputDialog(
268
                                this,
269
                                Messages.getText("chooseAttribute"),
270
                                null,
271
                                JOptionPane.PLAIN_MESSAGE,
272
                                null,
273
                                atributes,
274
                                featureType.getName());
275
                if (attribute == null) {
276
                        JOptionPane.showMessageDialog(
277
                                        this,
278
                                        Messages.getText("chooseAttribute"),
279
                                        "WFS",
280
                                        JOptionPane.ERROR_MESSAGE
281
                        );
282
                }
283
                return attribute;                
284
        }
285

    
286
        /**
287
         * thrown when the last button is clicked
288
         */
289
        protected void lastButtonActionPerformed() {        
290
                ((JFrame)serverConnectFrame).setVisible(true);
291
                parent.setVisible(false);
292
        }         
293

    
294
        /**
295
         * thrown when the cancel button is clicked
296
         */
297
        protected void cancelSearchesButtonActionPerformed() {        
298
                for (int i=0 ; i<searchThreads.size() ; i++){
299
                        searchThread st = (searchThread)searchThreads.toArray()[i];
300
                        st.stop();            
301
                }     
302
                searchThreads.clear();
303
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));        
304
        } 
305

    
306
        /**
307
         * thrown when the close button is clicked
308
         */
309
        protected void closeButtonActionPerformed() {        
310
                parent.setVisible(false);
311
        } 
312

    
313
        /**
314
         * @return the selected query
315
         */
316
        protected GazetteerQuery doQuery() {        
317
                query = client.createNewQuery();
318
                query.setName(upperPanel.getName());
319
                query.setNameFilter(lowerPanel.getConcordancia());
320
                query.setFieldAttribute(featureAttribute);
321
                query.setFeatures(lowerPanel.getAllTypes());
322
                query.setRecsByPage(lowerPanel.getResultsByPage());
323
                query.setCoordinates(lowerPanel.getCoordinates());
324
                query.setCoordinatesFilter(lowerPanel.getCoordinatesOption());
325
                query.setCoordinatesClicked(upperPanel.isRestrictAreaClicked());
326
                query.getOptions().getAspect().setGoTo(lowerPanel.isGoToClicked());
327
                query.getOptions().getAspect().setKeepOld(lowerPanel.isKeepOldClicked());
328
                query.getOptions().getAspect().setPaintCurrent(lowerPanel.isMarkedPlaceClicked());
329
                query.getOptions().getSearch().setWithAccents(lowerPanel.isAccentsSearchEnabled());
330
                query.setProperties(lowerPanel.getProperties());
331
                return query;
332
        } 
333
        
334
        /**
335
         * It returns the query that the user has selected
336
         * @throws Exception 
337
         * 
338
         */
339
        private void doSearch() throws Exception {        
340
                features = client.getFeature(doQuery());
341
                if (features == null) {
342
                        JOptionPane.showMessageDialog(this,
343
                                        Messages.getText("errorGetRecords"),
344
                                        "Error",
345
                                        JOptionPane.ERROR_MESSAGE);
346
                }
347
        } 
348

    
349
        /**
350
         * Show the results 
351
         */
352
        private void showResults() {        
353
                if (features.length == 0){
354
                        JOptionPane.showMessageDialog(this,
355
                                        Messages.getText("anyResult"),
356
                                        Messages.getText("gazetteer_search"),
357
                                        JOptionPane.INFORMATION_MESSAGE);
358
                }else{
359
                        showResultsActionPerformed(features);
360
                }
361
        } 
362

    
363
        /**
364
         * @param features 
365
         */
366
        protected void showResultsActionPerformed(Feature[] features) {        
367
                new ShowResultsDialog(client,
368
                                features,
369
                                lowerPanel.getResultsByPage(),
370
                                doQuery());
371
        } 
372

    
373
        /**
374
         * 
375
         * @param parent The parent to set.
376
         */
377
        public void setParent(JFrame parent) {        
378
                this.parent = parent;
379
        } 
380

    
381
        /**
382
         * This class is used to manage the searches.
383
         * It contains method to start and to stop a thread. It is
384
         * necessary to create because "stop" method (for the Thread class)
385
         * is deprecated.
386
         * 
387
         * 
388
         * @author Jorge Piera Llodra (piera_jor@gva.es)
389
         */
390
        private class searchThread implements Runnable {
391
                volatile Thread myThread = null;
392

    
393
                public  searchThread() {        
394
                        myThread = new Thread(this);
395
                        myThread.start();
396
                } 
397

    
398
                public void stop() {        
399
                        myThread.stop();
400
                } 
401

    
402
                /*
403
                 * (non-Javadoc)
404
                 * @see java.lang.Runnable#run()
405
                 */
406
                public void run() {        
407
                        try {
408
                                doSearch();
409
                        } catch (Exception e) {
410
                                e.printStackTrace();
411
                        }
412
                        if ((features != null) && (features.length >= 0)) {
413
                                showResults();
414
                        }
415
                        searchThreads.remove(this);
416
                        if (searchThreads.size() == 0){
417
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));       
418
                        }
419
                } 
420
        }
421

    
422
        public GazetteerQuery getQuery() {
423
                return query;
424
        }
425

    
426
        public String getCurrentServer() {
427
                return currentServer;
428
        }
429

    
430
        public void setCurrentServer(String currentServer) {
431
                this.currentServer = currentServer;
432
        }
433
}