Statistics
| Revision:

gvsig-gazetteer / org.gvsig.gazetteer / trunk / org.gvsig.gazetteer / org.gvsig.gazetteer.lib / src / main / java / org / gvsig / gazetteer / ui / serverconnect / ServerConnectDialogPanel.java @ 102

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

    
55
import javax.swing.JFrame;
56
import javax.swing.JOptionPane;
57
import javax.swing.JPanel;
58

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

    
71

    
72

    
73
/**
74
 * It implements the Jpanel thas has the events control
75
 * for the connection panel. It is composed by some buttons
76
 * and a panel.
77
 * @author Jorge Piera Llodra (piera_jor@gva.es)
78
 */
79
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
80

    
81
    private static Logger logger = LoggerFactory.getLogger(ServerConnectDialogPanel.class);
82

    
83
        private static final GazetteerManager gazetteerManager = GazetteerLocator.getGazetteerManager();
84
        private static final long serialVersionUID = 7022169996238047820L;
85
        private static TreeMap serverList = new TreeMap();
86
        private ServerConnectPanel controlsPanel = null;
87
        private JFrame parent = null;
88
        protected GazetteerClient client = null;
89
        protected String serversFile = "servers/GazetteerServers.txt";
90
        protected String currentServer = "";
91
        private ConnectThread connectThread = null;
92
        protected boolean isUpdating = false;
93

    
94
        /**
95
         * Constructor
96
         * @param parent
97
         */
98
        public  ServerConnectDialogPanel(JFrame parent) {
99
                this.parent = parent;
100
                this.setLayout(new BorderLayout());
101
                add(getControlsPanel(),BorderLayout.CENTER);
102
                //Loads the servers
103
                if (loadServerList(serversFile)) {
104
                //Load the protocols
105
                controlsPanel.loadDrivers(
106
                        gazetteerManager.getDrivers());
107
                //Load the first protocol
108
                if (controlsPanel.getServer() != null) {
109
                        controlsPanel.setProtocol(
110
                                        controlsPanel.getServer().getServiceSubType());
111
                }
112

    
113
                } else {
114
                    String _msg = Messages.getText("_Unable_to_load_server_list");
115
                    String _tit = Messages.getText("gazetteer_search");
116
                    JOptionPane.showMessageDialog(
117
                        this,
118
                        _msg,
119
                        _tit,
120
                        JOptionPane.ERROR_MESSAGE);
121
                }
122
        }
123

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

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

    
147
        /**
148
         * This method loads a server list in the combo
149
         * @param sfile
150
         */
151
        private boolean loadServerList(String sfile) {
152
                if (loadServersFromFile(sfile)) {
153
                Iterator iter = serverList.keySet().iterator();
154
                while (iter.hasNext()) {
155
                    ServerData server = (ServerData) serverList.get((String) iter.next());
156
                    controlsPanel.addServer(server);
157
                }
158
                return true;
159
                }
160
                return false;
161
        }
162

    
163
        /**
164
         * It loads a server list from a text file
165
         * @param sfile
166
         * File that contains the rervers
167
         */
168
        private boolean loadServersFromFile(String sfile) {
169
                File file = null;
170
                try {
171
                  URL uf = getClass().getClassLoader().getResource(sfile);
172
                  if (uf == null) {
173
                  throw new FileNotFoundException(file.getAbsolutePath());
174
                  }
175

    
176
                  file = new File(uf.getFile());
177
                        if (file.exists()) {
178
                                BufferedReader fr = new BufferedReader(new FileReader(file));
179
                                String s;
180
                                while ((s = fr.readLine()) != null) {
181
                                        addTreeMapServer(new ServerData(s,"",""));
182
                                }
183
                                return true;
184
                        }
185
                } catch (Exception e) {
186
                    logger.info("While reading gazetteer servers list file: " + e.getMessage());
187
                }
188
                return false;
189
        }
190

    
191
        /*
192
         * (non-Javadoc)
193
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
194
         */
195
        public void actionPerformed(ActionEvent e) {
196
                if (e.getActionCommand().compareTo(CatalogConstants.CONNECT_BUTTON_ACTION_COMMAND)==0) {
197
                        connectButtonActionPerformed();
198
                }else if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
199
                        searchButtonActionPerformed();
200
                }else if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0) {
201
                        closeButtonActionPerformed();
202
                }else if (e.getActionCommand().compareTo(CatalogConstants.PROTOCOL_COMBO_ACTION_COMMAND)==0){
203
                    if (!isUpdating){
204
                        isUpdating = true;
205
                        controlsPanel.updateServerByProtocol();
206
                        isUpdating = false;
207
                    }
208
                }else if (e.getActionCommand().compareTo(CatalogConstants.SERVER_COMBO_ACTION_COMMAND)==0) {
209
                    if (!isUpdating){
210
                        isUpdating = true;
211
                        controlsPanel.updateProtocol();
212
                        isUpdating = false;
213
                    }
214
                }
215
        }
216

    
217
        /**
218
         * Action when the search button is clicked
219
         */
220
        protected void searchButtonActionPerformed() {
221
                setEnabled(false);
222
                new SearchDialog(client,parent);
223
        }
224

    
225
        /**
226
         * It is thrown the the connect button is clicked
227
         */
228
        protected void connectButtonActionPerformed() {
229
                controlsPanel.enableSearchButton(false);
230
                IGazetteerServiceDriver driver = (IGazetteerServiceDriver)controlsPanel.getDriver();
231
                if( driver.needsUsername(driver.getServiceName()) ) {
232
                    String username=requestUsername(driver.getUsername());
233
                    driver.setUsername(username);
234
                }
235
                //Create a new Gazetteer client
236
                client = new GazetteerClient(controlsPanel.getServerAddress(),
237
                                (IGazetteerServiceDriver)controlsPanel.getDriver());
238
                if (connectThread != null){
239
                        connectThread.stop();
240
                }
241
                connectThread = new ConnectThread();
242
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
243
        }
244

    
245
        private String requestUsername (String oldUsername){
246
            String strRequireUserName = Messages.getText("_Insert_Geonames_username");
247
            String newUsername = JOptionPane.showInputDialog(strRequireUserName,oldUsername);
248
            if ( newUsername==null ){
249
                return oldUsername;
250
            }
251
            return newUsername;
252
        }
253

    
254
        /**
255
         *  * It is thrown the the close button is clicked
256
         */
257
        protected void closeButtonActionPerformed() {
258
                parent.setVisible(false);
259
                System.exit(0);
260
        }
261

    
262
        /**
263
         * @return Returns the serversFile.
264
         */
265
        public String getServersFile() {
266
                return serversFile;
267
        }
268

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

    
276
        /**
277
         * @return Returns the currentServer.
278
         */
279
        public String getCurrentServer() {
280
                return currentServer;
281
        }
282

    
283
        /**
284
         * @return Returns the client.
285
         */
286
        public GazetteerClient getCliente() {
287
                return client;
288
        }
289

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

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

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