Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / legend / gui / tablelayers / TableLayers.java @ 11704

History | View | Annotate | Download (16.8 KB)

1
package com.iver.cit.gvsig.project.documents.view.legend.gui.tablelayers;
2

    
3
import java.awt.Component;
4
import java.awt.Dimension;
5
import java.awt.GridLayout;
6
import java.awt.event.MouseEvent;
7
import java.awt.event.MouseListener;
8
import java.util.ArrayList;
9

    
10
import javax.swing.ImageIcon;
11
import javax.swing.JFrame;
12
import javax.swing.JPanel;
13
import javax.swing.JScrollPane;
14
import javax.swing.JTable;
15
import javax.swing.table.AbstractTableModel;
16
import javax.swing.table.TableCellRenderer;
17
import javax.swing.table.TableColumn;
18

    
19
import com.iver.andami.PluginServices;
20
import com.iver.cit.gvsig.fmap.core.FShape;
21
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
22
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
23
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
24
import com.iver.cit.gvsig.fmap.rendering.EditionManagerLegend;
25
import com.iver.cit.gvsig.fmap.rendering.IVectorialLegend;
26
import com.iver.cit.gvsig.project.documents.gui.SymbolCellEditor;
27
import com.iver.cit.gvsig.project.documents.gui.TableSymbolCellRenderer;
28
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ActivatedCellEditor;
29
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.BlockedCellEditor;
30
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.CellIconOptionRenderer;
31
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.DisabledCellEditor;
32
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.FilledCellEditor;
33
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.PresentCellEditor;
34
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ValueCellEditor;
35

    
36

    
37
/**
38
 * DOCUMENT ME!
39
 *
40
 * @author Vicente Caballero Navarro
41
 */
42
public class TableLayers extends JPanel {
43
    private ImageIcon selected = new ImageIcon(this.getClass().getClassLoader()
44
                                                   .getResource("images/selected.png"));
45
    private ImageIcon notselected = new ImageIcon(this.getClass()
46
                                                      .getClassLoader()
47
                                                      .getResource("images/notSelected.png"));
48
    private ImageIcon blocked = new ImageIcon(this.getClass().getClassLoader()
49
                                                  .getResource("images/blocked.png"));
50
    private ImageIcon unblocked = new ImageIcon(this.getClass().getClassLoader()
51
                                                    .getResource("images/unblocked.png"));
52
    private ImageIcon active = new ImageIcon(this.getClass().getClassLoader()
53
                                                 .getResource("images/active.png"));
54
    private ImageIcon defuse = new ImageIcon(this.getClass().getClassLoader()
55
                                                 .getResource("images/defuse.png"));
56
    private ImageIcon disable = new ImageIcon(this.getClass().getClassLoader()
57
                                                  .getResource("images/disabled.png"));
58
    private ImageIcon notdisable = new ImageIcon(this.getClass().getClassLoader()
59
                                                     .getResource("images/notdisabled.png"));
60
    private ImageIcon fill = new ImageIcon(this.getClass().getClassLoader()
61
                                               .getResource("images/fill.png"));
62
    private ImageIcon notfill = new ImageIcon(this.getClass().getClassLoader()
63
                                                  .getResource("images/notfill.png"));
64
    private boolean DEBUG = false;
65
    private ReadableVectorial source;
66
    private EditionManagerLegend eml;
67
    private JTable table;
68
        private ArrayList statusListeners=new ArrayList();
69

    
70
    public TableLayers(ReadableVectorial source2, IVectorialLegend legend2) {
71
        super(new GridLayout(1, 0));
72
        this.source = source2;
73
        this.eml = new EditionManagerLegend(legend2);
74

    
75
        table = new JTable(new MyTableModel());
76
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
77
        table.setShowHorizontalLines(false);
78

    
79
        //Create the scroll pane and add the table to it.
80
        JScrollPane scrollPane = new JScrollPane(table);
81

    
82
        //Set up column sizes.
83
        initColumnSizes(table);
84

    
85
        setUpStatusColumn(table, table.getColumnModel().getColumn(0));
86
        setUpValueColumn(table, table.getColumnModel().getColumn(1));
87
        setUpActivateColumn(table, table.getColumnModel().getColumn(2));
88
        setUpDisableColumn(table, table.getColumnModel().getColumn(3));
89
        setUpBlockColumn(table, table.getColumnModel().getColumn(4));
90
        setUpFillColumn(table, table.getColumnModel().getColumn(5));
91
        setUpSymbolColumn(table, table.getColumnModel().getColumn(6));
92

    
93
        //Add the scroll pane to this panel.
94
        add(scrollPane);
95
    }
96

    
97
    /*
98
     * This method picks good column sizes.
99
     * If all column heads are wider than the column's cells'
100
     * contents, then you can just use column.sizeWidthToFit().
101
     */
102
    private void initColumnSizes(JTable table) {
103
        MyTableModel model = (MyTableModel) table.getModel();
104
        TableColumn column = null;
105
        Component comp = null;
106
        int headerWidth = 0;
107
        int cellWidth = 0;
108
        Object[] longValues = model.longValues;
109
        TableCellRenderer headerRenderer = table.getTableHeader()
110
                                                .getDefaultRenderer();
111

    
112
        for (int i = 0; i < 7; i++) {
113
            column = table.getColumnModel().getColumn(i);
114

    
115
            comp = headerRenderer.getTableCellRendererComponent(null,
116
                    column.getHeaderValue(), false, false, 0, 0);
117
            headerWidth = comp.getPreferredSize().width;
118

    
119
            comp = table.getDefaultRenderer(model.getColumnClass(i))
120
                        .getTableCellRendererComponent(table, longValues[i],
121
                    false, false, 0, i);
122
            cellWidth = comp.getPreferredSize().width;
123

    
124
            if (DEBUG) {
125
                System.out.println("Initializing width of column " + i + ". " +
126
                    "headerWidth = " + headerWidth + "; cellWidth = " +
127
                    cellWidth);
128
            }
129

    
130
            ///column.setPreferredWidth(Math.max(headerWidth, cellWidth));
131
            column.setPreferredWidth((headerWidth + cellWidth) / 2);
132
        }
133
    }
134

    
135
    /**
136
     * DOCUMENT ME!
137
     *
138
     * @param table DOCUMENT ME!
139
     * @param column DOCUMENT ME!
140
     */
141
    public void setUpSymbolColumn(JTable table, TableColumn column) {
142
        ///SymbolCellEditor symboleditor = new SymbolCellEditor();
143
        ///column.setCellEditor(symboleditor);
144

    
145
        TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true);
146
        column.setCellRenderer(renderer);
147
    }
148
    public void setUpValueColumn(JTable table, TableColumn column) {
149
        ValueCellEditor valueEditor = new ValueCellEditor();
150
        column.setCellEditor(valueEditor);
151
    }
152
    /**
153
     * DOCUMENT ME!
154
     *
155
     * @param table DOCUMENT ME!
156
     * @param column DOCUMENT ME!
157
     */
158
    public void setUpStatusColumn(JTable table, TableColumn column) {
159

    
160
        PresentCellEditor presenteditor = new PresentCellEditor(eml,table,selected,
161
                notselected);
162
        presenteditor.addMouseListener(new MouseListener(){
163

    
164
                        public void mouseClicked(MouseEvent e) {
165
                                if (e.getClickCount()==2){
166
                                        for (int i=0;i<statusListeners.size();i++) {
167
                                                ((StatusListener)statusListeners.get(i)).click();
168
                                        }
169
                                }
170
                        }
171

    
172
                        public void mouseEntered(MouseEvent e) {
173
                        }
174

    
175
                        public void mouseExited(MouseEvent e) {
176
                        }
177

    
178
                        public void mousePressed(MouseEvent e) {
179
                        }
180

    
181
                        public void mouseReleased(MouseEvent e) {
182
                        }
183

    
184
                });
185
        column.setCellEditor(presenteditor);
186

    
187
        CellIconOptionRenderer renderer = new CellIconOptionRenderer(true);
188
        column.setCellRenderer(renderer);
189
    }
190

    
191
    /**
192
     * DOCUMENT ME!
193
     *
194
     * @param table DOCUMENT ME!
195
     * @param column DOCUMENT ME!
196
     */
197
    public void setUpActivateColumn(JTable table, TableColumn column) {
198
        ActivatedCellEditor activatededitor = new ActivatedCellEditor(eml,table,active,
199
                defuse);
200
        column.setCellEditor(activatededitor);
201

    
202
        CellIconOptionRenderer renderer = new CellIconOptionRenderer(true);
203
        column.setCellRenderer(renderer);
204
    }
205

    
206
    /**
207
     * DOCUMENT ME!
208
     *
209
     * @param table DOCUMENT ME!
210
     * @param column DOCUMENT ME!
211
     */
212
    public void setUpDisableColumn(JTable table, TableColumn column) {
213
        DisabledCellEditor disablededitor = new DisabledCellEditor(eml,table,notdisable,
214
                disable);
215
        column.setCellEditor(disablededitor);
216

    
217
        CellIconOptionRenderer renderer = new CellIconOptionRenderer(true);
218
        column.setCellRenderer(renderer);
219
    }
220

    
221
    /**
222
     * DOCUMENT ME!
223
     *
224
     * @param table DOCUMENT ME!
225
     * @param column DOCUMENT ME!
226
     */
227
    public void setUpBlockColumn(JTable table, TableColumn column) {
228
        BlockedCellEditor blockeditor = new BlockedCellEditor(eml,table,blocked,
229
                unblocked);
230
        column.setCellEditor(blockeditor);
231

    
232
        CellIconOptionRenderer renderer = new CellIconOptionRenderer(true);
233
        column.setCellRenderer(renderer);
234
    }
235

    
236
    /**
237
     * DOCUMENT ME!
238
     *
239
     * @param table DOCUMENT ME!
240
     * @param column DOCUMENT ME!
241
     */
242
    public void setUpFillColumn(JTable table, TableColumn column) {
243
        FilledCellEditor fillededitor = new FilledCellEditor(eml,table,fill,
244
                notfill);
245
        column.setCellEditor(fillededitor);
246

    
247
        CellIconOptionRenderer renderer = new CellIconOptionRenderer(true);
248
        column.setCellRenderer(renderer);
249
    }
250

    
251
    /**
252
     * Create the GUI and show it.  For thread safety, this method should be
253
     * invoked from the event-dispatching thread.
254
     */
255
    private static void createAndShowGUI() {
256
        //Create and set up the window.
257
        JFrame frame = new JFrame("TableRenderDemo");
258
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
259

    
260
        //Create and set up the content pane.
261
        TableLayers newContentPane = new TableLayers(null, null);
262
        newContentPane.setOpaque(true); //content panes must be opaque
263
        newContentPane.DEBUG = true;
264
        frame.setContentPane(newContentPane);
265

    
266
        //Display the window.
267
        frame.pack();
268
        frame.setVisible(true);
269
    }
270

    
271
    /**
272
     * DOCUMENT ME!
273
     *
274
     * @param args DOCUMENT ME!
275
     */
276
    public static void main(String[] args) {
277
        //Schedule a job for the event-dispatching thread:
278
        //creating and showing this application's GUI.
279
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
280
                public void run() {
281
                    createAndShowGUI();
282
                }
283
            });
284
    }
285

    
286
    /**
287
     * DOCUMENT ME!
288
     *
289
     * @param source DOCUMENT ME!
290
     */
291
    public void setSource(ReadableVectorial source) {
292
        this.source = source;
293
    }
294

    
295
    /**
296
     * DOCUMENT ME!
297
     *
298
     * @param legend DOCUMENT ME!
299
     */
300
    public void setLegend(IVectorialLegend legend) {
301
        this.eml = new EditionManagerLegend(legend);
302
    }
303

    
304
    class MyTableModel extends AbstractTableModel {
305
        private String[] columnNames = {
306
                PluginServices.getText(this, "estado"),
307
                PluginServices.getText(this, "nombre"),
308
                PluginServices.getText(this, "activar"),
309
                PluginServices.getText(this, "inutilizar"),
310
                PluginServices.getText(this, "bloquear"),
311
                PluginServices.getText(this, "relleno"),
312
                PluginServices.getText(this, "simbolo")
313
            };
314

    
315
        //        private Object[][] data = {
316
        //            {new Boolean(true), "Nombre1",new Boolean(true) ,new Boolean(false),new Boolean(false),
317
        //                    "s?mbolo"},
318
        //                    {new Boolean(false), "Nombre2",new Boolean(true),new Boolean(false),new Boolean(false),
319
        //                    "s?mbolo"},
320
        //                    {new Boolean(false), "Nombre3",new Boolean(true),new Boolean(false),new Boolean(false),
321
        //                    "s?mbolo"},
322
        //                    {new Boolean(false), "Nombre4",new Boolean(true),new Boolean(false),new Boolean(false),
323
        //                    "s?mbolo"},
324
        //                    {new Boolean(false), "Nombre5",new Boolean(true),new Boolean(false),new Boolean(false),
325
        //                    "s?mbolo"}
326
        //        };
327
        public final Object[] longValues = {
328
                new ImageIcon(), "Nombre1", new ImageIcon(), new ImageIcon(),
329
                new ImageIcon(), new ImageIcon(),
330
                SymbologyFactory.createDefaultSymbolByShapeType(FShape.MULTI)
331
            };
332

    
333
        public int getColumnCount() {
334
            return columnNames.length;
335
        }
336

    
337
        public int getRowCount() {
338
            return eml.getRowCount();
339
        }
340

    
341
        public String getColumnName(int col) {
342
            return columnNames[col];
343
        }
344

    
345
        public Object getValueAt(int row, int col) {
346
            switch (col) {
347
            case 0:
348

    
349
                if (eml.isPresent(row)) {
350
                    return selected;
351
                }
352

    
353
                return notselected;
354

    
355
            case 1:
356
                return eml.getValue(row);
357

    
358
            case 2:
359

    
360
                if (eml.isActived(row)) {
361
                    return active;
362
                }
363

    
364
                return defuse;
365

    
366
            case 3:
367

    
368
                if (eml.isDisable(row)) {
369
                    return notdisable;
370
                }
371

    
372
                return disable;
373

    
374
            case 4:
375

    
376
                if (eml.isBlocked(row)) {
377
                    return blocked;
378
                }
379

    
380
                return unblocked;
381

    
382
            case 5:
383

    
384
                if (eml.isFilled(row)) {
385
                    return fill;
386
                }
387

    
388
                return notfill;
389

    
390
            case 6:
391
                    return eml.getSymbol(row);
392

    
393
            default:
394
                return null;
395
            }
396
        }
397

    
398
        /*
399
         * JTable uses this method to determine the default renderer/
400
         * editor for each cell.  If we didn't implement this method,
401
         * then the last column would contain text ("true"/"false"),
402
         * rather than a check box.
403
         */
404
        public Class getColumnClass(int c) {
405
            return getValueAt(0, c).getClass();
406
        }
407

    
408
        /*
409
         * Don't need to implement this method unless your table's
410
         * editable.
411
         */
412
        public boolean isCellEditable(int row, int col) {
413
                return true;
414
        }
415

    
416
        /*
417
         * Don't need to implement this method unless your table's
418
         * data can change.
419
         */
420
        public void setValueAt(Object value, int row, int col) {
421
//            if (DEBUG) {
422
//                System.out.println("Setting value at " + row + "," + col +
423
//                    " to " + value + " (an instance of " + value.getClass() +
424
//                    ")");
425
//            }
426
//
427
//            ///data[row][col] = value;
428
//            ////////////////
429
//            switch (col) {
430
//            case 0:
431
//                    if (value.equals(selected)) {
432
//                    eml.setPresent(row,true);
433
//                }else {
434
//                        eml.setPresent(row,false);
435
//                }
436
//            case 1:
437
//                eml.setValue(row,value);
438
//            case 2:
439
//                    if (value.equals(active)) {
440
//                    eml.setActived(row,true);
441
//                }else {
442
//                        eml.setActived(row,false);
443
//                }
444
//            case 3:
445
//                    if (value.equals(disable)) {
446
//                    eml.setDisable(row,true);
447
//                }else {
448
//                        eml.setDisable(row,false);
449
//                }
450
//            case 4:
451
//                    if (value.equals(blocked)) {
452
//                    eml.setBlocked(row,true);
453
//                }else {
454
//                        eml.setBlocked(row,false);
455
//                }
456
//            case 5:
457
//                    if (value.equals(fill)) {
458
//                    eml.setFilled(row,true);
459
//                }else {
460
//                        eml.setFilled(row,false);
461
//                }
462
//            case 6:
463
//               eml.setSymbol(row,value);
464
//            }
465
//            /////////////
466
//            fireTableCellUpdated(row, col);
467
//
468
//            if (DEBUG) {
469
//                System.out.println("New value of data:");
470
//                printDebugData();
471
//            }
472
        }
473

    
474
        private void printDebugData() {
475
            int numRows = getRowCount();
476
            int numCols = getColumnCount();
477

    
478
            for (int i = 0; i < numRows; i++) {
479
                System.out.print("    row " + i + ":");
480

    
481
                for (int j = 0; j < numCols; j++) {
482
                    /// System.out.print("  " + data[i][j]);
483
                }
484

    
485
                System.out.println();
486
            }
487

    
488
            System.out.println("--------------------------");
489
        }
490
    }
491

    
492
        public String getPresentSubLayer() {
493
                return eml.getPresentSubLayer();
494
        }
495
        public void addStatusListener(StatusListener listener) {
496
                statusListeners.add(listener);
497
        }
498
}