Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appGazetteer / src / org / gvsig / gazetteer / ui / serverconnect / ServerConnectDialogPanel.java @ 37871

History | View | Annotate | Download (8.35 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.gazetteer.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.JFrame;
56
import javax.swing.JPanel;
57

    
58
import org.gvsig.catalog.drivers.DiscoveryServiceCapabilities;
59
import org.gvsig.catalog.utils.CatalogConstants;
60
import org.gvsig.gazetteer.GazetteerClient;
61
import org.gvsig.gazetteer.GazetteerLocator;
62
import org.gvsig.gazetteer.GazetteerManager;
63
import org.gvsig.gazetteer.drivers.IGazetteerServiceDriver;
64
import org.gvsig.gazetteer.ui.search.SearchDialog;
65
import org.gvsig.i18n.Messages;
66
import org.gvsig.utils.swing.jcomboServer.ServerData;
67

    
68

    
69

    
70
/**
71
 * It implements the Jpanel thas has the events control
72
 * for the connection panel. It is composed by some buttons
73
 * and a panel.
74
 * @author Jorge Piera Llodra (piera_jor@gva.es)
75
 */
76
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
77
        private static final GazetteerManager gazetteerManager = GazetteerLocator.getGazetteerManager();
78
        private static final long serialVersionUID = 7022169996238047820L;
79
        private static TreeMap serverList = new TreeMap();
80
        private ServerConnectPanel controlsPanel = null;
81
        private JFrame parent = null;
82
        protected GazetteerClient client = null;
83
        protected String serversFile = "servers/GazetteerServers.txt";
84
        protected String currentServer = "";
85
        private ConnectThread connectThread = null;
86
        protected boolean isUpdating = false;
87

    
88
        /**
89
         * Constructor
90
         * @param parent 
91
         */
92
        public  ServerConnectDialogPanel(JFrame parent) {
93
                this.parent = parent;
94
                this.setLayout(new BorderLayout());
95
                add(getControlsPanel(),BorderLayout.CENTER);
96
                //Loads the servers
97
                loadServerList(serversFile);
98
                //Load the protocols
99
                controlsPanel.loadDrivers(
100
                                gazetteerManager.getDrivers());
101
                //Load the first protocol
102
                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
103
        } 
104

    
105
        /**
106
        * @return the main panel
107
         */
108
        public ServerConnectPanel getControlsPanel() {        
109
                if (controlsPanel == null) {
110
                        controlsPanel = new ServerConnectPanel();
111
                        controlsPanel.addActionListener(this);
112
                        controlsPanel.enableSearchButton(false);
113
                }
114
                return controlsPanel;
115
        } 
116
        
117
        /**
118
         * It adds a server in the TreeMap Object
119
         * @param server 
120
         */
121
        protected static void addTreeMapServer(ServerData server) {        
122
                if (ServerConnectDialogPanel.serverList == null) {
123
                        ServerConnectDialogPanel.serverList = new TreeMap();
124
                }
125
                serverList.put(server.getServerAddress(), server);
126
        } 
127

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

    
141
        /**
142
         * It loads a server list from a text file
143
         * @param sfile 
144
         * File that contains the rervers
145
         */
146
        private void loadServersFromFile(String sfile) {        
147
                File file = null;
148
                try {
149
                        file = new File(sfile);
150
                        if (file.exists()) {
151
                                BufferedReader fr = new BufferedReader(new FileReader(file));
152
                                String s;
153
                                while ((s = fr.readLine()) != null) {
154
                                        addTreeMapServer(new ServerData(s,"",""));
155
                                }
156
                        } else {
157
                                System.out.println("No se encuentra el fichero '" +
158
                                                file.getPath() + "'");
159
                        }
160
                } catch (FileNotFoundException e) {
161
                        System.out.println("No se encuentra el fichero '" + file.getPath() +
162
                        "'");
163
                        //e.printStackTrace();
164
                } catch (IOException e) {
165
                        System.out.println("Error de entrada salida en la lectura del fichero");
166
                        //e.printStackTrace();
167
                }
168
        } 
169
        
170
        /*
171
         * (non-Javadoc)
172
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
173
         */
174
        public void actionPerformed(ActionEvent e) {        
175
                if (e.getActionCommand().compareTo(CatalogConstants.CONNECT_BUTTON_ACTION_COMMAND)==0) {
176
                        connectButtonActionPerformed();
177
                }else if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
178
                        searchButtonActionPerformed();
179
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0) {
180
                        closeButtonActionPerformed();
181
                }else if (e.getActionCommand().compareTo(CatalogConstants.PROTOCOL_COMBO_ACTION_COMMAND)==0){
182
                    if (!isUpdating){
183
                        isUpdating = true;
184
                        controlsPanel.updateServerByProtocol();
185
                        isUpdating = false;
186
                    }        
187
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVER_COMBO_ACTION_COMMAND)==0) {
188
                    if (!isUpdating){
189
                        isUpdating = true;
190
                        controlsPanel.updateProtocol();
191
                        isUpdating = false;
192
                    }
193
                }
194
        } 
195

    
196
        /**
197
         * Action when the search button is clicked
198
         */
199
        protected void searchButtonActionPerformed() {        
200
                setEnabled(false);
201
                new SearchDialog(client,parent);
202
        } 
203

    
204
        /**
205
         * It is thrown the the connect button is clicked
206
         */
207
        protected void connectButtonActionPerformed() {        
208
                controlsPanel.enableSearchButton(false);                
209
                //Create a new Gazetteer client
210
                client = new GazetteerClient(controlsPanel.getServerAddress(),
211
                                (IGazetteerServiceDriver)controlsPanel.getDriver());
212
                if (connectThread != null){
213
                        connectThread.stop();
214
                }
215
                connectThread = new ConnectThread();
216
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
217
        } 
218

    
219
        /**
220
         *  * It is thrown the the close button is clicked
221
         */
222
        protected void closeButtonActionPerformed() {        
223
                parent.setVisible(false);
224
                System.exit(0);
225
        } 
226

    
227
        /**
228
         * @return Returns the serversFile.
229
         */
230
        public String getServersFile() {        
231
                return serversFile;
232
        } 
233

    
234
        /**
235
         * @param serversFile The serversFile to set.
236
         */
237
        public void setServersFile(String serversFile) {        
238
                this.serversFile = serversFile;
239
        } 
240

    
241
        /**
242
         * @return Returns the currentServer.
243
         */
244
        public String getCurrentServer() {        
245
                return currentServer;
246
        } 
247

    
248
        /**
249
         * @return Returns the client.
250
         */
251
        public GazetteerClient getCliente() {        
252
                return client;
253
        } 
254
        
255
        /**
256
         * This class is used to manage the searches.
257
         * It contains method to start and to stop a thread. It is
258
         * necessary to create because "stop" method (for the Thread class)
259
         * is deprecated.
260
         * 
261
         * 
262
         * @author Jorge Piera Llodra (piera_jor@gva.es)
263
         */
264
        private class ConnectThread implements Runnable {
265
                volatile Thread myThread = null;
266

    
267
                public  ConnectThread() {        
268
                        myThread = new Thread(this);
269
                        myThread.start();
270
                } 
271

    
272
                public void stop(){
273
                        myThread.stop();
274
                }
275
                /*
276
                 * (non-Javadoc)
277
                 * @see java.lang.Runnable#run()
278
                 */
279
                public void run() {        
280
                        try {
281
                                DiscoveryServiceCapabilities capabilities = client.getCapabilities();
282
                                if (capabilities.isAvailable()){
283
                                        controlsPanel.enableSearchButton(true);
284
                                        currentServer = controlsPanel.getServerAddress();
285
                                        searchButtonActionPerformed();
286
                                }        
287
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));   
288
                                controlsPanel.setServerReply(capabilities.getServerMessage());                                
289
                        } catch (Exception e) {
290
                                controlsPanel.setServerReply(Messages.getText(e.toString()));
291
                                e.printStackTrace();
292
                        }        
293
                }
294
        }        
295
}