gvsig-raster / org.gvsig.raster.mosaic / trunk / org.gvsig.raster.mosaic / org.gvsig.raster.mosaic.app / src / main / java / org / gvsig / raster / mosaic / app / MainDialogActions.java @ 2193
History | View | Annotate | Download (6.39 KB)
1 |
package org.gvsig.raster.mosaic.app; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.List; |
7 |
|
8 |
import org.gvsig.andami.PluginServices; |
9 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
10 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
11 |
import org.gvsig.fmap.dal.coverage.RasterLocator; |
12 |
import org.gvsig.fmap.dal.coverage.exception.NotSupportedExtensionException; |
13 |
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException; |
14 |
import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
15 |
import org.gvsig.fmap.dal.exception.InitializeException; |
16 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
17 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
18 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
19 |
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException; |
20 |
import org.gvsig.i18n.Messages; |
21 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
22 |
import org.gvsig.raster.impl.provider.RasterProvider; |
23 |
import org.gvsig.raster.mosaic.io.MosaicDataParameters; |
24 |
import org.gvsig.raster.mosaic.io.MosaicRasterFormat; |
25 |
import org.gvsig.raster.mosaic.swing.main.AddFilesEvent; |
26 |
import org.gvsig.raster.mosaic.swing.main.AddFilesListener; |
27 |
import org.gvsig.raster.mosaic.swing.main.AddFilesPanel; |
28 |
import org.gvsig.raster.swing.RasterSwingLibrary; |
29 |
import org.gvsig.tools.locator.LocatorException; |
30 |
import org.slf4j.Logger; |
31 |
import org.slf4j.LoggerFactory; |
32 |
|
33 |
public class MainDialogActions implements AddFilesListener { |
34 |
private Logger log = LoggerFactory.getLogger(MainDialogActions.class); |
35 |
private String file = null; |
36 |
private String folder = null; |
37 |
private AddFilesPanel addFilesPanel = null; |
38 |
private AbstractViewPanel selectedView = null; |
39 |
|
40 |
public MainDialogActions(AddFilesPanel panel, String file, String folder) { |
41 |
this.file = file;
|
42 |
this.folder = folder;
|
43 |
this.addFilesPanel = panel;
|
44 |
} |
45 |
|
46 |
public void loadLayer() { |
47 |
RasterDataStore mainRasterStore = (RasterDataStore)addFilesPanel.getResult(); |
48 |
if(mainRasterStore == null) { |
49 |
return;
|
50 |
} |
51 |
|
52 |
try {
|
53 |
saveMosaicFileLayer(mainRasterStore); |
54 |
} catch (IOException e) { |
55 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_saving_mosaic"), addFilesPanel, e);
|
56 |
} |
57 |
|
58 |
try {
|
59 |
loadLayerInView(mainRasterStore); |
60 |
} catch (LoadLayerException e) {
|
61 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_loading_layer"), addFilesPanel, e);
|
62 |
} |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Saves the new layer in disk
|
67 |
* @param layerName
|
68 |
* @param file
|
69 |
* @param uriList
|
70 |
* @throws IOException
|
71 |
*/
|
72 |
private String saveMosaicFileLayer(RasterDataStore mainRasterStore) throws IOException { |
73 |
MosaicDataParameters params = (MosaicDataParameters)mainRasterStore.getParameters(); |
74 |
ArrayList<File> uriList = new ArrayList<File>(); |
75 |
List<RasterProvider> providers = params.getProviders();
|
76 |
|
77 |
for (int i = 0; i < providers.size(); i++) { |
78 |
uriList.add(new File(providers.get(i).getURI())); |
79 |
} |
80 |
|
81 |
try {
|
82 |
return MosaicRasterFormat.saveMosaicRasterFormat(file, folder, uriList);
|
83 |
} catch (IOException e) { |
84 |
RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_mrf"), addFilesPanel, e);
|
85 |
} |
86 |
return null; |
87 |
} |
88 |
|
89 |
private void loadLayerInView(RasterDataStore mainRasterStore) throws LoadLayerException { |
90 |
MapContextManager mcm = MapContextLocator.getMapContextManager(); |
91 |
FLyrRaster lyr = (FLyrRaster) mcm.createLayer(file, mainRasterStore); |
92 |
lyr.reload(); |
93 |
|
94 |
getView().getMapControl().getMapContext().beginAtomicEvent(); |
95 |
getView().getMapControl().getMapContext().getLayers().addLayer(lyr); |
96 |
getView().getMapControl().getMapContext().invalidate(); |
97 |
getView().getMapControl().getMapContext().endAtomicEvent(); |
98 |
} |
99 |
|
100 |
private AbstractViewPanel getView() {
|
101 |
if(selectedView == null) { |
102 |
IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
|
103 |
for (int i = 0; i < wList.length; i++) { |
104 |
if(wList[i] instanceof AbstractViewPanel) |
105 |
selectedView = (AbstractViewPanel)wList[i]; |
106 |
} |
107 |
} |
108 |
return selectedView;
|
109 |
} |
110 |
|
111 |
public void actionAddFiles(AddFilesEvent e) { |
112 |
File[] fList = e.getFileList(); |
113 |
for (int i = 0; i < fList.length; i++) { |
114 |
try {
|
115 |
RasterDataStore store = RasterLocator.getManager().getProviderServices().open(fList[i].getAbsolutePath()); |
116 |
if(store != null) |
117 |
addFilesPanel.addDstFile( |
118 |
store, |
119 |
getLayerNameFromFileName(store.getFullName()), |
120 |
store.getBandCount(), |
121 |
store.getDataType()[0],
|
122 |
store.getCellSize()); |
123 |
} catch (RasterDriverException e1) {
|
124 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
125 |
} catch (InitializeException e1) {
|
126 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
127 |
} catch (ProviderNotRegisteredException e1) {
|
128 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
129 |
} catch (LocatorException e1) {
|
130 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
131 |
} catch (NotSupportedExtensionException e1) {
|
132 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
133 |
} |
134 |
} |
135 |
} |
136 |
|
137 |
public void actionAddFolder(AddFilesEvent e) { |
138 |
File[] folderList = e.getFileList(); |
139 |
for (int i = 0; i < folderList.length; i++) { |
140 |
try {
|
141 |
RasterDataStore store = RasterLocator.getManager().getProviderServices().open(folderList[i].getAbsolutePath()); |
142 |
if(store != null) |
143 |
addFilesPanel.addDstFile( |
144 |
store, |
145 |
getLayerNameFromFileName(store.getFullName()), |
146 |
store.getBandCount(), |
147 |
store.getDataType()[0],
|
148 |
store.getCellSize()); |
149 |
} catch (RasterDriverException e1) {
|
150 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
151 |
} catch (InitializeException e1) {
|
152 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
153 |
} catch (ProviderNotRegisteredException e1) {
|
154 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
155 |
} catch (LocatorException e1) {
|
156 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
157 |
} catch (NotSupportedExtensionException e1) {
|
158 |
log.debug("Error loading a RasterDataStore from the list", e1);
|
159 |
} |
160 |
} |
161 |
} |
162 |
|
163 |
private String getLayerNameFromFileName(String fullName) { |
164 |
if(fullName.contains(File.separator)) { |
165 |
int index = fullName.lastIndexOf(File.separator); |
166 |
fullName = fullName.substring(index + 1);
|
167 |
} |
168 |
|
169 |
/*if(fullName.contains(".")) {
|
170 |
int index = fullName.lastIndexOf(".");
|
171 |
fullName = fullName.substring(0, index);
|
172 |
}*/
|
173 |
return fullName;
|
174 |
} |
175 |
} |