Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / ui / SearchDialogPanel.java @ 3081

History | View | Annotate | Download (9.33 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;
42

    
43

    
44

    
45
import es.gva.cit.catalogClient.metadataXML.XMLNode;
46
import es.gva.cit.gazetteer.GazetteerClient;
47
import es.gva.cit.gazetteer.parsers.wfsg.WfsgDescribeFeatureTypeParser;
48
import es.gva.cit.gazetteer.querys.Feature;
49
import es.gva.cit.gazetteer.querys.ThesaurusName;
50
import es.gva.cit.gazetteer.querys.Query;
51

    
52
import java.awt.Dimension;
53
import java.awt.FlowLayout;
54
import java.awt.event.ActionEvent;
55
import java.awt.event.ActionListener;
56

    
57
import javax.swing.BoxLayout;
58
import javax.swing.JButton;
59
import javax.swing.JFrame;
60
import javax.swing.JOptionPane;
61
import javax.swing.JPanel;
62
import javax.swing.event.TreeSelectionEvent;
63
import javax.swing.event.TreeSelectionListener;
64

    
65

    
66
/**
67
 * @author Jorge Piera Llodra (piera_jor@gva.es)
68
 */
69
public class SearchDialogPanel extends JPanel implements ActionListener,TreeSelectionListener {
70
//  It is needed to close the frame
71
    private JFrame parent;
72
    
73
    //Panels
74
    protected JPanel ppalPanel = null;
75
    protected SearchPanel controlsPanel = null;
76
    protected JPanel buttonsPanel = null;
77

    
78
    //Buttons
79
    private JButton searchButton = null;
80
    private JButton closeButton = null;
81

    
82
    //Otros
83
    protected GazetteerClient client = null;
84
    protected XMLNode[] nodesRecords = null;
85
    protected ThesaurusName currentFeature = null;
86
    protected String attribute = null;
87
 
88
    /**
89
     * This method initializes
90
     *
91
     */
92
    public SearchDialogPanel(GazetteerClient client) {
93
        super();
94
        this.client = client;
95
        initialize();
96
       
97
    }
98

    
99
    /**
100
     * This method initializes this
101
     *
102
     * @return void
103
     */
104
    private void initialize() {
105
        ppalPanel = new JPanel();
106
        ppalPanel.setLocation(0, 0);
107
        ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));
108

    
109
        ppalPanel.add(getControlsPanel(), null);
110
        ppalPanel.add(getButtonPanel(), null);
111

    
112
        add(ppalPanel);
113

    
114
        setDefaultButtonListeners();
115
    }
116

    
117
    public void setCatalogClient(GazetteerClient catCli) {
118
        this.client = catCli;
119
    }
120

    
121
    public JPanel getControlsPanel() {
122
        if (controlsPanel == null) {
123
            controlsPanel = new SearchPanel(client.getLnkIGazetteerDriver().getVectorFeatures());
124
            controlsPanel.setPreferredSize(new java.awt.Dimension(727, 455));
125
            controlsPanel.setLocation(0, 0);
126
            if (client.getProtocol().equals("IDEC"))
127
                controlsPanel.getJScrollThesauro().setEnabled(false);
128
        }
129

    
130
        return controlsPanel;
131
    }
132

    
133
    public JPanel getButtonPanel() {
134
        if (buttonsPanel == null) {
135
            buttonsPanel = new JPanel(new FlowLayout());
136
            buttonsPanel.add(getSearchButton());
137
            buttonsPanel.add(getCloseButton());
138
        }
139

    
140
        return buttonsPanel;
141
    }
142

    
143
    public JButton getSearchButton() {
144
        if (searchButton == null) {
145
            searchButton = new JButton("Buscar");
146
            searchButton.setSize(new Dimension(30, 20));
147
            searchButton.setActionCommand("Buscar");
148
        }
149

    
150
        return searchButton;
151
    }
152
    
153
    public JButton getCloseButton() {
154
        if (closeButton == null) {
155
            closeButton = new JButton("Cerrar");
156
            closeButton.setSize(new Dimension(30, 20));
157
            closeButton.setActionCommand("Cerrar");
158
        }
159

    
160
        return closeButton;
161
    }   
162
    
163
    public ThesaurusName getFeatureSelected(){
164
        //return (Feature) controlsPanel.getTipoList().getSelectedValue();
165
        return controlsPanel.getType();
166
    }
167
    
168
    public void setDefaultButtonListeners() {
169
        getSearchButton().addActionListener(this);
170
        getCloseButton().addActionListener(this);
171
        controlsPanel.getTipoList().addTreeSelectionListener(this);
172
    }
173

    
174
    /* (non-Javadoc)
175
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
176
     */
177
    public void actionPerformed(ActionEvent e) {
178
        //Buscar
179
        if (e.getActionCommand() == "Buscar") {
180
            searchButtonActionPerformed();
181
        } 
182
        if (e.getActionCommand() == "Cerrar") {
183
            closeButtonActionPerformed();
184
        }
185
        
186
        
187
    }
188
    
189
    protected void searchButtonActionPerformed(){
190
        if ((currentFeature == null) && (client.getProtocol().equals("WFS-G"))){
191
            JOptionPane.showMessageDialog(
192
                    this,
193
                    "El protocolo WFS-G no permite hacer b?squedas si no se " +
194
                    "elige un elemento del tesauro",
195
                    "WFS-G",
196
                    JOptionPane.ERROR_MESSAGE
197
                    );
198
        }else{
199
            doSearch();
200

    
201
            if ((nodesRecords != null) && (nodesRecords.length > 0)) {
202
                showResults();
203
            }
204
        }
205
    }
206
    
207
    protected void closeButtonActionPerformed(){
208
        parent.setVisible(false);
209
    }
210

    
211
    private Query doQuery() {
212
       return new Query(controlsPanel.getName(),
213
            controlsPanel.getConcordancia(),
214
            controlsPanel.getType(),
215
            getFieldAttribute(),
216
            controlsPanel.getNPaginas(),
217
            controlsPanel.getCoordenadas(),
218
            controlsPanel.getCoordenadasOpcion());
219
    }
220
    /**
221
     * it opens a window with a combo and returns a field attribute name
222
     * @return
223
     */
224
    private String getFieldAttribute(){
225
        if (!(client.getProtocol().equals("WFS-G")))
226
            return null;
227
        
228
        Object[] possibilities = new Object[currentFeature.getFields().length];
229
        for (int i=0 ; i< possibilities.length ; i++)
230
            possibilities[i] = currentFeature.getFields()[i].getName();
231
        
232
        String s = (String)JOptionPane.showInputDialog(
233
                            this,
234
                            "Elige un atributo para hacer la b?squeda",
235
                            "WFS-G",
236
                            JOptionPane.PLAIN_MESSAGE,
237
                            null,
238
                            possibilities,
239
                            currentFeature.getFields()[0].getName());
240

    
241
        if ((s != null) && (s.length() > 0)) {
242
            attribute = s;
243
            return s;
244
        }
245

    
246
        return null;
247
    }
248

    
249
    private void doSearch() {
250
        nodesRecords = client.getLnkIGazetteerDriver().getFeature(client.getUrl(),
251
                doQuery());
252

    
253
        if (nodesRecords == null) {
254
            JOptionPane.showMessageDialog(this,
255
                "Se ha producido un error al hacer el get records", "Error",
256
                JOptionPane.ERROR_MESSAGE);
257
        } 
258
        
259
    }
260

    
261
    private void showResults() {
262
        Feature[] features = client.getLnkIGazetteerDriver().parseFeatures(nodesRecords[0],currentFeature,attribute);
263
        
264
        if (features.length == 0){
265
            JOptionPane.showMessageDialog(this,
266
                    "La b?squeda no ha producido ning?n resultado", "B?squeda de Gazetteer",
267
                    JOptionPane.INFORMATION_MESSAGE);
268
        }else{
269
            ShowResultsActionPerformed(features);
270
        }
271
    }
272
    
273
    private void ShowResultsActionPerformed(Feature[] features){
274
        new ShowResultsDialog(client,features,controlsPanel.getNPaginas());
275
    }
276
    
277
    
278
   
279
    /**
280
     * It loads the fields for the feature
281
     * @param feature
282
     */
283
    private void loadCurrentFeature(){
284
        XMLNode[] fields = client.getLnkIGazetteerDriver().describeFeatureType(client.getUrl(),
285
               currentFeature.getName());
286
       
287
        if (client.getProtocol().equals("WFS-G"))        
288
            new WfsgDescribeFeatureTypeParser().parse(fields[0],currentFeature);
289
             
290
    }
291

    
292
    /* (non-Javadoc)
293
     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
294
     */
295
    public void valueChanged(TreeSelectionEvent e) {
296
        if (currentFeature == null){
297
            currentFeature = getFeatureSelected();
298
            loadCurrentFeature();
299
       }else{
300
            ThesaurusName feature = getFeatureSelected();
301
            if (!(currentFeature.equals(feature))){
302
               if (feature != null){
303
                   currentFeature = feature;
304
                   loadCurrentFeature();
305
               }
306
            }
307
        }
308
        
309
    }
310

    
311
/**
312
 * @param parent The parent to set.
313
 */
314
public void setParent(JFrame parent) {
315
    this.parent = parent;
316
}
317
}