gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / layerdatatype / ProcessEndActions.java @ 4181
History | View | Annotate | Download (6.12 KB)
1 | 2480 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2011-2012 Prodevelop S.L
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
18 | * MA 02110-1301, USA.
|
||
19 | *
|
||
20 | */
|
||
21 | package org.gvsig.raster.tools.app.basic.tool.layerdatatype; |
||
22 | |||
23 | import java.io.File; |
||
24 | 4181 | fdiaz | import java.net.URI; |
25 | import java.net.URISyntaxException; |
||
26 | 2480 | nbrodin | import java.util.HashMap; |
27 | |||
28 | import org.gvsig.andami.PluginServices; |
||
29 | import org.gvsig.andami.ui.mdiManager.IWindow; |
||
30 | import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
||
31 | import org.gvsig.fmap.dal.DALLocator; |
||
32 | import org.gvsig.fmap.dal.DataManager; |
||
33 | import org.gvsig.fmap.dal.DataStore; |
||
34 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
35 | import org.gvsig.fmap.dal.coverage.store.parameter.RasterDataParameters; |
||
36 | import org.gvsig.fmap.dal.coverage.util.ProviderServices; |
||
37 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
38 | import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
||
39 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
40 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
41 | import org.gvsig.fmap.mapcontext.MapContextManager; |
||
42 | import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
||
43 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
44 | import org.gvsig.fmap.mapcontext.layers.FLayers; |
||
45 | import org.gvsig.raster.algorithm.process.IProcessActions; |
||
46 | import org.gvsig.raster.swing.RasterSwingLibrary; |
||
47 | import org.gvsig.raster.swing.RasterSwingLocator; |
||
48 | import org.gvsig.raster.tools.algorithm.layerdatatype.LayerDatatypeProcess; |
||
49 | import org.gvsig.raster.util.RasterNotLoadException; |
||
50 | |||
51 | /**
|
||
52 | * Actions when the process finalizes or is interrupted
|
||
53 | 4181 | fdiaz | *
|
54 | 2480 | nbrodin | * Nacho Brodin (nachobrodin@gmail.com)
|
55 | */
|
||
56 | public class ProcessEndActions implements IProcessActions { |
||
57 | private IWindow window = null; |
||
58 | private FLayer inputLyr = null; |
||
59 | |||
60 | /**
|
||
61 | * Constructor
|
||
62 | * @param window
|
||
63 | * @param lyr
|
||
64 | * The input layer is only used to select the view where the new layer be loaded
|
||
65 | */
|
||
66 | public ProcessEndActions(IWindow window, FLayer lyr) {
|
||
67 | this.window = window;
|
||
68 | this.inputLyr = lyr;
|
||
69 | } |
||
70 | 4181 | fdiaz | |
71 | 2480 | nbrodin | @SuppressWarnings("unchecked") |
72 | public void end(Object params) { |
||
73 | if(window != null) { |
||
74 | PluginServices.getMDIManager().closeWindow(window); |
||
75 | window = null;
|
||
76 | } |
||
77 | if(params instanceof HashMap<?, ?>) { |
||
78 | HashMap<String, Object> map = (HashMap<String, Object>) params; |
||
79 | String fName = (String)map.get(LayerDatatypeProcess.FILENAME); |
||
80 | long milis = (Long)map.get(LayerDatatypeProcess.TIME); |
||
81 | processFinalize(fName, milis); |
||
82 | RasterSwingLocator.getSwingManager().showSummaryProcessDialog(fName, milis); |
||
83 | } |
||
84 | |||
85 | } |
||
86 | 4181 | fdiaz | |
87 | 2480 | nbrodin | /**
|
88 | * Acciones que se realizan al finalizar de crear los recortes de imagen.
|
||
89 | * Este m?todo es llamado por el thread TailRasterProcess al finalizar.
|
||
90 | */
|
||
91 | private void processFinalize(String fileName, long milis) { |
||
92 | if (!new File(fileName).exists()) |
||
93 | return;
|
||
94 | |||
95 | if (RasterSwingLibrary.messageBoxYesOrNot("cargar_toc", this)) { |
||
96 | try {
|
||
97 | loadLayer(fileName, fileName.substring(fileName.lastIndexOf(File.separator) + 1)); |
||
98 | } catch (RasterNotLoadException e) {
|
||
99 | RasterSwingLibrary.messageBoxError("error_load_layer", e);
|
||
100 | } |
||
101 | } |
||
102 | } |
||
103 | 4181 | fdiaz | |
104 | 2480 | nbrodin | /**
|
105 | 4181 | fdiaz | * Gets the view
|
106 | 2480 | nbrodin | * @return
|
107 | */
|
||
108 | private AbstractViewPanel getView() {
|
||
109 | IWindow[] w = PluginServices.getMDIManager().getAllWindows();
|
||
110 | for (int i = 0; i < w.length; i++) { |
||
111 | if(w[i] instanceof AbstractViewPanel) { |
||
112 | FLayers lyrs = ((AbstractViewPanel)w[i]).getMapControl().getMapContext().getLayers(); |
||
113 | for (int j = 0; j < lyrs.getLayersCount(); j++) { |
||
114 | FLayer lyr = lyrs.getLayer(j); |
||
115 | if(inputLyr == lyr) {
|
||
116 | return ((AbstractViewPanel) w[i]);
|
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | } |
||
121 | return null; |
||
122 | } |
||
123 | 4181 | fdiaz | |
124 | 2480 | nbrodin | @SuppressWarnings("deprecation") |
125 | private void loadLayer(String fileName, String layerName) throws RasterNotLoadException { |
||
126 | if(fileName == null) |
||
127 | return;
|
||
128 | |||
129 | //Seleccionamos la vista de gvSIG
|
||
130 | AbstractViewPanel theView = getView(); |
||
131 | |||
132 | theView.getMapControl().getMapContext().beginAtomicEvent(); |
||
133 | |||
134 | try {
|
||
135 | DataManager dataManager = DALLocator.getDataManager(); |
||
136 | 4181 | fdiaz | |
137 | 2480 | nbrodin | ProviderServices provServ = RasterLocator.getManager().getProviderServices(); |
138 | RasterDataParameters storeParameters = provServ.createParameters(fileName); |
||
139 | 4181 | fdiaz | URI uri;
|
140 | try {
|
||
141 | uri = new URI(fileName); |
||
142 | } catch (URISyntaxException e1) { |
||
143 | throw new RasterNotLoadException("Can't create URI from "+fileName, e1); |
||
144 | } |
||
145 | storeParameters.setURI(uri); |
||
146 | |||
147 | 2480 | nbrodin | MapContextManager mcm = MapContextLocator.getMapContextManager(); |
148 | 4181 | fdiaz | |
149 | 2480 | nbrodin | DataStore dataStore = null;
|
150 | try {
|
||
151 | dataStore = dataManager.createStore(storeParameters); |
||
152 | } catch (ValidateDataParametersException e) {
|
||
153 | throw new RasterNotLoadException("Error al cargar la capa."); |
||
154 | } catch (InitializeException e) {
|
||
155 | throw new RasterNotLoadException("Error al cargar la capa."); |
||
156 | } catch (ProviderNotRegisteredException e) {
|
||
157 | throw new RasterNotLoadException("Error al cargar la capa."); |
||
158 | } |
||
159 | 4181 | fdiaz | |
160 | 2480 | nbrodin | if(layerName == null) { |
161 | int endIndex = fileName.lastIndexOf("."); |
||
162 | if (endIndex < 0) |
||
163 | endIndex = fileName.length(); |
||
164 | 4181 | fdiaz | |
165 | 2480 | nbrodin | layerName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, endIndex); |
166 | } |
||
167 | 4181 | fdiaz | |
168 | 2480 | nbrodin | FLayer lyr = mcm.createLayer(layerName, dataStore); |
169 | theView.getMapControl().getMapContext().getLayers().addLayer(lyr); |
||
170 | |||
171 | } catch (LoadLayerException e) {
|
||
172 | 4181 | fdiaz | throw new RasterNotLoadException("Error al cargar la capa.", e); |
173 | } |
||
174 | 2480 | nbrodin | theView.getMapControl().getMapContext().invalidate(); |
175 | theView.getMapControl().getMapContext().endAtomicEvent(); |
||
176 | } |
||
177 | 4181 | fdiaz | |
178 | 2480 | nbrodin | public void interrupted() { |
179 | |||
180 | } |
||
181 | 4181 | fdiaz | |
182 | 2480 | nbrodin | public void updateProgress(int current, int total) { |
183 | 4181 | fdiaz | |
184 | 2480 | nbrodin | } |
185 | } |