📄 tableutilities.java
字号:
package com.ibm.j2x.swing.util;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.swing.JTable;
import org.apache.commons.collections.map.LinkedMap;
import com.ibm.j2x.swing.table.BeanTableModel;
import com.ibm.j2x.swing.table.TableData;
import com.ibm.j2x.swing.table.TableModelColumnData;
import com.ibm.j2x.swing.table.TableModelData;
import com.ibm.j2x.util.XMLUtilities;
/**
* The TableUtilities class provides helper methods for implementing the TableModel-Free
* design.
* @author MAbernethy
*/
public class TableUtilities
{
private static final String MAPPING_NAME = "com/ibm/j2x/swing/table/resources/table_mapping.xml";
/**
* This method retrieves the column information for a specified table from the
* specified file name, which should be a well-formed XML file, and returns it
* for use in a BeanTableModel
* @param filename the XML file that contains the table information
* @param cls the class using the JTable. This may be null. It is used
* for identification purposes.
* @param name the name of the table. This may be null. It is used
* for identification purposes.
* @return the LinkedMap in the form of [<column name 1>, <column field 2>], [<column name 2>, <column field 2>]
*/
public static LinkedMap getColumnInfo(String filename, Class cls, String name)
{
LinkedMap map = new LinkedMap();
List l = ((TableData)XMLUtilities.decode(filename, MAPPING_NAME, TableData.class)).getTableModelData();
for (Iterator i=l.iterator(); i.hasNext();)
{
TableModelData d = (TableModelData)i.next();
if ( (cls == null || d.getClassName().equals(cls.getName())) &&
(name == null || d.getName().equals(name)))
{
for (Iterator j=d.getColumns().iterator(); j.hasNext();)
{
TableModelColumnData c = (TableModelColumnData)j.next();
map.put(c.getName(), c.getField());
}
return map;
}
}
return map;
}
/**
* Gets a BeanTableModel from the file filename. This retrieves the column info
* from the <code>getColumnInfo()</code> method and wraps a BeanTableModle around it.
* @param filename the XML file that contains the table information
* @param cls the class using the JTable. This may be null. It is used
* for identification purposes.
* @param name the name of the table. This may be null. It is used
* for identification purposes.
* @return a BeanTableModel with the column information already set
*/
public static BeanTableModel getTableModel(String filename, Class cls, String name)
{
LinkedMap map = getColumnInfo(filename, cls, name);
BeanTableModel m = new BeanTableModel();
m.setColumnInfo(map);
return m;
}
/**
* The convenience method to use with the TableModel-Free design. It will get
* the table information for the table specified from the specified XML file,
* and then using the specified JTable and Collection, set the BeanTableModel on
* the JTable and pass the Collection to the JTable.
* <p>Using this single method can ultimately replace all TableModels. It take care
* of setting the model for the JTable.
* @param filename the XML file that contains the table information
* @param cls the class using the JTable. This may be null. It is used
* for identification purposes.
* @param name the name of the table. This may be null. It is used
* for identification purposes.
* @param view the JTable to set the model on
* @param model the Collection that contains the data
*/
public static void setViewToModel(String filename, Class cls, String name, JTable view, Collection model)
{
BeanTableModel b = getTableModel(filename, cls, name);
b.setValues(model);
view.setModel(b);
}
/**
* The convenience method to use with the TableModel-Free design. It will get
* the table information for the table specified from the specified XML file,
* and then using the specified JTable and Collection, set the BeanTableModel on
* the JTable and pass the Collection to the JTable.
* <p>Using this single method can ultimately replace all TableModels. It take care
* of setting the model for the JTable.
* @param filename the XML file that contains the table information
* @param name the name of the table. This may be null. It is used
* for identification purposes.
* @param view the JTable to set the model on
* @param model the Collection that contains the data
*/
public static void setViewToModel(String filename, String name, JTable view, Collection model)
{
setViewToModel(filename, null, name, view, model);
}
/**
* The convenience method to use with the TableModel-Free design. It will get
* the table information for the table specified from the specified XML file,
* and then using the specified JTable and Collection, set the BeanTableModel on
* the JTable and pass the Collection to the JTable.
* <p>Using this single method can ultimately replace all TableModels. It take care
* of setting the model for the JTable.
* @param filename the XML file that contains the table information
* @param cls the class using the JTable. This may be null. It is used
* for identification purposes.
* @param view the JTable to set the model on
* @param model the Collection that contains the data
*/
public static void setViewToModel(String filename, Class cls, JTable view, Collection model)
{
setViewToModel(filename, cls, null, view, model);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -