Statistics
| Revision:

root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / ui / search / SearchDialogPanel.java @ 3229

History | View | Annotate | Download (11.1 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
*
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
9
*
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
* GNU General Public License for more details.
14
*
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
*
19
* For more information, contact:
20
*
21
*  Generalitat Valenciana
22
*   Conselleria d'Infraestructures i Transport
23
*   Av. Blasco Ib??ez, 50
24
*   46010 VALENCIA
25
*   SPAIN
26
*
27
*      +34 963862235
28
*   gvsig@gva.es
29
*      www.gvsig.gva.es
30
*
31
*    or
32
*
33
*   IVER T.I. S.A
34
*   Salamanca 50
35
*   46005 Valencia
36
*   Spain
37
*
38
*   +34 963163400
39
*   dac@iver.es
40
*/
41
package es.gva.cit.gazetteer.ui.search;
42

    
43

    
44

    
45
import es.gva.cit.catalogClient.metadataXML.XMLNode;
46
import es.gva.cit.catalogClient.traductor.ITranslator;
47
import es.gva.cit.catalogClient.traductor.Translator;
48
import es.gva.cit.gazetteer.GazetteerClient;
49
import es.gva.cit.gazetteer.querys.Feature;
50
import es.gva.cit.gazetteer.querys.Query;
51
import es.gva.cit.gazetteer.querys.ThesaurusName;
52
import es.gva.cit.gazetteer.ui.showResults.ShowResultsDialog;
53
import es.gva.cit.gazetteer.wfsg.parsers.WfsgDescribeFeatureTypeParser;
54

    
55
import java.awt.Dimension;
56
import java.awt.FlowLayout;
57
import java.awt.event.ActionEvent;
58
import java.awt.event.ActionListener;
59

    
60
import javax.swing.BoxLayout;
61
import javax.swing.JButton;
62
import javax.swing.JFrame;
63
import javax.swing.JOptionPane;
64
import javax.swing.JPanel;
65
import javax.swing.event.TreeSelectionEvent;
66
import javax.swing.event.TreeSelectionListener;
67

    
68

    
69
/**
70
 * @author Jorge Piera Llodra (piera_jor@gva.es)
71
 */
72
public class SearchDialogPanel extends JPanel implements ActionListener,TreeSelectionListener {
73
//  It is needed to close the frame
74
    private JFrame parent;
75
    
76
    //Panels
77
    protected JPanel ppalPanel = null;
78
    protected SearchUpperPanel upperPanel = null;
79
    protected SearchLowerPanel lowerPanel = null;
80
    protected JPanel buttonsPanel = null;
81

    
82
    //Buttons
83
    private JButton searchButton = null;
84
    private JButton closeButton = null;
85
    private JButton sizeButton = null;
86
    
87
    //Otros
88
    protected GazetteerClient client = null;
89
    protected XMLNode[] nodesRecords = null;
90
    protected ThesaurusName currentFeature = null;
91
    protected String attribute = null;
92
    protected boolean isMinimized = true;
93
    private ITranslator translator = null;
94
 
95
    /**
96
     * This method initializes
97
     *
98
     */
99
    public SearchDialogPanel(GazetteerClient client,ITranslator translator) {
100
        super();
101
        this.translator = translator;
102
        this.client = client;
103
        this.isMinimized = true;
104
        initialize();
105
       
106
    }
107

    
108
    /**
109
     * This method initializes this
110
     *
111
     * @return void
112
     */
113
    private void initialize() {
114
        ppalPanel = new JPanel();
115
        ppalPanel.setBounds(0, 0,625,330);
116
        ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));
117

    
118
        ppalPanel.add(getUpperPanel(), null);
119
        ppalPanel.add(getLowerPanel(), null);
120
        ppalPanel.add(getButtonPanel(), null);
121

    
122
        add(ppalPanel);
123
        
124
        getLowerPanel().setVisible(false);
125
        setDefaultButtonListeners();
126
    }
127
    
128
    /**
129
     * It Gets the upperPanel 
130
     * @return
131
     */
132
    
133
   public SearchUpperPanel getUpperPanel() {
134
       if (upperPanel == null){
135
            upperPanel = new SearchUpperPanel(translator);  
136
          
137
        }
138
        return upperPanel;
139
       
140
    }
141
   /**
142
    * It Gets the lowePanel 
143
    * @return
144
    */
145
   
146
  public SearchLowerPanel getLowerPanel() {
147
      if (lowerPanel == null){
148
           lowerPanel = new SearchLowerPanel(client.getLnkIGazetteerDriver().getVectorFeatures(),translator);  
149
           if (client.getProtocol().equals("IDEC"))
150
               lowerPanel.getJScrollThesauro().setEnabled(false);
151
      }
152
      return lowerPanel;
153
   }
154
   
155

    
156
    public void setCatalogClient(GazetteerClient catCli) {
157
        this.client = catCli;
158
    } 
159

    
160
    public JPanel getButtonPanel() {
161
        if (buttonsPanel == null) {
162
            buttonsPanel = new JPanel(new FlowLayout());
163
            buttonsPanel.add(getSearchButton());
164
            buttonsPanel.add(getCloseButton());
165
        }
166

    
167
        return buttonsPanel;
168
    }
169

    
170
    public JButton getSearchButton() {
171
        if (searchButton == null) {
172
            searchButton = new JButton(Translator.getText(translator,"searchButton"));
173
            searchButton.setSize(new Dimension(30, 20));
174
            searchButton.setActionCommand("search");
175
        }
176

    
177
        return searchButton;
178
    }
179
    
180
    public JButton getCloseButton() {
181
        if (closeButton == null) {
182
            closeButton = new JButton(Translator.getText(translator,"close"));
183
            closeButton.setSize(new Dimension(30, 20));
184
            closeButton.setActionCommand("close");
185
        }
186

    
187
        return closeButton;
188
    }   
189
    
190
    /**
191
     * It  gets the change size button
192
     * @return
193
     */
194
    public JButton getSizeButton() {
195
        if (sizeButton == null) {
196
            sizeButton = upperPanel.getSizeButton();
197
            sizeButton.setActionCommand("size");
198
        }
199
        return sizeButton;
200
    }  
201
    
202
    public ThesaurusName getFeatureSelected(){
203
        //return (Feature) controlsPanel.getTipoList().getSelectedValue();
204
        return lowerPanel.getType();
205
    }
206
    
207
    public void setDefaultButtonListeners() {
208
        getSearchButton().addActionListener(this);
209
        getCloseButton().addActionListener(this);
210
        getSizeButton().addActionListener(this);
211
        lowerPanel.getTipoList().addTreeSelectionListener(this);
212
    }
213
    
214
    /* (non-Javadoc)
215
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
216
     */
217
    public void actionPerformed(ActionEvent e) {
218
        //Buscar
219
        if (e.getActionCommand() == "search") {
220
            searchButtonActionPerformed();
221
        } 
222
        if (e.getActionCommand() == "close") {
223
            closeButtonActionPerformed();
224
        }
225
        if (e.getActionCommand() == "size") {
226
            sizeButtonActionPerformed();
227
        }   
228
    }
229
    
230
    protected void sizeButtonActionPerformed(){
231
        if (isMinimized){
232
            parent.setSize(525,450);
233
            getLowerPanel().setVisible(true);
234
            getLowerPanel().repaint();
235
            getUpperPanel().setUpIcon();
236
            parent.setVisible(true);
237
        }else{
238
            parent.setSize(525,115);
239
            getLowerPanel().setVisible(false);
240
            getUpperPanel().setDownIcon();
241
        }
242
        isMinimized = !isMinimized;
243
    }
244
    
245
    
246
    protected void searchButtonActionPerformed(){
247
        if ((currentFeature == null) && (client.getProtocol().equals("WFS-G"))){
248
            JOptionPane.showMessageDialog(
249
                    this,
250
                    "El protocolo WFS-G no permite hacer b?squedas si no se " +
251
                    "elige un elemento del tesauro",
252
                    "WFS-G",
253
                    JOptionPane.ERROR_MESSAGE
254
                    );
255
        }else{
256
            doSearch();
257

    
258
            if ((nodesRecords != null) && (nodesRecords.length > 0)) {
259
                showResults();
260
            }
261
        }
262
    }
263
    
264
    protected void closeButtonActionPerformed(){
265
        parent.setVisible(false);
266
    }
267

    
268
    private Query doQuery() {
269
       Query query =  new Query(upperPanel.getName(),
270
               lowerPanel.getConcordancia(),
271
               lowerPanel.getType(),
272
               getFieldAttribute(),
273
               lowerPanel.getNPaginas(),
274
               lowerPanel.getCoordenadas(),
275
               lowerPanel.getCoordenadasOpcion());
276
       query.setCoordinatesClicked(upperPanel.getRestrictAreaClicked());
277
       return query;
278
    }
279
    /**
280
     * it opens a window with a combo and returns a field attribute name
281
     * @return
282
     */
283
    private String getFieldAttribute(){
284
        if (!(client.getProtocol().equals("WFS-G")))
285
            return null;
286
        
287
        Object[] possibilities = new Object[currentFeature.getFields().length];
288
        for (int i=0 ; i< possibilities.length ; i++)
289
            possibilities[i] = currentFeature.getFields()[i].getName();
290
        
291
        String s = (String)JOptionPane.showInputDialog(
292
                            this,
293
                            "Elige un atributo para hacer la b?squeda",
294
                            "WFS-G",
295
                            JOptionPane.PLAIN_MESSAGE,
296
                            null,
297
                            possibilities,
298
                            currentFeature.getFields()[0].getName());
299

    
300
        if ((s != null) && (s.length() > 0)) {
301
            attribute = s;
302
            return s;
303
        }
304

    
305
        return null;
306
    }
307

    
308
    private void doSearch() {
309
        nodesRecords = client.getLnkIGazetteerDriver().getFeature(client.getUrl(),
310
                doQuery());
311

    
312
        if (nodesRecords == null) {
313
            JOptionPane.showMessageDialog(this,
314
                "Se ha producido un error al hacer el get records", "Error",
315
                JOptionPane.ERROR_MESSAGE);
316
        } 
317
        
318
    }
319

    
320
    private void showResults() {
321
        Feature[] features = client.getLnkIGazetteerDriver().parseFeatures(nodesRecords[0],currentFeature,attribute);
322
        
323
        if (features.length == 0){
324
            JOptionPane.showMessageDialog(this,
325
                    "La b?squeda no ha producido ning?n resultado", "B?squeda de Gazetteer",
326
                    JOptionPane.INFORMATION_MESSAGE);
327
        }else{
328
            ShowResultsActionPerformed(features);
329
        }
330
    }
331
    
332
    protected void ShowResultsActionPerformed(Feature[] features){
333
        new ShowResultsDialog(client,features,lowerPanel.getNPaginas());
334
    }
335
    
336
    
337
   
338
    /**
339
     * It loads the fields for the feature
340
     * @param feature
341
     */
342
    private void loadCurrentFeature(){
343
        XMLNode[] fields = client.getLnkIGazetteerDriver().describeFeatureType(client.getUrl(),
344
               currentFeature.getName());
345
       
346
        if (client.getProtocol().equals("WFS-G"))        
347
            new WfsgDescribeFeatureTypeParser().parse(fields[0],currentFeature);
348
             
349
    }
350

    
351
    /* (non-Javadoc)
352
     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
353
     */
354
    public void valueChanged(TreeSelectionEvent e) {
355
        if (currentFeature == null){
356
            currentFeature = getFeatureSelected();
357
            loadCurrentFeature();
358
       }else{
359
            ThesaurusName feature = getFeatureSelected();
360
            if (!(currentFeature.equals(feature))){
361
               if (feature != null){
362
                   currentFeature = feature;
363
                   loadCurrentFeature();
364
               }
365
            }
366
        }
367
        
368
    }
369

    
370
/**
371
 * @param parent The parent to set.
372
 */
373
public void setParent(JFrame parent) {
374
    this.parent = parent;
375
}
376
}