Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / ui / serverConnect / ServerConnectDialogPanel.java @ 3510

History | View | Annotate | Download (9.14 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.catalogClient.ui.serverConnect;
42

    
43
import es.gva.cit.catalogClient.CatalogClient;
44
import es.gva.cit.catalogClient.metadataXML.XMLNode;
45
import es.gva.cit.catalogClient.traductor.ITranslator;
46
import es.gva.cit.catalogClient.traductor.Translator;
47
import es.gva.cit.catalogClient.ui.search.SearchDialog;
48
import es.gva.cit.catalogClient.utils.comboServer.ServerData;
49

    
50
import java.awt.Dimension;
51
import java.awt.FlowLayout;
52
import java.awt.event.ActionEvent;
53
import java.awt.event.ActionListener;
54

    
55
import java.io.BufferedReader;
56
import java.io.File;
57
import java.io.FileNotFoundException;
58
import java.io.FileReader;
59
import java.io.IOException;
60

    
61
import java.util.Iterator;
62
import java.util.TreeMap;
63

    
64
import javax.swing.BoxLayout;
65
import javax.swing.JButton;
66
import javax.swing.JPanel;
67

    
68

    
69
/**
70
 * @author Jorge Piera Llodra (piera_jor@gva.es)
71
 */
72
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
73
    private static TreeMap serverList = new TreeMap();
74

    
75
    //Panels
76
    private JPanel ppalPanel = null;
77
    private ServerConnectPanel controlsPanel = null;
78
    private JPanel buttonsPanel = null;
79

    
80
    //Buttons
81
    private JButton connect = null;
82
    private JButton search = null;
83
 
84
    //Others
85
    private CatalogClient client = null;
86
  private String serversFile = "servers/CatalogServers.txt";
87
  private String currentServer = "";
88
    private ITranslator translator = null;
89
   
90

    
91
    public ServerConnectDialogPanel(ITranslator translator) {
92
                
93
        this.translator = translator;
94
        
95
        ppalPanel = new JPanel();
96
        ppalPanel.setLayout(new BoxLayout(ppalPanel, BoxLayout.Y_AXIS));
97

    
98
        ppalPanel.add(getControlsPanel(), null);
99
        ppalPanel.add(getButtonPanel(), null);
100
        
101
        add(ppalPanel);
102

    
103
        setDefaultButtonListeners();
104

    
105
        loadServerList(serversFile);
106
        
107
        
108
    }
109

    
110
    /**
111
     * Gets the panel where are all the controls
112
     * @return 
113
     * JPanel
114
     */
115
    public JPanel getControlsPanel() {
116
        if (controlsPanel == null) {
117
            controlsPanel = new ServerConnectPanel(translator);
118
            controlsPanel.setSize(625, 230);
119
            controlsPanel.getZ3950Button().addActionListener(this);
120
            controlsPanel.getSrwButton().addActionListener(this);
121
            controlsPanel.getCswButton().addActionListener(this);
122
            controlsPanel.getServidoresCombo().addActionListener(this);
123
        }
124

    
125
        return controlsPanel;
126
    }
127

    
128
    /**
129
     * Gets the panel where are all the buttons
130
     * @return
131
     * JPanel
132
     */   
133
    public JPanel getButtonPanel() {
134
        if (buttonsPanel == null) {
135
            buttonsPanel = new JPanel(new FlowLayout());
136
            buttonsPanel.add(getConnectButton());
137
            buttonsPanel.add(getSearchButton());
138
        }
139

    
140
        return buttonsPanel;
141
    }
142
    
143
    /**
144
     * Gets the Connect Button
145
     * @return
146
     * JButton
147
     */
148
    public JButton getConnectButton() {
149
        if (connect == null) {
150
            connect = new JButton(Translator.getText(translator,"connectButton"));
151
            connect.setSize(new Dimension(30, 20));
152
            connect.setActionCommand("Connect");
153
        }
154

    
155
        return connect;
156
    }
157

    
158
    /**
159
     * Gets the Serach Button
160
     * @return
161
     * JButton
162
     */
163
    public JButton getSearchButton() {
164
        if (search == null) {
165
            search = new JButton(Translator.getText(translator,"searchButton"));
166
            search.setSize(new Dimension(30, 20));
167
            search.setActionCommand("Search");
168
            search.setEnabled(false);
169
        }
170

    
171
        return search;
172
    }
173

    
174
    /**
175
     * It adds a server in the TreeMap Object
176
     * @param name
177
     */
178
    public static void addTreeMapServer(ServerData server) {
179
        if (ServerConnectDialogPanel.serverList == null) {
180
            ServerConnectDialogPanel.serverList = new TreeMap();
181
        }
182

    
183
        serverList.put(server.getServerAddress(), server);
184
      
185
    }
186

    
187
  /**
188
   * Sets the listeners
189
   */
190
    public void setDefaultButtonListeners() {
191
        getConnectButton().addActionListener(this);
192
        getSearchButton().addActionListener(this);
193
    }
194

    
195
    /**
196
     * This method loads a server list in the combo
197
     * @param sfile
198
     */
199
    public void loadServerList(String sfile) {
200
        loadServersFromFile(sfile);
201
        
202
        Iterator iter = serverList.keySet().iterator();
203
        while (iter.hasNext()) {
204
            ServerData server = (ServerData) serverList.get((String) iter.next());
205
            controlsPanel.getServerCombo().addServer(server);
206
        }            
207
    }  
208
    
209
    /**
210
     * It loads a server list from a text file
211
     * @param file
212
     */
213
    public void loadServersFromFile(String sfile){
214
        File file = null;
215
        try {
216
            file = new File(sfile);
217

    
218
            if (file.exists()) {
219
                BufferedReader fr = new BufferedReader(new FileReader(file));
220
                String s;
221

    
222
                while ((s = fr.readLine()) != null) {
223
                    addTreeMapServer(new ServerData(s,"",""));
224
                }
225
            } else {
226
                System.out.println("No se encuentra el fichero '" +
227
                    file.getPath() + "'");
228
            }
229
        } catch (FileNotFoundException e) {
230
            System.out.println("No se encuentra el fichero '" + file.getPath() +
231
                "'");
232
            //e.printStackTrace();
233
        } catch (IOException e) {
234
            System.out.println("Error de entrada salida en la lectura del fichero");
235
            //e.printStackTrace();
236
        }
237

    
238
    }
239

    
240
    /* (non-Javadoc)
241
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
242
     */
243
    public void actionPerformed(ActionEvent e) {
244
        //Connect
245
        if (e.getActionCommand() == "Connect") {
246
            connectButtonActionPerformed();
247
        }
248

    
249
        //BUSCAR
250
        if (e.getActionCommand() == "Search") {
251
            searchButtonActionPerformed(); 
252
        }
253

    
254
        if ((e.getActionCommand() == "Z39.50") ||
255
                (e.getActionCommand() == "SRU/SRW") ||
256
                (e.getActionCommand() == "CS-W") ||
257
                (e.getActionCommand() == "comboBoxChanged")) {
258
            search.setEnabled(false);
259
        }
260

    
261
        if (e.getActionCommand() == "Z39.50") {
262
            controlsPanel.getDbText().setEnabled(true);
263
        }
264

    
265
        if ((e.getActionCommand() == "SRU/SRW") ||
266
                (e.getActionCommand() == "CS-W")) {
267
            controlsPanel.getDbText().setEnabled(false);
268
        }
269
        
270
        if (e.getActionCommand() == "servidoresCombo"){
271
            try {
272
                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
273
            }catch(NullPointerException ex){
274
                //The server is not loaded 
275
            }
276
            
277
        }
278
        
279
    }
280
    
281
    /**
282
         * Search Button action
283
         */
284
        public void searchButtonActionPerformed() {
285
            setEnabled(false);
286
      new SearchDialog(client,true,"");
287
        }
288

    
289
    /**
290
     * This method try to connect with the server
291
     */
292
        private void connectButtonActionPerformed() {
293
        search.setEnabled(false);
294

    
295
        //Create a new atalogClient
296
        client = new CatalogClient(controlsPanel.getServerAddress(),
297
                controlsPanel.getProtocol(), controlsPanel.getDatabase());
298

    
299
        
300
        if (client.getCapabilities()){
301
            search.setEnabled(true);
302
            currentServer = controlsPanel.getServerAddress();
303
        }
304
        
305
        controlsPanel.setRespuesta(translator.getText(client.getServerStatus()));
306
        }
307

    
308
    /**
309
     * @return Returns the serversFile.
310
     */
311
    public String getServersFile() {
312
        return serversFile;
313
    }
314

    
315
    /**
316
     * @param serversFile The serversFile to set.
317
     */
318
    public void setServersFile(String serversFile) {
319
        this.serversFile = serversFile;
320
    }
321

    
322
    /**
323
     * @return Returns the currentServer.
324
     */
325
    public String getCurrentServer() {
326
        return currentServer;
327
    }
328

    
329
    /**
330
     * @return Returns the cliente.
331
     */
332
    public CatalogClient getClient() {
333
        return client;
334
    }
335

    
336

    
337
}