Statistics
| Revision:

svn-gvsig-desktop / branches / CatalogYNomenclator_v1_1_0_1005 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / ui / serverconnect / ServerConnectDialogPanel.java @ 12750

History | View | Annotate | Download (7.76 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 es.gva.cit.catalogClient.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.i18n.Messages;
59

    
60
import com.iver.utiles.swing.jcomboServer.ServerData;
61

    
62
import es.gva.cit.catalogClient.CatalogClient;
63
import es.gva.cit.catalogClient.drivers.CatalogCapabilities;
64
import es.gva.cit.catalogClient.drivers.ICatalogServiceDriver;
65
import es.gva.cit.catalogClient.ui.search.SearchDialog;
66
import es.gva.cit.catalogClient.utils.CatalogClientRegister;
67
import es.gva.cit.catalogClient.utils.CatalogConstants;
68

    
69
/**
70
 * 
71
 * 
72
 * 
73
 * @author Jorge Piera Llodra (piera_jor@gva.es)
74
 */
75
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
76
        private static final long serialVersionUID = 1224880378648403038L;
77
        private static TreeMap serverList = new TreeMap();
78
        private ServerConnectPanel controlsPanel = null;
79
        private JFrame parent = null;
80
        protected CatalogClient client = null;
81
        protected String serversFile = "servers/CatalogServers.txt";
82
        protected String currentServer = "";
83
        private ConnectThread connectThread = null;
84

    
85
        /**
86
         * Constructor
87
         * @param parent 
88
         */
89
        public  ServerConnectDialogPanel(JFrame parent) {
90
                this.parent = parent;
91
                this.setLayout(new BorderLayout());
92
                add(getControlsPanel(),BorderLayout.CENTER);
93
                //Loads the servers
94
                loadServerList(serversFile);
95
                //Load the protocols
96
                controlsPanel.loadDrivers(
97
                                CatalogClientRegister.getInstance().getDrivers());
98
        } 
99

    
100
        /**
101
        * @return the main panel
102
         */
103
        public ServerConnectPanel getControlsPanel() {        
104
                if (controlsPanel == null) {
105
                        controlsPanel = new ServerConnectPanel();
106
                        controlsPanel.addActionListener(this);
107
                        controlsPanel.enableSearchButton(false);
108
                }
109
                return controlsPanel;
110
        } 
111

    
112
        /**
113
         * It adds a server in the TreeMap Object
114
         * @param server 
115
         */
116
        protected static void addTreeMapServer(ServerData server) {        
117
                if (ServerConnectDialogPanel.serverList == null) {
118
                        ServerConnectDialogPanel.serverList = new TreeMap();
119
                }
120
                serverList.put(server.getServerAddress(), server);
121
        } 
122

    
123
        /**
124
         * This method loads a server list in the combo
125
         * @param sfile 
126
         */
127
        private void loadServerList(String sfile) {        
128
                loadServersFromFile(sfile);
129
                Iterator iter = serverList.keySet().iterator();
130
                while (iter.hasNext()) {
131
                        ServerData server = (ServerData) serverList.get((String) iter.next());
132
                        controlsPanel.addServer(server);
133
                }            
134
        } 
135
        
136
        /**
137
         * It loads a server list from a text file
138
         * @param sfile 
139
         * File that contains the rervers
140
         */
141
        private void loadServersFromFile(String sfile) {        
142
                File file = null;
143
                try {
144
                        file = new File(sfile);
145
                        if (file.exists()) {
146
                                BufferedReader fr = new BufferedReader(new FileReader(file));
147
                                String s;
148
                                while ((s = fr.readLine()) != null) {
149
                                        addTreeMapServer(new ServerData(s,"",""));
150
                                }
151
                        } else {
152
                                System.out.println("No se encuentra el fichero '" +
153
                                                file.getPath() + "'");
154
                        }
155
                } catch (FileNotFoundException e) {
156
                        System.out.println("No se encuentra el fichero '" + file.getPath() +
157
                        "'");
158
                        //e.printStackTrace();
159
                } catch (IOException e) {
160
                        System.out.println("Error de entrada salida en la lectura del fichero");
161
                        //e.printStackTrace();
162
                }
163
        } 
164
        
165
        /*
166
         * (non-Javadoc)
167
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
168
         */
169
        public void actionPerformed(ActionEvent e) {        
170
                if (e.getActionCommand().compareTo(CatalogConstants.CONNECT_BUTTON_ACTION_COMMAND)==0) {
171
                        connectButtonActionPerformed();
172
                }
173
                if (e.getActionCommand().compareTo(CatalogConstants.SEARCH_BUTTON_ACTION_COMMAND)==0) {
174
                        searchButtonActionPerformed();
175
                }
176
                if (e.getActionCommand().compareTo(CatalogConstants.CLOSE_BUTTON_ACTION_COMMAND)==0) {
177
                        closeButtonActionPerformed();
178
                }
179
                if (e.getActionCommand().compareTo(CatalogConstants.PROTOCOL_COMBO_ACTION_COMMAND)==0){
180
                        try {
181
                                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
182
                        }catch(NullPointerException ex){
183
                                //The server is not loaded 
184
                        }
185
                }
186
        } /**
187
         * Action when the search button is clicked
188
         */
189
        public void searchButtonActionPerformed() {        
190
                setEnabled(false);
191
                new SearchDialog(client,parent);
192
        } 
193

    
194
        /**
195
         * It is thrown the the connect button is clicked
196
         */
197
        public void connectButtonActionPerformed() {        
198
                controlsPanel.enableSearchButton(false);                
199
                //Create a new Gazetteer client
200
                client = new CatalogClient(controlsPanel.getServerAddress(),
201
                                controlsPanel.getDatabase(),
202
                                (ICatalogServiceDriver)controlsPanel.getDriver());
203
                if (connectThread != null){
204
                        connectThread.stop();
205
                }
206
                connectThread = new ConnectThread();
207
                setCursor(new Cursor(Cursor.WAIT_CURSOR));
208
        } 
209

    
210
        /**
211
         *  * It is thrown the the close button is clicked
212
         */
213
        public void closeButtonActionPerformed() {        
214
                parent.setVisible(false);
215
        } 
216

    
217
        /**
218
         * @return Returns the serversFile.
219
         */
220
        public String getServersFile() {        
221
                return serversFile;
222
        } 
223

    
224
        /**
225
         * @param serversFile The serversFile to set.
226
         */
227
        public void setServersFile(String serversFile) {        
228
                this.serversFile = serversFile;
229
        } 
230

    
231
        /**
232
         * @return Returns the currentServer.
233
         */
234
        public String getCurrentServer() {        
235
                return currentServer;
236
        } 
237

    
238
        /**
239
         * @return Returns the client.
240
         */
241
        public CatalogClient getClient() {        
242
                return client;
243
        } 
244
        
245
        /**
246
         * This class is used to manage the searches.
247
         * It contains method to start and to stop a thread. It is
248
         * necessary to create because "stop" method (for the Thread class)
249
         * is deprecated.
250
         * 
251
         * 
252
         * @author Jorge Piera Llodra (piera_jor@gva.es)
253
         */
254
        private class ConnectThread implements Runnable {
255
                volatile Thread myThread = null;
256

    
257
                public  ConnectThread() {        
258
                        myThread = new Thread(this);
259
                        myThread.start();
260
                } 
261

    
262
                public void stop(){
263
                        myThread.stop();
264
                }
265
                /*
266
                 * (non-Javadoc)
267
                 * @see java.lang.Runnable#run()
268
                 */
269
                public void run() {        
270
                        try {
271
                                CatalogCapabilities capabilities = client.getCapabilities();
272
                                if (capabilities.isAvailable()){
273
                                        controlsPanel.enableSearchButton(true);
274
                                        currentServer = controlsPanel.getServerAddress();
275
                                        searchButtonActionPerformed();                                
276
                                } 
277
                                setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 
278
                                controlsPanel.setServerReply(capabilities.getServerMessage());
279
                                
280
                        } catch (Exception e) {
281
                                controlsPanel.setServerReply(Messages.getText(e.toString()));
282
                                e.printStackTrace();
283
                        }        
284
                }
285
        }        
286
}