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