Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap-share / src / main / java / com / iver / ai2 / gvsig3d / legend / extrusion / SymbolTableExtrusion.java @ 25860

History | View | Annotate | Download (15.3 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
*
3
* Copyright (C) 2005 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 com.iver.ai2.gvsig3d.legend.extrusion;
20

    
21
import java.awt.Component;
22
import java.awt.Dimension;
23
import java.awt.GridLayout;
24
import java.awt.Point;
25
import java.awt.Rectangle;
26
import java.awt.event.ActionListener;
27
import java.awt.event.MouseAdapter;
28
import java.awt.event.MouseEvent;
29
import java.util.ArrayList;
30
import java.util.Hashtable;
31

    
32
import javax.swing.JPanel;
33
import javax.swing.JScrollPane;
34
import javax.swing.event.ChangeEvent;
35
import javax.swing.table.DefaultTableModel;
36
import javax.swing.table.TableCellEditor;
37
import javax.swing.table.TableColumn;
38

    
39
import com.hardcode.gdbms.engine.values.NullValue;
40
import com.iver.andami.PluginServices;
41
import com.iver.andami.ui.mdiFrame.JMenuItem;
42
import com.iver.andami.ui.mdiFrame.JPopUpMenu;
43
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
44
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
45
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
46
import com.iver.cit.gvsig.project.documents.gui.SymbolCellEditor;
47
import com.iver.cit.gvsig.project.documents.gui.TableSymbolCellRenderer;
48
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.IntervalCellEditor;
49
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ValueCellEditor;
50

    
51

    
52
import com.iver.utiles.swing.jtable.JTable;
53
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
54

    
55
public class SymbolTableExtrusion  extends JPanel {
56
        private static final long serialVersionUID = -8694846716328735113L;
57
        private static Hashtable<String,TableCellEditor> cellEditors = new Hashtable<String,TableCellEditor>();
58

    
59
        public static final String VALUES_TYPE = "values";
60
        public static final String INTERVALS_TYPE = "intervals";
61
        private JTable table;
62
        private String type;
63
        private int shapeType;
64
        private OnTableMouseAdapter rightClickActions;
65

    
66
        /**
67
         * Crea un nuevo FSymbolTable.
68
         *
69
         * @param type
70
         *            tipo de valor si es intervalo: "intervals" y si es por
71
         *            valores: "values".
72
         */
73
        public SymbolTableExtrusion(Component ownerComponent, String type, int shapeType) {
74
                super(new GridLayout(1, 0));
75
                this.type = type;
76
                this.shapeType = shapeType;
77

    
78
                rightClickActions = new OnTableMouseAdapter();
79
                table = new JTable();
80
                table.setModel(new MyTableModel());
81
                table.setPreferredScrollableViewportSize(new Dimension(480, 110));
82

    
83
                initializeCellEditors();
84

    
85
                // Create the scroll pane and add the table to it.
86
                JScrollPane scrollPane = new JScrollPane(table);
87

    
88
                // Set up column sizes.
89
                // initColumnSizes(table);
90
                setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
91

    
92
                if(cellEditors.get(type) == null)
93
                        throw new Error("Symbol table type not set!");
94

    
95
                setUpValueColumn(table, table.getColumnModel().getColumn(0),cellEditors.get(this.type));
96
                setUpLabelColumn(table, table.getColumnModel().getColumn(1));
97

    
98
                // Add the scroll pane to this panel.
99
                add(scrollPane);
100
                table.setRowSelectionAllowed(true);
101
                table.addMouseListener(rightClickActions);
102
        }
103
        /**
104
         * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
105
         */
106
        private void initializeCellEditors() {
107
                this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor());
108
                this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor());
109
        }
110
        /**
111
         * A?ade un nuevo CellEditor a la lista de disponibles
112
         *
113
         * @param key String con el nombre identificativo del CellEditor
114
         * @param cellEditor CellEditor que va a ser a?adido
115
         */
116
        public static void addCellEditor(String key,TableCellEditor cellEditor ) {
117
                cellEditors.put(key, cellEditor);
118
        }
119
        /**
120
         * Obtiene el valor de los elementos de una fila seleccionada
121
         *
122
         * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
123
         */
124
        public Object[] getSelectedRowElements() {
125
                Object[] values = new Object[2];
126

    
127
                MyTableModel m = (MyTableModel) table.getModel();
128
                int[] selectedRows = table.getSelectedRows();
129

    
130
                if(selectedRows.length != 1)
131
                        return null;
132

    
133
                for (int i = 0; i < 2; i++) {
134
                        values[i] = m.getValueAt(selectedRows[0], i);
135
                }
136

    
137
                return values;
138
        }
139
        /**
140
         * A?ade una fila al modelo.
141
         *
142
         * @param vector
143
         *            Fila en forma de vector de Object para a?adir al modelo.
144
         */
145
        public void addRow(Object[] vector) {
146
                MyTableModel m = (MyTableModel) table.getModel();
147
                m.addRow(vector);
148

    
149
        }
150

    
151
        /**
152
         * Elimina la fila que tiene como clave el objeto que se pasa como
153
         * par?metro.
154
         *
155
         * @param obj
156
         *            clave del objeto a eliminar.
157
         */
158
        public void removeRow(Object obj) {
159
                MyTableModel m = (MyTableModel) table.getModel();
160

    
161
                for (int i = 0; i < m.getRowCount(); i++) {
162
                        if (m.getValueAt(i, 0) instanceof NullUniqueValue
163
                                        || m.getValueAt(i, 0) instanceof NullIntervalValue) {
164
                                m.removeRow(i);
165
                        }
166
                }
167
        }
168

    
169
        /**
170
         * Elimina las filas que est?n seleccionadas.
171
         */
172
        public void removeSelectedRows() {
173
                if (table.getCellEditor() != null) {
174
                        table.getCellEditor().cancelCellEditing();
175
                }
176

    
177
                MyTableModel m = (MyTableModel) table.getModel();
178
                int[] selectedRows = table.getSelectedRows();
179

    
180
                for (int i = selectedRows.length - 1; i >= 0; i--) {
181
                        m.removeRow(selectedRows[i]);
182
                }
183
        }
184

    
185
        /**
186
         * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
187
         * como par?metro.
188
         *
189
         * @param symbols
190
         *            Array de s?mbolos
191
         * @param values
192
         *            Array de valores.
193
         * @param descriptions
194
         *            Array de descripciones.
195
         */
196
        public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
197
                        Object[] descriptions) {
198
                ISymbol theSymbol;
199

    
200
                for (int i = 0; i < symbols.length; i++) {
201
                        theSymbol = symbols[i];
202
                        if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
203
                                addTableRecord(theSymbol, values[i], descriptions[i]);
204
                }
205
        }
206

    
207
        /**
208
         * A?ade una fila con los objetos que se pasan como par?metros.
209
         *
210
         * @param symbol
211
         *            s?mbolo de la fila.
212
         * @param value
213
         *            Valor de la fila.
214
         * @param description
215
         *            Descripci?n.
216
         */
217
        public void addTableRecord(ISymbol symbol, Object value, Object description) {
218
                Object[] theRow = new Object[2];
219
                theRow[0] = value;
220
                theRow[1] = description;
221
                addRow(theRow);
222
        }
223

    
224
        /**
225
         * Devuelve el valor a partie del n?mero de fila y columna.
226
         *
227
         * @param row
228
         *            n?mero de fila.
229
         * @param col
230
         *            n?mero de columna.
231
         *
232
         * @return Objeto.
233
         */
234
        public Object getFieldValue(int row, int col) {
235
                MyTableModel m = (MyTableModel) table.getModel();
236

    
237
                return m.getValueAt(row, col);
238
        }
239

    
240
        /**
241
         * Devuelve el n?mero total de filas que contiene el modelo.
242
         *
243
         * @return N?mero de filas.
244
         */
245
        public int getRowCount() {
246
                MyTableModel m = (MyTableModel) table.getModel();
247

    
248
                return m.getRowCount();
249
        }
250

    
251
        /**
252
         * Elimina todas las filas del modelo.
253
         */
254
        public void removeAllItems() {
255
                table.setModel(new MyTableModel());
256
                setUpValueColumn(table, table.getColumnModel().getColumn(0),cellEditors.get(this.type));
257
                setUpLabelColumn(table, table.getColumnModel().getColumn(1));
258
        }
259

    
260
        /**
261
         * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
262
         * como par?metro.
263
         *
264
         * @param table2
265
         *            Tabla.
266
         * @param column
267
         *            Columna.
268
         */
269
        public void setUpLabelColumn(JTable table2, TableColumn column) {
270
                TextFieldCellEditor labeleditor = new TextFieldCellEditor();
271
                column.setCellEditor(labeleditor);
272
        }
273

    
274
        /**
275
         * Inicializa el cell editor de tipo valor de la columna que se pasa como
276
         * par?metro.
277
         *
278
         * @param table2
279
         *            Tabla.
280
         * @param column
281
         *            Columna.
282
         * @param tableCellEditor
283
         */
284
        public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) {
285
                column.setCellEditor(tableCellEditor);
286
        }
287
        /**
288
         * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
289
         * par?metro.
290
         *
291
         * @param table2
292
         *            Tabla.
293
         * @param column
294
         *            Columna.
295
         */
296
        public void setUpSymbolColumn(JTable table2, TableColumn column) {
297
                // Set up the editor
298
                column.setMaxWidth(100);
299
                column.setWidth(60);
300
                column.setPreferredWidth(60);
301
                column.setMinWidth(50);
302

    
303
                // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
304
                SymbolCellEditor symboleditor = new SymbolCellEditor(shapeType);
305
                column.setCellEditor(symboleditor);
306

    
307
                TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
308
                column.setCellRenderer(renderer);
309
        }
310

    
311
        /**
312
         * Modelo que propio que se aplica a la tabla.
313
         *
314
         * @author Vicente Caballero Navarro
315
         */
316
        class MyTableModel extends DefaultTableModel {
317
                private static final long serialVersionUID = 1L;
318

    
319
                // AbstractTableModel {
320
                private String[] columnNames = {
321
                                PluginServices.getText(this, "Valor_inicial"),
322
                                PluginServices.getText(this, "Valor_extrusionado") };
323

    
324
                /**
325
                 * Devuelve el n?mero de columnas.
326
                 *
327
                 * @return N?mero de columnas.
328
                 */
329
                public int getColumnCount() {
330
                        return columnNames.length;
331
                }
332

    
333
                /**
334
                 * Devuelve el String del valor de la columna.
335
                 *
336
                 * @param col
337
                 *            N?mero de columna.
338
                 *
339
                 * @return Nombre de la columna.
340
                 */
341
                public String getColumnName(int col) {
342
                        return columnNames[col];
343
                }
344

    
345
                /**
346
                 * JTable uses this method to determine the default renderer/ editor for
347
                 * each cell. If we didn't implement this method, then the last column
348
                 * would contain text ("true"/"false"), rather than a check box.
349
                 */
350
                public Class getColumnClass(int c) {
351
                        if (getValueAt(0, c) == null) {
352
                                return NullValue.class;
353
                        }
354
                        return getValueAt(0, c).getClass();
355
                }
356

    
357
                /*
358
                 * Don't need to implement this method unless your table's editable.
359
                 */
360
                public boolean isCellEditable(int row, int col) {
361
                        // Note that the data/cell address is constant,
362
                        // no matter where the cell appears onscreen.
363
                        // if (col > 0) {
364
                        return true;
365
                }
366

    
367
                @Override
368
                public Object getValueAt(int row, int column) {
369
//                        if(column == 1)
370
//                                return ((ISymbol)getValueAt(row,0)).getDescription();
371

    
372
                        return super.getValueAt(row, column);
373
                }
374

    
375
                @Override
376
                public void setValueAt(Object aValue, int row, int column) {
377

    
378
//                        if(column == 1){
379
//                                ISymbol symbol = (ISymbol) getValueAt(row,0);
380
//                                symbol.setDescription((String) aValue);
381
//                                setValueAt(symbol,row,0);
382
//                        }
383

    
384
                        super.setValueAt(aValue, row, column);
385
                }
386

    
387
        }
388

    
389
        private class OnTableMouseAdapter extends MouseAdapter {
390

    
391
                private JPopUpMenu menu = new JPopUpMenu();
392

    
393
                // group option
394
                private JMenuItem groupItem = new JMenuItem(
395
                                PluginServices.getText(this, "group"));
396
                private ActionListener groupAction = new ActionListener() {
397
                        public void actionPerformed(java.awt.event.ActionEvent e) {
398
                                hidePopUp();
399
                                int[] selectedRows = table.getSelectedRows();
400
                                if (selectedRows.length > 1) {
401
                                        DefaultTableModel model = (DefaultTableModel) table
402
                                                        .getModel();
403
                                        int theRow = selectedRows[0];
404
//                                        ISymbol symboToBeApplied = (ISymbol) model.getValueAt(
405
//                                                        theRow, 0);
406
                                        String labelToBeApplied = (String) model.getValueAt(
407
                                                        theRow, 1);
408
                                        ArrayList<Object> valuesToBeApplied = new ArrayList<Object>(
409
                                                        selectedRows.length);
410
                                        for (int i = 0; i < selectedRows.length; i++) {
411
                                                valuesToBeApplied.add(model.getValueAt(selectedRows[i],
412
                                                                0));
413
                                        }
414

    
415
                                        for (int i = selectedRows.length - 1; i > 0; i--) {
416
                                                model.removeRow(selectedRows[i]);
417
                                        }
418

    
419
//                                        model.setValueAt(symboToBeApplied, theRow, 0);
420
                                        model.setValueAt(labelToBeApplied, theRow, 1);
421
                                        table.clearSelection();
422
                                        table.doLayout();
423
                                }
424
                        }
425
                };
426

    
427
                // combine option
428
                private JMenuItem combineItem = new JMenuItem(
429
                                PluginServices.getText(this, "combine"));
430
                private ActionListener combineAction = new ActionListener() {
431
                        public void actionPerformed(java.awt.event.ActionEvent e) {
432
                                hidePopUp();
433
                                int[] selectedRows = table.getSelectedRows();
434
                                if (selectedRows.length > 1) {
435
                                        DefaultTableModel model = (DefaultTableModel) table
436
                                                        .getModel();
437
                                        int theRow = selectedRows[0];
438
//                                        ISymbol symboToBeApplied = (ISymbol) model.getValueAt(
439
//                                                        theRow, 0);
440
                                        String labelToBeApplied = (String) model.getValueAt(
441
                                                        theRow, 1);
442
                                        ArrayList<Object> valuesToBeApplied = new ArrayList<Object>(
443
                                                        selectedRows.length);
444
                                        for (int i = 0; i < selectedRows.length; i++) {
445
                                                valuesToBeApplied.add(model.getValueAt(selectedRows[i],
446
                                                                0));
447
                                        }
448

    
449
                                        for (int i = selectedRows.length - 1; i > 0; i--) {
450
                                                model.removeRow(selectedRows[i]);
451
                                        }
452

    
453
//                                        model.setValueAt(symboToBeApplied, theRow, 0);
454
                                        model.setValueAt(labelToBeApplied, theRow, 1);
455
                                        table.clearSelection();
456
                                        table.doLayout();
457
                                }
458
                        }
459
                };
460

    
461
                private boolean menuEmpty = false;
462

    
463

    
464
                {
465
                        groupItem.addActionListener(groupAction);
466
                        if (VALUES_TYPE.equals(type)) {
467
                                menu.add(groupItem);
468
                        } else if (INTERVALS_TYPE.equals(type)) {
469
                                menu.add(combineItem);
470
                        } else {
471
                                menuEmpty = true;
472
                        }
473

    
474
                }
475

    
476
                @Override
477
                public void mouseExited(MouseEvent e) {
478
                        // if we click outside the popup menu
479
                        if (menu.isVisible()) {
480
                                Rectangle tableBounds = table.getBounds();
481
                                tableBounds.setLocation(table.getLocationOnScreen());
482
                                if (!tableBounds.contains(getClickLocation(e))) {
483
                                        hidePopUp();
484
                                }
485
                        }
486

    
487
                }
488

    
489
                @Override
490
                public void mouseClicked(MouseEvent e) {
491
                        super.mouseClicked(e);
492

    
493
                        // if we click outside the popup menu
494
                        if (menu.isVisible()
495
                                        && !menu.getBounds().contains(getClickLocation(e))) {
496
                                hidePopUp();
497
                        }
498

    
499
                        if (e.getButton() == MouseEvent.BUTTON3) {
500
                                e.consume();
501
                                int[] selectedRows = table.getSelectedRows();
502
                                if (selectedRows.length > 0) {
503
                                        Point realClickLocation = getClickLocation(e);
504
                                        menu.setLocation(realClickLocation);
505
                                        showPopUp();
506
                                }
507
                        }
508

    
509
                }
510

    
511
                private void showPopUp() {
512
                        if (!menuEmpty) {
513
                                table.setEnabled(false);
514
                                table.editingCanceled(new ChangeEvent(table));
515
                                menu.setVisible(true);
516
                        }
517
                }
518

    
519
                private void hidePopUp() {
520
                        if (!menuEmpty ) {
521
                                menu.setVisible(false);
522
                                table.setEnabled(true);
523
                        }
524
                }
525

    
526
                private Point getClickLocation(MouseEvent e) {
527
                        Point tableLocation = table.getLocationOnScreen();
528
                        Point relativeClickPoint = e.getPoint();
529
                        Point realClickLocation = new Point(tableLocation.x
530
                                        + relativeClickPoint.x, tableLocation.y
531
                                        + relativeClickPoint.y);
532
                        return realClickLocation;
533
                }
534

    
535
        }
536

    
537
}