Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appCatalog / src / org / gvsig / catalog / ui / serverconnect / ServerConnectDialogPanel.java @ 28434

History | View | Annotate | Download (9.26 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.util.Iterator;
53
import java.util.TreeMap;
54

    
55
import javax.swing.Icon;
56
import javax.swing.ImageIcon;
57
import javax.swing.JFrame;
58
import javax.swing.JPanel;
59

    
60
import org.gvsig.catalog.CatalogClient;
61
import org.gvsig.catalog.CatalogLocator;
62
import org.gvsig.catalog.CatalogManager;
63
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
64
import org.gvsig.catalog.drivers.ICatalogServiceDriver;
65
import org.gvsig.catalog.ui.search.SearchDialog;
66
import org.gvsig.catalog.ui.serverproperties.ServerPropertiesDialog;
67
import org.gvsig.catalog.utils.CatalogConstants;
68
import org.gvsig.i18n.Messages;
69

    
70
import com.iver.utiles.swing.jcomboServer.ServerData;
71

    
72

    
73
/**
74
 * 
75
 * 
76
 * 
77
 * @author Jorge Piera Llodra (piera_jor@gva.es)
78
 */
79
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
80
        private static final CatalogManager catalogManager = CatalogLocator.getCatalogManager();
81
        private static final long serialVersionUID = 1224880378648403038L;
82
        private static TreeMap serverList = new TreeMap();
83
        private ServerConnectPanel controlsPanel = null;
84
        private JFrame parent = null;
85
        protected CatalogClient client = null;
86
        protected String serversFile = "servers/CatalogServers.txt";
87
        protected String currentServer = "";
88
        private ConnectThread connectThread = null;
89
        
90
        /**
91
         * Constructor
92
         * @param parent 
93
         */
94
        public  ServerConnectDialogPanel(JFrame parent) {
95
                this.parent = parent;
96
                this.setLayout(new BorderLayout());
97
                add(getControlsPanel(),BorderLayout.CENTER);
98
                //Loads the servers
99
                loadServerList(serversFile);
100
                //Load the protocols
101
                controlsPanel.loadDrivers(
102
                                catalogManager.getDrivers());
103
                //Load the first protocol
104
                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
105
                enableServerPropertiesButton();
106
                getControlsPanel().setPropertiesIcon(getPropertiesIcon());
107
        } 
108

    
109
        /**
110
         * @return the main panel
111
         */
112
        public ServerConnectPanel getControlsPanel() {        
113
                if (controlsPanel == null) {
114
                        controlsPanel = new ServerConnectPanel();
115
                        controlsPanel.addActionListener(this);
116
                        controlsPanel.enableSearchButton(false);
117
                }
118
                return controlsPanel;
119
        } 
120

    
121
        /**
122
         * It adds a server in the TreeMap Object
123
         * @param server 
124
         */
125
        protected static void addTreeMapServer(ServerData server) {        
126
                if (ServerConnectDialogPanel.serverList == null) {
127
                        ServerConnectDialogPanel.serverList = new TreeMap();
128
                }
129
                serverList.put(server.getServerAddress(), server);
130
        } 
131

    
132
        /**
133
         * This method loads a server list in the combo
134
         * @param sfile 
135
         */
136
        private void loadServerList(String sfile) {        
137
                loadServersFromFile(sfile);
138
                Iterator iter = serverList.keySet().iterator();
139
                while (iter.hasNext()) {
140
                        ServerData server = (ServerData) serverList.get((String) iter.next());
141
                        controlsPanel.addServer(server);
142
                }            
143
        } 
144

    
145
        /**
146
         * It loads a server list from a text file
147
         * @param sfile 
148
         * File that contains the rervers
149
         */
150
        private void loadServersFromFile(String sfile) {        
151
                File file = null;
152
                try {
153
                        file = new File(sfile);
154
                        if (file.exists()) {
155
                                BufferedReader fr = new BufferedReader(new FileReader(file));
156
                                String s;
157
                                while ((s = fr.readLine()) != null) {
158
                                        addTreeMapServer(new ServerData(s,"",""));
159
                                }
160
                        } else {
161
                                System.out.println("No se encuentra el fichero '" +
162
                                                file.getPath() + "'");
163
                        }
164
                } catch (FileNotFoundException e) {
165
                        System.out.println("No se encuentra el fichero '" + file.getPath() +
166
                        "'");
167
                        //e.printStackTrace();
168
                } catch (IOException e) {
169
                        System.out.println("Error de entrada salida en la lectura del fichero");
170
                        //e.printStackTrace();
171
                }
172
        } 
173

    
174
        /*
175
         * (non-Javadoc)
176
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
177
         */
178
        public void actionPerformed(ActionEvent e) {        
179
                if (e.getActionCommand().compareTo(CatalogConstants.CONNECT_BUTTON_ACTION_COMMAND)==0) {
180
                        connectButtonActionPerformed();
181
                }else if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
182
                        searchButtonActionPerformed();
183
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0) {
184
                        closeButtonActionPerformed();
185
                }else if (e.getActionCommand().compareTo(CatalogConstants.PROTOCOL_COMBO_ACTION_COMMAND)==0){
186
                        enableServerPropertiesButton();
187
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVERPROPERTIES_BUTTON_ACTION_COMMAND)==0) {
188
                        serverPropertiesButtonActionPerformed();
189
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVER_COMBO_ACTION_COMMAND)==0) {
190
                        controlsPanel.updateProtocol();
191
                }
192
        }
193

    
194
        /**
195
         * Enable the server properties button
196
         */
197
        private void enableServerPropertiesButton(){
198
                if(((ICatalogServiceDriver)controlsPanel.getDriver()).getProfile() == null){
199
                        controlsPanel.enableServerPropertiesButton(false);
200
                }else{
201
                        controlsPanel.enableServerPropertiesButton(true);
202
                }
203
        }
204
        
205
        /**
206
         * Action when the search button is clicked
207
         */
208
        protected void searchButtonActionPerformed() {        
209
                setEnabled(false);
210
                new SearchDialog(client,parent);
211
        } 
212

    
213
        /**
214
         * It is thrown the the server properties button is clicked
215
         */
216
        protected void serverPropertiesButtonActionPerformed(){
217
                createClient();
218
                new ServerPropertiesDialog(
219
                                controlsPanel.getServer(),
220
                                client,
221
                                ((ICatalogServiceDriver)controlsPanel.getDriver()).getProfile());
222
        }
223

    
224
        /**
225
         * It is thrown the the connect button is clicked
226
         */
227
        protected void connectButtonActionPerformed() {        
228
                controlsPanel.enableSearchButton(false);                
229
                createClient();                
230
                if (connectThread != null){
231
                        connectThread.stop();
232
                }
233
                connectThread = new ConnectThread();
234
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
235
        } 
236

    
237
        /**
238
         * Create the catalog client
239
         */
240
        protected void createClient(){
241
                ServerData serverData = null;
242
                if (client != null){
243
                        serverData = client.getServerData();
244
                }
245
                client = new CatalogClient(controlsPanel.getServerAddress(),
246
                                controlsPanel.getDatabase(),
247
                                (ICatalogServiceDriver)controlsPanel.getDriver());
248
                client.setServerData(serverData);
249
        }
250
        
251
        /**
252
         *  * It is thrown the the close button is clicked
253
         */
254
        protected void closeButtonActionPerformed() {        
255
                parent.setVisible(false);
256
                System.exit(0);
257
        } 
258

    
259
        /**
260
         * @return Returns the serversFile.
261
         */
262
        public String getServersFile() {        
263
                return serversFile;
264
        } 
265

    
266
        /**
267
         * @param serversFile The serversFile to set.
268
         */
269
        public void setServersFile(String serversFile) {        
270
                this.serversFile = serversFile;
271
        } 
272

    
273
        /**
274
         * @return Returns the currentServer.
275
         */
276
        public String getCurrentServer() {        
277
                return currentServer;
278
        } 
279

    
280
        /**
281
         * @return Returns the client.
282
         */
283
        public CatalogClient getClient() {        
284
                return client;
285
        } 
286
        
287
        protected Icon getPropertiesIcon(){
288
                return new ImageIcon("./gvSIG/extensiones/org.gvsig.catalog/images/serverProperties.png");
289
        }
290

    
291
        /**
292
         * This class is used to manage the searches.
293
         * It contains method to start and to stop a thread. It is
294
         * necessary to create because "stop" method (for the Thread class)
295
         * is deprecated.
296
         * 
297
         * 
298
         * @author Jorge Piera Llodra (piera_jor@gva.es)
299
         */
300
        private class ConnectThread implements Runnable {
301
                volatile Thread myThread = null;
302

    
303
                public  ConnectThread() {        
304
                        myThread = new Thread(this);
305
                        myThread.start();
306
                } 
307

    
308
                public void stop(){
309
                        myThread.stop();
310
                }
311
                /*
312
                 * (non-Javadoc)
313
                 * @see java.lang.Runnable#run()
314
                 */
315
                public void run() {        
316
                        try {
317
                                DiscoveryServiceCapabilities capabilities = client.getCapabilities();
318
                                if (capabilities.isAvailable()){
319
                                        controlsPanel.enableSearchButton(true);
320
                                        currentServer = controlsPanel.getServerAddress();
321
                                        searchButtonActionPerformed();                                
322
                                } 
323
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 
324
                                controlsPanel.setServerReply(capabilities.getServerMessage());
325

    
326
                        } catch (Exception e) {
327
                                controlsPanel.setServerReply(Messages.getText(e.toString()));
328
                                e.printStackTrace();
329
                        }        
330
                }
331
        }        
332
}