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

View differences:

ServerConnectDialogPanel.java
48 48
import java.io.File;
49 49
import java.io.FileNotFoundException;
50 50
import java.io.FileReader;
51
import java.io.IOException;
52 51
import java.net.URL;
53 52
import java.util.Iterator;
54 53
import java.util.TreeMap;
......
57 56
import javax.swing.JOptionPane;
58 57
import javax.swing.JPanel;
59 58

  
60
import org.slf4j.Logger;
61
import org.slf4j.LoggerFactory;
62

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

  
73 71

  
74 72

  
......
79 77
 * @author Jorge Piera Llodra (piera_jor@gva.es)
80 78
 */
81 79
public class ServerConnectDialogPanel extends JPanel implements ActionListener {
82
    
80

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

  
85 83
	private static final GazetteerManager gazetteerManager = GazetteerLocator.getGazetteerManager();
86 84
	private static final long serialVersionUID = 7022169996238047820L;
87 85
	private static TreeMap serverList = new TreeMap();
......
95 93

  
96 94
	/**
97 95
	 * Constructor
98
	 * @param parent 
96
	 * @param parent
99 97
	 */
100 98
	public  ServerConnectDialogPanel(JFrame parent) {
101 99
		this.parent = parent;
......
111 109
	        	controlsPanel.setProtocol(
112 110
	        			controlsPanel.getServer().getServiceSubType());
113 111
	        }
114
	        
112

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

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

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

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

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

  
178 176
	          file = new File(uf.getFile());
179 177
			if (file.exists()) {
180 178
				BufferedReader fr = new BufferedReader(new FileReader(file));
......
188 186
		    logger.info("While reading gazetteer servers list file: " + e.getMessage());
189 187
		}
190 188
		return false;
191
	} 
192
	
189
	}
190

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

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

  
227 225
	/**
228 226
	 * It is thrown the the connect button is clicked
229 227
	 */
230
	protected void connectButtonActionPerformed() {        
231
		controlsPanel.enableSearchButton(false);		
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
		}
232 235
		//Create a new Gazetteer client
233 236
		client = new GazetteerClient(controlsPanel.getServerAddress(),
234 237
				(IGazetteerServiceDriver)controlsPanel.getDriver());
......
237 240
		}
238 241
		connectThread = new ConnectThread();
239 242
		setCursor(new Cursor(Cursor.WAIT_CURSOR));
240
	} 
243
	}
241 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

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

  
250 262
	/**
251 263
	 * @return Returns the serversFile.
252 264
	 */
253
	public String getServersFile() {        
265
	public String getServersFile() {
254 266
		return serversFile;
255
	} 
267
	}
256 268

  
257 269
	/**
258 270
	 * @param serversFile The serversFile to set.
259 271
	 */
260
	public void setServersFile(String serversFile) {        
272
	public void setServersFile(String serversFile) {
261 273
		this.serversFile = serversFile;
262
	} 
274
	}
263 275

  
264 276
	/**
265 277
	 * @return Returns the currentServer.
266 278
	 */
267
	public String getCurrentServer() {        
279
	public String getCurrentServer() {
268 280
		return currentServer;
269
	} 
281
	}
270 282

  
271 283
	/**
272 284
	 * @return Returns the client.
273 285
	 */
274
	public GazetteerClient getCliente() {        
286
	public GazetteerClient getCliente() {
275 287
		return client;
276
	} 
277
	
288
	}
289

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

  
290
		public  ConnectThread() {        
302
		public  ConnectThread() {
291 303
			myThread = new Thread(this);
292 304
			myThread.start();
293
		} 
305
		}
294 306

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

Also available in: Unified diff