Statistics
| Revision:

root / branches / CatalogYNomenclator_v1_1_0_1005 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / ui / serverconnect / ServerConnectDialogPanel.java @ 12758

History | View | Annotate | Download (7.87 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.CatalogDriverRegister;
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
                                CatalogDriverRegister.getInstance().getDrivers());
98
                //Load the first protocol
99
                controlsPanel.setProtocol(controlsPanel.getServer().getServiceSubType());
100
        } 
101

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

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

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

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

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

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

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

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

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

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

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