Statistics
| Revision:

root / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / gazetteer / ui / ServerConnectDialogPanel.java @ 3073

History | View | Annotate | Download (8.95 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
import es.gva.cit.catalogClient.metadataXML.XMLNode;
44
import es.gva.cit.gazetteer.GazetteerClient;
45

    
46
import java.awt.Dimension;
47
import java.awt.FlowLayout;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50

    
51
import java.io.BufferedReader;
52
import java.io.File;
53
import java.io.FileNotFoundException;
54
import java.io.FileReader;
55
import java.io.IOException;
56

    
57
import java.util.Iterator;
58
import java.util.TreeMap;
59

    
60
import javax.swing.BoxLayout;
61
import javax.swing.JButton;
62
import javax.swing.JPanel;
63

    
64

    
65
/**
66
 * @author Jorge Piera Llodra (piera_jor@gva.es)
67
 */
68
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
69
    private static TreeMap serverList = new TreeMap();
70

    
71
    //Panels
72
    JPanel ppalPanel = null;
73
    ServerConnectPanel controlsPanel = null;
74
    JPanel buttonsPanel = null;
75

    
76
    //Buttons
77
    JButton connect = null;
78
    JButton search = null;
79

    
80
    //Others
81
    protected GazetteerClient client = null;
82
    protected String serversFile = "GazServers.txt";
83
    protected String currentServer = "";
84

    
85
    public ServerConnectDialogPanel() {
86
        ppalPanel = new JPanel();
87
        ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));
88

    
89
        ppalPanel.add(getControlsPanel(), null);
90
        ppalPanel.add(getButtonPanel(), null);
91

    
92
        add(ppalPanel);
93

    
94
        setDefaultButtonListeners();
95

    
96
        //Loads the servers
97
        loadServerList(serversFile);
98
    }
99

    
100
    public JPanel getControlsPanel() {
101
        if (controlsPanel == null) {
102
            controlsPanel = new ServerConnectPanel();
103
            controlsPanel.getWFSGButton().addActionListener(this);
104
            controlsPanel.getADLButton().addActionListener(this);
105
            controlsPanel.getServidoresCombo().addActionListener(this);
106
        }
107

    
108
        return controlsPanel;
109
    }
110

    
111
    public JPanel getButtonPanel() {
112
        if (buttonsPanel == null) {
113
            buttonsPanel = new JPanel(new FlowLayout());
114
            buttonsPanel.add(getConnectButton());
115
            buttonsPanel.add(getSearchButton());
116
        }
117

    
118
        return buttonsPanel;
119
    }
120

    
121
    public JButton getConnectButton() {
122
        if (connect == null) {
123
            connect = new JButton("Conectar");
124
            connect.setSize(new Dimension(30, 20));
125
            connect.setActionCommand("Connect");
126
        }
127

    
128
        return connect;
129
    }
130

    
131
    public JButton getSearchButton() {
132
        if (search == null) {
133
            search = new JButton("Buscar");
134
            search.setSize(new Dimension(30, 20));
135
            search.setActionCommand("Search");
136
            search.setEnabled(false);
137
        }
138

    
139
        return search;
140
    }
141

    
142
    public static void addServer(String name) {
143
        if (ServerConnectDialogPanel.serverList == null) {
144
            ServerConnectDialogPanel.serverList = new TreeMap();
145
        }
146

    
147
        if (!ServerConnectDialogPanel.serverList.containsKey(name)) {
148
            ServerConnectDialogPanel.serverList.put(name, name);
149
        }
150
    }
151

    
152
    public void setDefaultButtonListeners() {
153
        getConnectButton().addActionListener(this);
154
        getSearchButton().addActionListener(this);
155
    }
156

    
157
    public void loadServerList(String sfile) {
158
        File file = null;
159

    
160
        try {
161
            file = new File(sfile);
162

    
163
            // Cargo el fichero si existe
164
            if (file.exists()) {
165
                BufferedReader fr = new BufferedReader(new FileReader(file));
166
                String s;
167

    
168
                while ((s = fr.readLine()) != null) {
169
                    ServerConnectDialogPanel.addServer(s);
170
                }
171
            } else {
172
                System.out.println("No se encuentra el fichero '" +
173
                    file.getPath() + "'");
174
            }
175

    
176
            // Si hay servers en el TreeMap los cargo en el Combo
177
            Iterator iter = ServerConnectDialogPanel.serverList.keySet()
178
                                                               .iterator();
179

    
180
            while (iter.hasNext()) {
181
                controlsPanel.getServerCombo().addItem((String) iter.next());
182
            }
183
        } catch (FileNotFoundException e) {
184
            // TODO Auto-generated catch block
185
            System.out.println("No se encuentra el fichero '" + file.getPath() +
186
                "'");
187
            e.printStackTrace();
188
        } catch (IOException e) {
189
            // TODO Auto-generated catch block
190
            e.printStackTrace();
191
        }
192
    }
193

    
194
    /* (non-Javadoc)
195
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
196
     */
197
    public void actionPerformed(ActionEvent e) {
198
        //Connect
199
        if (e.getActionCommand() == "Connect") {
200
           connectButtonActionPerformed();
201
        }
202

    
203
        //BUSCAR
204
        if (e.getActionCommand() == "Search") {
205
            searchButtonActionPerformed();
206
        }
207

    
208
        if ((e.getActionCommand() == "WFS-G") ||
209
                (e.getActionCommand() == "ADL") ||
210
                (e.getActionCommand() == "comboBoxChanged")) {
211
            search.setEnabled(false);
212
        }
213

    
214
              
215
    }
216
    public void searchButtonActionPerformed(){
217
        setEnabled(false);
218
        SearchDialog frame = new SearchDialog(client);
219
    }
220
    
221
    public void connectButtonActionPerformed(){
222
        doConectar();
223
    }
224

    
225
    private void doConectar() {
226
        search.setEnabled(false);
227

    
228
        //Create a new atalogClient
229
        client = new GazetteerClient(controlsPanel.getServer(),
230
                controlsPanel.getProtocol());
231

    
232
        String msg = "";
233

    
234
        //try to connect
235
        if (!client.serverReady()) {
236
            msg = "No se encuentra el servidor";
237
        } else if (!client.getLnkIGazetteerDriver().isProtocolSupported(client.getUrl())) {
238
            msg = "El servidor No soporta el protocolo especificado";
239
        } else {
240
            //getCapabilities
241
            XMLNode[] nodesCapabilities = client.getLnkIGazetteerDriver()
242
                                              .getCapabilities(client.getUrl());
243

    
244
            if (nodesCapabilities == null) {
245
                msg = "Error al hacer un GetCapabilities." +
246
                    "Esto puede ser debido a dos razones: " +
247
                    "O bien ha habido un error al intentar " +
248
                    "hacer la operaci?n, o bien el servidor " +
249
                    "no soporta el protocolo especificado";
250
            } else {
251
                //Configure the client
252
                if (!client.getLnkIGazetteerDriver().setParameters(nodesCapabilities)) {
253
                    if (!(client.getLnkIGazetteerDriver()
254
                                     .getServerAnswerReady().equals(""))) {
255
                        msg = client.getLnkIGazetteerDriver()
256
                                     .getServerAnswerReady();
257
                    } else {
258
                        msg = "Error al procesar la respuesta. " +
259
                            "Se ha encontrado el servidor, pero posiblemente" +
260
                            " no soporta el protocolo especificado";
261
                    }
262
                } else {
263
                    //Show the answer
264
                    msg = client.getLnkIGazetteerDriver()
265
                                 .getServerAnswerReady();
266

    
267
                    search.setEnabled(true);
268
                    currentServer = controlsPanel.getServer();
269
                }
270
            }
271
        }
272
        controlsPanel.setRespuesta(msg);
273
    }
274

    
275
    /**
276
     * @return Returns the serversFile.
277
     */
278
    public String getServersFile() {
279
        return serversFile;
280
    }
281

    
282
    /**
283
     * @param serversFile The serversFile to set.
284
     */
285
    public void setServersFile(String serversFile) {
286
        this.serversFile = serversFile;
287
    }
288

    
289
    /**
290
     * @return Returns the currentServer.
291
     */
292
    public String getCurrentServer() {
293
        return currentServer;
294
    }
295

    
296
    /**
297
     * @return Returns the client.
298
     */
299
    public GazetteerClient getCliente() {
300
        return client;
301
    }
302
}