Revision 2076 branches/gvSIG_03_SLD/applications/appgvSIG/src/com/iver/cit/gvsig/gui/thememanager/legendmanager/panels/FSymbolTable.java

View differences:

FSymbolTable.java
46 46
 */
47 47
package com.iver.cit.gvsig.gui.thememanager.legendmanager.panels;
48 48

  
49
/**
50
 * @author fjp
51
 *
52
 * To change the template for this generated type comment go to
53
 * Window>Preferences>Java>Code Generation>Code and Comments
54
 */
55

  
56
/*
57
 * TableRenderDemo.java is a 1.4 application that requires no other files.
58
 */
59

  
60 49
import java.awt.Component;
61 50
import java.awt.Dimension;
62 51
import java.awt.GridLayout;
......
69 58
import javax.swing.table.TableColumn;
70 59

  
71 60
import com.iver.andami.PluginServices;
72
import com.iver.andami.Utilities;
73 61
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
62
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FCellIntervalRenderer;
63
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FCellSymbolRenderer;
64
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FIntervalCellEditor;
65
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FLabelCellEditor;
66
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FSymbolCellEditor;
67
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FValueCellEditor;
74 68

  
75 69

  
76

  
70
/**
71
 * JPanel que contiene la tabla con los s?mbolos intervalos o valores y
72
 * etiquetado de estos valores.
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
77 76
public class FSymbolTable extends JPanel {
77
	private static final long serialVersionUID = 1L;
78 78

  
79
	private boolean DEBUG = true;
80
	
79
//	private boolean DEBUG = true;
80

  
81 81
	// private  MyTableModel m_TableModel;
82
	
83 82
	private JTable table;
84
	
85
	
86
	public void addRow(Object[] vector)
87
	{
83
	private String type;
84

  
85
	/**
86
	 * Crea un nuevo FSymbolTable.
87
	 *
88
	 * @param type tipo de valor si es intervalo: "intervals" y si es por valores: "values".
89
	 */
90
	public FSymbolTable(String type) {
91
		super(new GridLayout(1, 0));
92
		this.type = type;
93
		table = new JTable(new MyTableModel());
94
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
95

  
96
		//Create the scroll pane and add the table to it.
97
		JScrollPane scrollPane = new JScrollPane(table);
98

  
99
		//Set up column sizes.
100
		initColumnSizes(table);
101
		setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
102
		setUpValueColumn(table, table.getColumnModel().getColumn(1));
103
		setUpLabelColumn(table, table.getColumnModel().getColumn(2));
104
		//Add the scroll pane to this panel.
105
		add(scrollPane);
106
		table.setRowSelectionAllowed(false);
107
	}
108

  
109
	public void addRow(Object[] vector) {
88 110
		MyTableModel m = (MyTableModel) table.getModel();
89
		m.addRow(vector); 
111
		m.addRow(vector);
90 112
	}
91
	public void removeSelectedRows()
92
	{
113

  
114
	public void removeSelectedRows() {
93 115
		MyTableModel m = (MyTableModel) table.getModel();
94
		int [] selectedRows = table.getSelectedRows();
95
		for (int i=selectedRows.length-1; i >= 0; i--)
96
		{
116
		int[] selectedRows = table.getSelectedRows();
117

  
118
		for (int i = selectedRows.length - 1; i >= 0; i--) {
97 119
			m.removeRow(selectedRows[i]);
98 120
		}
99 121
	}
100
	
101
	public void fillTableFromSymbolList(FSymbol[] symbols, Object[] values, String[] descriptions)
102
	{	
103
		// Object clave;
104
		FSymbol theSymbol; 
105 122

  
106
		for (int i=0; i < symbols.length; i++)
107
		{ 
123
	/**
124
	 * Rellena la tabla con los s?mbolos valores y descripciones que se pasan como par?metro.
125
	 *
126
	 * @param symbols Array de s?mbolos
127
	 * @param values Array de valores.
128
	 * @param descriptions Array de descripciones.
129
	 */
130
	public void fillTableFromSymbolList(FSymbol[] symbols, Object[] values,
131
		String[] descriptions) {
132
		FSymbol theSymbol;
133

  
134
		for (int i = 0; i < symbols.length; i++) {
108 135
			theSymbol = symbols[i];
136

  
109 137
			Object[] theRow = new Object[3];
110 138
			theRow[0] = theSymbol;
111 139
			theRow[1] = values[i];
112 140
			theRow[2] = descriptions[i];
113 141
			addRow(theRow);
114 142
		}
115
		
116 143
	}
117
	
118
	
119
	public Object getFieldValue(int row, int col)
120
	{
144

  
145
	public Object getFieldValue(int row, int col) {
121 146
		MyTableModel m = (MyTableModel) table.getModel();
122
		return m.getValueAt(row, col); 
147

  
148
		return m.getValueAt(row, col);
123 149
	}
124
	public int getRowCount()
125
	{
150

  
151
	public int getRowCount() {
126 152
		MyTableModel m = (MyTableModel) table.getModel();
127
		return m.getRowCount(); 
153

  
154
		return m.getRowCount();
128 155
	}
129
	
130
	public void removeAllItems()
131
	{
156

  
157
	public void removeAllItems() {
132 158
		table.setModel(new MyTableModel());
133
		setUpSymbolColumn(table, table.getColumnModel().getColumn(0));		
159
		setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
160
		setUpValueColumn(table, table.getColumnModel().getColumn(1));
161
		setUpLabelColumn(table, table.getColumnModel().getColumn(2));
134 162
	}
135 163

  
136
	public FSymbolTable() {
137
		super(new GridLayout(1,0));
138
		
164
	/**
165
	 * Inicializa el cell editor de tipo descripci?n de la columna que se pasa como par?metro.
166
	 *
167
	 * @param table2 Tabla.
168
	 * @param column Columna.
169
	 */
170
	public void setUpLabelColumn(JTable table2, TableColumn column) {
171
		FLabelCellEditor labeleditor = new FLabelCellEditor();
172
		column.setCellEditor(labeleditor);
173
	}
139 174

  
140
		table = new JTable(new MyTableModel());
141
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
175
	/**
176
	 * Inicializa el cell editor de tipo valor de la columna que se pasa como par?metro.
177
	 *
178
	 * @param table2 Tabla.
179
	 * @param column Columna.
180
	 */
181
	public void setUpValueColumn(JTable table2, TableColumn column) {
182
		if (type.equals("intervals")) {
183
			FIntervalCellEditor intervaleditor = new FIntervalCellEditor();
184
			column.setCellEditor(intervaleditor);
142 185

  
143
		//Create the scroll pane and add the table to it.
144
		JScrollPane scrollPane = new JScrollPane(table);
145

  
146
		//Set up column sizes.
147
		initColumnSizes(table);
148
		
149
		setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
150

  
151
		//Add the scroll pane to this panel.
152
		add(scrollPane);
153
		
154
		table.setRowSelectionAllowed(true);
186
			FCellIntervalRenderer renderer = new FCellIntervalRenderer(true);
187
			column.setCellRenderer(renderer);
188
		} else {
189
			FValueCellEditor valueeditor = new FValueCellEditor();
190
			column.setCellEditor(valueeditor);
191
		}
155 192
	}
156 193

  
157 194
	/*
......
160 197
	 * contents, then you can just use column.sizeWidthToFit().
161 198
	 */
162 199
	private void initColumnSizes(JTable table) {
163
		MyTableModel model = (MyTableModel)table.getModel();
200
		MyTableModel model = (MyTableModel) table.getModel();
164 201
		TableColumn column = null;
165 202
		Component comp = null;
166 203
		int headerWidth = 0;
167 204
		int cellWidth = 0;
168
		TableCellRenderer headerRenderer =
169
			table.getTableHeader().getDefaultRenderer();
170

  
171
		/* for (int i = 0; i < table.getColumnCount(); i++) {
172
			column = table.getColumnModel().getColumn(i);
173

  
174
			comp = headerRenderer.getTableCellRendererComponent(
175
								 null, column.getHeaderValue(),
176
								 false, false, 0, 0);
177
			headerWidth = comp.getPreferredSize().width;
178

  
179
			comp = table.getDefaultRenderer(model.getColumnClass(i)).
180
							 getTableCellRendererComponent(
181
								 table, longValues[i],
182
								 false, false, 0, i);
183
			cellWidth = comp.getPreferredSize().width;
184

  
185
			if (DEBUG) {
186
				System.out.println("Initializing width of column "
187
								   + i + ". "
188
								   + "headerWidth = " + headerWidth
189
								   + "; cellWidth = " + cellWidth);
190
			}
191

  
192
			//XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
193
			column.setPreferredWidth(Math.max(headerWidth, cellWidth));
194
		} */
205
		TableCellRenderer headerRenderer = table.getTableHeader()
206
												.getDefaultRenderer();
195 207
	}
196 208

  
197
	public void setUpSymbolColumn(JTable table,
198
								 TableColumn m_FSymbolComlumn) {
209
	/**
210
	 * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como par?metro.
211
	 *
212
	 * @param table2 Tabla.
213
	 * @param column Columna.
214
	 */	
215
	public void setUpSymbolColumn(JTable table, TableColumn m_FSymbolComlumn) {
199 216
		//Set up the editor 
200
		/* JComboBox comboBox = new JComboBox();
201
		comboBox.addItem("Snowboarding");
202
		comboBox.addItem("Rowing");
203
		comboBox.addItem("Knitting");
204
		comboBox.addItem("Speed reading");
205
		comboBox.addItem("Pool");
206
		comboBox.addItem("None of the above");
207
		sportColumn.setCellEditor(new DefaultCellEditor(comboBox));
208

  
209
		//Set up tool tips 
210
		DefaultTableCellRenderer renderer =
211
				new DefaultTableCellRenderer();
212
		renderer.setToolTipText("Click for combo box"); */
213
		
214
		
215 217
		m_FSymbolComlumn.setMaxWidth(100);
216 218
		m_FSymbolComlumn.setWidth(60);
217 219
		m_FSymbolComlumn.setPreferredWidth(60);
218 220
		m_FSymbolComlumn.setMinWidth(50);
219
		
220
		FCellEditor editor = new FCellEditor();
221
		m_FSymbolComlumn.setCellEditor(editor);
222
		
223
		FCellRenderer renderer = new FCellRenderer(true);
221

  
222
		FSymbolCellEditor symboleditor = new FSymbolCellEditor();
223

  
224
		m_FSymbolComlumn.setCellEditor(symboleditor);
225

  
226
		FCellSymbolRenderer renderer = new FCellSymbolRenderer(true);
224 227
		m_FSymbolComlumn.setCellRenderer(renderer);
225 228
	}
226 229

  
227
	class MyTableModel extends DefaultTableModel { //  AbstractTableModel {
228
	
229
		private String[] columnNames = {PluginServices.getText(this,"Simbolo"),
230
				PluginServices.getText(this,"Valor"),
231
				PluginServices.getText(this,"Etiqueta") };
230
	/**
231
	 * Modelo que propio que se aplica a la tabla.
232
	 *
233
	 * @author Vicente Caballero Navarro
234
	 */
235
	class MyTableModel extends DefaultTableModel { /**
236
		 * 
237
		 */
238
		private static final long serialVersionUID = 1L;
239
	//  AbstractTableModel {
232 240

  
241
		private String[] columnNames = {
242
				PluginServices.getText(this, "Simbolo"),
243
				PluginServices.getText(this, "Valor"),
244
				PluginServices.getText(this, "Etiqueta")
245
			};
246

  
247
		/**
248
		 * DOCUMENT ME!
249
		 *
250
		 * @return DOCUMENT ME!
251
		 */
233 252
		public int getColumnCount() {
234 253
			return columnNames.length;
235 254
		}
236 255

  
256
		/**
257
		 * DOCUMENT ME!
258
		 *
259
		 * @param col DOCUMENT ME!
260
		 *
261
		 * @return DOCUMENT ME!
262
		 */
237 263
		public String getColumnName(int col) {
238 264
			return columnNames[col];
239 265
		}
......
256 282
			//Note that the data/cell address is constant,
257 283
			//no matter where the cell appears onscreen.
258 284
			//if (col > 0) {
259
				return true;
285
			return true;
286

  
260 287
			/* } else {
261
				return false;
262
			} */
288
			   return false;
289
			   } */
263 290
		}
264 291

  
265 292
		/*
......
267 294
		 * data can change.
268 295
		 */
269 296
		/* public void setValueAt(Object value, int row, int col) {
270
			if (DEBUG) {
271
				System.out.println("Setting value at " + row + "," + col
272
								   + " to " + value
273
								   + " (an instance of "
274
								   + value.getClass() + ")");
275
			}
276
			// FSymbol theSymbol;
277
			switch (col)
278
			{
279
				case 0: // Simbolo
280
					// m_FRenderer.setValueSymbol(row,(FSymbol) value);
281
					break;
282
				case 1: // Clave
283
					FInterval newInterval = FInterval.parseString((String) value);
284
					this.setValueAt(newInterval,row,col);
285
					break;
286
				case 2: // Etiqueta
287
					// theSymbol = m_FRenderer.getSymbolByID(row);
288
					// theSymbol.m_Descrip = (String) value;
289
					
290
			}
291
			// data[row][col] = value;
292
			fireTableCellUpdated(row, col); 
293

  
294
		}  */
295

  
297
		   if (DEBUG) {
298
		           System.out.println("Setting value at " + row + "," + col
299
		                                              + " to " + value
300
		                                              + " (an instance of "
301
		                                              + value.getClass() + ")");
302
		   }
303
		   // FSymbol theSymbol;
304
		   switch (col)
305
		   {
306
		           case 0: // Simbolo
307
		                   // m_FRenderer.setValueSymbol(row,(FSymbol) value);
308
		                   break;
309
		           case 1: // Clave
310
		                   FInterval newInterval = FInterval.parseString((String) value);
311
		                   this.setValueAt(newInterval,row,col);
312
		                   break;
313
		           case 2: // Etiqueta
314
		                   // theSymbol = m_FRenderer.getSymbolByID(row);
315
		                   // theSymbol.m_Descrip = (String) value;
316
		
317
		   }
318
		   // data[row][col] = value;
319
		   fireTableCellUpdated(row, col);
320
		   }  */
296 321
		/* private void printDebugData() {
297
			int numRows = getRowCount();
298
			int numCols = getColumnCount();
299

  
300
			for (int i=0; i < numRows; i++) {
301
				System.out.print("    row " + i + ":");
302
				for (int j=0; j < numCols; j++) {
303
					System.out.print("  " + data[i][j]);
304
				}
305
				System.out.println();
306
			}
307
			System.out.println("--------------------------");
308
		} */
322
		   int numRows = getRowCount();
323
		   int numCols = getColumnCount();
324
		   for (int i=0; i < numRows; i++) {
325
		           System.out.print("    row " + i + ":");
326
		           for (int j=0; j < numCols; j++) {
327
		                   System.out.print("  " + data[i][j]);
328
		           }
329
		           System.out.println();
330
		   }
331
		   System.out.println("--------------------------");
332
		   } */
309 333
	}
310

  
311 334
}

Also available in: Unified diff