svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / project / documents / view / legend / gui / tablelayers / TableLayers.java @ 40557
History | View | Annotate | Download (16.8 KB)
1 | 40557 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright (C) 2007-2013 gvSIG Association.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 3
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | 40435 | jjdelcerro | package org.gvsig.editing.project.documents.view.legend.gui.tablelayers; |
25 | |||
26 | import java.awt.Component; |
||
27 | import java.awt.Dimension; |
||
28 | import java.awt.GridLayout; |
||
29 | import java.awt.event.MouseEvent; |
||
30 | import java.awt.event.MouseListener; |
||
31 | import java.util.ArrayList; |
||
32 | |||
33 | import javax.swing.ImageIcon; |
||
34 | import javax.swing.JFrame; |
||
35 | import javax.swing.JPanel; |
||
36 | import javax.swing.JScrollPane; |
||
37 | import javax.swing.JTable; |
||
38 | import javax.swing.table.AbstractTableModel; |
||
39 | import javax.swing.table.TableCellRenderer; |
||
40 | import javax.swing.table.TableColumn; |
||
41 | |||
42 | import org.gvsig.andami.PluginServices; |
||
43 | import org.gvsig.app.project.documents.gui.TableSymbolCellRenderer; |
||
44 | import org.gvsig.app.project.documents.view.legend.edition.gui.ValueCellEditor; |
||
45 | import org.gvsig.editing.fmap.rendering.EditionManagerLegend; |
||
46 | import org.gvsig.editing.project.documents.view.legend.edition.gui.ActivatedCellEditor; |
||
47 | import org.gvsig.editing.project.documents.view.legend.edition.gui.BlockedCellEditor; |
||
48 | import org.gvsig.editing.project.documents.view.legend.edition.gui.CellIconOptionRenderer; |
||
49 | import org.gvsig.editing.project.documents.view.legend.edition.gui.DisabledCellEditor; |
||
50 | import org.gvsig.editing.project.documents.view.legend.edition.gui.FilledCellEditor; |
||
51 | import org.gvsig.editing.project.documents.view.legend.edition.gui.PresentCellEditor; |
||
52 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
53 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
54 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
55 | import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend; |
||
56 | |||
57 | |||
58 | |||
59 | /**
|
||
60 | * DOCUMENT ME!
|
||
61 | *
|
||
62 | * @author Vicente Caballero Navarro
|
||
63 | */
|
||
64 | public class TableLayers extends JPanel { |
||
65 | private ImageIcon selected = PluginServices.getIconTheme().get("images-selected-icon"); |
||
66 | private ImageIcon notselected = PluginServices.getIconTheme().get("images-notselected-icon"); |
||
67 | private ImageIcon blocked = PluginServices.getIconTheme().get("images-blocked-icon"); |
||
68 | private ImageIcon unblocked = PluginServices.getIconTheme().get("images-unblocked-icon"); |
||
69 | private ImageIcon active = PluginServices.getIconTheme().get("images-active-icon"); |
||
70 | private ImageIcon defuse = PluginServices.getIconTheme().get("images-defuse-icon"); |
||
71 | private ImageIcon disable = PluginServices.getIconTheme().get("images-disabled-icon"); |
||
72 | private ImageIcon notdisable =PluginServices.getIconTheme().get("images-notdisabled-icon"); |
||
73 | private ImageIcon fill = PluginServices.getIconTheme().get("images-fill-icon"); |
||
74 | private ImageIcon notfill = PluginServices.getIconTheme().get("images-notfill-icon"); |
||
75 | private boolean DEBUG = false; |
||
76 | private FeatureStore store;
|
||
77 | private EditionManagerLegend eml;
|
||
78 | private JTable table; |
||
79 | private ArrayList statusListeners=new ArrayList(); |
||
80 | |||
81 | public TableLayers(FeatureStore store, IVectorLegend legend2) {
|
||
82 | super(new GridLayout(1, 0)); |
||
83 | this.store = store;
|
||
84 | this.eml = new EditionManagerLegend(legend2); |
||
85 | |||
86 | table = new JTable(new MyTableModel()); |
||
87 | table.setPreferredScrollableViewportSize(new Dimension(500, 70)); |
||
88 | table.setShowHorizontalLines(false);
|
||
89 | |||
90 | //Create the scroll pane and add the table to it.
|
||
91 | JScrollPane scrollPane = new JScrollPane(table); |
||
92 | |||
93 | //Set up column sizes.
|
||
94 | initColumnSizes(table); |
||
95 | |||
96 | setUpStatusColumn(table, table.getColumnModel().getColumn(0));
|
||
97 | setUpValueColumn(table, table.getColumnModel().getColumn(1));
|
||
98 | setUpActivateColumn(table, table.getColumnModel().getColumn(2));
|
||
99 | setUpDisableColumn(table, table.getColumnModel().getColumn(3));
|
||
100 | setUpBlockColumn(table, table.getColumnModel().getColumn(4));
|
||
101 | setUpFillColumn(table, table.getColumnModel().getColumn(5));
|
||
102 | setUpSymbolColumn(table, table.getColumnModel().getColumn(6));
|
||
103 | |||
104 | //Add the scroll pane to this panel.
|
||
105 | add(scrollPane); |
||
106 | } |
||
107 | |||
108 | /*
|
||
109 | * This method picks good column sizes.
|
||
110 | * If all column heads are wider than the column's cells'
|
||
111 | * contents, then you can just use column.sizeWidthToFit().
|
||
112 | */
|
||
113 | private void initColumnSizes(JTable table) { |
||
114 | MyTableModel model = (MyTableModel) table.getModel(); |
||
115 | TableColumn column = null; |
||
116 | Component comp = null; |
||
117 | int headerWidth = 0; |
||
118 | int cellWidth = 0; |
||
119 | Object[] longValues = model.longValues; |
||
120 | TableCellRenderer headerRenderer = table.getTableHeader()
|
||
121 | .getDefaultRenderer(); |
||
122 | |||
123 | for (int i = 0; i < 7; i++) { |
||
124 | column = table.getColumnModel().getColumn(i); |
||
125 | |||
126 | comp = headerRenderer.getTableCellRendererComponent(null,
|
||
127 | column.getHeaderValue(), false, false, 0, 0); |
||
128 | headerWidth = comp.getPreferredSize().width; |
||
129 | |||
130 | comp = table.getDefaultRenderer(model.getColumnClass(i)) |
||
131 | .getTableCellRendererComponent(table, longValues[i], |
||
132 | false, false, 0, i); |
||
133 | cellWidth = comp.getPreferredSize().width; |
||
134 | |||
135 | if (DEBUG) {
|
||
136 | System.out.println("Initializing width of column " + i + ". " + |
||
137 | "headerWidth = " + headerWidth + "; cellWidth = " + |
||
138 | cellWidth); |
||
139 | } |
||
140 | |||
141 | ///column.setPreferredWidth(Math.max(headerWidth, cellWidth));
|
||
142 | column.setPreferredWidth((headerWidth + cellWidth) / 2);
|
||
143 | } |
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * DOCUMENT ME!
|
||
148 | *
|
||
149 | * @param table DOCUMENT ME!
|
||
150 | * @param column DOCUMENT ME!
|
||
151 | */
|
||
152 | public void setUpSymbolColumn(JTable table, TableColumn column) { |
||
153 | ///SymbolCellEditor symboleditor = new SymbolCellEditor();
|
||
154 | ///column.setCellEditor(symboleditor);
|
||
155 | |||
156 | TableSymbolCellRenderer renderer = new TableSymbolCellRenderer(true); |
||
157 | column.setCellRenderer(renderer); |
||
158 | } |
||
159 | public void setUpValueColumn(JTable table, TableColumn column) { |
||
160 | ValueCellEditor valueEditor = new ValueCellEditor();
|
||
161 | column.setCellEditor(valueEditor); |
||
162 | } |
||
163 | /**
|
||
164 | * DOCUMENT ME!
|
||
165 | *
|
||
166 | * @param table DOCUMENT ME!
|
||
167 | * @param column DOCUMENT ME!
|
||
168 | */
|
||
169 | public void setUpStatusColumn(JTable table, TableColumn column) { |
||
170 | |||
171 | PresentCellEditor presenteditor = new PresentCellEditor(eml,table,selected,
|
||
172 | notselected); |
||
173 | presenteditor.addMouseListener(new MouseListener(){ |
||
174 | |||
175 | public void mouseClicked(MouseEvent e) { |
||
176 | if (e.getClickCount()==2){ |
||
177 | for (int i=0;i<statusListeners.size();i++) { |
||
178 | ((StatusListener)statusListeners.get(i)).click(); |
||
179 | } |
||
180 | } |
||
181 | } |
||
182 | |||
183 | public void mouseEntered(MouseEvent e) { |
||
184 | } |
||
185 | |||
186 | public void mouseExited(MouseEvent e) { |
||
187 | } |
||
188 | |||
189 | public void mousePressed(MouseEvent e) { |
||
190 | } |
||
191 | |||
192 | public void mouseReleased(MouseEvent e) { |
||
193 | } |
||
194 | |||
195 | }); |
||
196 | column.setCellEditor(presenteditor); |
||
197 | |||
198 | CellIconOptionRenderer renderer = new CellIconOptionRenderer(true); |
||
199 | column.setCellRenderer(renderer); |
||
200 | } |
||
201 | |||
202 | /**
|
||
203 | * DOCUMENT ME!
|
||
204 | *
|
||
205 | * @param table DOCUMENT ME!
|
||
206 | * @param column DOCUMENT ME!
|
||
207 | */
|
||
208 | public void setUpActivateColumn(JTable table, TableColumn column) { |
||
209 | ActivatedCellEditor activatededitor = new ActivatedCellEditor(eml,table,active,
|
||
210 | defuse); |
||
211 | column.setCellEditor(activatededitor); |
||
212 | |||
213 | CellIconOptionRenderer renderer = new CellIconOptionRenderer(true); |
||
214 | column.setCellRenderer(renderer); |
||
215 | } |
||
216 | |||
217 | /**
|
||
218 | * DOCUMENT ME!
|
||
219 | *
|
||
220 | * @param table DOCUMENT ME!
|
||
221 | * @param column DOCUMENT ME!
|
||
222 | */
|
||
223 | public void setUpDisableColumn(JTable table, TableColumn column) { |
||
224 | DisabledCellEditor disablededitor = new DisabledCellEditor(eml,table,notdisable,
|
||
225 | disable); |
||
226 | column.setCellEditor(disablededitor); |
||
227 | |||
228 | CellIconOptionRenderer renderer = new CellIconOptionRenderer(true); |
||
229 | column.setCellRenderer(renderer); |
||
230 | } |
||
231 | |||
232 | /**
|
||
233 | * DOCUMENT ME!
|
||
234 | *
|
||
235 | * @param table DOCUMENT ME!
|
||
236 | * @param column DOCUMENT ME!
|
||
237 | */
|
||
238 | public void setUpBlockColumn(JTable table, TableColumn column) { |
||
239 | BlockedCellEditor blockeditor = new BlockedCellEditor(eml,table,blocked,
|
||
240 | unblocked); |
||
241 | column.setCellEditor(blockeditor); |
||
242 | |||
243 | CellIconOptionRenderer renderer = new CellIconOptionRenderer(true); |
||
244 | column.setCellRenderer(renderer); |
||
245 | } |
||
246 | |||
247 | /**
|
||
248 | * DOCUMENT ME!
|
||
249 | *
|
||
250 | * @param table DOCUMENT ME!
|
||
251 | * @param column DOCUMENT ME!
|
||
252 | */
|
||
253 | public void setUpFillColumn(JTable table, TableColumn column) { |
||
254 | FilledCellEditor fillededitor = new FilledCellEditor(eml,table,fill,
|
||
255 | notfill); |
||
256 | column.setCellEditor(fillededitor); |
||
257 | |||
258 | CellIconOptionRenderer renderer = new CellIconOptionRenderer(true); |
||
259 | column.setCellRenderer(renderer); |
||
260 | } |
||
261 | |||
262 | /**
|
||
263 | * Create the GUI and show it. For thread safety, this method should be
|
||
264 | * invoked from the event-dispatching thread.
|
||
265 | */
|
||
266 | private static void createAndShowGUI() { |
||
267 | //Create and set up the window.
|
||
268 | JFrame frame = new JFrame("TableRenderDemo"); |
||
269 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
270 | |||
271 | //Create and set up the content pane.
|
||
272 | TableLayers newContentPane = new TableLayers(null, null); |
||
273 | newContentPane.setOpaque(true); //content panes must be opaque |
||
274 | newContentPane.DEBUG = true;
|
||
275 | frame.setContentPane(newContentPane); |
||
276 | |||
277 | //Display the window.
|
||
278 | frame.pack(); |
||
279 | frame.setVisible(true);
|
||
280 | } |
||
281 | |||
282 | /**
|
||
283 | * DOCUMENT ME!
|
||
284 | *
|
||
285 | * @param args DOCUMENT ME!
|
||
286 | */
|
||
287 | public static void main(String[] args) { |
||
288 | //Schedule a job for the event-dispatching thread:
|
||
289 | //creating and showing this application's GUI.
|
||
290 | javax.swing.SwingUtilities.invokeLater(new Runnable() { |
||
291 | public void run() { |
||
292 | createAndShowGUI(); |
||
293 | } |
||
294 | }); |
||
295 | } |
||
296 | |||
297 | /**
|
||
298 | * DOCUMENT ME!
|
||
299 | *
|
||
300 | * @param source DOCUMENT ME!
|
||
301 | */
|
||
302 | public void setStore(FeatureStore store) { |
||
303 | this.store = store;
|
||
304 | } |
||
305 | |||
306 | /**
|
||
307 | * DOCUMENT ME!
|
||
308 | *
|
||
309 | * @param legend DOCUMENT ME!
|
||
310 | */
|
||
311 | public void setLegend(IVectorLegend legend) { |
||
312 | this.eml = new EditionManagerLegend(legend); |
||
313 | } |
||
314 | |||
315 | class MyTableModel extends AbstractTableModel { |
||
316 | private String[] columnNames = { |
||
317 | PluginServices.getText(this, "estado"), |
||
318 | PluginServices.getText(this, "nombre"), |
||
319 | PluginServices.getText(this, "activar"), |
||
320 | PluginServices.getText(this, "inutilizar"), |
||
321 | PluginServices.getText(this, "bloquear"), |
||
322 | PluginServices.getText(this, "relleno"), |
||
323 | PluginServices.getText(this, "simbolo") |
||
324 | }; |
||
325 | |||
326 | // private Object[][] data = {
|
||
327 | // {new Boolean(true), "Nombre1",new Boolean(true) ,new Boolean(false),new Boolean(false),
|
||
328 | // "s?mbolo"},
|
||
329 | // {new Boolean(false), "Nombre2",new Boolean(true),new Boolean(false),new Boolean(false),
|
||
330 | // "s?mbolo"},
|
||
331 | // {new Boolean(false), "Nombre3",new Boolean(true),new Boolean(false),new Boolean(false),
|
||
332 | // "s?mbolo"},
|
||
333 | // {new Boolean(false), "Nombre4",new Boolean(true),new Boolean(false),new Boolean(false),
|
||
334 | // "s?mbolo"},
|
||
335 | // {new Boolean(false), "Nombre5",new Boolean(true),new Boolean(false),new Boolean(false),
|
||
336 | // "s?mbolo"}
|
||
337 | // };
|
||
338 | public final Object[] longValues = { |
||
339 | new ImageIcon(), "Nombre1", new ImageIcon(), new ImageIcon(), |
||
340 | new ImageIcon(), new ImageIcon(), |
||
341 | MapContextLocator.getSymbolManager().createSymbol( |
||
342 | TYPES.AGGREGATE) |
||
343 | }; |
||
344 | |||
345 | public int getColumnCount() { |
||
346 | return columnNames.length;
|
||
347 | } |
||
348 | |||
349 | public int getRowCount() { |
||
350 | return eml.getRowCount();
|
||
351 | } |
||
352 | |||
353 | public String getColumnName(int col) { |
||
354 | return columnNames[col];
|
||
355 | } |
||
356 | |||
357 | public Object getValueAt(int row, int col) { |
||
358 | switch (col) {
|
||
359 | case 0: |
||
360 | |||
361 | if (eml.isPresent(row)) {
|
||
362 | return selected;
|
||
363 | } |
||
364 | |||
365 | return notselected;
|
||
366 | |||
367 | case 1: |
||
368 | return eml.getValue(row);
|
||
369 | |||
370 | case 2: |
||
371 | |||
372 | if (eml.isActived(row)) {
|
||
373 | return active;
|
||
374 | } |
||
375 | |||
376 | return defuse;
|
||
377 | |||
378 | case 3: |
||
379 | |||
380 | if (eml.isDisable(row)) {
|
||
381 | return notdisable;
|
||
382 | } |
||
383 | |||
384 | return disable;
|
||
385 | |||
386 | case 4: |
||
387 | |||
388 | if (eml.isBlocked(row)) {
|
||
389 | return blocked;
|
||
390 | } |
||
391 | |||
392 | return unblocked;
|
||
393 | |||
394 | case 5: |
||
395 | |||
396 | if (eml.isFilled(row)) {
|
||
397 | return fill;
|
||
398 | } |
||
399 | |||
400 | return notfill;
|
||
401 | |||
402 | case 6: |
||
403 | return eml.getSymbol(row);
|
||
404 | |||
405 | default:
|
||
406 | return null; |
||
407 | } |
||
408 | } |
||
409 | |||
410 | /*
|
||
411 | * JTable uses this method to determine the default renderer/
|
||
412 | * editor for each cell. If we didn't implement this method,
|
||
413 | * then the last column would contain text ("true"/"false"),
|
||
414 | * rather than a check box.
|
||
415 | */
|
||
416 | public Class getColumnClass(int c) { |
||
417 | return getValueAt(0, c).getClass(); |
||
418 | } |
||
419 | |||
420 | /*
|
||
421 | * Don't need to implement this method unless your table's
|
||
422 | * editable.
|
||
423 | */
|
||
424 | public boolean isCellEditable(int row, int col) { |
||
425 | return true; |
||
426 | } |
||
427 | |||
428 | /*
|
||
429 | * Don't need to implement this method unless your table's
|
||
430 | * data can change.
|
||
431 | */
|
||
432 | public void setValueAt(Object value, int row, int col) { |
||
433 | // if (DEBUG) {
|
||
434 | // System.out.println("Setting value at " + row + "," + col +
|
||
435 | // " to " + value + " (an instance of " + value.getClass() +
|
||
436 | // ")");
|
||
437 | // }
|
||
438 | //
|
||
439 | // ///data[row][col] = value;
|
||
440 | // ////////////////
|
||
441 | // switch (col) {
|
||
442 | // case 0:
|
||
443 | // if (value.equals(selected)) {
|
||
444 | // eml.setPresent(row,true);
|
||
445 | // }else {
|
||
446 | // eml.setPresent(row,false);
|
||
447 | // }
|
||
448 | // case 1:
|
||
449 | // eml.setValue(row,value);
|
||
450 | // case 2:
|
||
451 | // if (value.equals(active)) {
|
||
452 | // eml.setActived(row,true);
|
||
453 | // }else {
|
||
454 | // eml.setActived(row,false);
|
||
455 | // }
|
||
456 | // case 3:
|
||
457 | // if (value.equals(disable)) {
|
||
458 | // eml.setDisable(row,true);
|
||
459 | // }else {
|
||
460 | // eml.setDisable(row,false);
|
||
461 | // }
|
||
462 | // case 4:
|
||
463 | // if (value.equals(blocked)) {
|
||
464 | // eml.setBlocked(row,true);
|
||
465 | // }else {
|
||
466 | // eml.setBlocked(row,false);
|
||
467 | // }
|
||
468 | // case 5:
|
||
469 | // if (value.equals(fill)) {
|
||
470 | // eml.setFilled(row,true);
|
||
471 | // }else {
|
||
472 | // eml.setFilled(row,false);
|
||
473 | // }
|
||
474 | // case 6:
|
||
475 | // eml.setSymbol(row,value);
|
||
476 | // }
|
||
477 | // /////////////
|
||
478 | // fireTableCellUpdated(row, col);
|
||
479 | //
|
||
480 | // if (DEBUG) {
|
||
481 | // System.out.println("New value of data:");
|
||
482 | // printDebugData();
|
||
483 | // }
|
||
484 | } |
||
485 | |||
486 | private void printDebugData() { |
||
487 | int numRows = getRowCount();
|
||
488 | int numCols = getColumnCount();
|
||
489 | |||
490 | for (int i = 0; i < numRows; i++) { |
||
491 | System.out.print(" row " + i + ":"); |
||
492 | |||
493 | for (int j = 0; j < numCols; j++) { |
||
494 | /// System.out.print(" " + data[i][j]);
|
||
495 | } |
||
496 | |||
497 | System.out.println();
|
||
498 | } |
||
499 | |||
500 | System.out.println("--------------------------"); |
||
501 | } |
||
502 | } |
||
503 | |||
504 | public String getPresentSubLayer() { |
||
505 | return eml.getPresentSubLayer();
|
||
506 | } |
||
507 | public void addStatusListener(StatusListener listener) { |
||
508 | statusListeners.add(listener); |
||
509 | } |
||
510 | } |