Statistics
| Revision:

gvsig-gazetteer / trunk / appGazetteer / src / org / gvsig / gazetteer / ui / search / SearchDialogPanel.java @ 14

History | View | Annotate | Download (11.9 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.andami.IconThemeHelper;
56
import org.gvsig.catalog.ui.search.SearchButtonPanel;
57
import org.gvsig.catalog.utils.CatalogConstants;
58
import org.gvsig.gazetteer.GazetteerClient;
59
import org.gvsig.gazetteer.querys.Feature;
60
import org.gvsig.gazetteer.querys.FeatureType;
61
import org.gvsig.gazetteer.querys.FeatureTypeAttribute;
62
import org.gvsig.gazetteer.querys.GazetteerQuery;
63
import org.gvsig.gazetteer.ui.showresults.ShowResultsDialog;
64
import org.gvsig.i18n.Messages;
65

    
66

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

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

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

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

    
123
        } 
124

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

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

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

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

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

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

    
221

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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