Statistics
| Revision:

root / branches / CatalogYNomenclator_v1_1_0_1005 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / ui / search / SearchDialogPanel.java @ 12552

History | View | Annotate | Download (11 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.catalogClient.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.JFrame;
50
import javax.swing.JOptionPane;
51
import javax.swing.JPanel;
52

    
53
import org.gvsig.i18n.Messages;
54

    
55
import es.gva.cit.catalogClient.CatalogClient;
56
import es.gva.cit.catalogClient.querys.MetadataSearch;
57
import es.gva.cit.catalogClient.querys.Query;
58
import es.gva.cit.catalogClient.querys.ServicesSearch;
59
import es.gva.cit.catalogClient.schemas.discoverer.Record;
60
import es.gva.cit.catalogClient.ui.showresults.ShowResultsDialog;
61
import es.gva.cit.catalogClient.utils.CatalogConstants;
62
import es.gva.cit.gazetteer.querys.Feature;
63

    
64
/**
65
 * @author Jorge Piera Llodra (piera_jor@gva.es)
66
 */
67
public class SearchDialogPanel extends JPanel implements ActionListener {
68
        private JFrame parent;
69
        private Query query = null;
70
        protected Object serverConnectFrame;
71
        protected SearchUpperPanel upperPanel = null;
72
        protected SearchLowerPanel lowerPanel = null;
73
        protected SearchButtonPanel buttonsPanel = null;
74
        protected CatalogClient client = null;
75
        protected boolean isMinimized = true;
76
        private Collection searchThreads = null;
77
        private String currentServer = null;
78
        private Record[] records = null;
79
        
80
        /**
81
         * This method initializes
82
         * @param client 
83
         * @param translator 
84
         */
85
        public  SearchDialogPanel(CatalogClient client, Object serverConnectFrame) {        
86
                super();
87
                searchThreads = new java.util.ArrayList();
88
                this.client = client;
89
                this.isMinimized = true;
90
                this.serverConnectFrame = serverConnectFrame;
91
                initialize();
92

    
93
        } 
94

    
95
        /**
96
         * This method initializes this
97
         */
98
        private void initialize() {        
99
                setLayout(new BorderLayout());
100
                add(getUpperPanel(),BorderLayout.NORTH);
101
                add(getLowerPanel(),BorderLayout.CENTER);
102
                add(getButtonPanel(),BorderLayout.SOUTH);
103
                getLowerPanel().setVisible(false);
104
        } 
105

    
106
        /**
107
         * It Gets the upperPanel
108
         * @return 
109
         */
110
        public SearchUpperPanel getUpperPanel() {        
111
                if (upperPanel == null){
112
                        upperPanel = new SearchUpperPanel(); 
113
                        upperPanel.addActionListener(this);
114
                }
115
                return upperPanel;
116

    
117
        } 
118

    
119
        /**
120
         * It Gets the lowePanel
121
         * @return 
122
         */
123
        public SearchLowerPanel getLowerPanel() {        
124
                if (lowerPanel == null){
125
                        lowerPanel = new SearchLowerPanel();
126
                        lowerPanel.addCoordinatesRelationship(
127
                                        Messages.getText("coordinatesContains"));
128
                        lowerPanel.addCoordinatesRelationship(
129
                                        Messages.getText("coordinatesFullyOutsideOf"));
130
                        addCathegories();
131
                        addScales();
132
                }
133
                return lowerPanel;
134
        } 
135

    
136
        /**
137
         * Add the services
138
         */
139
        private void addServices(){
140
              upperPanel.addService(new MetadataSearch(Messages.getText("data")));
141
              upperPanel.addService(new ServicesSearch(Messages.getText("services")));
142
        }
143
        
144
        /**
145
         * Adds the default cathegories
146
         */
147
        private void addCathegories(){
148
                lowerPanel.addCathegory(Messages.getText("cathegoryAny"));
149
                lowerPanel.addCathegory(Messages.getText("cathegoryBiota"));
150
                lowerPanel.addCathegory(Messages.getText("cathegoryBoundaries"));
151
                lowerPanel.addCathegory(Messages.getText("cathegoryClimatologyMeteorologyAtmosphere"));
152
                lowerPanel.addCathegory(Messages.getText("cathegoryEconomy"));
153
                lowerPanel.addCathegory(Messages.getText("cathegoryElevation"));
154
                lowerPanel.addCathegory(Messages.getText("cathegoryEnvironment"));
155
                lowerPanel.addCathegory(Messages.getText("cathegoryFarming"));
156
                lowerPanel.addCathegory(Messages.getText("cathegoryGeoscientificInformation"));
157
                lowerPanel.addCathegory(Messages.getText("cathegoryHealth"));
158
                lowerPanel.addCathegory(Messages.getText("cathegoryImageryBaseMapsEarthCover"));
159
                lowerPanel.addCathegory(Messages.getText("cathegoryInlandWaters"));
160
                lowerPanel.addCathegory(Messages.getText("cathegoryIntelligenceMilitary"));
161
                lowerPanel.addCathegory(Messages.getText("cathegoryLocation"));
162
                lowerPanel.addCathegory(Messages.getText("cathegoryOceans"));
163
                lowerPanel.addCathegory(Messages.getText("cathegoryPlanningCadastre"));
164
                lowerPanel.addCathegory(Messages.getText("cathegorySociety"));
165
                lowerPanel.addCathegory(Messages.getText("cathegoryStructure"));
166
                lowerPanel.addCathegory(Messages.getText("cathegoryTransportation"));
167
                lowerPanel.addCathegory(Messages.getText("cathegoryUtilitiesCommunication"));
168
        }
169

    
170
        /**
171
         * Adds the default scales
172
         */
173
        private void addScales(){
174
                lowerPanel.addScale(Messages.getText("scaleAny"));
175
                lowerPanel.addScale(Messages.getText("scaleI"));
176
                lowerPanel.addScale(Messages.getText("scaleII"));
177
                lowerPanel.addScale(Messages.getText("scaleIII"));
178
                lowerPanel.addScale(Messages.getText("scaleIV"));
179
                lowerPanel.addScale(Messages.getText("scaleV"));
180
                lowerPanel.addScale(Messages.getText("scaleVI"));
181
        }
182

    
183
        /**
184
         * @return the buttons panel
185
         */
186
        public JPanel getButtonPanel() {        
187
                if (buttonsPanel == null) {
188
                        buttonsPanel = new SearchButtonPanel();
189
                        buttonsPanel.addActionListener(this);                        
190
                }
191
                return buttonsPanel;
192
        }         
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
197
         */        
198
        public void actionPerformed(ActionEvent e) {        
199
                if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
200
                        searchButtonActionPerformed();
201
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0){
202
                        closeButtonActionPerformed();
203
                }else if(e.getActionCommand().compareTo(CatalogConstants.RESIZE_BUTTON_ACTION_COMMAND)==0){
204
                        resizeButtonActionPerformed();
205
                }else if (e.getActionCommand().compareTo(CatalogConstants.CANCEL_BUTTON_ACTION_COMMAND)==0){
206
                        cancelSearchesButtonActionPerformed();
207
                }else if (e.getActionCommand().compareTo(CatalogConstants.LAST_BUTTON_ACTION_COMMAND)==0){
208
                        lastButtonActionPerformed();
209
                }
210
        } 
211

    
212
        /**
213
         * thrown when the resize button is clicked
214
         */
215
        protected void resizeButtonActionPerformed() {        
216
                if (isMinimized){
217
                        parent.setSize(parent.getWidth(),487);
218
                        getLowerPanel().setVisible(true);
219
                        getUpperPanel().setUpIcon();
220
                }else{
221
                        parent.setSize(parent.getWidth(),160);
222
                        getLowerPanel().setVisible(false);                        
223
                        getUpperPanel().setDownIcon();
224
                }
225
                isMinimized = !isMinimized;
226
        } 
227

    
228
        /**
229
         * thrown when the search button is clicked
230
         * @throws Exception 
231
         */
232
        protected void searchButtonActionPerformed() {        
233
                SearchThread st =  new SearchThread();
234
                searchThreads.add(st);   
235
                setCursor(new Cursor(Cursor.WAIT_CURSOR));       
236
        } 
237
        
238

    
239
        /**
240
         * thrown when the last button is clicked
241
         */
242
        protected void lastButtonActionPerformed() {        
243
                ((JFrame)serverConnectFrame).setVisible(true);
244
                parent.setVisible(false);
245
        }         
246

    
247
        /**
248
         * thrown when the cancel button is clicked
249
         */
250
        protected void cancelSearchesButtonActionPerformed() {        
251
                for (int i=0 ; i<searchThreads.size() ; i++){
252
                        SearchThread st = (SearchThread)searchThreads.toArray()[i];
253
                        st.stop();            
254
                }     
255
                searchThreads.clear();
256
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));        
257
        } 
258

    
259
        /**
260
         * thrown when the close button is clicked
261
         */
262
        protected void closeButtonActionPerformed() {        
263
                parent.setVisible(false);
264
        } 
265

    
266
        /**
267
         * @return the selected query
268
         */
269
        protected Query doQuery() {        
270
                Query query = client.createNewQuery();
271
                query.setService(upperPanel.getService());
272
                query.setTitle(upperPanel.getTitle());
273
                query.setTitleFilter(lowerPanel.getTitleOption());
274
                query.setAbstract(lowerPanel.getAbstract());
275
                query.setThemeKey(lowerPanel.getKeys());
276
                query.setTopic(lowerPanel.getCathegory());
277
                query.setScale(lowerPanel.getScale());
278
                query.setProvider(lowerPanel.getProvider());
279
                query.setDateFrom(lowerPanel.getDateFrom());
280
                query.setDateTo(lowerPanel.getDateTo());
281
                query.setCoordenates(lowerPanel.getCoordinates());
282
                query.setCoordenatesFilter(lowerPanel.getCoordinatesOption());
283
                query.setMinimized(isMinimized);
284
                query.setCoordinatesClicked(upperPanel.getRestrictAreaClicked());
285
                return query;
286
        } 
287

    
288
        /**
289
         * It returns the query that the user has selected
290
         * @throws Exception 
291
         * 
292
         */
293
        private void doSearch() throws Exception {        
294
                records = client.getRecords(doQuery(), 1);
295
                if (records == null) {
296
                        JOptionPane.showMessageDialog(this,
297
                                        Messages.getText("errorGetRecords"),
298
                                        "Error",
299
                                        JOptionPane.ERROR_MESSAGE);
300
                } else if (records.length == 1) {
301
                        JOptionPane.showMessageDialog(this,
302
                                        Messages.getText("anyResult"),
303
                                        Messages.getText("catalog_search"),
304
                                        JOptionPane.INFORMATION_MESSAGE);
305
                }
306
        } 
307

    
308
        /**
309
         * Show the results 
310
         */
311
        private void showResults() {        
312
                new ShowResultsDialog(client, records, 1);
313
        } 
314

    
315
        /**
316
         * @param features 
317
         */
318
        protected void showResultsActionPerformed(Feature[] features) {        
319
                new ShowResultsDialog(client,
320
                                records,
321
                                10);                                
322
        } 
323

    
324
        /**
325
         * 
326
         * @param parent The parent to set.
327
         */
328
        public void setParent(JFrame parent) {        
329
                this.parent = parent;
330
        } 
331

    
332
        /**
333
         * This class is used to manage the searches.
334
         * It contains method to start and to stop a thread. It is
335
         * necessary to create because "stop" method (for the Thread class)
336
         * is deprecated.
337
         * 
338
         * 
339
         * @author Jorge Piera Llodra (piera_jor@gva.es)
340
         */
341
        private class SearchThread implements Runnable {
342
                volatile Thread myThread = null;
343

    
344
                public  SearchThread() {        
345
                        myThread = new Thread(this);
346
                        myThread.start();
347
                } 
348

    
349
                public void stop() {        
350
                        myThread.stop();
351
                } 
352

    
353
                /*
354
                 * (non-Javadoc)
355
                 * @see java.lang.Runnable#run()
356
                 */
357
                public void run() {        
358
                        try {
359
                                doSearch();
360
                        } catch (Exception e) {
361
                                e.printStackTrace();
362
                        }
363
                        if ((records != null) && (records.length > 0)) {
364
                                showResults();
365
                        }
366
                        searchThreads.remove(this);
367
                        if (searchThreads.size() == 0){
368
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));       
369
                        }
370
                } 
371
        }
372

    
373
        public Query getQuery() {
374
                return query;
375
        }
376

    
377
        public String getCurrentServer() {
378
                return currentServer;
379
        }
380

    
381
        public void setCurrentServer(String currentServer) {
382
                this.currentServer = currentServer;
383
        }
384
}