Statistics
| Revision:

gvsig-raster / org.gvsig.raster.georeferencing / trunk / org.gvsig.raster.georeferencing / org.gvsig.raster.georeferencing.app / org.gvsig.raster.georeferencing.app.georeferencingclient / src / main / java / org / gvsig / raster / georeferencing / app / georeferencingclient / listener / ApplicationControlsListener.java @ 1712

History | View | Annotate | Download (17.2 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.georeferencing.app.georeferencingclient.listener;
23

    
24
import java.awt.event.ActionEvent;
25
import java.awt.geom.Point2D;
26
import java.io.IOException;
27

    
28
import javax.swing.event.TableModelEvent;
29

    
30
import org.gvsig.andami.PluginServices;
31
import org.gvsig.andami.ui.mdiManager.IWindow;
32
import org.gvsig.fmap.dal.coverage.datastruct.GeoPoint;
33
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
34
import org.gvsig.fmap.mapcontext.layers.FLayers;
35
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
36
import org.gvsig.i18n.Messages;
37
import org.gvsig.raster.georeferencing.app.georeferencingclient.Georeferencing;
38
import org.gvsig.raster.georeferencing.framework.GeoreferencingFrameworkLocator;
39
import org.gvsig.raster.georeferencing.framework.GeoreferencingFrameworkManager;
40
import org.gvsig.raster.georeferencing.swing.control.GeorefControlPanel;
41
import org.gvsig.raster.georeferencing.swing.control.GeorefControlPanelListener;
42
import org.gvsig.raster.georeferencing.swing.impl.option.GeorefOptionsDialog;
43
import org.gvsig.raster.swing.impl.pagedtable.NotInitializeException;
44
import org.gvsig.raster.swing.pagedtable.PagedTable;
45
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
46
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
47

    
48
/**
49
 * Listener para los botones de control de la aplicaci?n de georreferenciaci?n.
50
 * F?sicamente est?n en el panel de la tabla de puntos de control.
51
 *
52
 * 25/01/2008
53
 * @author Nacho Brodin nachobrodin@gmail.com
54
 */
55
public class ApplicationControlsListener implements GeorefControlPanelListener {
56

    
57
        private PagedTable                tablePanel          = null;
58
        private Georeferencing            appMain             = null;
59
        private boolean                   enableTableEvent    = true;
60
        private GeorefControlPanel        controlPanel        = null;
61
        private GeorefOptionsDialog       optionsDialog       = null;
62
        GeoreferencingFrameworkManager    fManager            = null;
63
        //private GeoPointsPersistence      persistence         = null;
64

    
65
        /**
66
         * Flag que estar? a true despu?s de crear un nuevo punto de control. Volver? a false
67
         * cuando se ejecute el evento de inserci?n de fila en una tabla. Esto es necesario para
68
         * que no se centre el punto sobre la vista despu?s de insertar un nuevo punto. Ya que las
69
         * vistas se van a la posici?n (0,0)
70
         */
71
        private boolean                   newPointCreated     = false;
72

    
73
        /**
74
         * Constructor. Asigna el panel con los controles
75
         * @param tablePanel
76
         */
77
        public ApplicationControlsListener(PagedTable tablePanel, Georeferencing appMain) {
78
                this.tablePanel = tablePanel;
79
                this.appMain = appMain;
80
                this.controlPanel = appMain.getGeorefControlPanel();
81
                this.controlPanel.setListener(this);
82
                this.fManager = GeoreferencingFrameworkLocator.getManager();
83
        }
84

    
85
        /**
86
         * Eventos de los botones del cuadro de tabla y selecci?n de fila de tabla
87
         */
88
        public void actionPerformed(ActionEvent e) {
89
                //-------------------------------------
90
                //Finalizaci?n de la georreferenciaci?n
91
                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bEndGeoref)
92
                        endGeoref();
93

    
94
                //-----------------------------------------
95
                //Centrar la vista en el punto seleccionado
96
                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bCenterView) {
97
                        try {
98
                                appMain.getSyncViews().centerToPoint(tablePanel.getSelectedRows()[0]);
99
                        } catch (NotInitializeException e1) {
100
                                RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", tablePanel, e1);
101
                        }
102
                }
103

    
104
                //---------------------------------
105
                //Selecci?n de una fila de la tabla
106
                if(e.getActionCommand().equals("SELECT_ROW")) {
107
                        if(appMain.getOptionsDataModel().isCenterView() && !newPointCreated)
108
                                appMain.getSyncViews().centerToPoint(e.getID());
109
                        newPointCreated = false;
110
                }
111

    
112
                //----------------------------------------------------------
113
                //Seleccion de opciones. Se asignan las propiedades al abrir
114
                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bOptions) {
115
                        if(optionsDialog == null)
116
                                optionsDialog = new GeorefOptionsDialog(Georeferencing.DEFAULT_DEGREE, this);
117
                        optionsDialog.setDegree(appMain.getOptionsDataModel().getDegree());
118
                        optionsDialog.setAlgorithm(appMain.getOptionsDataModel().getAlgorithm());
119
                        optionsDialog.setBackGroundColor(appMain.getOptionsDataModel().getBackgroundColor());
120
                        optionsDialog.setTextColor(appMain.getOptionsDataModel().getTextColor());
121
                        optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getAddErrorsCSVCheck().setSelected(getOptions().isAddErrorsCSV());
122
                        optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getCenterViewCheck().setSelected(getOptions().isCenterView());
123
                        optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getShowNumberCheck().setSelected(getOptions().isShowNumber());
124
                        optionsDialog.setThresholdError(appMain.getOptionsDataModel().getThreshold());
125
                        optionsDialog.setInterpolationMethod(appMain.getOptionsDataModel().getInterpolationMethod());
126
                        optionsDialog.setOutFile(appMain.getOptionsDataModel().getOutFile());
127
                        optionsDialog.setXCellSize(appMain.getOptionsDataModel().getXCellSize());
128
                        optionsDialog.setYCellSize(appMain.getOptionsDataModel().getYCellSize());
129
                        fManager.showWindow(optionsDialog, 400, 420, Messages.getText("georeferencing"));
130
                }
131

    
132
                //------------------------------
133
                //Selecci?n de puntos de control
134
                if(controlPanel.getButtonIDPressed(e.getSource()) == tablePanel.getToolSelectPointButton()) {
135
                        if( tablePanel.getToolSelectPointButton().isSelected()) {
136
                                try {
137
                                        if(!getPointManager().isSelectPointToolSelected()) {
138
                                                //No hay filas en la tabla
139
                                                if(tablePanel.getTable().getRowCount() == 0) {
140
                                                        RasterToolsUtil.messageBoxError("no_rows", tablePanel);
141
                                                        tablePanel.getToolSelectPointButton().setSelected(false);
142
                                                        return;
143
                                                }
144

    
145
                                                //No hay filas seleccionadas en la tabla
146
                                                if(tablePanel.getTable().getSelectedRow() == -1) {
147
                                                        RasterToolsUtil.messageBoxError("no_rows_selected", tablePanel);
148
                                                        tablePanel.getToolSelectPointButton().setSelected(false);
149
                                                        return;
150
                                                }
151
                                                getViewsManager().selectPointTool(true);
152
                                        }
153
                                } catch (NotInitializeException e1) {
154
                                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", tablePanel, e1);
155
                                }
156
                        } else {
157
                                getViewsManager().selectPointTool(false);
158
                        }
159
                }
160

    
161
                //-------
162
                //Testear
163
                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bTest) {
164
                        appMain.addTestRasterLayer(appMain.getLayer());
165
                }
166

    
167
                //------------
168
                //Fin del Test
169
                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bEndTest) {
170
                        if(appMain != null && appMain.getLastTestLayer() != null) {
171
                                appMain.getLastTestLayer().setRemoveRasterFlag(false);
172
                                appMain.removeTestRasterLayer();
173
                        }
174
                }
175

    
176
                //------------
177
                //Salvar a CSV
178
//                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bSaveToAscii) {
179
//                        getPersistence().exportToCSV(getPointManager().getPointList(), getOptions().isAddErrorsCSV());
180
//                }
181
//
182
//                //----------------
183
//                //Cargar desde CSV
184
//                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bLoadFromAscii) {
185
//                        setEnableTableEvent(false);
186
//                        getPersistence().importFromCSV(getPointManager(), tablePanel);
187
//                        setEnableTableEvent(true);
188
//                }
189
//
190
//                //------------
191
//                //Salvar a RMF
192
//                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bSaveToXml) {
193
//                        getPersistence().saveToRMF(getPointManager().getPointList(), appMain.getLayer().getDataSource());
194
//                }
195
//
196
//                //----------------
197
//                //Cargar desde RMF
198
//                if(controlPanel.getButtonIDPressed(e.getSource()) == GeorefControlPanel.bLoadFromXml) {
199
//                        setEnableTableEvent(false);
200
//                        getPersistence().loadFromRMF(appMain.getLayer().getDataSource(), getPointManager(), tablePanel);
201
//                        setEnableTableEvent(true);
202
//                }
203
        }
204

    
205
        /**
206
         * Acciones de finalizaci?n de georreferenciaci?n. La secuencia de acciones es:
207
         * <UL>
208
         * <LI>Preguntar si est? seguro de finalizar.</LI>
209
         * <LI>Preguntar si se desea salvar la transformaci?n (Solo en caso de ser af?n).</LI>
210
         * <LI>Preguntar si se desea cargar el resultado en la vista (Solo en caso de ser con mapa de referencia).</LI>
211
         * </UL>
212
         */
213
        private void endGeoref() {
214
                if(RasterToolsUtil.messageBoxYesOrNot("ask_end_georef", null)) {
215
                        boolean saveTransform = false;
216
                        if(appMain.getOptionsDataModel().getAlgorithm() == Georeferencing.AFFINE) {
217
                                if(RasterToolsUtil.messageBoxYesOrNot("ask_save_transformation", null))
218
                                        saveTransform = true;
219
                                else {
220
                                        appMain.removeTestRasterLayer();
221
                                        appMain.closeAllWindows();
222
                                        return;
223
                                }
224
                        }
225

    
226
                        if(appMain.getOptionsDataModel().getType() == Georeferencing.WITH_MAP) {
227
                                if(appMain.getLastTestLayer() != null)
228
                                        appMain.getLastTestLayer().setRemoveRasterFlag(false);
229

    
230
                                if(RasterToolsUtil.messageBoxYesOrNot("ask_load_view", null)) {
231
                                        FLayers lyrs = appMain.getLayers();
232
                                        if(lyrs != null) {
233
                                                try {
234
                                                        if(appMain.getLastTestLayer() != null){
235
                                                                lyrs.removeLayer(appMain.getLastTestLayer());
236
                                                                PluginServices.getMainFrame().enableControls();
237
                                                        }
238
                                                } catch(NullPointerException ex){
239
                                                        //No hay raster para eliminar
240
                                                }
241
                                        }
242

    
243
                                        //Cargar en la vista
244
                                        if(appMain.getOptionsDataModel().getAlgorithm() == Georeferencing.AFFINE) {
245
                                                appMain.addTestRasterLayer(appMain.getLayer());
246
                                        }
247
                                        if(appMain.getOptionsDataModel().getAlgorithm() == Georeferencing.POLYNOMIAL) {
248
                                                if(appMain.getLastTestLayer() != null && lyrs != null) {
249
                                                        appMain.getLayers().addLayer(appMain.getLastTestLayer());
250
                                                } else {
251
                                                        RasterToolsUtil.messageBoxInfo("raster_not_created", null);
252
                                                        return;
253
                                                }
254
                                        }
255
                                } else {
256
                                        appMain.removeTestRasterLayer();
257
                                }
258
                        }
259

    
260
                        if (saveTransform) {
261
                                // Salvar transformaci?n
262
                                if (appMain.getLastTestLayer() == null) {
263
                                        RasterToolsUtil.messageBoxInfo("raster_not_created", null);
264
                                        return;
265
                                }
266

    
267
                                // Guardamos la GeoReferenciacion de cada dataset
268
                                IRasterDataSource dataSource = appMain.getLastTestLayer().getDataSource();
269
                                try {
270
                                        for (int i = 0; i < dataSource.getDatasetCount(); i++) {
271
                                                dataSource.saveObjectToRmf(i, RasterDataset.class, dataSource.getDataset(i)[0]);
272
                                                RasterUtilities.createWorldFile(dataSource.getNameDatasetStringList(0, 0)[i], dataSource.getAffineTransform(0), (int)dataSource.getWidth(), (int)dataSource.getHeight());
273
                                        }
274
                                } catch (RmfSerializerException e) {
275
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", null, e);
276
                                } catch (IOException e) {
277
                                        RasterToolsUtil.messageBoxError("error_salvando_rmf", null, e);
278
                                }
279
                        }
280
                        
281
                        appMain.closeAllWindows();
282
                }
283
        }
284

    
285
        /**
286
         * Evento de modificaci?n de la tabla de puntos de control. El a?adir, borrar
287
         * o mover puntos dentro de la tabla hace que estos se creen, borren o muevan en
288
         * la lista de GeoPoints.
289
         */
290
        public void tableChanged(TableModelEvent e) {
291
                if(!enableTableEvent)
292
                        return;
293
                setEnableTableEvent(false);
294
                try{
295

    
296
                        //-----------------------------------
297
                        //Inserci?n de nuevo fila en la tabla
298
                        if(e.getType() == TableModelEvent.INSERT) {
299
                                long id = appMain.getSyncViews().addPoint(new Point2D.Double(0, 0), new Point2D.Double(0, 0));
300
                                tablePanel.initializeRow(e.getFirstRow(), id);
301
                                appMain.getSyncViews().calcPointsNumeration();
302

    
303
                                //Activamos la herramienta de pinchar punto en la vista al a?adir punto
304
                                tablePanel.getToolSelectPointButton().setSelected(true);
305
                                appMain.getSyncViews().selectPointTool(true);
306

    
307
                                newPointCreated = true;
308
                        }
309

    
310
                        //-----------------------------
311
                        //Eliminado de fila de la tabla
312
                        if(e.getType() == TableModelEvent.DELETE) {
313
                                for (int i = e.getLastRow(); i >= e.getFirstRow(); i--)
314
                                        appMain.getSyncViews().removePoint(i);
315
                                appMain.getSyncViews().calcPointsNumeration();
316
                                tablePanel.updateErrors();
317
                        }
318

    
319
                        //-------------------------------------
320
                        //Actualizaci?n de una fila de la tabla
321
                        if(e.getType() == TableModelEvent.UPDATE) {
322
                                appMain.getSyncViews().calcPointsNumeration();
323
                                //Revisamos el flag de activo de cada punto poniendo el valor que pone en la entrada de la tabla
324
                                for (int i = 0; i < tablePanel.getRowCount(); i++) {
325
                                        boolean active = ((Boolean) tablePanel.getValueAt(i, 0)).booleanValue();
326
                                        int position = appMain.getSyncViews().getPointPositionByNumber(i);
327
                                        if (position < 0)
328
                                                return;
329
                                        GeoPoint pt = appMain.getGeoPointList().get(position);
330
                                        pt.setActive(active);
331
                                        appMain.getSyncViews().setVisiblePoint(active, position);
332

    
333
                                        //Para la funcionalidad de cambio de coordenadas desde la tabla: reasignamos el valor
334
                                        //de las coordenadas por si han cambiado estas
335
                                        double[] values = tablePanel.getCoordinates(i);
336

    
337
                                        appMain.getSyncViews().setCoordinates(position, values[0], values[1], values[2], values[3]);
338
                                }
339
                                tablePanel.updateErrors();
340
                        }
341

    
342
                }catch (NotInitializeException ex) {
343
                        RasterToolsUtil.messageBoxError("table_not_initialize", tablePanel, ex);
344
                }
345
                setEnableTableEvent(true);
346
        }
347

    
348
        /**
349
         * Eventos de los botones del cuadro de opciones
350
         */
351
        public void actionButtonPressed(ButtonsPanelEvent e) {
352

    
353
                //---------------------------------------------------
354
                //Bot?n de Aplicar y Aceptar en el cuadro de opciones
355
                if (e.getButton() == ButtonsPanel.BUTTON_APPLY || e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
356
                        if(optionsDialog != null) {
357

    
358
                                //Asignamos las propiedades seleccionadas
359
                                appMain.getOptionsDataModel().setAlgorithm(optionsDialog.getAlgorithm());
360
                                appMain.getOptionsDataModel().setBackGroundColor(optionsDialog.getBackGroundColor());
361
                                appMain.getOptionsDataModel().setTextColor(optionsDialog.getTextColor());
362
                                appMain.getOptionsDataModel().setAddErrorsCSV(optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getAddErrorsCSVCheck().isSelected());
363
                                appMain.getOptionsDataModel().setCenterView(optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getCenterViewCheck().isSelected());
364
                                appMain.getOptionsDataModel().setShowNumber(optionsDialog.getOptionsPanel(this).getCheckOptionsPanel().getShowNumberCheck().isSelected());
365
                                appMain.getOptionsDataModel().setInterpolationMethod(optionsDialog.getInterpolationMethod());
366
                                appMain.getOptionsDataModel().setOutFile(optionsDialog.getOptionsPanel(this).getOutFileSelectionPanel().getOutFile());
367

    
368
                                //Recalculamos los errores si ha cambiado el grado
369
                                if(optionsDialog.getDegree() != appMain.getOptionsDataModel().getDegree()) {
370
                                        appMain.getOptionsDataModel().setDegree(optionsDialog.getDegree());
371
                                        tablePanel.updateErrors();
372
                                }
373

    
374
                                try {
375
                                        appMain.getOptionsDataModel().setThreshold(Double.valueOf(optionsDialog.getThresholdError().getValue()).doubleValue());
376
                                        appMain.getOptionsDataModel().setXCellSize(optionsDialog.getOptionsPanel(this).getCellSizePanel().getXCellSizeValue());
377
                                        appMain.getOptionsDataModel().setYCellSize(optionsDialog.getOptionsPanel(this).getCellSizePanel().getYCellSizeValue());
378
                                } catch(NumberFormatException exc) {
379
                                        RasterToolsUtil.messageBoxError("error_numeric_format", null, exc);
380
                                }
381

    
382
                                appMain.getSyncViews().setGraphicsColor(appMain.getOptionsDataModel().getTextColor());
383
                                appMain.getSyncViews().setBackgroundColor(appMain.getOptionsDataModel().getBackGroundColor());
384
                                appMain.getSyncViews().setActiveNumberPoint(appMain.getOptionsDataModel().isShowNumber());
385

    
386
                        }
387
                }
388

    
389
                //--------------------------------------------------
390
                // Bot?n de Cerrar y Aceptar en el cuadro de opciones
391
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL || e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
392
                        if(optionsDialog != null) {
393
                                fManager.closeWindow(optionsDialog);
394
                        }
395
                }
396
        }
397

    
398
        /*private GeorefOptionsDialog getOptions() {
399
                if(georefOptions == null)
400
                        georefOptions = GeoreferencingSwingLocator.getSwingManager().createWindowOptions(appMain.getOptionsDataModel().getDegree(), this);
401
                return georefOptions;
402
        }*/
403

    
404
        /**
405
         * Consulta si est?n activos los eventos de tabla o no
406
         * @return true si est?n activos y false si no lo est?n
407
         */
408
        public boolean isEnableTableEvent() {
409
                return enableTableEvent;
410
        }
411

    
412
        /**
413
         * Activa o desactiva los eventos de tabla
414
         * @param enableTableEvent true para activarlos o false para desactivarlos
415
         */
416
        public void setEnableTableEvent(boolean enableTableEvent) {
417
                this.enableTableEvent = enableTableEvent;
418
        }
419

    
420

    
421
        /**
422
         * Obtiene una instancia del objeto que gestiona la
423
         * persistencia en disco de los puntos de control
424
         * @return GeoPointsPersistence
425
         */
426
        /*public GeoPointsPersistence getPersistence() {
427
                if(persistence == null)
428
                        persistence = new GeoPointsPersistence();
429
                return persistence;
430
        }*/
431
}