Statistics
| Revision:

gvsig-catalog / trunk / appCatalog / src / org / gvsig / catalog / ui / serverconnect / ServerConnectDialogPanel.java @ 12

History | View | Annotate | Download (10.1 KB)

1

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

    
56
import javax.swing.Icon;
57
import javax.swing.ImageIcon;
58
import javax.swing.JFrame;
59
import javax.swing.JOptionPane;
60
import javax.swing.JPanel;
61

    
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

    
65
import org.gvsig.catalog.CatalogClient;
66
import org.gvsig.catalog.CatalogLocator;
67
import org.gvsig.catalog.CatalogManager;
68
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
69
import org.gvsig.catalog.drivers.ICatalogServiceDriver;
70
import org.gvsig.catalog.ui.search.SearchDialog;
71
import org.gvsig.catalog.ui.serverproperties.ServerPropertiesDialog;
72
import org.gvsig.catalog.utils.CatalogConstants;
73
import org.gvsig.i18n.Messages;
74
import org.gvsig.utils.swing.jcomboServer.ServerData;
75

    
76

    
77

    
78
/**
79
 * 
80
 * 
81
 * 
82
 * @author Jorge Piera Llodra (piera_jor@gva.es)
83
 */
84
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
85
    
86
    private static Logger logger =
87
        LoggerFactory.getLogger(ServerConnectDialogPanel.class);
88
    
89
        private static final CatalogManager catalogManager = CatalogLocator.getCatalogManager();
90
        private static final long serialVersionUID = 1224880378648403038L;
91
        private static TreeMap serverList = new TreeMap();
92
        private ServerConnectPanel controlsPanel = null;
93
        private JFrame parent = null;
94
        protected CatalogClient client = null;
95
        
96
        protected String serversFile = "servers"
97
            + File.separator+ "CatalogServers.txt";
98
        
99
        protected String currentServer = "";
100
        private ConnectThread connectThread = null;
101
        
102
        /**
103
         * Constructor
104
         * @param parent 
105
         */
106
        public  ServerConnectDialogPanel(JFrame parent) {
107
                this.parent = parent;
108
                this.setLayout(new BorderLayout());
109
                add(getControlsPanel(),BorderLayout.CENTER);
110
                //Loads the servers
111
                int n_servers = loadServerList(serversFile);
112

    
113
              //Load the protocols
114
        controlsPanel.loadDrivers(
115
                catalogManager.getDrivers());
116
        getControlsPanel().setPropertiesIcon(getPropertiesIcon());
117

    
118
                if (n_servers == 0) {
119
                    return;
120
                }
121
                
122
                //Load the first protocol
123
                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
124
                enableServerPropertiesButton();
125
        } 
126

    
127
        /**
128
         * @return the main panel
129
         */
130
        public ServerConnectPanel getControlsPanel() {        
131
                if (controlsPanel == null) {
132
                        controlsPanel = new ServerConnectPanel();
133
                        controlsPanel.addActionListener(this);
134
                        controlsPanel.enableSearchButton(false);
135
                }
136
                return controlsPanel;
137
        } 
138

    
139
        /**
140
         * It adds a server in the TreeMap Object
141
         * @param server 
142
         */
143
        protected static void addTreeMapServer(ServerData server) {        
144
                if (ServerConnectDialogPanel.serverList == null) {
145
                        ServerConnectDialogPanel.serverList = new TreeMap();
146
                }
147
                serverList.put(server.getServerAddress(), server);
148
        } 
149

    
150
        /**
151
         * This method loads a server list in the combo
152
         * @param sfile 
153
         */
154
        private int loadServerList(String sfile) {
155
            
156
                int n = loadServersFromFile(sfile);
157
                
158
                if (n == 0) {
159
                    return 0;
160
                }
161
                
162
                Iterator iter = serverList.keySet().iterator();
163
                while (iter.hasNext()) {
164
                        ServerData server = (ServerData) serverList.get((String) iter.next());
165
                        controlsPanel.addServer(server);
166
                }
167
                
168
                return n;
169
        } 
170

    
171
        /**
172
         * It loads a server list from a text file
173
         * @param sfile 
174
         * File that contains the rervers
175
         */
176
        private int loadServersFromFile(String sfile) {
177
            
178
            int servers_loaded = 0;
179
            
180
                File file = null;
181
                try {
182
                    URL uf = getClass().getClassLoader().getResource(sfile);
183

    
184
                        if (uf != null) {
185
                            file = new File(uf.getFile());
186
                            if (!file.exists()) {
187
                                throw new FileNotFoundException(file.getAbsolutePath());
188
                            }
189
                            FileReader _fr = new FileReader(file);
190
                                BufferedReader _br = new BufferedReader(_fr);
191
                                String s;
192
                                while ((s = _br.readLine()) != null) {
193
                                        addTreeMapServer(new ServerData(s,"",""));
194
                                        servers_loaded++;
195
                                }
196
                                _br.close();
197
                                _fr.close();
198
                        } else {
199
                            throw new FileNotFoundException(sfile);
200
                        }
201
                } catch (Exception e) {
202
                    
203
                    logger.info("Error while loading server list file: " + e.getMessage());
204
                    
205
            String tit = Messages.getText("_Loading_servers");
206
            String msg = Messages.getText("_Unable_to_load_server_list_file")
207
                + ": " + sfile;
208
            JOptionPane.showMessageDialog(
209
                this,
210
                msg, tit, JOptionPane.ERROR_MESSAGE);
211
                }
212
                
213
                return servers_loaded;
214
        } 
215

    
216
        /*
217
         * (non-Javadoc)
218
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
219
         */
220
        public void actionPerformed(ActionEvent e) {        
221
                if (e.getActionCommand().compareTo(CatalogConstants.CONNECT_BUTTON_ACTION_COMMAND)==0) {
222
                        connectButtonActionPerformed();
223
                }else if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
224
                        searchButtonActionPerformed();
225
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0) {
226
                        closeButtonActionPerformed();
227
                }else if (e.getActionCommand().compareTo(CatalogConstants.PROTOCOL_COMBO_ACTION_COMMAND)==0){
228
                        enableServerPropertiesButton();
229
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVERPROPERTIES_BUTTON_ACTION_COMMAND)==0) {
230
                        serverPropertiesButtonActionPerformed();
231
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVER_COMBO_ACTION_COMMAND)==0) {
232
                        controlsPanel.updateProtocol();
233
                }
234
        }
235

    
236
        /**
237
         * Enable the server properties button
238
         */
239
        private void enableServerPropertiesButton(){
240
                if(((ICatalogServiceDriver)controlsPanel.getDriver()).getProfile() == null){
241
                        controlsPanel.enableServerPropertiesButton(false);
242
                }else{
243
                        controlsPanel.enableServerPropertiesButton(true);
244
                }
245
        }
246
        
247
        /**
248
         * Action when the search button is clicked
249
         */
250
        protected void searchButtonActionPerformed() {        
251
                setEnabled(false);
252
                new SearchDialog(client,parent);
253
        } 
254

    
255
        /**
256
         * It is thrown the the server properties button is clicked
257
         */
258
        protected void serverPropertiesButtonActionPerformed(){
259
                createClient();
260
                new ServerPropertiesDialog(
261
                                controlsPanel.getServer(),
262
                                client,
263
                                ((ICatalogServiceDriver)controlsPanel.getDriver()).getProfile());
264
        }
265

    
266
        /**
267
         * It is thrown the the connect button is clicked
268
         */
269
        protected void connectButtonActionPerformed() {        
270
                controlsPanel.enableSearchButton(false);                
271
                createClient();                
272
                if (connectThread != null){
273
                        connectThread.stop();
274
                }
275
                connectThread = new ConnectThread();
276
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
277
        } 
278

    
279
        /**
280
         * Create the catalog client
281
         */
282
        protected void createClient(){
283
                ServerData serverData = null;
284
                if (client != null){
285
                        serverData = client.getServerData();
286
                }
287
                client = new CatalogClient(controlsPanel.getServerAddress(),
288
                                controlsPanel.getDatabase(),
289
                                (ICatalogServiceDriver)controlsPanel.getDriver());
290
                client.setServerData(serverData);
291
        }
292
        
293
        /**
294
         *  * It is thrown the the close button is clicked
295
         */
296
        protected void closeButtonActionPerformed() {        
297
                parent.setVisible(false);
298
                System.exit(0);
299
        } 
300

    
301
        /**
302
         * @return Returns the serversFile.
303
         */
304
        public String getServersFile() {        
305
                return serversFile;
306
        } 
307

    
308
        /**
309
         * @param serversFile The serversFile to set.
310
         */
311
        public void setServersFile(String srv_file) {        
312
                this.serversFile = srv_file;
313
        } 
314

    
315
        /**
316
         * @return Returns the currentServer.
317
         */
318
        public String getCurrentServer() {        
319
                return currentServer;
320
        } 
321

    
322
        /**
323
         * @return Returns the client.
324
         */
325
        public CatalogClient getClient() {        
326
                return client;
327
        } 
328
        
329
        protected Icon getPropertiesIcon(){
330
                return new ImageIcon("./gvSIG/extensiones/org.gvsig.catalog/images/serverProperties.png");
331
        }
332

    
333
        /**
334
         * This class is used to manage the searches.
335
         * It contains method to start and to stop a thread. It is
336
         * necessary to create because "stop" method (for the Thread class)
337
         * is deprecated.
338
         * 
339
         * 
340
         * @author Jorge Piera Llodra (piera_jor@gva.es)
341
         */
342
        private class ConnectThread implements Runnable {
343
                volatile Thread myThread = null;
344

    
345
                public  ConnectThread() {        
346
                        myThread = new Thread(this);
347
                        myThread.start();
348
                } 
349

    
350
                public void stop(){
351
                        myThread.stop();
352
                }
353
                /*
354
                 * (non-Javadoc)
355
                 * @see java.lang.Runnable#run()
356
                 */
357
                public void run() {        
358
                        try {
359
                                DiscoveryServiceCapabilities capabilities = client.getCapabilities();
360
                                if (capabilities.isAvailable()){
361
                                        controlsPanel.enableSearchButton(true);
362
                                        currentServer = controlsPanel.getServerAddress();
363
                                        searchButtonActionPerformed();                                
364
                                } 
365
                                controlsPanel.setServerReply(capabilities.getServerMessage());
366

    
367
                        } catch (Exception e) {
368
                                controlsPanel.setServerReply(Messages.getText(e.toString()));
369
                                logger.info("While connecting to server.", e);
370
                        }
371
                        
372
            setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 
373
                }
374
        }        
375
}