Revision 102

View differences:

org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/GazetteerManager.java
2 2
*
3 3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4 4
* of the Valencian Government (CIT)
5
* 
5
*
6 6
* This program is free software; you can redistribute it and/or
7 7
* modify it under the terms of the GNU General Public License
8 8
* as published by the Free Software Foundation; either version 2
9 9
* of the License, or (at your option) any later version.
10
* 
10
*
11 11
* This program is distributed in the hope that it will be useful,
12 12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
* GNU General Public License for more details.
15
* 
15
*
16 16
* You should have received a copy of the GNU General Public License
17 17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19
* MA  02110-1301, USA.
20
* 
20
*
21 21
*/
22 22

  
23 23
/*
24 24
* AUTHORS (In addition to CIT):
25 25
* 2009 {Iver T.I.}   {Task}
26 26
*/
27
 
27

  
28 28
package org.gvsig.gazetteer;
29 29

  
30
import java.util.Map;
31

  
30 32
import org.gvsig.gazetteer.drivers.IGazetteerServiceDriver;
31 33

  
32 34
/**
33 35
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
34 36
 */
35 37
public interface GazetteerManager {
36
	
37 38
	/**
38
	 * It is used to retrieve a driver that supports a concrete 
39
	 * It is used to retrieve a driver that supports a concrete
39 40
	 * protocol
40 41
	 * @param protocol
41 42
	 * Gazetteer protocol
......
43 44
	 * The concrete gazatteer service driver
44 45
	 */
45 46
	public IGazetteerServiceDriver getDriver(String protocol);
46
	
47

  
47 48
	/**
48 49
	 * @return a list with all the gazetteer drivers
49 50
	 */
50 51
	public IGazetteerServiceDriver[] getDrivers();
51
	
52

  
52 53
	/**
53
	 * This method is used to register a new gazetter driver 
54
	 * This method is used to register a new gazetter driver
54 55
	 * that manage a concrete protocol
55 56
	 * @param driver
56 57
	 * Gazetteer driver to register
57 58
	 */
58 59
	public void register(String name, Class driver);
60

  
61
	/**
62
	 * Returns username for a given protocol
63
	 * @param protocol
64
	 * @return
65
	 */
66
	public String getUsername(String protocol);
67

  
68
	/**
69
	 * Saves username for a given protocol
70
	 * @param protocol
71
	 * @param username
72
	 */
73
	public void setUsername(String protocol, String username);
74

  
75
	/**
76
	 * Receives Map with the saved usernames for the different protocols
77
	 * @param usernames
78
	 */
79
	public void setUsernames(Map usernames);
80

  
59 81
}
60 82

  
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/ui/serverconnect/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
}
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/drivers/AbstractGazetteerServiceDriver.java
44 44

  
45 45
import org.gvsig.catalog.drivers.AbstractDiscoveryServiceDriver;
46 46
import org.gvsig.catalog.querys.DiscoveryServiceQuery;
47
import org.gvsig.gazetteer.GazetteerLocator;
47 48
import org.gvsig.gazetteer.querys.FeatureType;
48 49
import org.gvsig.gazetteer.querys.FeatureTypeAttribute;
49 50
import org.gvsig.gazetteer.querys.GazetteerQuery;
......
51 52

  
52 53
/**
53 54
 * This class must be inherited by all the gazetteer drivers. It contains
54
 * the common attributes. 
55
 * the common attributes.
55 56
 * @author Jorge Piera Llodra (piera_jor@gva.es)
56 57
 */
57 58
public abstract class AbstractGazetteerServiceDriver extends AbstractDiscoveryServiceDriver implements IGazetteerServiceDriver {
58 59
	private FeatureType[] featureTypes;
59 60
	private GazetteerQuery query;
60
	private String projection = null;	
61
	private String projection = null;
61 62

  
62 63
	/**
63 64
	 * @return Returns the feature.
64 65
	 */
65
	public FeatureType[] getFeatureTypes() {        
66
	public FeatureType[] getFeatureTypes() {
66 67
		return featureTypes;
67
	} 
68
	}
68 69

  
69 70
	/**
70 71
	 * @param feature The feature to set.
71 72
	 */
72
	public void setFeatureTypes(FeatureType[] featureTypes) {        
73
	public void setFeatureTypes(FeatureType[] featureTypes) {
73 74
		this.featureTypes = featureTypes;
74
	} 	
75
	}
75 76

  
76 77
	/*
77 78
	 * (non-Javadoc)
......
80 81
	public DiscoveryServiceQuery createQuery(){
81 82
		return new GazetteerQuery();
82 83
	}
83
	
84

  
84 85
	/**
85 86
	 * @return Returns the query.
86 87
	 */
87
	public GazetteerQuery getQuery() {        
88
	public GazetteerQuery getQuery() {
88 89
		return query;
89
	} 
90
	}
90 91

  
91 92
	/**
92 93
	 * @param query The query to set.
93 94
	 */
94
	public void setQuery(GazetteerQuery query) {        
95
	public void setQuery(GazetteerQuery query) {
95 96
		this.query = query;
96
	}	
97
	}
97 98

  
98 99
	/*
99 100
	 * (non-Javadoc)
100 101
	 * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#describeFeatureType(java.net.URI, java.lang.String)
101 102
	 */
102
	public FeatureTypeAttribute[] describeFeatureType(URI uri, String feature) {        
103
	public FeatureTypeAttribute[] describeFeatureType(URI uri, String feature) {
103 104
		return null;
104
	} 	
105
	}
105 106

  
106 107
	/*
107 108
	 * (non-Javadoc)
108 109
	 * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#isDescribeFeatureTypeNeeded()
109 110
	 */
110
	public boolean isDescribeFeatureTypeNeeded()  {        
111
	public boolean isDescribeFeatureTypeNeeded()  {
111 112
		return false;
112
	} 
113
	}
113 114

  
114 115
	/*
115 116
	 * (non-Javadoc)
116 117
	 * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getProjection()
117
	 */	 
118
	 */
118 119
	public String getProjection(){
119 120
		return projection;
120 121
	}
......
126 127
	public void setProjection(String projection){
127 128
		this.projection = projection;
128 129
	}
130

  
131
    public boolean needsUsername(String protocol) {
132
        return false;
133
    }
134

  
135
    public String getUsername() {
136
        return GazetteerLocator.getGazetteerManager().getUsername(this.getServiceName());
137
    }
138

  
139
    public void setUsername(String username) {
140
        GazetteerLocator.getGazetteerManager().setUsername(this.getServiceName(),username);
141

  
142
    }
129 143
}
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/drivers/IGazetteerServiceDriver.java
50 50
/**
51 51
 * This interface contains the common methods that have to be
52 52
 * implemented by all the gazetteer drivers.
53
 * 
54
 * 
53
 *
54
 *
55 55
 * @author Jorge Piera Llodra (piera_jor@gva.es)
56 56
 */
57 57
public interface IGazetteerServiceDriver extends IDiscoveryServiceDriver{
......
62 62
	 * @return Array of the attributes of a feature
63 63
	 */
64 64
	public FeatureTypeAttribute[] describeFeatureType(URI uri, String featureType) throws Exception;
65
	
65

  
66 66
	/**
67 67
	 * There are protocols that need to invoke the describeFeatureType
68 68
	 * operation before to do a getFeature.
69
	 * @return if the describeFeatureType operation is needed. 
69
	 * @return if the describeFeatureType operation is needed.
70 70
	 */
71 71
	public boolean isDescribeFeatureTypeNeeded();
72
	
72

  
73 73
	/**
74 74
	 * It returns the foubd records
75 75
	 * @param uri Server URI
76 76
	 * @param query Query with the search parameters
77 77
	 * @return The features
78
	 */	
78
	 */
79 79
	public Feature[] getFeature(URI uri, GazetteerQuery query) throws Exception;
80
		
80

  
81 81
	/**
82 82
	 * @return the projection
83 83
	 */
......
88 88
	 */
89 89
	public void setProjection(String projection);
90 90

  
91
	/**
92
	 * Indicates if the protocol requires a username
93
	 * @param protocol
94
	 * @return
95
	 */
96
	public boolean needsUsername(String protocol);
97

  
98
	/**
99
	 * Returns username for protocol
100
	 * @return
101
	 */
102
	public String getUsername();
103

  
104
	/**
105
	 * Saves username for protocol
106
	 */
107
	public void setUsername(String username);
91 108
}
92 109

  
93 110

  
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/geonames/GeonamesServiceDriver.java
2 2
 *
3 3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4 4
 * of the Valencian Government (CIT)
5
 * 
5
 *
6 6
 * This program is free software; you can redistribute it and/or
7 7
 * modify it under the terms of the GNU General Public License
8 8
 * as published by the Free Software Foundation; either version 2
9 9
 * of the License, or (at your option) any later version.
10
 * 
10
 *
11 11
 * This program is distributed in the hope that it will be useful,
12 12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
 * GNU General Public License for more details.
15
 * 
15
 *
16 16
 * You should have received a copy of the GNU General Public License
17 17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19
 * MA  02110-1301, USA.
20
 * 
20
 *
21 21
 */
22 22

  
23 23
/*
......
46 46

  
47 47

  
48 48
public class GeonamesServiceDriver extends AbstractGazetteerServiceDriver {
49
	
50
	
49

  
50

  
51 51
	public GeonamesServiceDriver() {
52 52
		super();
53
		setProjection("EPSG:4326");		
53
		setProjection("EPSG:4326");
54 54
	}
55 55

  
56 56
	/*
......
58 58
	 * @see es.gva.cit.gazetteer.drivers.IGazetteerServiceDriver#getFeature(java.net.URI, es.gva.cit.gazetteer.querys.GazetteerQuery)
59 59
	 */
60 60
	public Feature[] getFeature(URI uri, GazetteerQuery query) throws Exception {
61
	    WebService.setUserName(getUsername());
61 62
		ToponymSearchCriteria searchCriteria = new ToponymSearchCriteria();
62 63
		searchCriteria.setQ(query.getName());
63 64
		ToponymSearchResult searchResult = WebService.search(searchCriteria);
......
70 71
	}
71 72

  
72 73
	private Feature converToponym(Toponym toponym) {
73
		Feature feature = new Feature(toponym.getFeatureCode(), 
74
		Feature feature = new Feature(toponym.getFeatureCode(),
74 75
				toponym.getName(),
75 76
				toponym.getName(),
76 77
				new Point2D.Double(toponym.getLongitude(), toponym.getLatitude()));
77
		
78

  
78 79
		return feature;
79 80
	}
80 81

  
......
103 104
	public ServerData getOneServer() {
104 105
		return new ServerData("www.geonames.org", "GEONAMES");
105 106
	}
106
	
107
	
107

  
108
    @Override
109
    public boolean needsUsername(String protocol) {
110
        return true;
111
    }
108 112
}
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.lib/src/main/java/org/gvsig/gazetteer/impl/DefaultGazetteerManager.java
2 2
*
3 3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4 4
* of the Valencian Government (CIT)
5
* 
5
*
6 6
* This program is free software; you can redistribute it and/or
7 7
* modify it under the terms of the GNU General Public License
8 8
* as published by the Free Software Foundation; either version 2
9 9
* of the License, or (at your option) any later version.
10
* 
10
*
11 11
* This program is distributed in the hope that it will be useful,
12 12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14
* GNU General Public License for more details.
15
* 
15
*
16 16
* You should have received a copy of the GNU General Public License
17 17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 19
* MA  02110-1301, USA.
20
* 
20
*
21 21
*/
22 22

  
23 23
/*
24 24
* AUTHORS (In addition to CIT):
25 25
* 2009 {Iver T.I.}   {Task}
26 26
*/
27
 
27

  
28 28
package org.gvsig.gazetteer.impl;
29 29

  
30 30
import java.util.Iterator;
31
import java.util.Map;
31 32

  
32 33
import org.gvsig.gazetteer.GazetteerManager;
33 34
import org.gvsig.gazetteer.drivers.IGazetteerServiceDriver;
......
42 43
 */
43 44
public class DefaultGazetteerManager implements GazetteerManager{
44 45
	private static final Logger logger = LoggerFactory.getLogger(DefaultGazetteerManager.class);
45
	private static final String DRIVER_REGISTER_NAME = "GazetteerDrivers";	
46
	
46
	private static final String DRIVER_REGISTER_NAME = "GazetteerDrivers";
47

  
48
	private Map<String,String> usernames;
49

  
47 50
	/* (non-Javadoc)
48 51
	 * @see org.gvsig.gazetteer.GazetteerManager#getDriver(java.lang.String)
49 52
	 */
......
51 54
		ExtensionPointManager extensionPointManager = ToolsLocator
52 55
		.getExtensionPointManager();
53 56
		ExtensionPoint extensionPoint = extensionPointManager.add(DRIVER_REGISTER_NAME);
54
		
57

  
55 58
		Iterator extensions = extensionPoint.iterator();
56 59
		while (extensions.hasNext()){
57 60
			ExtensionPoint.Extension extension = (ExtensionPoint.Extension)extensions.next();
......
65 68
				logger.error("Impossible to create a gazetteer driver", e);
66 69
			} catch (IllegalAccessException e) {
67 70
				logger.error("Impossible to create a gazetteer driver", e);
68
			}			
71
			}
69 72
		}
70 73
		return null;
71 74
	}
......
79 82
		.getExtensionPointManager();
80 83
		ExtensionPoint extensionPoint = extensionPointManager.add(DRIVER_REGISTER_NAME);
81 84
		drivers = new IGazetteerServiceDriver[extensionPoint.getCount()];
82
		Iterator extensions = extensionPoint.iterator();		
83
		int i = 0;		
85
		Iterator extensions = extensionPoint.iterator();
86
		int i = 0;
84 87
		while (extensions.hasNext()){
85 88
			ExtensionPoint.Extension extension = (ExtensionPoint.Extension)extensions.next();
86 89
			IGazetteerServiceDriver driver;
87 90
			try {
88 91
				driver = (IGazetteerServiceDriver)extension.create();
89
				drivers[i] = driver;				
92
				drivers[i] = driver;
90 93
			} catch (InstantiationException e) {
91 94
				logger.error("Impossible to create a gazetteer driver", e);
92 95
			} catch (IllegalAccessException e) {
93 96
				logger.error("Impossible to create a gazetteer driver", e);
94
			}			
95
			
97
			}
98

  
96 99
			i++;
97 100
		}
98 101
		return drivers;
......
108 111
		extensionPoint.append(name.toLowerCase(), "", driver);
109 112
	}
110 113

  
114
    @Override
115
    public String getUsername(String protocol) {
116
        if (usernames!=null){
117
            return (String)usernames.get(protocol);
118
        }
119
        return null;
120
    }
121

  
122
    @Override
123
    public void setUsername(String protocol, String username) {
124
        if (usernames!=null){
125
            usernames.put(protocol, username);
126
        }
127
    }
128

  
129
    @Override
130
    public void setUsernames(Map usernames) {
131
        this.usernames=usernames;
132
    }
133

  
111 134
}
112 135

  
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.extension/src/main/resources-plugin/plugin-persistence.def
1
<?xml version="1.0"?>
2
<!--
3
Definitions of plugin persistence org.gvsig.gazetteer.extension.
4
 -->
5
<definitions>
6
  <version>1.0.0</version>
7
  <classes>
8
    <class name="org.gvsig.gazetteer.extension">
9
      <description>Persistence for the gazetteer plugin</description>
10
      <fields>
11
        <field name="usernames" type="Map" classOfItems="java.lang.String" mandatory="false">
12
          <description></description>
13
        </field>
14
      </fields>
15
    </class>
16
  </classes>
17
</definitions>
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.extension/src/main/resources-plugin/i18n/text.properties
140 140
wmsColumn=Mapa
141 141
wmsError=Ha ocurrido un error al cargar el mapa
142 142
_Unable_to_load_server_list=No se pudo cargar lista de servidores
143
_Insert_Geonames_username=Inserte su nombre de usuario de Geonames.\nPuede obtenerlo en http://www.geonames.org/login
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.extension/src/main/resources-plugin/i18n/text_en.properties
137 137
wmsColumn=Map
138 138
wmsError=Map loading error
139 139
_Unable_to_load_server_list=Unable to load server list
140
_Insert_Geonames_username=Insert Geonames user name.\nYou can get it at http://www.geonames.org/login
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.extension/src/main/java/org/gvsig/gazetteer/GazetteerExtension.java
43 43
import java.util.Calendar;
44 44
import java.util.Date;
45 45
import java.util.GregorianCalendar;
46
import java.util.HashMap;
47
import java.util.Map;
46 48

  
47 49
import org.gvsig.andami.IconThemeHelper;
48 50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.PluginsLocator;
52
import org.gvsig.andami.PluginsManager;
49 53
import org.gvsig.andami.persistence.serverData.ServerDataPersistence;
50 54
import org.gvsig.andami.plugins.Extension;
51 55
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
52 56
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
53 57
import org.gvsig.gazetteer.gui.ConnectDialog;
58
import org.gvsig.tools.dynobject.DynObject;
54 59
import org.gvsig.utils.swing.jcomboServer.ServerData;
55 60

  
56 61

  
......
58 63
 * @author Jorge Piera Llodra (piera_jor@gva.es)
59 64
 */
60 65
public class GazetteerExtension extends Extension {
61
	
66

  
62 67
	public static void main(String[] args) {
63
	    
68

  
64 69
	}
65 70

  
66 71
	public void initialize() {
......
72 77
	}
73 78

  
74 79
	public void postInitialize() {
75

  
80
	    PluginsManager pluginsManager = PluginsLocator.getManager();
81
	    PluginServices plugin=pluginsManager.getPlugin(this);
82
	    DynObject config = plugin.getPluginProperties();
83
	    Map usernames=(Map)config.getDynValue("usernames");
84
	    if (usernames==null){
85
	        usernames=new HashMap();
86
	        config.setDynValue("usernames", usernames);
87
	    }
88
	    GazetteerLocator.getGazetteerManager().setUsernames(usernames);
76 89
	}
77 90

  
78 91
	public void execute(String actionCommand) {
......
124 137

  
125 138
	/**
126 139
	 * It creates a server list by default
127
	 * 
140
	 *
128 141
	 * @return
129 142
	 */
130 143
	private ServerData[] getDefaultServers() {
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/org.gvsig.gazetteer.extension/src/main/java/org/gvsig/gazetteer/loaders/FeatureLoader.java
77 77

  
78 78
/**
79 79
 * This class is used to load a new feature like a layer in gvSIG
80
 * 
80
 *
81 81
 * @author Jorge Piera Llodra (piera_jor@gva.es)
82 82
 */
83
public class FeatureLoader {     
83
public class FeatureLoader {
84 84
	private static final Logger LOG =
85 85
        LoggerFactory.getLogger(FeatureLoader.class);
86
	
86

  
87 87
    private static GeometryManager geometryManager = GeometryLocator.getGeometryManager();
88 88
	private static MapContextManager mapContextManager = MapContextLocator.getMapContextManager();
89
	
89

  
90 90
	/**
91 91
	 * Coordinates Transformer
92 92
	 */
93
	private ICoordTrans coordTrans;	
94
	
93
	private ICoordTrans coordTrans;
94

  
95 95
	/**
96 96
	 * @param projection
97 97
	 * Server projection
98 98
	 */
99 99
	public FeatureLoader(String sProjection){
100
		AbstractViewPanel activeView = 
100
		AbstractViewPanel activeView =
101 101
			(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
102
		
103
		
102

  
103

  
104 104
		IProjection projection = CRSFactory.getCRS(sProjection);
105 105
		if (projection == null){
106 106
			projection = activeView.getMapControl().getViewPort().getProjection();
107 107
		}
108
		
108

  
109 109
		coordTrans=  projection.getCT(activeView.getMapControl().getViewPort().getProjection());
110 110
	}
111 111
	/**
......
126 126

  
127 127
		if (query.getOptions().getAspect().isGoTo()){
128 128
			focusCenter(feature);
129
		}        
129
		}
130 130

  
131 131
		return true;
132
	}    
132
	}
133 133

  
134 134
	/**
135
	 * This method focus the toponim in the center of the view 
135
	 * This method focus the toponim in the center of the view
136 136
	 * @param feature
137 137
	 * Feature that contains the coordinates
138 138
	 */
139 139
	private void focusCenter(Feature feature){
140
		AbstractViewPanel activeView = 
140
		AbstractViewPanel activeView =
141 141
			(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
142 142

  
143 143
		IProjection projection = activeView.getProjection();
144 144
		ViewPort viewPort = activeView.getMapControl().getViewPort();
145 145
		MapContext mapContext = activeView.getMapControl().getMapContext();
146 146
		Point2D point = getReprojectedPoint(feature.getCoordinates());
147
				
147

  
148 148
		if (viewPort.getAdjustedEnvelope() != null){
149 149
			Toolkit kit = Toolkit.getDefaultToolkit();
150 150
			double dpi = kit.getScreenResolution();
151
			
151

  
152 152
			Envelope envelope = viewPort.getAdjustedEnvelope();
153 153
			Rectangle2D rectangle = new Rectangle2D.Double(envelope.getLowerCorner().getX(),
154 154
					envelope.getLowerCorner().getY(),
155 155
					envelope.getUpperCorner().getX() - envelope.getLowerCorner().getX(),
156 156
					envelope.getUpperCorner().getY() - envelope.getLowerCorner().getY());
157
			
157

  
158 158
			Rectangle2D extent = projection.getExtent(rectangle,
159 159
			        new Double(25000).doubleValue(),
160 160
					new Double(viewPort.getImageWidth()).doubleValue(),
161 161
					new Double(viewPort.getImageHeight()).doubleValue(),
162 162
					viewPort.getMapUnits(),
163 163
					viewPort.getDistanceUnits(),
164
					dpi);				
164
					dpi);
165 165
			if (extent != null){
166 166
				try {
167 167
					envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D);
......
172 172
							point.getY() + extent.getHeight()/2,
173 173
							SUBTYPES.GEOM2D);
174 174
					envelope.setLowerCorner(loweCorner);
175
					envelope.setUpperCorner(upperCorner);				
175
					envelope.setUpperCorner(upperCorner);
176 176
					mapContext.zoomToEnvelope(envelope);
177 177
				} catch (CreateEnvelopeException e) {
178 178
				    LOG.error("Error creating the envelope", e);
179 179
				} catch (CreateGeometryException e) {
180 180
				    LOG.error("Error creating the envelope", e);
181
				}			
181
				}
182 182
			}
183
		}		
184
	} 
183
		}
184
	}
185 185

  
186 186
	/**
187 187
	 * It adds a new Label to the current view
......
191 191
	 * To remove or keep the old searches
192 192
	 */
193 193
	private void addAndDrawLabel(Feature feature,boolean isRemoveOldClicked,boolean isMarkedPlaceClicked){
194
		AbstractViewPanel activeView = 
194
		AbstractViewPanel activeView =
195 195
			(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow();
196 196
		MapContext mc = activeView.getMapControl().getMapContext();
197 197
		GraphicLayer lyr = mc.getGraphicsLayer();
198
		
198

  
199 199
		if (isRemoveOldClicked){
200 200
			lyr.clearAllGraphics();
201
		}	
202
		
201
		}
202

  
203 203
		if (isMarkedPlaceClicked){
204 204
			int pointSymbol = lyr.addSymbol(getSymbol());
205 205
			int textSymbol = lyr.addSymbol(getTextSymbol(feature.getName()));
206
			
207
				
206

  
207

  
208 208
			Point2D point2d = getReprojectedPoint(feature.getCoordinates());
209 209
			feature.setCoordinates(point2d);
210 210
			Point point;
......
217 217
				PluginServices.getMainFrame().enableControls();
218 218
			} catch (CreateGeometryException e) {
219 219
			    LOG.error("Error creating the point", e);
220
			}			
221
		}			
222
		
220
			}
221
		}
222

  
223 223
		mc.invalidate();
224 224
	}
225
	
225

  
226 226
	/**
227 227
	 * Creates a FSymbol
228 228
	 * @return
......
239 239
		theSymbol.setText(text);
240 240
		return theSymbol;
241 241
	}
242
	
242

  
243 243
	/**
244 244
	 * Creates a FSymbol
245 245
	 * @return
......
253 253
		theSymbol.setSize(4);
254 254
		return theSymbol;
255 255
	}
256
	
256

  
257 257
	/**
258 258
	 * Reprojects the new point
259 259
	 * @param ptOrig
......
263 263
	 */
264 264
	private Point2D getReprojectedPoint(Point2D ptOrigin){
265 265
		Point2D ptDest = null;
266
		return getCoordTrans().convert(ptOrigin, ptDest);
266
		ICoordTrans ct = getCoordTrans();
267
		if(ct != null) {
268
		    return ct.convert(ptOrigin, ptDest);
269
		}
270
		return ptOrigin;
267 271
	}
268 272
	/**
269 273
	 * @return the coordTrans
org.gvsig.gazetteer/trunk/org.gvsig.gazetteer/pom.xml
13 13
  <parent>
14 14
      <groupId>org.gvsig</groupId>
15 15
      <artifactId>org.gvsig.desktop</artifactId>
16
      <version>2.0.107</version>
16
      <version>2.0.109-SNAPSHOT</version>
17 17
  </parent>
18 18

  
19 19
  <url>https://devel.gvsig.org/redmine/projects/gvsig-gazetteer</url>

Also available in: Unified diff