svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / OpenLayerOnStart.java @ 46737
History | View | Annotate | Download (5.98 KB)
1 | 43328 | jjdelcerro | package org.gvsig.app.extension; |
---|---|---|---|
2 | |||
3 | import java.awt.GridBagConstraints; |
||
4 | import java.beans.PropertyVetoException; |
||
5 | import java.io.File; |
||
6 | import java.util.ArrayList; |
||
7 | import java.util.List; |
||
8 | import org.apache.commons.io.FilenameUtils; |
||
9 | import org.apache.commons.lang3.StringUtils; |
||
10 | import org.cresques.cts.IProjection; |
||
11 | import org.gvsig.andami.Arguments; |
||
12 | import org.gvsig.andami.PluginsLocator; |
||
13 | import org.gvsig.andami.PluginsManager; |
||
14 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
15 | import org.gvsig.app.ApplicationLocator; |
||
16 | import org.gvsig.app.ApplicationManager; |
||
17 | import org.gvsig.app.project.Project; |
||
18 | import org.gvsig.app.project.ProjectManager; |
||
19 | import org.gvsig.app.project.documents.DocumentManager; |
||
20 | import org.gvsig.app.project.documents.view.ViewDocument; |
||
21 | import org.gvsig.app.project.documents.view.ViewManager; |
||
22 | import org.gvsig.app.project.documents.view.gui.IView; |
||
23 | import org.gvsig.fmap.dal.DALLocator; |
||
24 | import org.gvsig.fmap.dal.DataManager; |
||
25 | import org.gvsig.fmap.dal.DataServerExplorer; |
||
26 | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
||
27 | import org.gvsig.fmap.dal.DataStore; |
||
28 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
29 | import org.gvsig.fmap.dal.DataTypes; |
||
30 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemFileFilter; |
||
31 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
32 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
33 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
34 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
35 | import org.gvsig.tools.dynobject.DynField; |
||
36 | import org.slf4j.Logger; |
||
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | public class OpenLayerOnStart implements Runnable { |
||
40 | |||
41 | private static final Logger logger = LoggerFactory.getLogger(OpenLayerOnStart.class); |
||
42 | |||
43 | @Override
|
||
44 | public void run() { |
||
45 | PluginsManager PluginManager = PluginsLocator.getManager(); |
||
46 | DataManager dataManager = DALLocator.getDataManager(); |
||
47 | ApplicationManager application = ApplicationLocator.getManager(); |
||
48 | ProjectManager projectManager = application.getProjectManager(); |
||
49 | DocumentManager viewManager = projectManager.getDocumentManager(ViewManager.TYPENAME); |
||
50 | MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
||
51 | Project project = application.getProjectManager().getCurrentProject(); |
||
52 | |||
53 | Arguments args = PluginManager.getArguments(); |
||
54 | List<String> params = new ArrayList<>(args.parameters()); |
||
55 | params.remove(0);
|
||
56 | params.remove(0);
|
||
57 | for( String param : params ) { |
||
58 | if( !StringUtils.isEmpty(param) && param.endsWith(Project.FILE_EXTENSION) ) {
|
||
59 | try {
|
||
60 | File f = new File(param); |
||
61 | application.getUIManager().closeAllWindows(); |
||
62 | project = projectManager.createProject(); |
||
63 | project.loadState(f); |
||
64 | projectManager.setCurrentProject(project); |
||
65 | } catch (Throwable th) { |
||
66 | logger.warn("Can't load project '" + param + "'.", th); |
||
67 | } |
||
68 | break;
|
||
69 | } |
||
70 | } |
||
71 | |||
72 | ViewDocument viewDoc = null;
|
||
73 | FilesystemServerExplorer explorer = null;
|
||
74 | FilesystemFileFilter filter = null;
|
||
75 | IProjection proj = null;
|
||
76 | for( String param : params ) { |
||
77 | try {
|
||
78 | if( FilenameUtils.isExtension(param, "gvproj") ) { |
||
79 | continue;
|
||
80 | } |
||
81 | if( explorer == null ) { |
||
82 | DataServerExplorerParameters explorerparams = dataManager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
83 | explorer = (FilesystemServerExplorer) dataManager.openServerExplorer(FilesystemServerExplorer.NAME, explorerparams); |
||
84 | filter = explorer.getFilter(DataServerExplorer.MODE_GEOMETRY | DataServerExplorer.MODE_RASTER, "All formats");
|
||
85 | } |
||
86 | |||
87 | File f = new File(param); |
||
88 | if( filter.accept(f) ) {
|
||
89 | if( viewDoc == null ) { |
||
90 | viewDoc = (ViewDocument) application.getActiveDocument(ViewManager.TYPENAME); |
||
91 | if( viewDoc==null || !viewDoc.getMapContext().getLayers().isEmpty() ) { |
||
92 | application.getUIManager().closeAllWindows(); |
||
93 | viewDoc = (ViewDocument) viewManager.createDocument(); |
||
94 | project.addDocument(viewDoc); |
||
95 | } |
||
96 | proj = viewDoc.getMapContext().getProjection(); |
||
97 | IView viewWindow = (IView) viewManager.getMainWindow(viewDoc); |
||
98 | application.getUIManager().addWindow(viewWindow, GridBagConstraints.CENTER);
|
||
99 | try {
|
||
100 | application.getUIManager().setMaximum((IWindow) viewWindow, true);
|
||
101 | } catch (PropertyVetoException e) { |
||
102 | |||
103 | } |
||
104 | } |
||
105 | DataStoreParameters storeParams = explorer.createStoreParameters(f); |
||
106 | // Buscamos por el parametro de la proyeccion
|
||
107 | // que sean obligatorios y est?n a null
|
||
108 | // y le ponemos la proyeccion de la vista
|
||
109 | DynField[] fields = storeParams.getDynClass().getDynFields();
|
||
110 | for (DynField field : fields) {
|
||
111 | if (field.getType() == DataTypes.CRS && field.isMandatory()) {
|
||
112 | if (storeParams.getDynValue(field.getName()) == null) { |
||
113 | storeParams.setDynValue(field.getName(), proj); |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | DataStore store = dataManager.openStore(storeParams.getDataStoreName(), storeParams); |
||
118 | FLayer layer = mapContextManager.createLayer(store.getName(), store); |
||
119 | viewDoc.getMapContext().getLayers().add(layer); |
||
120 | } |
||
121 | } catch (Throwable th) { |
||
122 | logger.warn("Can't load layer '" + param + "'.", th); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | } |
||
127 | } |