Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / ui / table / GCPTablePanel.java @ 18530

History | View | Annotate | Download (14.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.georeferencing.ui.table;
20

    
21
import java.awt.BorderLayout;
22
import java.awt.Dimension;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.geom.Point2D;
26
import java.util.ArrayList;
27

    
28
import javax.swing.JButton;
29
import javax.swing.JPanel;
30
import javax.swing.JToggleButton;
31
import javax.swing.table.DefaultTableModel;
32

    
33
import org.gvsig.georeferencing.main.ApplicationControlsListener;
34
import org.gvsig.gui.beans.table.TableContainer;
35
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
36
import org.gvsig.gui.beans.table.models.GCPModel;
37
import org.gvsig.raster.util.RasterToolsUtil;
38

    
39
import com.iver.andami.PluginServices;
40
import com.iver.andami.ui.mdiManager.IWindow;
41
import com.iver.andami.ui.mdiManager.WindowInfo;
42

    
43
/**
44
 * Panel que contiene la tabla de puntos de control
45
 * 
46
 * 22/12/2007
47
 * @author Nacho Brodin (nachobrodin@gmail.com)
48
 */
49
public class GCPTablePanel extends JPanel implements IWindow {
50
        private static final long             serialVersionUID    = 1L;
51
        private String[]                      columnNames         = {"-", "N?", "X", "Y", "X'", "Y'", "Error X", "Error Y", "RMS", "-"};
52
        private int[]                         columnWidths        = {25, 25, 90, 90, 90, 90, 50, 50, 50, 0};
53
        private TableContainer                table               = null;
54
        
55
        private JPanel                        buttonsPanel        = null;
56
        private ApplicationControlsListener   buttonsListener     = null;
57
        private JButton                       saveToXml           = null;
58
        private JButton                       loadFromXml         = null;
59
        private JButton                       saveToAscii         = null;
60
        private JButton                       loadFromAscii       = null;
61
        private JButton                       options             = null;
62
        private JButton                       process             = null;
63
        private JButton                       endGeoref           = null;
64
        private JButton                       centerView          = null;
65
        private JToggleButton                 addPoint            = null;
66
        
67
        private int                           w                   = 640;
68
        private int                           h                   = 100;
69
        private int                           posX                = 0;
70
        private int                           posY                = 0;
71
        
72
        /**
73
         * Constructor.
74
         * Crea la composici?n de controles de zoom.
75
         */
76
        public GCPTablePanel(int posX, int posY, int w, int h) {
77
                setPosition(posX, posY);
78
                setWindowsSize(w, h);
79
        }
80
                
81
        /**
82
         * Asigna el listener para los botones
83
         * @param listener
84
         */
85
        public void initialize(ApplicationControlsListener listener) {
86
                this.buttonsListener = listener;
87
                try {
88
                        init();
89
                } catch (NotInitializeException e) {
90
                        RasterToolsUtil.messageBoxError(RasterToolsUtil.getText(this, "table_not_initialize"), this);
91
                }
92
        }
93
        
94
        /**
95
         * Asigna la posici?n de la ventana
96
         * @param posX Posici?n en X
97
         * @param posY Posici?n en Y
98
         */
99
        public void setPosition(int posX, int posY) {
100
                this.posX = posX;
101
                this.posY = posY;
102
        }
103
        
104
        /**
105
         * Asigna la posici?n de la ventana
106
         * @param posX Posici?n en X
107
         * @param posY Posici?n en Y
108
         */
109
        public void setWindowsSize(int w, int h) {
110
                this.w = w;
111
                this.h = h;
112
        }
113
        
114
        /**
115
         * Inicializaci?n de los componentes gr?ficos
116
         */
117
        private void init() throws NotInitializeException  {
118
                setLayout(new BorderLayout());
119
                setSize(new java.awt.Dimension(w, h)); 
120
                
121
                GridBagConstraints gb = new GridBagConstraints();
122
                gb.insets = new java.awt.Insets(0, 0, 0, 0);
123
                gb.gridy = 0;
124
                gb.gridx = 0;
125
                gb.weightx = 1D; //El espacio sobrante se distribuye horizontalmente
126
                gb.weighty = 1D; //El espacio sobrante se distribuye verticalmente
127
                gb.fill = GridBagConstraints.BOTH; //El componente se hace tan ancho como espacio disponible tiene
128
                gb.anchor = GridBagConstraints.NORTH; //Alineamos las cajas arriba
129
                add(getTable(), BorderLayout.CENTER);
130
                
131
                gb.gridx = 1;
132
                add(getButtonsPanel(), BorderLayout.EAST);
133
        }
134
        
135
        /**
136
         * Obtiene la tabla de puntos de control
137
         * @return TableContainer
138
         */
139
        public TableContainer getTable() throws NotInitializeException {
140
                if(table == null) {
141
                        ArrayList listeners = new ArrayList();
142
                        listeners.add(buttonsListener);
143
                        table = new TableContainer(columnNames, columnWidths, listeners);
144
                        table.setModel("GCPModel");
145
                        table.initialize();
146
                        table.setControlVisible(true);
147
                        table.setMoveRowsButtonsVisible(true);
148
                        table.setEditable(true);
149
                        table.getTable().getJTable().getColumnModel().getColumn(columnNames.length - 1).setMinWidth(0);
150
                        table.getTable().getJTable().getColumnModel().getColumn(columnNames.length - 1).setMaxWidth(0);
151
                        table.getModel().addTableModelListener(buttonsListener);
152
                }
153
                return table;
154
        }
155
        
156
        /**
157
         * Obtiene el panel de botones
158
         * @return JPanel
159
         */
160
        public JPanel getButtonsPanel() {
161
                if(buttonsPanel == null) {
162
                        buttonsPanel = new JPanel();
163
                        buttonsPanel.setLayout(new GridBagLayout());
164
                        
165
                        GridBagConstraints gb = new GridBagConstraints();
166
                        gb.insets = new java.awt.Insets(0, 0, 1, 1);
167
                        gb.gridy = 0;
168
                        gb.gridx = 0;
169
                        buttonsPanel.add(getSaveToXMLButton(), gb);
170
                        
171
                        gb.gridy = 0;
172
                        gb.gridx = 1;
173
                        buttonsPanel.add(getLoadFromXMLButton(), gb);
174
                        
175
                        gb.gridy = 0;
176
                        gb.gridx = 2;
177
                        buttonsPanel.add(getExporToCSVButton(), gb);
178
                        
179
                        gb.gridy = 0;
180
                        gb.gridx = 3;
181
                        buttonsPanel.add(getLoadFromCSVButton(), gb);
182
                        
183
                        gb.gridy = 1;
184
                        gb.gridx = 0;
185
                        buttonsPanel.add(getOptionsButton(), gb);
186
                        
187
                        gb.gridy = 1;
188
                        gb.gridx = 1;
189
                        buttonsPanel.add(getProcessButton(), gb);
190
                        
191
                        gb.gridy = 1;
192
                        gb.gridx = 2;
193
                        buttonsPanel.add(getToolSelectPointButton(), gb);
194
                        
195
                        gb.gridy = 1;
196
                        gb.gridx = 3;
197
                        buttonsPanel.add(getEndGeorefButton(), gb);
198
                        
199
                        gb.gridy = 2;
200
                        gb.gridx = 0;
201
                        buttonsPanel.add(getCenterButton(), gb);
202
                }
203
                return buttonsPanel;
204
        }
205
        
206
        /**
207
         * Obtiene el bot?n de finalizar georreferenciaci?n. Si no existe se crea.
208
         * @return JButton
209
         */
210
        public JButton getEndGeorefButton() {
211
                if(endGeoref == null) {
212
                        endGeoref = new JButton();
213
                        endGeoref.setPreferredSize(new Dimension(22, 19));
214
                        endGeoref.setIcon(RasterToolsUtil.getIcon("exit-icon"));
215
                        endGeoref.setToolTipText(RasterToolsUtil.getText(this, "end_georef"));
216
                        endGeoref.addActionListener(buttonsListener);
217
                }
218
                return endGeoref;
219
        }
220
        
221
        /**
222
         * Obtiene el bot?n de procesar georreferenciaci?n. Si no existe se crea.
223
         * @return JButton
224
         */
225
        public JButton getProcessButton() {
226
                if(endGeoref == null) {
227
                        process = new JButton();
228
                        process.setPreferredSize(new Dimension(22, 19));
229
                        process.setToolTipText(RasterToolsUtil.getText(this, "process"));
230
                        process.addActionListener(buttonsListener);
231
                        process.setIcon(RasterToolsUtil.getIcon("process-icon"));
232
                }
233
                return process;
234
        }
235
                
236
        /**
237
         * Obtiene el bot?n de procesar georreferenciaci?n. Si no existe se crea.
238
         * @return JButton
239
         */
240
        public JButton getLoadFromCSVButton() {
241
                if(endGeoref == null) {
242
                        loadFromAscii = new JButton();
243
                        loadFromAscii.setPreferredSize(new Dimension(22, 19));
244
                        loadFromAscii.setToolTipText(RasterToolsUtil.getText(this, "load_from_ascii"));
245
                        loadFromAscii.addActionListener(buttonsListener);
246
                        loadFromAscii.setIcon(RasterToolsUtil.getIcon("importfromcsv-icon"));
247
                }
248
                return loadFromAscii;
249
        }
250
        
251
        /**
252
         * Obtiene el bot?n de exportar a Ascii. Si no existe se crea.
253
         * @return JButton
254
         */
255
        public JButton getExporToCSVButton() {
256
                if(endGeoref == null) {
257
                        saveToAscii = new JButton();
258
                        saveToAscii.setPreferredSize(new Dimension(22, 19));
259
                        saveToAscii.setToolTipText(RasterToolsUtil.getText(this, "save_to_ascii"));
260
                        saveToAscii.addActionListener(buttonsListener);
261
                        saveToAscii.setIcon(RasterToolsUtil.getIcon("exporttocsv-icon"));
262
                }
263
                return saveToAscii;
264
        }
265
        
266
        /**
267
         * Obtiene el bot?n de cargar desde XML. Si no existe se crea.
268
         * @return JButton
269
         */
270
        public JButton getLoadFromXMLButton() {
271
                if(endGeoref == null) {
272
                        loadFromXml = new JButton();
273
                        loadFromXml.setPreferredSize(new Dimension(22, 19));
274
                        loadFromXml.setToolTipText(RasterToolsUtil.getText(this, "load_from_xml"));
275
                        loadFromXml.addActionListener(buttonsListener);
276
                        loadFromXml.setIcon(RasterToolsUtil.getIcon("tfwload-icon"));
277
                }
278
                return loadFromXml;
279
        }
280
        
281
        /**
282
         * Obtiene el bot?n de salvar a XML. Si no existe se crea.
283
         * @return JButton
284
         */
285
        public JButton getSaveToXMLButton() {
286
                if(endGeoref == null) {
287
                        saveToXml = new JButton();
288
                        saveToXml.setPreferredSize(new Dimension(22, 19));
289
                        saveToXml.setToolTipText(RasterToolsUtil.getText(this, "save_to_xml"));
290
                        saveToXml.addActionListener(buttonsListener);
291
                        saveToXml.setIcon(RasterToolsUtil.getIcon("save-icon"));
292
                }
293
                return saveToXml;
294
        }
295
        
296
        /**
297
         * Obtiene el bot?n de procesar georreferenciaci?n. Si no existe se crea.
298
         * @return JButton
299
         */
300
        public JButton getOptionsButton() {
301
                if(endGeoref == null) {
302
                        options = new JButton();
303
                        options.setPreferredSize(new Dimension(22, 19));
304
                        options.setToolTipText(RasterToolsUtil.getText(this, "options"));
305
                        options.addActionListener(buttonsListener);
306
                        options.setIcon(RasterToolsUtil.getIcon("options-icon"));
307
                }
308
                return options;
309
        }
310
        
311
        /**
312
         * Obtiene el bot?n de procesar georreferenciaci?n. Si no existe se crea.
313
         * @return JButton
314
         */
315
        public JToggleButton getToolSelectPointButton() {
316
                if(addPoint == null) {
317
                        addPoint = new JToggleButton();
318
                        addPoint.setPreferredSize(new Dimension(22, 19));
319
                        addPoint.setToolTipText(RasterToolsUtil.getText(this, "add-point"));
320
                        addPoint.addActionListener(buttonsListener);
321
                        addPoint.setIcon(RasterToolsUtil.getIcon("add-icon"));
322
                }
323
                return addPoint;
324
        }
325
        
326
        /**
327
         * Obtiene el bot?n de procesar georreferenciaci?n. Si no existe se crea.
328
         * @return JButton
329
         */
330
        public JButton getCenterButton() {
331
                if(centerView == null) {
332
                        centerView = new JButton();
333
                        centerView.setPreferredSize(new Dimension(22, 19));
334
                        centerView.setToolTipText(RasterToolsUtil.getText(this, "center_view"));
335
                        centerView.addActionListener(buttonsListener);
336
                        centerView.setIcon(RasterToolsUtil.getIcon("centerpoint-icon"));
337
                }
338
                return centerView;
339
        }
340
        
341
        /*
342
         * (non-Javadoc)
343
         * @see com.iver.andami.ui.mdiManager.IWindow#getWindowInfo()
344
         */
345
        public WindowInfo getWindowInfo() {
346
                WindowInfo m_viewinfo = new WindowInfo(WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE);
347
                //if (getClippingPanel().getFLayer() != null)
348
                        //m_viewinfo.setAdditionalInfo(getClippingPanel().getFLayer().getName());
349
                m_viewinfo.setTitle(PluginServices.getText(this, "points_panel"));
350
                m_viewinfo.setX(posX);
351
                m_viewinfo.setY(posY);
352
                m_viewinfo.setHeight(h);
353
                m_viewinfo.setWidth(w);
354
                return m_viewinfo;
355
        }
356
        
357
        //****************************************************
358
        //Acceso a los datos de la tabla
359
        
360
        /**
361
         * Obtiene las coordenadas de una fila
362
         * @param row Fila de la que se quieren obtener las coordenadas
363
         * @return Array con 4 valores coordenadas real X, coordenada real Y, coordenadas raster X y coordenada raste Y
364
         */
365
        public double[] getCoordinates(int row) {
366
                try {
367
                        double xMap = 0; 
368
                        Object value = getTable().getModel().getValueAt(row, 2);
369
                        if(value instanceof Double) 
370
                                xMap = ((Double)value).doubleValue();
371
                        else if(value instanceof String) 
372
                                xMap = Double.valueOf(((String)value)).doubleValue();
373

    
374
                        double yMap = 0;
375
                        value = getTable().getModel().getValueAt(row, 3);
376
                        if(value instanceof Double) 
377
                                yMap = ((Double)value).doubleValue();
378
                        else if(value instanceof String) 
379
                                yMap = Double.valueOf(((String)value)).doubleValue();
380

    
381
                        double xRaster = 0;
382
                        value = getTable().getModel().getValueAt(row, 4);
383
                        if(value instanceof Double) 
384
                                xRaster = ((Double)value).doubleValue();
385
                        else if(value instanceof String) 
386
                                xRaster = Double.valueOf(((String)value)).doubleValue();
387

    
388
                        double yRaster = 0;
389
                        value = getTable().getModel().getValueAt(row, 5);
390
                        if(value instanceof Double) 
391
                                yRaster = ((Double)value).doubleValue();
392
                        else if(value instanceof String) 
393
                                yRaster = Double.valueOf(((String)value)).doubleValue();
394
                        
395
                        return new double[]{xMap, yMap, xRaster, yRaster};
396
                }catch (NotInitializeException ex) {
397
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", table);
398
                }catch (NumberFormatException ex1) {
399
                        RasterToolsUtil.messageBoxYesOrNot("value_not_valid", table);
400
                }
401
                return null;
402
        }
403

    
404
        /**
405
         * 
406
         * @param row
407
         * @return
408
         */
409
        public boolean getChecked(int row) {
410
                try {
411
                        Object value = getTable().getModel().getValueAt(row, 0);
412
                        if(value != null && value instanceof Boolean) 
413
                                return ((Boolean)value).booleanValue();
414
                }catch (NotInitializeException ex) {
415
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", table);
416
                }
417
                return false;
418
        }
419
        
420
        /**
421
         * A?ade una fila de datos al final de la tabla
422
         * @param checked
423
         * @param mX
424
         * @param mY
425
         * @param pX
426
         * @param pY
427
         * @param z
428
         * @param eX
429
         * @param eY
430
         * @param rms
431
         */
432
        public void addRow(boolean checked, Point2D map, Point2D pixel, double z, double eX, double eY, double rms, long id) {
433
                try {
434
                        int rowCount = getTable().getRowCount();
435
                        DefaultTableModel model = getTable().getModel();
436
                        if(model instanceof GCPModel) {
437
                                Object[] rowData = ((GCPModel)model).getNewLine();
438
                                rowData[0] = new Boolean(checked);
439
                                rowData[1] = rowCount + "";
440
                                rowData[2] = map.getX() + "";
441
                                rowData[3] = map.getY() + "";
442
                                rowData[4] = pixel.getX() + "";
443
                                rowData[5] = pixel.getY() + "";
444
                                rowData[rowData.length - 1] = new Long(id);
445
                                getTable().addRow(rowData);
446
                        }
447
                } catch (NotInitializeException e) {
448
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", table);
449
                }
450
        }
451
        
452
        /**
453
         * Elimina todos los puntos de la tabla
454
         */
455
        public void removeAllPoints() {
456
                try {
457
                        getTable().removeAllRows();
458
                } catch (NotInitializeException e) {
459
                        RasterToolsUtil.messageBoxYesOrNot("table_not_initialize", table);
460
                }
461
        }
462
}
463