Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / project / documents / view / legend / gui / tablelayers / TableLayers.java @ 10626

History | View | Annotate | Download (16.7 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.v02.FSymbol;
22
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
23
import com.iver.cit.gvsig.fmap.rendering.EditionManagerLegend;
24
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
25
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ActivatedCellEditor;
26
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.BlockedCellEditor;
27
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.CellIconOptionRenderer;
28
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.DisabledCellEditor;
29
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.FCellSymbolRenderer;
30
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.FilledCellEditor;
31
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.PresentCellEditor;
32
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.SymbolCellEditor;
33
import com.iver.cit.gvsig.project.documents.view.legend.edition.gui.ValueCellEditor;
34

    
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
331
        public int getColumnCount() {
332
            return columnNames.length;
333
        }
334

    
335
        public int getRowCount() {
336
            return eml.getRowCount();
337
        }
338

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

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

    
347
                if (eml.isPresent(row)) {
348
                    return selected;
349
                }
350

    
351
                return notselected;
352

    
353
            case 1:
354
                return eml.getValue(row);
355

    
356
            case 2:
357

    
358
                if (eml.isActived(row)) {
359
                    return active;
360
                }
361

    
362
                return defuse;
363

    
364
            case 3:
365

    
366
                if (eml.isDisable(row)) {
367
                    return notdisable;
368
                }
369

    
370
                return disable;
371

    
372
            case 4:
373

    
374
                if (eml.isBlocked(row)) {
375
                    return blocked;
376
                }
377

    
378
                return unblocked;
379

    
380
            case 5:
381

    
382
                if (eml.isFilled(row)) {
383
                    return fill;
384
                }
385

    
386
                return notfill;
387

    
388
            case 6:
389
                    return eml.getSymbol(row);
390

    
391
            default:
392
                return null;
393
            }
394
        }
395

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

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

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

    
472
        private void printDebugData() {
473
            int numRows = getRowCount();
474
            int numCols = getColumnCount();
475

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

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

    
483
                System.out.println();
484
            }
485

    
486
            System.out.println("--------------------------");
487
        }
488
    }
489

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