Revision 2796 trunk/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
import java.awt.Dimension;
50
import java.awt.GridLayout;
49
import com.hardcode.gdbms.engine.values.NullValue;
51 50

  
52
import javax.swing.JPanel;
53
import javax.swing.JScrollPane;
54
import javax.swing.table.DefaultTableModel;
55
import javax.swing.table.TableColumn;
51
import com.iver.andami.PluginServices;
56 52

  
57
import com.hardcode.gdbms.engine.values.NullValue;
58
import com.iver.andami.PluginServices;
59 53
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
54
import com.iver.cit.gvsig.fmap.rendering.NullIntervalValue;
55
import com.iver.cit.gvsig.fmap.rendering.NullUniqueValue;
60 56
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.FCellSymbolRenderer;
61 57
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.IntervalCellEditor;
62 58
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.SymbolCellEditor;
63 59
import com.iver.cit.gvsig.gui.thememanager.legendmanager.panels.edition.ValueCellEditor;
60

  
64 61
import com.iver.utiles.swing.jtable.JTable;
65 62
import com.iver.utiles.swing.jtable.TextFieldCellEditor;
66 63

  
64
import java.awt.Dimension;
65
import java.awt.GridLayout;
67 66

  
67
import javax.swing.JPanel;
68
import javax.swing.JScrollPane;
69
import javax.swing.table.DefaultTableModel;
70
import javax.swing.table.TableColumn;
71

  
72

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

  
77
//	private boolean DEBUG = true;
82
    //	private boolean DEBUG = true;
83
    // private  MyTableModel m_TableModel;
84
    private JTable table;
85
    private String type;
78 86

  
79
	// private  MyTableModel m_TableModel;
80
	private JTable table;
81
	private String type;
87
    /**
88
     * Crea un nuevo FSymbolTable.
89
     *
90
     * @param type tipo de valor si es intervalo: "intervals" y si es por
91
     *        valores: "values".
92
     */
93
    public FSymbolTable(String type) {
94
        super(new GridLayout(1, 0));
95
        this.type = type;
96
        table = new JTable();
97
        table.setModel(new MyTableModel());
98
        table.setPreferredScrollableViewportSize(new Dimension(480, 110));
82 99

  
83
	/**
84
	 * Crea un nuevo FSymbolTable.
85
	 *
86
	 * @param type tipo de valor si es intervalo: "intervals" y si es por valores: "values".
87
	 */
88
	public FSymbolTable(String type) {
89
		super(new GridLayout(1, 0));
90
		this.type = type;
91
		table = new JTable();
92
		table.setModel(new MyTableModel());
93
		table.setPreferredScrollableViewportSize(new Dimension(480, 110));
100
        //Create the scroll pane and add the table to it.
101
        JScrollPane scrollPane = new JScrollPane(table);
94 102

  
95
		//Create the scroll pane and add the table to it.
96
		JScrollPane scrollPane = new JScrollPane(table);
103
        //Set up column sizes.
104
        //initColumnSizes(table);
105
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
106
        setUpValueColumn(table, table.getColumnModel().getColumn(1));
107
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
97 108

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

  
108
	public void addRow(Object[] vector) {
109
		MyTableModel m = (MyTableModel) table.getModel();
110
		m.addRow(vector);
111
	}
114
    /**
115
     * A?ade una fila al modelo.
116
     *
117
     * @param vector Fila en forma de vector de Object para a?adir al modelo.
118
     */
119
    public void addRow(Object[] vector) {
120
        MyTableModel m = (MyTableModel) table.getModel();
121
        m.addRow(vector);
122
    }
112 123

  
113
	public void removeSelectedRows() {
114
		if (table.getCellEditor()!=null)
115
		table.getCellEditor().cancelCellEditing();
116
		MyTableModel m = (MyTableModel) table.getModel();
117
		int[] selectedRows = table.getSelectedRows();
124
    /**
125
     * Elimina la fila que tiene como clave el objeto que se pasa como
126
     * par?metro.
127
     *
128
     * @param obj clave del objeto a eliminar.
129
     */
130
    public void removeRow(Object obj) {
131
        MyTableModel m = (MyTableModel) table.getModel();
118 132

  
119
		for (int i = selectedRows.length - 1; i >= 0; i--) {
120
			m.removeRow(selectedRows[i]);
121
		}
122
	}
133
        for (int i = 0; i < m.getRowCount(); i++) {
134
            if (m.getValueAt(i, 1) instanceof NullUniqueValue ||
135
                    m.getValueAt(i, 1) instanceof NullIntervalValue) {
136
                m.removeRow(i);
137
            }
138
        }
139
    }
123 140

  
124
	/**
125
	 * Rellena la tabla con los s?mbolos valores y descripciones que se pasan como par?metro.
126
	 *
127
	 * @param symbols Array de s?mbolos
128
	 * @param values Array de valores.
129
	 * @param descriptions Array de descripciones.
130
	 */
131
	public void fillTableFromSymbolList(FSymbol[] symbols, Object[] values,
132
		String[] descriptions) {
133
		FSymbol theSymbol;
141
    /**
142
     * Elimina las filas que est?n seleccionadas.
143
     */
144
    public void removeSelectedRows() {
145
        if (table.getCellEditor() != null) {
146
            table.getCellEditor().cancelCellEditing();
147
        }
134 148

  
135
		for (int i = 0; i < symbols.length; i++) {
136
			theSymbol = symbols[i];
137
			addTableRecord(theSymbol,values[i],descriptions[i]);
138
		}
139
	}
140
	public void addTableRecord(FSymbol symbol, Object value,
141
			String description) {
142
				Object[] theRow = new Object[3];
143
				theRow[0] = symbol;
144
				theRow[1] = value;
145
				theRow[2] = description;
146
				addRow(theRow);
147
	}
148
	public Object getFieldValue(int row, int col) {
149
		MyTableModel m = (MyTableModel) table.getModel();
149
        MyTableModel m = (MyTableModel) table.getModel();
150
        int[] selectedRows = table.getSelectedRows();
150 151

  
151
		return m.getValueAt(row, col);
152
	}
152
        for (int i = selectedRows.length - 1; i >= 0; i--) {
153
            m.removeRow(selectedRows[i]);
154
        }
155
    }
153 156

  
154
	public int getRowCount() {
155
		MyTableModel m = (MyTableModel) table.getModel();
157
    /**
158
     * Rellena la tabla con los s?mbolos valores y descripciones que se pasan
159
     * como par?metro.
160
     *
161
     * @param symbols Array de s?mbolos
162
     * @param values Array de valores.
163
     * @param descriptions Array de descripciones.
164
     */
165
    public void fillTableFromSymbolList(FSymbol[] symbols, Object[] values,
166
        String[] descriptions) {
167
        FSymbol theSymbol;
156 168

  
157
		return m.getRowCount();
158
	}
169
        for (int i = 0; i < symbols.length; i++) {
170
            theSymbol = symbols[i];
171
            addTableRecord(theSymbol, values[i], descriptions[i]);
172
        }
173
    }
159 174

  
160
	public void removeAllItems() {
161
		table.setModel(new MyTableModel());
162
		setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
163
		setUpValueColumn(table, table.getColumnModel().getColumn(1));
164
		setUpLabelColumn(table, table.getColumnModel().getColumn(2));
165
	}
175
    /**
176
     * A?ade una fila con los objetos que se pasan como par?metros.
177
     *
178
     * @param symbol s?mbolo de la fila.
179
     * @param value Valor de la fila.
180
     * @param description Descripci?n.
181
     */
182
    public void addTableRecord(FSymbol symbol, Object value, String description) {
183
        Object[] theRow = new Object[3];
184
        theRow[0] = symbol;
185
        theRow[1] = value;
186
        theRow[2] = description;
187
        addRow(theRow);
188
    }
166 189

  
167
	/**
168
	 * Inicializa el cell editor de tipo descripci?n de la columna que se pasa como par?metro.
169
	 *
170
	 * @param table2 Tabla.
171
	 * @param column Columna.
172
	 */
173
	public void setUpLabelColumn(JTable table2, TableColumn column) {
174
		TextFieldCellEditor labeleditor = new TextFieldCellEditor();
175
		column.setCellEditor(labeleditor);
176
	}
190
    /**
191
     * Devuelve el valor a partie del n?mero de fila y columna.
192
     *
193
     * @param row n?mero de fila.
194
     * @param col n?mero de columna.
195
     *
196
     * @return Objeto.
197
     */
198
    public Object getFieldValue(int row, int col) {
199
        MyTableModel m = (MyTableModel) table.getModel();
177 200

  
178
	/**
179
	 * Inicializa el cell editor de tipo valor de la columna que se pasa como par?metro.
180
	 *
181
	 * @param table2 Tabla.
182
	 * @param column Columna.
183
	 */
184
	public void setUpValueColumn(JTable table2, TableColumn column) {
185
		if (type.equals("intervals")) {
186
			//FIntervalCellEditor intervaleditor = new FIntervalCellEditor();
187
			IntervalCellEditor intervaleditor = new IntervalCellEditor();
188
			column.setCellEditor(intervaleditor);
201
        return m.getValueAt(row, col);
202
    }
189 203

  
190
			///FCellIntervalRenderer renderer = new FCellIntervalRenderer(true);
191
			///column.setCellRenderer(renderer);
192
		} else {
193
			///FValueCellEditor valueeditor = new FValueCellEditor();
194
			///TextFieldCellEditor valueeditor = new TextFieldCellEditor();
195
			ValueCellEditor valueeditor= new ValueCellEditor();
196
			column.setCellEditor(valueeditor);
197
		}
198
	}
204
    /**
205
     * Devuelve el n?mero total de filas que contiene el modelo.
206
     *
207
     * @return N?mero de filas.
208
     */
209
    public int getRowCount() {
210
        MyTableModel m = (MyTableModel) table.getModel();
199 211

  
200
	/*
201
	 * This method picks good column sizes.
202
	 * If all column heads are wider than the column's cells'
203
	 * contents, then you can just use column.sizeWidthToFit().
204
	 */
205
	//private void initColumnSizes(JTable table) {
206
		//MyTableModel model = (MyTableModel) table.getModel();
207
		//TableColumn column = null;
208
		//Component comp = null;
209
		//int headerWidth = 0;
210
		//int cellWidth = 0;
211
		//TableCellRenderer headerRenderer = table.getTableHeader()
212
		//										.getDefaultRenderer();
213
	//}
212
        return m.getRowCount();
213
    }
214 214

  
215
	/**
216
	 * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como par?metro.
217
	 *
218
	 * @param table2 Tabla.
219
	 * @param column Columna.
220
	 */	
221
	public void setUpSymbolColumn(JTable table, TableColumn m_FSymbolComlumn) {
222
		//Set up the editor 
223
		m_FSymbolComlumn.setMaxWidth(100);
224
		m_FSymbolComlumn.setWidth(60);
225
		m_FSymbolComlumn.setPreferredWidth(60);
226
		m_FSymbolComlumn.setMinWidth(50);
215
    /**
216
     * Elimina todas las filas del modelo.
217
     */
218
    public void removeAllItems() {
219
        table.setModel(new MyTableModel());
220
        setUpSymbolColumn(table, table.getColumnModel().getColumn(0));
221
        setUpValueColumn(table, table.getColumnModel().getColumn(1));
222
        setUpLabelColumn(table, table.getColumnModel().getColumn(2));
223
    }
227 224

  
228
		//FSymbolCellEditor symboleditor = new FSymbolCellEditor();
229
		SymbolCellEditor symboleditor = new SymbolCellEditor();
230
		m_FSymbolComlumn.setCellEditor(symboleditor);
225
    /**
226
     * Inicializa el cell editor de tipo descripci?n de la columna que se pasa
227
     * como par?metro.
228
     *
229
     * @param table2 Tabla.
230
     * @param column Columna.
231
     */
232
    public void setUpLabelColumn(JTable table2, TableColumn column) {
233
        TextFieldCellEditor labeleditor = new TextFieldCellEditor();
234
        column.setCellEditor(labeleditor);
235
    }
231 236

  
232
		FCellSymbolRenderer renderer = new FCellSymbolRenderer(true);
233
		m_FSymbolComlumn.setCellRenderer(renderer);
234
	}
237
    /**
238
     * Inicializa el cell editor de tipo valor de la columna que se pasa como
239
     * par?metro.
240
     *
241
     * @param table2 Tabla.
242
     * @param column Columna.
243
     */
244
    public void setUpValueColumn(JTable table2, TableColumn column) {
245
        if (type.equals("intervals")) {
246
            //FIntervalCellEditor intervaleditor = new FIntervalCellEditor();
247
            IntervalCellEditor intervaleditor = new IntervalCellEditor();
248
            column.setCellEditor(intervaleditor);
235 249

  
236
	/**
237
	 * Modelo que propio que se aplica a la tabla.
238
	 *
239
	 * @author Vicente Caballero Navarro
240
	 */
241
	class MyTableModel extends DefaultTableModel { /**
242
		 * 
243
		 */
244
		private static final long serialVersionUID = 1L;
245
	//  AbstractTableModel {
250
            ///FCellIntervalRenderer renderer = new FCellIntervalRenderer(true);
251
            ///column.setCellRenderer(renderer);
252
        } else {
253
            ///FValueCellEditor valueeditor = new FValueCellEditor();
254
            ///TextFieldCellEditor valueeditor = new TextFieldCellEditor();
255
            ValueCellEditor valueeditor = new ValueCellEditor();
256
            column.setCellEditor(valueeditor);
257
        }
258
    }
246 259

  
247
		private String[] columnNames = {
248
				PluginServices.getText(this, "Simbolo"),
249
				PluginServices.getText(this, "Valor"),
250
				PluginServices.getText(this, "Etiqueta")
251
			};
260
    /*
261
     * This method picks good column sizes.
262
     * If all column heads are wider than the column's cells'
263
     * contents, then you can just use column.sizeWidthToFit().
264
     */
252 265

  
253
		/**
254
		 * DOCUMENT ME!
255
		 *
256
		 * @return DOCUMENT ME!
257
		 */
258
		public int getColumnCount() {
259
			return columnNames.length;
260
		}
266
    //private void initColumnSizes(JTable table) {
267
    //MyTableModel model = (MyTableModel) table.getModel();
268
    //TableColumn column = null;
269
    //Component comp = null;
270
    //int headerWidth = 0;
271
    //int cellWidth = 0;
272
    //TableCellRenderer headerRenderer = table.getTableHeader()
273
    //										.getDefaultRenderer();
274
    //}
261 275

  
262
		/**
263
		 * DOCUMENT ME!
264
		 *
265
		 * @param col DOCUMENT ME!
266
		 *
267
		 * @return DOCUMENT ME!
268
		 */
269
		public String getColumnName(int col) {
270
			return columnNames[col];
271
		}
276
    /**
277
     * Inicializa el cell editor de tipo s?mbolo de la columna que se pasa como
278
     * par?metro.
279
     *
280
     * @param table2 Tabla.
281
     * @param column Columna.
282
     */
283
    public void setUpSymbolColumn(JTable table2, TableColumn column) {
284
        //Set up the editor 
285
        column.setMaxWidth(100);
286
        column.setWidth(60);
287
        column.setPreferredWidth(60);
288
        column.setMinWidth(50);
272 289

  
273
		/*
274
		 * JTable uses this method to determine the default renderer/
275
		 * editor for each cell.  If we didn't implement this method,
276
		 * then the last column would contain text ("true"/"false"),
277
		 * rather than a check box.
278
		 */
279
		public Class getColumnClass(int c) {
280
			if (getValueAt(0,c)==null)return NullValue.class;
281
			return getValueAt(0, c).getClass();
282
		}
290
        //FSymbolCellEditor symboleditor = new FSymbolCellEditor();
291
        SymbolCellEditor symboleditor = new SymbolCellEditor();
292
        column.setCellEditor(symboleditor);
283 293

  
284
		/*
285
		 * Don't need to implement this method unless your table's
286
		 * editable.
287
		 */
288
		public boolean isCellEditable(int row, int col) {
289
			//Note that the data/cell address is constant,
290
			//no matter where the cell appears onscreen.
291
			//if (col > 0) {
292
			return true;
294
        FCellSymbolRenderer renderer = new FCellSymbolRenderer(true);
295
        column.setCellRenderer(renderer);
296
    }
293 297

  
294
			/* } else {
295
			   return false;
296
			   } */
297
		}
298
    /**
299
     * Modelo que propio que se aplica a la tabla.
300
     *
301
     * @author Vicente Caballero Navarro
302
     */
303
    class MyTableModel extends DefaultTableModel { /**
304
         *
305
         */
298 306

  
299
		/*
300
		 * Don't need to implement this method unless your table's
301
		 * data can change.
302
		 */
303
		/* public void setValueAt(Object value, int row, int col) {
304
		   if (DEBUG) {
305
		           System.out.println("Setting value at " + row + "," + col
306
		                                              + " to " + value
307
		                                              + " (an instance of "
308
		                                              + value.getClass() + ")");
309
		   }
310
		   // FSymbol theSymbol;
311
		   switch (col)
312
		   {
313
		           case 0: // Simbolo
314
		                   // m_FRenderer.setValueSymbol(row,(FSymbol) value);
315
		                   break;
316
		           case 1: // Clave
317
		                   FInterval newInterval = FInterval.parseString((String) value);
318
		                   this.setValueAt(newInterval,row,col);
319
		                   break;
320
		           case 2: // Etiqueta
321
		                   // theSymbol = m_FRenderer.getSymbolByID(row);
322
		                   // theSymbol.m_Descrip = (String) value;
323
		
324
		   }
325
		   // data[row][col] = value;
326
		   fireTableCellUpdated(row, col);
327
		   }  */
328
		/* private void printDebugData() {
329
		   int numRows = getRowCount();
330
		   int numCols = getColumnCount();
331
		   for (int i=0; i < numRows; i++) {
332
		           System.out.print("    row " + i + ":");
333
		           for (int j=0; j < numCols; j++) {
334
		                   System.out.print("  " + data[i][j]);
335
		           }
336
		           System.out.println();
337
		   }
338
		   System.out.println("--------------------------");
339
		   } */
340
	}
307
        private static final long serialVersionUID = 1L;
308

  
309
        //  AbstractTableModel {
310
        private String[] columnNames = {
311
                PluginServices.getText(this, "Simbolo"),
312
                PluginServices.getText(this, "Valor"),
313
                PluginServices.getText(this, "Etiqueta")
314
            };
315

  
316
        /**
317
         * Devuelve el n?mero de columnas.
318
         *
319
         * @return N?mero de columnas.
320
         */
321
        public int getColumnCount() {
322
            return columnNames.length;
323
        }
324

  
325
        /**
326
         * Devuelve el String del valor de la columna.
327
         *
328
         * @param col N?mero de columna.
329
         *
330
         * @return Nombre de la columna.
331
         */
332
        public String getColumnName(int col) {
333
            return columnNames[col];
334
        }
335

  
336
        /*
337
         * JTable uses this method to determine the default renderer/
338
         * editor for each cell.  If we didn't implement this method,
339
         * then the last column would contain text ("true"/"false"),
340
         * rather than a check box.
341
         */
342
        public Class getColumnClass(int c) {
343
            if (getValueAt(0, c) == null) {
344
                return NullValue.class;
345
            }
346

  
347
            return getValueAt(0, c).getClass();
348
        }
349

  
350
        /*
351
         * Don't need to implement this method unless your table's
352
         * editable.
353
         */
354
        public boolean isCellEditable(int row, int col) {
355
            //Note that the data/cell address is constant,
356
            //no matter where the cell appears onscreen.
357
            //if (col > 0) {
358
            return true;
359

  
360
            /* } else {
361
               return false;
362
               } */
363
        }
364

  
365
        /*
366
         * Don't need to implement this method unless your table's
367
         * data can change.
368
         */
369
        /* public void setValueAt(Object value, int row, int col) {
370
           if (DEBUG) {
371
                   System.out.println("Setting value at " + row + "," + col
372
                                                      + " to " + value
373
                                                      + " (an instance of "
374
                                                      + value.getClass() + ")");
375
           }
376
           // FSymbol theSymbol;
377
           switch (col)
378
           {
379
                   case 0: // Simbolo
380
                           // m_FRenderer.setValueSymbol(row,(FSymbol) value);
381
                           break;
382
                   case 1: // Clave
383
                           FInterval newInterval = FInterval.parseString((String) value);
384
                           this.setValueAt(newInterval,row,col);
385
                           break;
386
                   case 2: // Etiqueta
387
                           // theSymbol = m_FRenderer.getSymbolByID(row);
388
                           // theSymbol.m_Descrip = (String) value;
389
        
390
           }
391
           // data[row][col] = value;
392
           fireTableCellUpdated(row, col);
393
           }  */
394
        /* private void printDebugData() {
395
           int numRows = getRowCount();
396
           int numCols = getColumnCount();
397
           for (int i=0; i < numRows; i++) {
398
                   System.out.print("    row " + i + ":");
399
                   for (int j=0; j < numCols; j++) {
400
                           System.out.print("  " + data[i][j]);
401
                   }
402
                   System.out.println();
403
           }
404
           System.out.println("--------------------------");
405
           } */
406
    }
341 407
}

Also available in: Unified diff