Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / trunk / org.gvsig.raster.multifile / org.gvsig.raster.multifile.app.multifileclient / src / main / java / org / gvsig / raster / multifile / app / MultifileClientExtension.java @ 2173

History | View | Annotate | Download (9.31 KB)

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

    
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ActionListener;
26
import java.io.File;
27
import java.io.IOException;
28
import java.util.ArrayList;
29
import java.util.List;
30
import java.util.Locale;
31

    
32
import javax.swing.JComponent;
33

    
34
import org.gvsig.andami.IconThemeHelper;
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.andami.plugins.Extension;
37
import org.gvsig.andami.ui.mdiManager.IWindow;
38
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel;
39
import org.gvsig.fmap.dal.coverage.RasterLocator;
40
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
41
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
42
import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation;
43
import org.gvsig.fmap.mapcontext.MapContextLocator;
44
import org.gvsig.fmap.mapcontext.MapContextManager;
45
import org.gvsig.fmap.mapcontext.exceptions.LoadLayerException;
46
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
47
import org.gvsig.i18n.Messages;
48
import org.gvsig.raster.fmap.layers.FLyrRaster;
49
import org.gvsig.raster.impl.provider.RasterProvider;
50
import org.gvsig.raster.multifile.app.panel.BandSelectorPanel;
51
import org.gvsig.raster.multifile.app.panel.MainWindow;
52
import org.gvsig.raster.multifile.io.MultiFileDataParameters;
53
import org.gvsig.raster.multifile.io.MultiFileFormat;
54
import org.gvsig.raster.swing.RasterSwingLibrary;
55
import org.gvsig.raster.swing.RasterSwingLocator;
56
import org.gvsig.raster.swing.basepanel.IButtonsPanel;
57
import org.gvsig.raster.swing.newlayer.CreateNewLayerPanel;
58
import org.gvsig.tools.ToolsLocator;
59
import org.gvsig.tools.extensionpoint.ExtensionPoint;
60
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
61

    
62
/**
63
 * Extension for adding grid netcdf support to gvSIG.
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class MultifileClientExtension extends Extension implements ActionListener {
67
        private CreateNewLayerPanel             newLayerPanel        = null;
68
        private MainWindow                      layerNamewindow      = null;
69
        private MainWindow                      bandSelectorwindow   = null;
70
        private BandSelectorPanel               panel                = null; 
71
        private AbstractViewPanel               selectedView         = null;
72
        private String                          file                 = null;
73
        private String                          folder               = null;
74
        
75
        public void initialize() {
76
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
77

    
78
                ExtensionPoint point = extensionPoints.get("AplicationPreferences");
79
                point = extensionPoints.get("RasterSEPropertiesDialog");
80
                point.append("Bandas", "", BandSelectorPanel.class);
81
                
82
                point = extensionPoints.add("GenericToolBarMenu");
83
                point.append("Multifile", "", MultiFileCreatorTocMenuEntry.getSingleton());
84
                MultiFileCreatorTocMenuEntry.setExtension(this);
85
                
86
                if (!Messages.hasLocales()) {
87
                        Messages.addLocale(Locale.getDefault());
88
                }
89

    
90
                Messages.addResourceFamily("org.gvsig.raster.multifile.app.i18n.text",
91
                                MultifileClientExtension.class.getClassLoader(),
92
                                MultifileClientExtension.class.getClass().getName());
93
                
94
                initilizeIcons();
95
        }
96

    
97
        public void execute(String actionCommand) {
98
                if (actionCommand.compareTo("MultifileCreator") == 0) {
99
                        layerNamewindow = new MainWindow((JComponent)getNewLayerPanel(), 
100
                                        Messages.getText("bands"), 300, 150, this);
101
                        PluginServices.getMDIManager().addCentredWindow(layerNamewindow);
102
            }
103
        }
104
        
105
        private void initilizeIcons() {
106
                IconThemeHelper.registerIcon(null, "multifile-icon", this);
107
    }
108

    
109
        public boolean isEnabled() {
110
                return true;
111
        }
112

    
113
        public boolean isVisible() {
114
                return false;
115
        }
116

    
117
        public CreateNewLayerPanel getNewLayerPanel() {
118
                if(newLayerPanel == null) {
119
                        newLayerPanel = RasterSwingLocator.getSwingManager().createNewLayerPanel();
120
                }
121
                return newLayerPanel;
122
        }
123

    
124
        public void actionPerformed(ActionEvent e) {
125
                //Accept the window with the layer name
126
                if(e.getSource() == layerNamewindow.getButtonsPanel().getButton(IButtonsPanel.BUTTON_ACCEPT)) {
127
                        file = getNewLayerPanel().getFileSelected();
128
                        folder = getNewLayerPanel().getDirectorySelected();
129
                        panel = new BandSelectorPanel(BandSelectorPanel.TYPE_DIALOG);
130
                        panel.getListener().setDestination(file, folder);
131
                        bandSelectorwindow = new MainWindow(panel, Messages.getText("bands"), 500, 400, this, true);
132
                        PluginServices.getMDIManager().addCentredWindow(bandSelectorwindow);
133
                }
134
                
135
                if(e.getSource() == bandSelectorwindow.getButtonsPanel().getButton(IButtonsPanel.BUTTON_USR1)) {
136
                        loadLayer();
137
                }
138
                
139
                if(e.getSource() == bandSelectorwindow.getButtonsPanel().getButton(IButtonsPanel.BUTTON_USR2)) {
140
                        
141
                }
142
                
143
        }
144
        
145
        
146
        private void saveMff(RasterDataStore mainRasterStore) {
147
                MultiFileDataParameters params = (MultiFileDataParameters)mainRasterStore.getParameters();
148
                ArrayList<File> uriList = new ArrayList<File>();
149
                List<RasterProvider> providers = params.getProviders();
150
                for (int i = 0; i < providers.size(); i++) {
151
                        uriList.add(new File(providers.get(i).getURI()));
152
                }
153
                try {
154
                        MultiFileFormat.saveMultiFileFormat(file, folder, uriList);
155
                } catch (IOException e) {
156
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_mff"), panel, e);
157
                }
158
        }
159
        
160
        private void saveMetadata() {
161
                int rBand = panel.getAssignedBand(RasterDataStore.RED_BAND);
162
                int gBand = panel.getAssignedBand(RasterDataStore.GREEN_BAND);
163
                int bBand = panel.getAssignedBand(RasterDataStore.BLUE_BAND);
164
                int aBand = panel.getAssignedBand(RasterDataStore.ALPHA_BAND);
165

    
166
                if (!isCorrectAssignedBand(rBand, gBand, bBand, aBand)) {
167
                        RasterSwingLibrary.messageBoxError(Messages.getText("combinacion_no_asignable"), panel);
168
                        return;
169
                }
170

    
171
                ColorInterpretation ci = RasterLocator.getManager().getDataStructFactory().createColorInterpretation(
172
                                new String[]{ColorInterpretation.ALPHA_BAND, ColorInterpretation.RED_BAND, ColorInterpretation.GREEN_BAND, ColorInterpretation.BLUE_BAND});
173
                try {
174
                        // Combinaci?n GRAY
175
                        if ((rBand == gBand) && (rBand == bBand) && (rBand >= 0)) {
176
                                for (int iBand = 0; iBand < panel.getARGBTable().getRowCount(); iBand++) {
177
                                        ci.setColorInterpValue(iBand, ColorInterpretation.UNDEF_BAND);
178
                                }
179
                                ci.setColorInterpValue(rBand, ColorInterpretation.GRAY_BAND);
180
                                ci.setColorInterpValue(aBand, ColorInterpretation.ALPHA_BAND);
181
                        } else {
182
                                // Combinaci?n RGB
183
                                for (int iBand = 0; iBand < panel.getARGBTable().getRowCount(); iBand++)
184
                                        ci.setColorInterpValue(iBand, panel.getColorInterpretationByBand(iBand));
185
                        }
186
                        RasterLocator.getManager().getProviderServices().saveObjectToRmfFile(file, ci);
187
                } catch (RmfSerializerException exc) {
188
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_salvando_rmf"), panel, exc);
189
                } catch (NotInitializeException exc) {
190
                        RasterSwingLibrary.messageBoxError(Messages.getText("table_not_initialize"), panel, exc);
191
                }
192
        }
193
        
194
        public void loadLayerInView(RasterDataStore mainRasterStore) throws LoadLayerException {
195
                MapContextManager mcm = MapContextLocator.getMapContextManager();
196
                FLyrRaster lyr = (FLyrRaster) mcm.createLayer(file, mainRasterStore);
197

    
198
                getView().getMapControl().getMapContext().beginAtomicEvent();
199
                getView().getMapControl().getMapContext().getLayers().addLayer(lyr);
200
                getView().getMapControl().getMapContext().invalidate();
201
                getView().getMapControl().getMapContext().endAtomicEvent();
202
        }
203

    
204
        private AbstractViewPanel getView() {
205
                if(selectedView == null) {
206
                        IWindow[] wList = PluginServices.getMDIManager().getAllWindows();
207
                        for (int i = 0; i < wList.length; i++) {
208
                                if(wList[i] instanceof AbstractViewPanel)
209
                                        selectedView = (AbstractViewPanel)wList[i];
210
                        }
211
                }
212
                return selectedView;
213
        }
214
        
215
        public void loadLayer() {
216
                RasterDataStore mainRasterStore = panel.getResult();
217
                if(mainRasterStore == null) {
218
                        return;
219
                }
220
                
221
                saveMff(mainRasterStore);
222
                saveMetadata();
223
                try {
224
                        loadLayerInView(mainRasterStore);
225
                } catch (LoadLayerException e) {
226
                        RasterSwingLibrary.messageBoxError(Messages.getText("error_loading_layer"), panel, e);
227
                }
228
        }
229
        
230
        private boolean isCorrectAssignedBand(int r, int g, int b, int a) {
231
                // Si es gris es correcta la asignacion
232
                if ((r == g) && (r == b) && (r >= 0)) {
233
                        // Si el alpha esta asignado a la misma banda es incorrecto
234
                        if (r == a)
235
                                return false;
236
                        // En caso contrario es correcto
237
                        return true;
238
                }
239

    
240
                // Si dos bandas coinciden, se dice que no es correcta la asignacion
241
                int list[] = { r, g, b, a };
242
                for (int i = 0; i <= 3; i++)
243
                        for (int j = 0; j <= 3; j++)
244
                                if ((i != j) && (list[i] == list[j]) && (list[i] > -1))
245
                                        return false;
246

    
247
                return true;
248
        }
249
}