Statistics
| Revision:

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

History | View | Annotate | Download (8.73 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
import org.w3c.dom.Node;
45

    
46
import es.gva.cit.gazetteer.GazetteerClient;
47
import es.gva.cit.gazetteer.parsers.adl.AdlFeatureParser;
48
import es.gva.cit.gazetteer.parsers.wfsg.WfsgDescribeFeatureTypeParser;
49
import es.gva.cit.gazetteer.querys.Feature;
50
import es.gva.cit.gazetteer.querys.ThesaurusName;
51
import es.gva.cit.gazetteer.querys.Query;
52

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

    
58
import javax.swing.BoxLayout;
59
import javax.swing.JButton;
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
    //Panels
71
    protected JPanel ppalPanel = null;
72
    protected SearchPanel controlsPanel = null;
73
    protected JPanel buttonsPanel = null;
74

    
75
    //Buttons
76
    private JButton searchButton = null;
77
    private JButton closeButton = null;
78

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

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

    
106
        ppalPanel.add(getControlsPanel(), null);
107
        ppalPanel.add(getButtonPanel(), null);
108

    
109
        add(ppalPanel);
110

    
111
        setDefaultButtonListeners();
112
    }
113

    
114
    public void setCatalogClient(GazetteerClient catCli) {
115
        this.client = catCli;
116
    }
117

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

    
127
        return controlsPanel;
128
    }
129

    
130
    public JPanel getButtonPanel() {
131
        if (buttonsPanel == null) {
132
            buttonsPanel = new JPanel(new FlowLayout());
133
            buttonsPanel.add(getSearchButton());
134
            buttonsPanel.add(getCloseButton());
135
        }
136

    
137
        return buttonsPanel;
138
    }
139

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

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

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

    
171
    /* (non-Javadoc)
172
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
173
     */
174
    public void actionPerformed(ActionEvent e) {
175
        //Buscar
176
        if (e.getActionCommand() == "Buscar") {
177
            searchButtonActionPerformed();
178
        } 
179
        if (e.getActionCommand() == "Cerrar") {
180
            closeButtonActionPerformed();
181
        }
182
        
183
        
184
    }
185
    
186
    protected void searchButtonActionPerformed(){
187
        doSearch();
188

    
189
        if ((nodesRecords != null) && (nodesRecords.length > 0)) {
190
            showResults();
191
        }
192
    }
193
    
194
    protected void closeButtonActionPerformed(){
195
        this.setVisible(false);
196
    }
197

    
198
    public Query doQuery() {
199
       return new Query(controlsPanel.getName(),
200
            controlsPanel.getConcordancia(),
201
            controlsPanel.getType(),
202
            getFieldAttribute(),
203
            controlsPanel.getNPaginas(),
204
            controlsPanel.getCoordenadas(),
205
            controlsPanel.getCoordenadasOpcion());
206
    }
207
    /**
208
     * it opens a window with a combo and returns a field attribute name
209
     * @return
210
     */
211
    private String getFieldAttribute(){
212
        if (!(client.getProtocol().equals("WFS-G")))
213
            return null;
214
        
215
        Object[] possibilities = new Object[currentFeature.getFields().length];
216
        for (int i=0 ; i< possibilities.length ; i++)
217
            possibilities[i] = currentFeature.getFields()[i].getName();
218
        
219
        String s = (String)JOptionPane.showInputDialog(
220
                            this,
221
                            "Elige un atributo para hacer la b?squeda",
222
                            "WFS-G",
223
                            JOptionPane.PLAIN_MESSAGE,
224
                            null,
225
                            possibilities,
226
                            currentFeature.getFields()[0].getName());
227

    
228
        if ((s != null) && (s.length() > 0)) {
229
            attribute = s;
230
            return s;
231
        }
232

    
233
        return null;
234
    }
235

    
236
    public void doSearch() {
237
        nodesRecords = client.getLnkIGazetteerDriver().getFeature(client.getUrl(),
238
                doQuery());
239

    
240
        if (nodesRecords == null) {
241
            JOptionPane.showMessageDialog(this,
242
                "Se ha producido un error al hacer el get records", "Error",
243
                JOptionPane.ERROR_MESSAGE);
244
        } 
245
        
246
    }
247

    
248
    public void showResults() {
249
        Feature[] features = client.getLnkIGazetteerDriver().parseFeatures(nodesRecords[0],currentFeature,attribute);
250
        
251
        if (features.length == 0){
252
            JOptionPane.showMessageDialog(this,
253
                    "La b?squeda no ha producido ning?n resultado", "B?squeda de Gazetteer",
254
                    JOptionPane.INFORMATION_MESSAGE);
255
        }else{
256
            ShowResultsActionPerformed(features);
257
        }
258
    }
259
    
260
    public void ShowResultsActionPerformed(Feature[] features){
261
        new ShowResultsDialog(client,features,controlsPanel.getNPaginas());
262
    }
263
    
264
    
265
   
266
    /**
267
     * It loads the fields for the feature
268
     * @param feature
269
     */
270
    private void loadCurrentFeature(){
271
        Node[] fields = client.getLnkIGazetteerDriver().describeFeatureType(client.getUrl(),
272
               currentFeature.getName());
273
       
274
        if (client.getProtocol().equals("WFS-G"))        
275
            new WfsgDescribeFeatureTypeParser().parse(fields[0],currentFeature);
276
             
277
    }
278

    
279
    /* (non-Javadoc)
280
     * @see javax.swing.event.TreeSelectionListener#valueChanged(javax.swing.event.TreeSelectionEvent)
281
     */
282
    public void valueChanged(TreeSelectionEvent e) {
283
        if (currentFeature == null){
284
            currentFeature = getFeatureSelected();
285
            loadCurrentFeature();
286
       }else{
287
            ThesaurusName feature = getFeatureSelected();
288
            if (!(currentFeature.equals(feature))){
289
               if (feature != null){
290
                   currentFeature = feature;
291
                   loadCurrentFeature();
292
               }
293
            }
294
        }
295
        
296
    }
297

    
298
}