Revision 2790 org.gvsig.raster.wmts/trunk/org.gvsig.raster.wmts/org.gvsig.raster.wmts.app.wmtsclient/src/main/java/org/gvsig/raster/wmts/app/wmtsclient/gui/wizard/WMTSWizard.java

View differences:

WMTSWizard.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
package org.gvsig.raster.wmts.app.wmtsclient.gui.wizard;
24 24

  
25 25
import java.awt.GridBagConstraints;
......
29 29
import java.io.File;
30 30
import java.io.FileInputStream;
31 31
import java.io.FileNotFoundException;
32
import java.io.FileOutputStream;
33 32
import java.io.IOException;
34 33
import java.net.MalformedURLException;
35 34
import java.net.URL;
......
40 39

  
41 40
import javax.swing.JOptionPane;
42 41

  
42
import org.apache.commons.io.IOUtils;
43 43
import org.cresques.cts.IProjection;
44
import org.gvsig.andami.Launcher;
45 44
import org.gvsig.andami.PluginServices;
46 45
import org.gvsig.andami.PluginsLocator;
46
import org.gvsig.andami.PluginsManager;
47 47
import org.gvsig.andami.persistence.serverData.ServerDataPersistence;
48 48
import org.gvsig.andami.ui.mdiManager.IWindow;
49 49
import org.gvsig.app.gui.WizardPanel;
50
import org.gvsig.app.gui.wizards.WizardListener;
51
import org.gvsig.app.gui.wizards.WizardListenerSupport;
52 50
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
53 51
import org.gvsig.compat.net.ICancellable;
54 52
import org.gvsig.fmap.dal.DALLocator;
......
64 62
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
65 63
import org.gvsig.gui.beans.Messages;
66 64
import org.gvsig.raster.util.CancelTaskImpl;
65
import org.gvsig.raster.wmts.app.wmtsclient.WMTSClientExtension;
67 66
import org.gvsig.raster.wmts.app.wmtsclient.layer.FLyrWMTS;
68 67
import org.gvsig.raster.wmts.io.WMTSDataParameters;
69 68
import org.gvsig.raster.wmts.io.WMTSServerExplorer;
......
74 73
import org.gvsig.raster.wmts.swing.WMTSSwingLocator;
75 74
import org.gvsig.raster.wmts.swing.WMTSWizardListener;
76 75
import org.gvsig.raster.wmts.swing.WmtsGUI;
77
import org.gvsig.tools.ToolsLocator;
78
import org.gvsig.tools.persistence.PersistenceManager;
76
import org.gvsig.tools.dynobject.DynObject;
79 77
import org.gvsig.utils.swing.jcomboServer.ServerData;
80 78
import org.slf4j.Logger;
81 79
import org.slf4j.LoggerFactory;
......
92 90
	private File                          pluginPersistenceFile  = null;
93 91
	private File                          serverPropertiesFile   = null;
94 92
	private ServerDataPersistence         serverDataPersistence  = null;
95
	private String                        serversPropsFile       = "servers.properties";
96
	
93
	private String                        serversPropsFilename       = "servers.properties";
94

  
97 95
	protected int                         page                   = 0;
98 96
	protected boolean                     connected              = false;
99 97
	private AddServerPanel                serverPanel            = null;
100 98
	private NextPrevPanel                 nextPrevPanel          = null;
101 99
	protected WmtsGUI                     wmtsParamsPanel        = null;
102
	
100

  
103 101
	private WMTSWizardListenerImpl        listenerSupport        = null;
104 102
	private static Preferences            fPrefs                 = Preferences.userRoot().node( "gvsig.wmts-wizard" );
105 103
	private boolean                       refreshing             = fPrefs.getBoolean("refresh_capabilities", false);
......
109 107
	private WMTSDataParameters            storeParams            = null;
110 108
	private WMTSServerExplorer            explorer               = null;
111 109

  
110
    private PluginServices plugin = null;
111

  
112 112
	public class WMTSWizardListenerImpl implements WMTSWizardListener {
113
		
113

  
114 114
		public void correctlyConfiguredEvent(boolean finishable) {
115 115
			callStateChanged(finishable);
116 116
		}
......
132 132
		firstPage = 0;
133 133
		page = firstPage;
134 134
		initialize();
135
		
135

  
136 136
		IWindow[] windows = PluginServices.getMDIManager().getAllWindows();
137 137
		for (int i = 0; i < windows.length; i++) {
138 138
			if(windows[i] instanceof AbstractViewPanel)
......
140 140
		}
141 141
		cancel = new CancelTaskImpl();
142 142
	}
143
	
143

  
144 144
	/**
145 145
	 * This method initializes this
146 146
	 */
147 147
	private void initialize() {
148
        PluginsManager pluginsManager = PluginsLocator.getManager();
149
        this.plugin = pluginsManager.getPlugin(WMTSClientExtension.class);
150

  
148 151
		setTabName("WMTS");
149 152
		setLayout(new GridBagLayout());
150 153
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
......
152 155
		gridBagConstraints1.weightx = 1.0;
153 156
		gridBagConstraints1.weighty = 1.0;
154 157
		gridBagConstraints1.insets = new java.awt.Insets(2, 12, 10, 12);
155
		
158

  
156 159
		setVisible(true);
157 160
		add(getIserverPanel(), gridBagConstraints1);
158
		
161

  
159 162
		gridBagConstraints1.insets = new java.awt.Insets(2, 0, 10, 0);
160 163
		add(getPanelPage2(), gridBagConstraints1);
161 164
		gridBagConstraints1.fill = GridBagConstraints.HORIZONTAL;
......
166 169
		activarVisualizarBotones();
167 170
		getIserverPanel().getTxtHost().setServerList(getServerDataPersistence().getArrayOfServerData());
168 171
	}
169
	
172

  
170 173
	/**
171 174
	 * This method checks for the options selected within the configuration
172 175
	 * dialog are correct.
......
176 179
	public boolean areSettingsValid() {
177 180
		return getPanelPage2().isCorrectlyConfigured();
178 181
	}
179
	
182

  
180 183
	private WMTSWizardListenerImpl getWizardListenerSupport() {
181 184
		if(listenerSupport == null) {
182 185
			listenerSupport = new WMTSWizardListenerImpl();
183 186
		}
184 187
		return listenerSupport;
185 188
	}
186
	
189

  
187 190
	/**
188 191
	 * Thread to connect to a WMTS server
189 192
	 * @author Nacho Brodin (nachobrodin@gmail.com)
......
192 195
		private ICancellable              cancel      = null;
193 196
		private WMTSServerExplorer        explorer    = null;
194 197
		private WMTSWizard                wizard      = null;
195
		
198

  
196 199
		public ConnectThread(ICancellable cancel, WMTSServerExplorer explorer, WMTSWizard wizard) {
197 200
			this.cancel = cancel;
198 201
			this.explorer = explorer;
199 202
			this.wizard = wizard;
200 203
		}
201
		
204

  
202 205
	    public void run() {
203 206
	    	if(!checkHostAndNetwork())
204 207
	    		return;
205
	    	
208

  
206 209
	    	try {
207 210
	    		explorer.connect(cancel);
208 211
	    		saveServerListFromComboBox();
......
213 216
			} finally {
214 217
				((CancelTaskImpl)cancel).setCanceled(false);
215 218
			}
216
			
219

  
217 220
			DataStoreParameters parameters = explorer.getStoredParameters();
218 221
			if(parameters instanceof WMTSDataParameters) {
219 222
				storeParams = (WMTSDataParameters) parameters;
220 223
			}
221
			
224

  
222 225
			if(parameters instanceof TileDataParameters) {
223 226
				DataParameters p = ((TileDataParameters) parameters).getDataParameters();
224 227
				if(p instanceof WMTSDataParameters)
225 228
					storeParams = (WMTSDataParameters) p;
226 229
			}
227
			
230

  
228 231
			wizard.rellenarControles();
229 232
	    }
230 233
	}
231
	
234

  
232 235
	/**
233 236
	 * Checks if the network and host are reachable
234 237
	 * @return true if both are reachable and false if they are not
......
249 252
		try {
250 253
			String host = getIserverPanel().getTxtHost().getModel().getSelectedItem().toString();
251 254
			WMTSServiceIdentification id = explorer.getOGCClient().getServiceIdentification();
252
			
255

  
253 256
			getIserverPanel().setTitle(id.getTitle());
254 257
			getNextPrevPanel().getJLabelServerTypeValue().setText(id.getServiceType());
255 258
			getIserverPanel().setTextInfo(id.getAbstract());
......
269 272
			getIserverPanel().getBtnCancel().setEnabled(false);
270 273
		}
271 274
	}
272
	
275

  
273 276
	public void actionPerformed(ActionEvent e) {
274 277
		if(e.getSource() == getIserverPanel().getBtnConnect()) {
275 278
			getIserverPanel().getBtnCancel().setEnabled(true);
276 279
			getIserverPanel().setTextInfo("Trying to connect...");
277
			
280

  
278 281
			String host = getIserverPanel().getTxtHost().getModel().getSelectedItem().toString();
279 282
			try {
280 283
				DataManager datamanager = DALLocator.getDataManager();
281 284
				WMTSServerExplorerParameters explorerParams = (WMTSServerExplorerParameters) datamanager.createServerExplorerParameters(WMTSServerExplorer.NAME);
282 285
				explorerParams.setHost(host);
283 286
				explorerParams.setLongitudeFirst(getIserverPanel().getChkInvertAxisOrder().isSelected());
284
				
287

  
285 288
				explorer = (WMTSServerExplorer) datamanager.createServerExplorer(explorerParams);
286
				
289

  
287 290
				((CancelTaskImpl)cancel).setCanceled(false);
288 291
				new ConnectThread(cancel, explorer, this).start();
289
				
292

  
290 293
			} catch (InitializeException e1) {
291 294
				getIserverPanel().setTextInfo("Error creating WMTS layer");
292 295
			} catch (ProviderNotRegisteredException e1) {
......
295 298
				getIserverPanel().setTextInfo("Parameters not valid. I cannot connect.");
296 299
			}
297 300
		}
298
		
301

  
299 302
		if(e.getSource() == getIserverPanel().getBtnCancel()) {
300 303
			getIserverPanel().setTextInfo("Cancelled...");
301 304
			((CancelTaskImpl)cancel).setCanceled(true);
302 305
			getIserverPanel().getBtnCancel().setEnabled(false);
303 306
		}
304
		
307

  
305 308
		if(e.getSource() == getNextPrevPanel().getBtnPrev()) {
306 309
			page --;
307 310
			if (page > firstPage) {
......
321 324
			}
322 325
			//getWizardListenerSupport().callStateChanged(getPanelPage2().isCorrectlyConfigured());
323 326
		}
324
		
327

  
325 328
		if(e.getSource() == getNextPrevPanel().getBtnNext()) {
326 329
			if (page > firstPage) {
327 330
				// si page es mayor que cero, vamos enfocando las
......
335 338
			activarVisualizarBotones();
336 339
		}
337 340
	}
338
	
341

  
339 342
	private AddServerPanel getIserverPanel() {
340 343
		if (serverPanel == null) {
341 344
			serverPanel = WMTSSwingLocator.getSwingManager().createAddServerPanel();
......
343 346
			serverPanel.getBtnConnect().addActionListener(this);
344 347
		}
345 348
		return serverPanel;
346
	}	
347
	
349
	}
350

  
348 351
	/**
349 352
	 * This method initializes jPanel
350 353
	 *
......
359 362
		return nextPrevPanel;
360 363
	}
361 364

  
362
	
365

  
363 366
	protected WmtsGUI getPanelPage2() {
364 367
		if (wmtsParamsPanel == null){
365 368
			wmtsParamsPanel = WMTSSwingLocator.getSwingManager().createMainWizard(getWizardListenerSupport());
......
397 400
			//getWizardListenerSupport().callStateChanged(getPanelPage2().isCorrectlyConfigured());
398 401
		}
399 402
	}
400
	
403

  
401 404
	/**
402 405
	 * Gets the list of servers that have been persisted or added in the properties file
403 406
	 * @return
404 407
	 */
405 408
	private ServerDataPersistence getServerDataPersistence() {
406
		if(serverDataPersistence == null) {
407
			PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
409
        DynObject pluginProperties = this.plugin.getPluginProperties();
410
        ServerDataPersistence servers =  (ServerDataPersistence) pluginProperties.getDynValue("servers");
408 411

  
409
			//Lee los servidores de la persistencia
410
			try {
411
				FileInputStream is = new FileInputStream(getPluginPersistenceFile());
412
				serverDataPersistence = (ServerDataPersistence) persistenceManager.getObject(is);
413
			} catch (FileNotFoundException e) {
414
				logger.info("Persistence file not found for WMS plugin", e);
415
			}
412
        if( servers == null || servers.isEmpty() ) {
413
            if( servers == null ) {
414
                servers = new ServerDataPersistence(ServerData.SERVER_TYPE_WMTS);
415
                pluginProperties.setDynValue("servers", servers);
416
            }
417
            //Lee los servidores del properties y los mezcla con los que hay en la persistencia
418
            Properties prop = new Properties();
419
            File propFile = new File(this.plugin.getPluginDirectory(),serversPropsFilename);
420
            FileInputStream fis = null;
421
            try {
422
                    fis = new FileInputStream(propFile);
423
                    prop.load(fis);
424
            } catch (FileNotFoundException e) {
425
                    logger.info("Properties file '"+propFile.getAbsolutePath()+"' not found", e);
426
            } catch (IOException e) {
427
                    logger.info("Error reading properties file '"+propFile.getAbsolutePath()+"'.", e);
428
            } finally {
429
                IOUtils.closeQuietly(fis);
430
            }
416 431

  
417
			if(serverDataPersistence == null)
418
				serverDataPersistence = new ServerDataPersistence(ServerData.SERVER_TYPE_WMTS);
432
            for (int i = 0; i < prop.size(); i++) {
433
                Object value = prop.get("server[" + i + "]");
434
                servers.addServerData(new ServerData(value.toString(), ServerData.SERVER_TYPE_WMTS));
435
            }
419 436

  
420
			//Lee los servidores del properties y los mezcla con los que hay en la persistencia
421
			Properties prop = new Properties();
422
			try {
423
				prop.load(new FileInputStream(getServerPropertiesFile()));
424
			} catch (FileNotFoundException e) {
425
				logger.info("Properties file not found for WMS plugin", e);
426
			} catch (IOException e) {
427
				logger.info("Error reading properties file", e);
428
			}
437
        }
438
        return servers;
429 439

  
430
			boolean newProjectServers = false;
431
			for (int i = 0; i < prop.size(); i++) {
432
				Object value = prop.get("server[" + i + "]");
433
				if(!serverDataPersistence.existsServer(value.toString())) {
434
					serverDataPersistence.addServerData(new ServerData(value.toString(), ServerData.SERVER_TYPE_WMTS));
435
					newProjectServers = true;
436
				}
437
			}
438 440

  
439
			//Si hay servidores nuevos los persiste
440
			if(newProjectServers) {
441
				try {
442
					FileOutputStream os = new FileOutputStream(getPluginPersistenceFile());
443
					persistenceManager.putObject(os, serverDataPersistence);
444
				} catch (FileNotFoundException e) {
445
					logger.info("Persistence file not found for WMS plugin", e);
446
				}
447
			}
448
		}
449

  
450
		return serverDataPersistence;
451 441
	}
452
	
442

  
453 443
	/**
454 444
	 * Saves the list of server reading from the <code>ComboBox</code>
455 445
	 */
456 446
	private void saveServerListFromComboBox() {
457
		PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
458
		ServerDataPersistence persistence = new ServerDataPersistence(ServerData.SERVER_TYPE_WMTS);
447
        ServerDataPersistence persistence = getServerDataPersistence();
459 448
		String urlSelected = getIserverPanel().getTxtHost().getModel().getSelectedItem().toString();
460
		
449

  
461 450
		boolean isSelectedInModel = false;
462 451
		for (int i = 0; i < getIserverPanel().getTxtHost().getItemCount(); i++) {
463 452
			String url = getIserverPanel().getTxtHost().getItemAt(i).toString();
464 453
			persistence.addServerData(new ServerData(url, ServerData.SERVER_TYPE_WMTS));
465
			if(url.equals(urlSelected))
454
			if(url.equals(urlSelected)) {
466 455
				isSelectedInModel = true;
456
			}
467 457
		}
468
		
458

  
469 459
		if(!isSelectedInModel)
470 460
			persistence.addServerData(new ServerData(urlSelected, ServerData.SERVER_TYPE_WMTS));
471
		
472
		if(getServerDataPersistence().compareTo(persistence) != 0) {
473
			try {
474
				FileOutputStream os = new FileOutputStream(getPluginPersistenceFile());
475
				persistenceManager.putObject(os, persistence);
476
			} catch (FileNotFoundException e) {
477
				logger.info("Persistence file not found for WMS plugin", e);
478
			}
479
		}
461

  
480 462
	}
481
	
482
	/**
483
	 * Gets the persistence file of this plugin
484
	 * @return
485
	 */
486
	private File getPluginPersistenceFile() {
487
		if(pluginPersistenceFile == null) {
488
			File persistenceFolder = new File(Launcher.getAppHomeDir()
489
					+ File.separator + "plugins" + File.separator + pluginName);
490
			
491
			if (!persistenceFolder.exists()) {
492
				persistenceFolder.mkdirs();
493
			}
494
			
495
			pluginPersistenceFile = new File(persistenceFolder.getAbsolutePath() + 
496
				File.separator + 
497
				"plugin-persistence.dat");
498
		}
499
		return pluginPersistenceFile;
500
	}
501
	
502
	/**
503
	 * Gets the file which contains the default list of servers
504
	 * @return
505
	 */
506
	private File getServerPropertiesFile() {
507
		if(serverPropertiesFile == null) {
508
			File pluginFolder = PluginsLocator.getManager().getPluginsFolder();
509
			serverPropertiesFile = new File(pluginFolder.getAbsolutePath() + 
510
					File.separator + 
511
					pluginName + 
512
					File.separator + 
513
					serversPropsFile);
514
		}
515
		return serverPropertiesFile;
516
	}
517 463

  
518 464
	/**
519 465
	 * Devuelve el host que est� escrito en el cuadro de texto del host o null
......
535 481

  
536 482
	public void initWizard() {
537 483
	}
538
	
484

  
539 485
	@Override
540 486
	public DataStoreParameters[] getParameters() {
541 487
		return null;
......
552 498
			RasterDataParameters[] paramsList = getPanelPage2().getDataParameters();
553 499
			List<FLyrWMTS> lyrList = getLayerList(paramsList);
554 500
			for (int i = 0; i < lyrList.size(); i++) {
555
				getMapCtrl().getMapContext().getLayers().addLayer(lyrList.get(i));	
501
				getMapCtrl().getMapContext().getLayers().addLayer(lyrList.get(i));
556 502
			}
557 503
		} catch (LoadLayerException e) {
558 504
			logger.info("Persistence file not found for WMS plugin", e);
559 505
		}
560 506
	}
561
	
507

  
562 508
	public List<FLyrWMTS> getLayerList(RasterDataParameters[] paramsList) throws LoadLayerException {
563 509
		List<FLyrWMTS> layerList = new ArrayList<FLyrWMTS>();
564 510
		for (int i = 0; i < paramsList.length; i++) {
......
574 520
					if(p instanceof WMTSDataParameters)
575 521
						title = ((WMTSDataParameters) p).getLayer().getTitle();
576 522
				}
577
				
523

  
578 524
				layer.setName(title);
579 525
				layer.setExplorer(explorer);
580 526
				//Ojo! Pruebas!
......
586 532
		}
587 533
		return layerList;
588 534
	}
589
}  
535
}

Also available in: Unified diff