svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / Panels / FSymbolTable.java @ 312
History | View | Annotate | Download (7.14 KB)
1 |
/*
|
---|---|
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 |
package com.iver.cit.gvsig.gui.Panels; |
8 |
|
9 |
/**
|
10 |
* @author fjp
|
11 |
*
|
12 |
* To change the template for this generated type comment go to
|
13 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
14 |
*/
|
15 |
|
16 |
/*
|
17 |
* TableRenderDemo.java is a 1.4 application that requires no other files.
|
18 |
*/
|
19 |
|
20 |
import java.awt.Color; |
21 |
import java.awt.Component; |
22 |
import java.awt.Dimension; |
23 |
import java.awt.GridLayout; |
24 |
import java.util.Iterator; |
25 |
import java.util.Set; |
26 |
import java.util.TreeMap; |
27 |
|
28 |
import javax.swing.JPanel; |
29 |
import javax.swing.JScrollPane; |
30 |
import javax.swing.JTable; |
31 |
import javax.swing.ListSelectionModel; |
32 |
import javax.swing.table.DefaultTableModel; |
33 |
import javax.swing.table.TableCellRenderer; |
34 |
import javax.swing.table.TableColumn; |
35 |
|
36 |
import com.iver.cit.gvsig.fmap.FInterval; |
37 |
import com.iver.cit.gvsig.fmap.FSymbol; |
38 |
import com.iver.mdiApp.Utilities; |
39 |
|
40 |
|
41 |
|
42 |
public class FSymbolTable extends JPanel { |
43 |
|
44 |
private boolean DEBUG = true; |
45 |
|
46 |
// private MyTableModel m_TableModel;
|
47 |
|
48 |
private JTable table; |
49 |
|
50 |
|
51 |
public void addRow(Object[] vector) |
52 |
{ |
53 |
MyTableModel m = (MyTableModel) table.getModel(); |
54 |
m.addRow(vector); |
55 |
} |
56 |
public void removeSelectedRows() |
57 |
{ |
58 |
MyTableModel m = (MyTableModel) table.getModel(); |
59 |
int [] selectedRows = table.getSelectedRows(); |
60 |
for (int i=selectedRows.length-1; i >= 0; i--) |
61 |
{ |
62 |
m.removeRow(selectedRows[i]); |
63 |
} |
64 |
} |
65 |
|
66 |
public void fillTableFromSymbolList(TreeMap symList) |
67 |
{ |
68 |
Object clave;
|
69 |
FSymbol theSymbol; |
70 |
|
71 |
Set set = symList.keySet();
|
72 |
int contador = 0; |
73 |
for (Iterator i=set.iterator(); i.hasNext();) |
74 |
{ |
75 |
clave = i.next(); |
76 |
theSymbol = (FSymbol) symList.get(clave); |
77 |
Object[] theRow = new Object[3]; |
78 |
theRow[0] = theSymbol;
|
79 |
theRow[1] = clave;
|
80 |
theRow[2] = theSymbol.m_Descrip;
|
81 |
addRow(theRow); |
82 |
contador ++; |
83 |
} |
84 |
System.out.println("Num. Simbolos = " + symList.size() + " Contador = " + contador); |
85 |
|
86 |
} |
87 |
|
88 |
|
89 |
public Object getFieldValue(int row, int col) |
90 |
{ |
91 |
MyTableModel m = (MyTableModel) table.getModel(); |
92 |
return m.getValueAt(row, col);
|
93 |
} |
94 |
public int getRowCount() |
95 |
{ |
96 |
MyTableModel m = (MyTableModel) table.getModel(); |
97 |
return m.getRowCount();
|
98 |
} |
99 |
|
100 |
public void removeAllItems() |
101 |
{ |
102 |
table.setModel(new MyTableModel());
|
103 |
setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
|
104 |
} |
105 |
|
106 |
public FSymbolTable() {
|
107 |
super(new GridLayout(1,0)); |
108 |
|
109 |
|
110 |
table = new JTable(new MyTableModel()); |
111 |
table.setPreferredScrollableViewportSize(new Dimension(500, 70)); |
112 |
|
113 |
//Create the scroll pane and add the table to it.
|
114 |
JScrollPane scrollPane = new JScrollPane(table); |
115 |
|
116 |
//Set up column sizes.
|
117 |
initColumnSizes(table); |
118 |
|
119 |
setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
|
120 |
|
121 |
//Add the scroll pane to this panel.
|
122 |
add(scrollPane); |
123 |
|
124 |
table.setRowSelectionAllowed(true);
|
125 |
} |
126 |
|
127 |
/*
|
128 |
* This method picks good column sizes.
|
129 |
* If all column heads are wider than the column's cells'
|
130 |
* contents, then you can just use column.sizeWidthToFit().
|
131 |
*/
|
132 |
private void initColumnSizes(JTable table) { |
133 |
MyTableModel model = (MyTableModel)table.getModel(); |
134 |
TableColumn column = null; |
135 |
Component comp = null; |
136 |
int headerWidth = 0; |
137 |
int cellWidth = 0; |
138 |
TableCellRenderer headerRenderer =
|
139 |
table.getTableHeader().getDefaultRenderer(); |
140 |
|
141 |
/* for (int i = 0; i < table.getColumnCount(); i++) {
|
142 |
column = table.getColumnModel().getColumn(i);
|
143 |
|
144 |
comp = headerRenderer.getTableCellRendererComponent(
|
145 |
null, column.getHeaderValue(),
|
146 |
false, false, 0, 0);
|
147 |
headerWidth = comp.getPreferredSize().width;
|
148 |
|
149 |
comp = table.getDefaultRenderer(model.getColumnClass(i)).
|
150 |
getTableCellRendererComponent(
|
151 |
table, longValues[i],
|
152 |
false, false, 0, i);
|
153 |
cellWidth = comp.getPreferredSize().width;
|
154 |
|
155 |
if (DEBUG) {
|
156 |
System.out.println("Initializing width of column "
|
157 |
+ i + ". "
|
158 |
+ "headerWidth = " + headerWidth
|
159 |
+ "; cellWidth = " + cellWidth);
|
160 |
}
|
161 |
|
162 |
//XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
|
163 |
column.setPreferredWidth(Math.max(headerWidth, cellWidth));
|
164 |
} */
|
165 |
} |
166 |
|
167 |
public void setUpSymbolColumn(JTable table, |
168 |
TableColumn m_FSymbolComlumn) {
|
169 |
//Set up the editor
|
170 |
/* JComboBox comboBox = new JComboBox();
|
171 |
comboBox.addItem("Snowboarding");
|
172 |
comboBox.addItem("Rowing");
|
173 |
comboBox.addItem("Knitting");
|
174 |
comboBox.addItem("Speed reading");
|
175 |
comboBox.addItem("Pool");
|
176 |
comboBox.addItem("None of the above");
|
177 |
sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
|
178 |
|
179 |
//Set up tool tips
|
180 |
DefaultTableCellRenderer renderer =
|
181 |
new DefaultTableCellRenderer();
|
182 |
renderer.setToolTipText("Click for combo box"); */
|
183 |
|
184 |
|
185 |
m_FSymbolComlumn.setMaxWidth(100);
|
186 |
m_FSymbolComlumn.setWidth(60);
|
187 |
m_FSymbolComlumn.setPreferredWidth(60);
|
188 |
m_FSymbolComlumn.setMinWidth(50);
|
189 |
|
190 |
FCellEditor editor = new FCellEditor();
|
191 |
m_FSymbolComlumn.setCellEditor(editor); |
192 |
|
193 |
FCellRenderer renderer = new FCellRenderer(true); |
194 |
m_FSymbolComlumn.setCellRenderer(renderer); |
195 |
} |
196 |
|
197 |
class MyTableModel extends DefaultTableModel { // AbstractTableModel { |
198 |
|
199 |
private String[] columnNames = {Utilities.getMessage(this,"Simbolo"), |
200 |
Utilities.getMessage(this,"Valor"), |
201 |
Utilities.getMessage(this,"Etiqueta") }; |
202 |
|
203 |
public int getColumnCount() { |
204 |
return columnNames.length;
|
205 |
} |
206 |
|
207 |
public String getColumnName(int col) { |
208 |
return columnNames[col];
|
209 |
} |
210 |
|
211 |
/*
|
212 |
* JTable uses this method to determine the default renderer/
|
213 |
* editor for each cell. If we didn't implement this method,
|
214 |
* then the last column would contain text ("true"/"false"),
|
215 |
* rather than a check box.
|
216 |
*/
|
217 |
public Class getColumnClass(int c) { |
218 |
return getValueAt(0, c).getClass(); |
219 |
} |
220 |
|
221 |
/*
|
222 |
* Don't need to implement this method unless your table's
|
223 |
* editable.
|
224 |
*/
|
225 |
public boolean isCellEditable(int row, int col) { |
226 |
//Note that the data/cell address is constant,
|
227 |
//no matter where the cell appears onscreen.
|
228 |
//if (col > 0) {
|
229 |
return true; |
230 |
/* } else {
|
231 |
return false;
|
232 |
} */
|
233 |
} |
234 |
|
235 |
/*
|
236 |
* Don't need to implement this method unless your table's
|
237 |
* data can change.
|
238 |
*/
|
239 |
/* public void setValueAt(Object value, int row, int col) {
|
240 |
if (DEBUG) {
|
241 |
System.out.println("Setting value at " + row + "," + col
|
242 |
+ " to " + value
|
243 |
+ " (an instance of "
|
244 |
+ value.getClass() + ")");
|
245 |
}
|
246 |
// FSymbol theSymbol;
|
247 |
switch (col)
|
248 |
{
|
249 |
case 0: // Simbolo
|
250 |
// m_FRenderer.setValueSymbol(row,(FSymbol) value);
|
251 |
break;
|
252 |
case 1: // Clave
|
253 |
FInterval newInterval = FInterval.parseString((String) value);
|
254 |
this.setValueAt(newInterval,row,col);
|
255 |
break;
|
256 |
case 2: // Etiqueta
|
257 |
// theSymbol = m_FRenderer.getSymbolByID(row);
|
258 |
// theSymbol.m_Descrip = (String) value;
|
259 |
|
260 |
}
|
261 |
// data[row][col] = value;
|
262 |
fireTableCellUpdated(row, col);
|
263 |
|
264 |
} */
|
265 |
|
266 |
/* private void printDebugData() {
|
267 |
int numRows = getRowCount();
|
268 |
int numCols = getColumnCount();
|
269 |
|
270 |
for (int i=0; i < numRows; i++) {
|
271 |
System.out.print(" row " + i + ":");
|
272 |
for (int j=0; j < numCols; j++) {
|
273 |
System.out.print(" " + data[i][j]);
|
274 |
}
|
275 |
System.out.println();
|
276 |
}
|
277 |
System.out.println("--------------------------");
|
278 |
} */
|
279 |
} |
280 |
|
281 |
} |