Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / legend / gui / SymbolTable.java @ 28407

History | View | Annotate | Download (22.3 KB)

1 7304 caballero
/*
2
 * Created on 27-abr-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.project.documents.view.legend.gui;
48
49 14522 jdominguez
import java.awt.Component;
50 7738 jaume
import java.awt.Dimension;
51
import java.awt.GridLayout;
52 14522 jdominguez
import java.awt.Point;
53
import java.awt.Rectangle;
54
import java.awt.event.ActionListener;
55
import java.awt.event.MouseAdapter;
56
import java.awt.event.MouseEvent;
57 25954 vcaballero
import java.beans.PropertyChangeEvent;
58
import java.beans.PropertyChangeListener;
59 14522 jdominguez
import java.util.ArrayList;
60 20779 vcaballero
import java.util.Hashtable;
61 7738 jaume
import javax.swing.JPanel;
62
import javax.swing.JScrollPane;
63 14522 jdominguez
import javax.swing.event.ChangeEvent;
64 7738 jaume
import javax.swing.table.DefaultTableModel;
65 20779 vcaballero
import javax.swing.table.TableCellEditor;
66 7738 jaume
import javax.swing.table.TableColumn;
67
import com.hardcode.gdbms.engine.values.NullValue;
68
import com.iver.andami.PluginServices;
69 14522 jdominguez
import com.iver.andami.ui.mdiFrame.JMenuItem;
70
import com.iver.andami.ui.mdiFrame.JPopUpMenu;
71 9638 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
72 7304 caballero
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
73
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
74 11704 jaume
import com.iver.cit.gvsig.project.documents.gui.SymbolCellEditor;
75
import com.iver.cit.gvsig.project.documents.gui.TableSymbolCellRenderer;
76 7304 caballero
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.IntervalCellEditor;
77
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ValueCellEditor;
78
import com.iver.utiles.swing.jtable.JTable;
79
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
80
81
/**
82
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
83
 * etiquetado de estos valores.
84 22710 vcaballero
 *
85 7304 caballero
 * @author Vicente Caballero Navarro
86
 */
87 14522 jdominguez
public class SymbolTable extends JPanel {
88 24681 vcaballero
    private static final long serialVersionUID = -8694846716328735113L;
89
    private static Hashtable<String,TableCellEditor> cellEditors = new Hashtable<String,TableCellEditor>();
90 20779 vcaballero
91 24681 vcaballero
    public static final String VALUES_TYPE = "values";
92
    public static final String INTERVALS_TYPE = "intervals";
93
    private JTable table;
94
    private String type;
95
    private int shapeType;
96
    private OnTableMouseAdapter rightClickActions;
97 10679 jaume
98 24681 vcaballero
    /**
99
     * Crea un nuevo FSymbolTable.
100
     *
101
     * @param type
102
     *            tipo de valor si es intervalo: "intervals" y si es por
103
     *            valores: "values".
104
     */
105
    public SymbolTable(Component ownerComponent, String type, int shapeType) {
106
        super(new GridLayout(1, 0));
107
        this.type = type;
108
        this.shapeType = shapeType;
109 22710 vcaballero
110 24681 vcaballero
        rightClickActions = new OnTableMouseAdapter();
111
        table = new JTable();
112
        table.setModel(new MyTableModel());
113
        table.setPreferredScrollableViewportSize(new Dimension(480, 110));
114 22710 vcaballero
115 24681 vcaballero
        initializeCellEditors();
116 22710 vcaballero
117 24681 vcaballero
        // Create the scroll pane and add the table to it.
118
        JScrollPane scrollPane = new JScrollPane(table);
119 7304 caballero
120 24681 vcaballero
        // Set up column sizes.
121
        // initColumnSizes(table);
122
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
123 22710 vcaballero
124 24681 vcaballero
        if(cellEditors.get(type) == null)
125
            throw new Error("Symbol table type not set!");
126 22710 vcaballero
127 24681 vcaballero
        setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
128
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
129 7304 caballero
130 24681 vcaballero
        // Add the scroll pane to this panel.
131
        add(scrollPane);
132
        table.setRowSelectionAllowed(true);
133
        table.addMouseListener(rightClickActions);
134
    }
135
    /**
136
     * Inicializa los valores de los CellEditors que la SymbolTable poseer? por defecto
137
     */
138
    private void initializeCellEditors() {
139
        this.cellEditors.put(this.INTERVALS_TYPE,new IntervalCellEditor());
140
        this.cellEditors.put(this.VALUES_TYPE, new ValueCellEditor());
141
    }
142
    /**
143
     * A?ade un nuevo CellEditor a la lista de disponibles
144
     *
145
     * @param key String con el nombre identificativo del CellEditor
146
     * @param cellEditor CellEditor que va a ser a?adido
147
     */
148
    public static void addCellEditor(String key,TableCellEditor cellEditor ) {
149
        cellEditors.put(key, cellEditor);
150
    }
151
    /**
152
     * Obtiene el valor de los elementos de una fila seleccionada
153
     *
154
     * @return Object[] Array con los objetos de cada una de las columnas de la fila seleccionada
155
     */
156
    public Object[] getSelectedRowElements() {
157
        Object[] values = new Object[3];
158 7304 caballero
159 24681 vcaballero
        MyTableModel m = (MyTableModel) table.getModel();
160
        int[] selectedRows = table.getSelectedRows();
161 20779 vcaballero
162 24681 vcaballero
        if(selectedRows.length != 1)
163
            return null;
164 20779 vcaballero
165 24681 vcaballero
        for (int i = 0; i < 3; i++) {
166
            values[i] = m.getValueAt(selectedRows[0], i);
167
        }
168 20779 vcaballero
169 24681 vcaballero
        return values;
170
    }
171
    /**
172
     * A?ade una fila al modelo.
173
     *
174
     * @param vector
175
     *            Fila en forma de vector de Object para a?adir al modelo.
176
     */
177
    public void addRow(Object[] vector) {
178
        MyTableModel m = (MyTableModel) table.getModel();
179
        m.addRow(vector);
180 22710 vcaballero
181 24681 vcaballero
    }
182 7304 caballero
183 24681 vcaballero
    /**
184
     * Elimina la fila que tiene como clave el objeto que se pasa como
185
     * par?metro.
186
     *
187
     * @param obj
188
     *            clave del objeto a eliminar.
189
     */
190
    public void removeRow(Object obj) {
191
        MyTableModel m = (MyTableModel) table.getModel();
192 7304 caballero
193 24681 vcaballero
        for (int i = 0; i < m.getRowCount(); i++) {
194
            if (m.getValueAt(i, 1) instanceof NullUniqueValue
195
                    || m.getValueAt(i, 1) instanceof NullIntervalValue) {
196
                m.removeRow(i);
197
            }
198
        }
199
    }
200 7304 caballero
201 24681 vcaballero
    /**
202
     * Elimina las filas que est?n seleccionadas.
203
     */
204
    public void removeSelectedRows() {
205
        if (table.getCellEditor() != null) {
206
            table.getCellEditor().cancelCellEditing();
207
        }
208 7304 caballero
209 24681 vcaballero
        MyTableModel m = (MyTableModel) table.getModel();
210
        int[] selectedRows = table.getSelectedRows();
211 7304 caballero
212 24681 vcaballero
        for (int i = selectedRows.length - 1; i >= 0; i--) {
213
            m.removeRow(selectedRows[i]);
214
        }
215
    }
216 7304 caballero
217 24681 vcaballero
    /**
218
     * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
219
     * como par?metro.
220
     *
221
     * @param symbols
222
     *            Array de s?mbolos
223
     * @param values
224
     *            Array de valores.
225
     * @param descriptions
226
     *            Array de descripciones.
227
     */
228
    public void fillTableFromSymbolList(ISymbol[] symbols, Object[] values,
229
            String[] descriptions) {
230
        ISymbol theSymbol;
231 7304 caballero
232 24681 vcaballero
        for (int i = 0; i < symbols.length; i++) {
233
            theSymbol = symbols[i];
234
            if(!(values[i] instanceof NullIntervalValue) && !(values[i] instanceof NullUniqueValue))
235
                addTableRecord(theSymbol, values[i], descriptions[i]);
236
        }
237
    }
238 7304 caballero
239 24681 vcaballero
    /**
240
     * A?ade una fila con los objetos que se pasan como par?metros.
241
     *
242
     * @param symbol
243
     *            s?mbolo de la fila.
244
     * @param value
245
     *            Valor de la fila.
246
     * @param description
247
     *            Descripci?n.
248
     */
249
    public void addTableRecord(ISymbol symbol, Object value, String description) {
250
        Object[] theRow = new Object[3];
251
        theRow[0] = symbol;
252
        theRow[1] = value;
253
        theRow[2] = description;
254
        addRow(theRow);
255
    }
256 7304 caballero
257 24681 vcaballero
    /**
258
     * Devuelve el valor a partie del n?mero de fila y columna.
259
     *
260
     * @param row
261
     *            n?mero de fila.
262
     * @param col
263
     *            n?mero de columna.
264
     *
265
     * @return Objeto.
266
     */
267
    public Object getFieldValue(int row, int col) {
268
        MyTableModel m = (MyTableModel) table.getModel();
269 7304 caballero
270 24681 vcaballero
        return m.getValueAt(row, col);
271
    }
272 7304 caballero
273 24681 vcaballero
    /**
274
     * Devuelve el n?mero total de filas que contiene el modelo.
275
     *
276
     * @return N?mero de filas.
277
     */
278
    public int getRowCount() {
279
        MyTableModel m = (MyTableModel) table.getModel();
280 7304 caballero
281 24681 vcaballero
        return m.getRowCount();
282
    }
283 7304 caballero
284 24681 vcaballero
    /**
285
     * Elimina todas las filas del modelo.
286
     */
287
    public void removeAllItems() {
288
        table.setModel(new MyTableModel());
289
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
290
        setUpValueColumn(table, table.getColumnModel().getColumn(1),cellEditors.get(this.type));
291
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
292
    }
293 7304 caballero
294 24681 vcaballero
    /**
295
     * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
296
     * como par?metro.
297
     *
298
     * @param table2
299
     *            Tabla.
300
     * @param column
301
     *            Columna.
302
     */
303
    public void setUpLabelColumn(JTable table2, TableColumn column) {
304
        TextFieldCellEditor labeleditor = new TextFieldCellEditor();
305
        column.setCellEditor(labeleditor);
306
    }
307 7304 caballero
308 24681 vcaballero
    /**
309
     * Inicializa el cell editor de tipo valor de la columna que se pasa como
310
     * par?metro.
311
     *
312
     * @param table2
313
     *            Tabla.
314
     * @param column
315
     *            Columna.
316
     * @param tableCellEditor
317
     */
318
    public void setUpValueColumn(JTable table2,TableColumn column, TableCellEditor tableCellEditor) {
319
        column.setCellEditor(tableCellEditor);
320
    }
321
    /**
322
     * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
323
     * par?metro.
324
     *
325
     * @param table2
326
     *            Tabla.
327
     * @param column
328
     *            Columna.
329
     */
330
    public void setUpSymbolColumn(JTable table2, TableColumn column) {
331
        // Set up the editor
332
        column.setMaxWidth(100);
333
        column.setWidth(60);
334
        column.setPreferredWidth(60);
335
        column.setMinWidth(50);
336 7304 caballero
337 24681 vcaballero
        // FSymbolCellEditor symboleditor = new FSymbolCellEditor();
338 25872 vcaballero
        SymbolCellEditor symboleditor = new SymbolCellEditor();
339 24681 vcaballero
        column.setCellEditor(symboleditor);
340 7304 caballero
341 24681 vcaballero
        TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
342
        column.setCellRenderer(renderer);
343
    }
344 7304 caballero
345 24681 vcaballero
    public int[] getSelectedRows(){
346
            return table.getSelectedRows();
347
    }
348 7304 caballero
349 24681 vcaballero
    public void moveDownRows(int startPos, int endPos, int numOfElements) {
350
            if(startPos > endPos)
351
                    return;
352
            if(startPos >= getRowCount()-1 )
353
                    return;
354
            if(startPos == getRowCount()-1)
355
                    return;
356 7304 caballero
357 24681 vcaballero
            Object[][] values = new Object[getRowCount()][3];
358
        for (int i = 0; i < getRowCount(); i++) {
359
                        values[i][0] = table.getModel().getValueAt(i,0);
360
                        values[i][1] = table.getModel().getValueAt(i,1);
361
                        values[i][2] = table.getModel().getValueAt(i,2);
362 10825 jaume
                }
363 7304 caballero
364 24681 vcaballero
        Object[][]aux = new Object[numOfElements][3];
365
        for (int i = 0; i < numOfElements; i++) {
366
367
                aux[numOfElements - i - 1][0] = values[startPos - i][0];
368
                aux[numOfElements - i - 1][1] = values[startPos - i][1];
369
                aux[numOfElements - i - 1][2] = values[startPos - i][2];
370 10825 jaume
                }
371 7304 caballero
372 24681 vcaballero
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
373
374
        values[startPos - numOfElements + 1][0] = targetVal[0][0];
375
        values[startPos - numOfElements + 1][1] = targetVal[0][1];
376
        values[startPos - numOfElements + 1][2] = targetVal[0][2];
377
378
        for (int i = 0; i < numOfElements; i++) {
379
                values[endPos - i][0] = aux[numOfElements - i - 1][0];
380
                values[endPos - i][1] = aux[numOfElements - i - 1][1];
381
                values[endPos - i][2] = aux[numOfElements - i - 1][2];
382 10825 jaume
                }
383 7304 caballero
384 24681 vcaballero
        ISymbol[] symbols = new ISymbol[getRowCount()];
385
        Object[] objects = new Object[getRowCount()];
386
        String[] cads = new String[getRowCount()];
387
388
        for (int i = 0; i < getRowCount(); i++) {
389
                        symbols[i] = (ISymbol) values[i][0];
390
                        objects[i] = values[i][1];
391
                        cads[i] = (String) values[i][2];
392 10825 jaume
                }
393 7304 caballero
394 24681 vcaballero
        removeAllItems();
395
        fillTableFromSymbolList(symbols,objects,cads);
396
            table.addRowSelectionInterval(endPos-numOfElements+1,endPos);
397
    }
398 22710 vcaballero
399 24681 vcaballero
400
    public void moveUpRows(int startPos, int endPos, int numOfElements) {
401
402
            if(startPos == 0)
403
                    return;
404
            if(endPos > startPos)
405
                    return;
406
407
408
        Object[][] values = new Object[getRowCount()][3];
409
        for (int i = 0; i < getRowCount(); i++) {
410
                        values[i][0] = table.getModel().getValueAt(i,0);
411
                        values[i][1] = table.getModel().getValueAt(i,1);
412
                        values[i][2] = table.getModel().getValueAt(i,2);
413 22710 vcaballero
                }
414
415 24681 vcaballero
        Object[][]aux = new Object[numOfElements][3];
416
        for (int i = 0; i < numOfElements; i++) {
417 22710 vcaballero
418 24681 vcaballero
                aux[i][0] = values[startPos + i][0];
419
                aux[i][1] = values[startPos + i][1];
420
                aux[i][2] = values[startPos + i][2];
421
                }
422 22710 vcaballero
423 24681 vcaballero
        Object [][] targetVal = {{values[endPos][0],values[endPos][1],values[endPos][2]}};
424
425
        values[startPos + numOfElements - 1][0] = targetVal[0][0];
426
        values[startPos + numOfElements - 1][1] = targetVal[0][1];
427
        values[startPos + numOfElements - 1][2] = targetVal[0][2];
428
429
        for (int i = 0; i < numOfElements; i++) {
430
431
                values[endPos + i][0] = aux[i][0];
432
                values[endPos + i][1] = aux[i][1];
433
                values[endPos + i][2] = aux[i][2];
434 22710 vcaballero
                }
435
436 24681 vcaballero
        ISymbol[] symbols = new ISymbol[getRowCount()];
437
        Object[] objects = new Object[getRowCount()];
438
        String[] cads = new String[getRowCount()];
439 22710 vcaballero
440 24681 vcaballero
        for (int i = 0; i < getRowCount(); i++) {
441
                        symbols[i] = (ISymbol) values[i][0];
442
                        objects[i] = values[i][1];
443
                        cads[i] = (String) values[i][2];
444
                }
445 14522 jdominguez
446 24681 vcaballero
        removeAllItems();
447
        fillTableFromSymbolList(symbols,objects,cads);
448
        table.addRowSelectionInterval(endPos,endPos+numOfElements-1);
449
    }
450 14522 jdominguez
451
452 24681 vcaballero
    /**
453
     * Modelo que propio que se aplica a la tabla.
454
     *
455
     * @author Vicente Caballero Navarro
456
     */
457
    class MyTableModel extends DefaultTableModel {
458
        private static final long serialVersionUID = 1L;
459 22710 vcaballero
460 24681 vcaballero
        // AbstractTableModel {
461
        private String[] columnNames = {
462
                PluginServices.getText(this, "Simbolo"),
463
                PluginServices.getText(this, "Valor"),
464
                PluginServices.getText(this, "Etiqueta") };
465 14522 jdominguez
466 24681 vcaballero
        /**
467
         * Devuelve el n?mero de columnas.
468
         *
469
         * @return N?mero de columnas.
470
         */
471
        public int getColumnCount() {
472
            return columnNames.length;
473
        }
474 14522 jdominguez
475 24681 vcaballero
        /**
476
         * Devuelve el String del valor de la columna.
477
         *
478
         * @param col
479
         *            N?mero de columna.
480
         *
481
         * @return Nombre de la columna.
482
         */
483
        public String getColumnName(int col) {
484
            return columnNames[col];
485
        }
486 22710 vcaballero
487 24681 vcaballero
        /**
488
         * JTable uses this method to determine the default renderer/ editor for
489
         * each cell. If we didn't implement this method, then the last column
490
         * would contain text ("true"/"false"), rather than a check box.
491
         */
492
        public Class getColumnClass(int c) {
493
            if (getValueAt(0, c) == null) {
494
                return NullValue.class;
495
            }
496
            return getValueAt(0, c).getClass();
497
        }
498 14522 jdominguez
499 24681 vcaballero
        /*
500
         * Don't need to implement this method unless your table's editable.
501
         */
502
        public boolean isCellEditable(int row, int col) {
503
            // Note that the data/cell address is constant,
504
            // no matter where the cell appears onscreen.
505
            // if (col > 0) {
506
            return true;
507
        }
508 14522 jdominguez
509 24681 vcaballero
        @Override
510
        public Object getValueAt(int row, int column) {
511
            if(column == 2)
512
                return ((ISymbol)getValueAt(row,0)).getDescription();
513 14522 jdominguez
514 24681 vcaballero
            return super.getValueAt(row, column);
515
        }
516 22710 vcaballero
517 24681 vcaballero
        @Override
518
        public void setValueAt(Object aValue, int row, int column) {
519 14522 jdominguez
520 25954 vcaballero
                if(column == 0){
521
                        ISymbol oldSymbol = (ISymbol) getValueAt(row,0);
522
                        ISymbol newSymbol = (ISymbol) aValue;
523
                        newSymbol.setDescription(oldSymbol.getDescription());
524
                        //TODO: Habr?a que enviar a la leyenda un replace(oldSymbol, newSymbol)
525
                        super.setValueAt(newSymbol, row, column);
526
                } else {
527
                        if(column == 2){
528
                                ISymbol symbol = (ISymbol) getValueAt(row,0);
529
                                symbol.setDescription((String) aValue);
530
                                setValueAt(symbol,row,0);
531
                        }
532 14522 jdominguez
533 25954 vcaballero
                        super.setValueAt(aValue, row, column);
534
                }
535 24681 vcaballero
        }
536 22710 vcaballero
537 24681 vcaballero
    }
538 14522 jdominguez
539 24681 vcaballero
    private class OnTableMouseAdapter extends MouseAdapter {
540 14522 jdominguez
541 24681 vcaballero
        private JPopUpMenu menu = new JPopUpMenu();
542 14522 jdominguez
543 24681 vcaballero
        // group option
544
        private JMenuItem groupItem = new JMenuItem(
545
                PluginServices.getText(this, "group"));
546
        private ActionListener groupAction = new ActionListener() {
547
            public void actionPerformed(java.awt.event.ActionEvent e) {
548
                hidePopUp();
549
                int[] selectedRows = table.getSelectedRows();
550
                if (selectedRows.length > 1) {
551
                    DefaultTableModel model = (DefaultTableModel) table
552
                            .getModel();
553
                    int theRow = selectedRows[0];
554
                    ISymbol symboToBeApplied = (ISymbol) model.getValueAt(
555
                            theRow, 0);
556
                    String labelToBeApplied = (String) model.getValueAt(
557
                            theRow, 2);
558
                    ArrayList<Object> valuesToBeApplied = new ArrayList<Object>(
559
                            selectedRows.length);
560
                    for (int i = 0; i < selectedRows.length; i++) {
561
                        valuesToBeApplied.add(model.getValueAt(selectedRows[i],
562
                                1));
563
                    }
564 14522 jdominguez
565 24681 vcaballero
                    for (int i = selectedRows.length - 1; i > 0; i--) {
566
                        model.removeRow(selectedRows[i]);
567
                    }
568 14522 jdominguez
569 24681 vcaballero
                    model.setValueAt(symboToBeApplied, theRow, 0);
570
                    model.setValueAt(labelToBeApplied, theRow, 2);
571
                    table.clearSelection();
572
                    table.doLayout();
573
                }
574
            }
575
        };
576 14522 jdominguez
577 24681 vcaballero
        // combine option
578
        private JMenuItem combineItem = new JMenuItem(
579
                PluginServices.getText(this, "combine"));
580
        private ActionListener combineAction = new ActionListener() {
581
            public void actionPerformed(java.awt.event.ActionEvent e) {
582
                hidePopUp();
583
                int[] selectedRows = table.getSelectedRows();
584
                if (selectedRows.length > 1) {
585
                    DefaultTableModel model = (DefaultTableModel) table
586
                            .getModel();
587
                    int theRow = selectedRows[0];
588
                    ISymbol symboToBeApplied = (ISymbol) model.getValueAt(
589
                            theRow, 0);
590
                    String labelToBeApplied = (String) model.getValueAt(
591
                            theRow, 2);
592
                    ArrayList<Object> valuesToBeApplied = new ArrayList<Object>(
593
                            selectedRows.length);
594
                    for (int i = 0; i < selectedRows.length; i++) {
595
                        valuesToBeApplied.add(model.getValueAt(selectedRows[i],
596
                                1));
597
                    }
598 14522 jdominguez
599 24681 vcaballero
                    for (int i = selectedRows.length - 1; i > 0; i--) {
600
                        model.removeRow(selectedRows[i]);
601
                    }
602 14522 jdominguez
603 24681 vcaballero
                    model.setValueAt(symboToBeApplied, theRow, 0);
604
                    model.setValueAt(labelToBeApplied, theRow, 2);
605
                    table.clearSelection();
606
                    table.doLayout();
607
                }
608
            }
609
        };
610
611
        private boolean menuEmpty = false;
612
613
614
        {
615
            groupItem.addActionListener(groupAction);
616
            if (VALUES_TYPE.equals(type)) {
617
                menu.add(groupItem);
618
            } else if (INTERVALS_TYPE.equals(type)) {
619
                menu.add(combineItem);
620
            } else {
621
                menuEmpty = true;
622
            }
623
624
        }
625
626
        @Override
627
        public void mouseExited(MouseEvent e) {
628
            // if we click outside the popup menu
629
            if (menu.isVisible()) {
630
                Rectangle tableBounds = table.getBounds();
631
                tableBounds.setLocation(table.getLocationOnScreen());
632
                if (!tableBounds.contains(getClickLocation(e))) {
633
                    hidePopUp();
634
                }
635
            }
636
637
        }
638
639
        @Override
640
        public void mouseClicked(MouseEvent e) {
641
            super.mouseClicked(e);
642
643
            // if we click outside the popup menu
644
            if (menu.isVisible()
645
                    && !menu.getBounds().contains(getClickLocation(e))) {
646
                hidePopUp();
647
            }
648
649
            if (e.getButton() == MouseEvent.BUTTON3) {
650
                e.consume();
651
                int[] selectedRows = table.getSelectedRows();
652
                if (selectedRows.length > 0) {
653
                    Point realClickLocation = getClickLocation(e);
654
                    menu.setLocation(realClickLocation);
655
                    showPopUp();
656
                }
657
            }
658
659
        }
660
661
        private void showPopUp() {
662
            if (!menuEmpty) {
663
                table.setEnabled(false);
664
                table.editingCanceled(new ChangeEvent(table));
665
                menu.setVisible(true);
666
            }
667
        }
668
669
        private void hidePopUp() {
670
            if (!menuEmpty ) {
671
                menu.setVisible(false);
672
                table.setEnabled(true);
673
            }
674
        }
675
676
        private Point getClickLocation(MouseEvent e) {
677
            Point tableLocation = table.getLocationOnScreen();
678
            Point relativeClickPoint = e.getPoint();
679
            Point realClickLocation = new Point(tableLocation.x
680
                    + relativeClickPoint.x, tableLocation.y
681
                    + relativeClickPoint.y);
682
            return realClickLocation;
683
        }
684
685
    }
686 7304 caballero
}