⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 assigntablemodel.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
字号:
/**
 * 
 */
package flow.graph.gui.graph.cell.ui.table;

import java.util.Vector;

import javax.swing.table.AbstractTableModel;
public class AssignTableModel extends AbstractTableModel {
	//private static final long serialVersionUID = -7495940408592595397L;
    private static final String[] columnNames = new String[]{ "数据类型名称", "请求类型"};
    private static final Class[] column_class = { String.class, String.class};

	private Vector content = null;

	public AssignTableModel() {
		content = new Vector();
	}

	public AssignTableModel(int count) {
		content = new Vector(count);
	}

	public void addRow(String name, String type) {
		Vector v = new Vector(2);
		v.add(0, name);
		v.add(1, type);
		content.add(v);
	}

	public void removeRow(int row) {
		content.remove(row);
	}

	public void removeRows(int row, int count) {
		for (int i = 0; i < count; i++) {
			if (content.size() > row) {
				content.remove(row);
			}
		}
	}

	/**
	 * 让表格中某些值可修改,但需要setValueAt(Object value, int row, int col)方法配合才能使修改生效
	 */
	public boolean isCellEditable(int rowIndex, int columnIndex) {
		//if (columnIndex == 0) {
		//	return false;
		//}
		return false;
	}

	/**
	 * 使修改的内容生效
	 */
	public void setValueAt(Object value, int row, int col) {
		((Vector) content.get(row)).remove(col);
		((Vector) content.get(row)).add(col, value);
		this.fireTableCellUpdated(row, col);
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public int getColumnCount() {
		//return CondictionTable._InputHeader.length;
		return columnNames.length;
	}

	public int getRowCount() {
		return content.size();
	}

	public Object getValueAt(int row, int col) {
		return ((Vector) content.get(row)).get(col);
	}

	/**
	 * 返回数据类型
	 */
	public Class getColumnClass(int col) {
		//预先已经知道该列的类型,故可以引用静态变量返回
		return column_class[col];
		//return getValueAt(0, col).getClass();
	}
	
    protected Vector convertToVector(String name, String type) {
        Vector v = new Vector();
		v.addElement(name);
		v.addElement(type);
        return v;
    }

	public void insertRow(int row, String name, String type) {
        insertRow(row, convertToVector(name,type));
    }

    public void insertRow(int row, Vector rowData) {
		content.insertElementAt(rowData, row); 
		justifyRows(row, row+1); 
	    fireTableRowsInserted(row, row);
	}

    private void justifyRows(int from, int to) { 
		// Sometimes the DefaultTableModel is subclassed 
		// instead of the AbstractTableModel by mistake. 
		// Set the number of rows for the case when getRowCount 
		// is overridden. 
		content.setSize(getRowCount()); 

		for (int i = from; i < to; i++) { 
			if (content.elementAt(i) == null) { 
				content.setElementAt(new Vector(), i); 
			}
			((Vector)content.elementAt(i)).setSize(getColumnCount());
		}
	}

    public void moveRow(int start, int end, int to) { 
		int shift = to - start; 
		int first, last; 
		if (shift < 0) { 
		    first = to; 
		    last = end; 
		}
		else { 
		    first = start; 
		    last = to + end - start;  
		}
		rotate(content, first, last + 1, shift); 

		fireTableRowsUpdated(first, last);
	}

    private static int gcd(int i, int j) {
		return (j == 0) ? i : gcd(j, i%j); 
	}

	private void rotate(Vector v, int a, int b, int shift) {
		int size = b - a; 
		int r = size - shift;
		int g = gcd(size, r); 
		for(int i = 0; i < g; i++) {
		    int to = i; 
		    Object tmp = v.elementAt(a + to); 
		    for(int from = (to + r) % size; from != i; from = (to + r) % size) {
			v.setElementAt(v.elementAt(a + from), a + to); 
			to = from; 
		    }
		    v.setElementAt(tmp, a + to); 
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -