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

📄 mytablemodel.java

📁 这是一个在linux环境下
💻 JAVA
字号:
package fr.umlv.projet.fenetre.table;
import java.awt.Component;
import java.util.ArrayList;
import java.util.List;

import javax.swing.Icon;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import fr.umlv.projet.procfs.Processuses;
import fr.umlv.projet.procfs.Processus;
import fr.umlv.projet.utils.UpDownArrow;

/**
 * To customize a model of table
 * @author Ma & Huang
 * @see fr.umlv.projet.fenetre.TabbedPane
 */
@SuppressWarnings("serial")
public class MyTableModel extends AbstractTableModel implements TableModelListener{
	
	private List<String> columnName = new ArrayList<String>();
	private Object[][] data = null;
	private List<Integer> indexes = new ArrayList<Integer>();
	private List<Integer> listPid = new ArrayList<Integer>();
	
	final Icon upIcon = new UpDownArrow(0);
	final Icon downIcon = new UpDownArrow(1);
	private String sortColumnName = "Pid";
	private boolean ascending = true;
	
	/**
	 * Construtor a model of table
	 * @param name the names of column
	 */
	public MyTableModel(String[] name){
		for(String s:name)
			columnName.add(s);
		allocate();
	}
	
	/**
	 * Construtor a model of table
	 * @param list a list that contain the names of column
	 */
	public MyTableModel(List<String> list){
		this.columnName = list;
	}
	
	@SuppressWarnings("unchecked")
	public Class getColumnClass(int column){
		String str = columnName.get(column);
		if(		str.equals("SleepAVG%")||str.equals("Tgid")||str.equals("Pid")||str.equals("PPid")||
				str.equals("TracerPid")||str.equals("Uid")||str.equals("Gid")||str.equals("FDSize")||
				str.equals("VmSize(KB)")||str.equals("VmLck(KB)")||str.equals("VmRSS(KB)")||
				str.equals("VmData(KB)")||str.equals("VmStk(KB)")||str.equals("VmExe(KB)")||
				str.equals("VmLib(KB)")||str.equals("VmPTE(KB)")||str.equals("Threads"))
			return Integer.class;
		else if(str.equals("CPU%")||str.equals("MEM%"))
			return Float.class;
		return String.class;
	}
	
	/**
	 * set the data of the table
	 * @param data the data of the table
	 */
	public void setData(Object[][] data){
		this.data = data;
	}
	
	
	public int getRowCount() {
		if(data==null)
			return 0;
		return Processuses.getProcessNumber();
	}
	
	public int getColumnCount() {
		return columnName.size();
	}
	
	
	public Object getValueAt(int row, int column) {
		return data[column][indexes.get(row)];
	}
	
	/**
	 * Returns the value in the data of the table
	 * @param row the number of the row
	 * @param column the number of the column
	 * @return the value in the data of the table
	 */
	public Object valueAt(int row, int column) {
		return data[column][row];
	}
	
	public void setValueAt(Object aValue, int row, int column) {
		this.setValueAt(aValue, indexes.get(row), column);
	}
	
	@Override
	public String getColumnName(int column)
	{
		return columnName.get(column);
	}
	
	/**
	 * Set the names of the columns
	 * @param name the names of the columns 
	 */
	public void setColumnName(String[] name){
		for(String s:name)
			columnName.add(s);
	}
	
	/**
	 * Set the names of the columns
	 * @param a list that contain the names of column 
	 */
	public void setColumnName(List<String> list){
		this.columnName = list;
	}
	
	/**
	 * Returns the list of the names of the columns of the table
	 * @return the list of the names of the columns of the table
	 */
	public List<String> getColumnName(){
		return columnName;
	}
	
	public void tableChanged(TableModelEvent e) {
		indexesChange(e);
	}

	/**
	 * the action of the indexes change if the data of the processus changed
	 * @param e the TableModelEvent
	 */
	public void indexesChange(TableModelEvent e){
		for(int i = 0;i<listPid.size();i++){
			if(!Processuses.isExist(listPid.get(i))){
				listPid.remove(i);
				int index = indexes.indexOf(i);
				indexes.remove(index);
				
				for(int p=0;p<indexes.size();p++){
					if(indexes.get(p)>i){
						int value = indexes.get(p);
						indexes.set(p,--value);
					}
				}
			i--;
			} 
		}
		List<Processus> listProcessus = Processuses.getProcessusList();
		for(int i=0;i<listProcessus.size();i++){
			if(!listPid.contains(listProcessus.get(i).getPid())){
				indexes.add(i);
				listPid.add(listProcessus.get(i).getPid());
			}
		}
	}
	
	/**
	 * To sort the data of the type of Stirng from small to big
	 * @param column the column that need sort
	 */
	public void sortStringUp(int column) {
		int rowCount = getRowCount();
		for(int i=0; i < rowCount; i++) {
			for(int j = i+1; j < rowCount; j++) {
//				System.out.println("i "+i+"  "+"j "+j);
				if(compareString(indexes.get(i), indexes.get(j), column) < 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * To sort the data of the type of Stirng from big to small
	 * @param column the column that need sort
	 */
	public void sortStringDown(int column) {
		int rowCount = getRowCount();
		for(int i=0; i < rowCount; i++) {
			for(int j = i+1; j < rowCount; j++) {
//				System.out.println("i "+i+"  "+"j "+j);
				if(compareString(indexes.get(i), indexes.get(j), column) > 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * To echange the value of two object
	 * @param i the index of the first object
	 * @param j the index of the second object
	 */
	private void swap(int i, int j) {
		int tmp = indexes.get(i);
		indexes.set(i,indexes.get(j));
		indexes.set(j,tmp);
	}
	
	/**
	 * Returns  the length of the longer string
	 * @param a the first string of the compare
	 * @param b the second string of the compare
	 * @return the length of the longer string
	 */
	private int plusCourt(String a,String b){
		if(a.length() >= b.length())
			return b.length();
		else
			 return a.length();
	}
	
	/**
	 * Returns the value is positive if the second value is bigger than the first
	 * @param i the index of the row
	 * @param j the index of the row
	 * @param column the index of column that need sort 
	 * @return the value is positive if the second value is bigger than the first
	 */
	private int compareString(int i, int j, int column) {
		String is = this.valueAt(i,column).toString().toLowerCase();
		String js = this.valueAt(j,column).toString().toLowerCase();
		int n = 0;
		int test = 0;
		char i1;
		char j1;
		while(n < plusCourt(is,js)){
			i1 = is.charAt(n);
			j1 = js.charAt(n);
			if(i1 != j1){
				test = (int)j1 - (int)i1;
				break;
			}
			else
				n++;
		}
		if(test == 0){
			if(js.length() > is.length())
				test = 1;
			else if(js.length() < is.length())
				test = -1;
			else
				test = 0;
		}
		return test;
	}
	
	/**
	 * initialiser les indexes of sort of the  table 
	 */
	private void allocate() {
		setListPid();
		for(int i=0; i < Processuses.getProcessNumber(); ++i) {
			indexes.add(i);
		}
	}
	
	/**
	 * To sort the data of the type of int from small to big
	 * @param column the column that need sort
	 */
	public void sortIntUp(int column) {
		int rowCount = getRowCount();
		for(int i=0; i < rowCount-1; i++) {
			for(int j = i+1; j < rowCount; j++) {
				if(compareInt(indexes.get(i), indexes.get(j), column) < 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * To sort the data of the type of int from big to small
	 * @param column the column that need sort
	 */
	public void sortIntDown(int column) {
		int rowCount = getRowCount();
		for(int i=0; i < rowCount-1; i++) {
			for(int j = i+1; j < rowCount; j++) {
				if(compareInt(indexes.get(i), indexes.get(j), column) > 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * Returns the value is positive if the second value is bigger than the first
	 * @param i the index of the row
	 * @param j the index of the row
	 * @param column the index of column that need sort 
	 * @return the value is positive if the second value is bigger than the first
	 */
	private int compareInt(int i, int j, int column) {
		String is = this.valueAt(i,column).toString();
		String js = this.valueAt(j,column).toString();
		int ii = Integer.parseInt(is);
		int ji = Integer.parseInt(js);
		return ji-ii;
	}
	
	/**
	 * To sort the data of the type of float from small to big
	 * @param column the column that need sort
	 */
	public void sortFloatUp(int column){
		int rowCount = getRowCount();
		for(int i=0; i < rowCount-1; i++) {
			for(int j = i+1; j < rowCount; j++) {
				if(compareFloat(indexes.get(i), indexes.get(j), column) < 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * To sort the data of the type of float from big to small
	 * @param column the column that need sort
	 */
	public void sortFloatDown(int column){
		int rowCount = getRowCount();
		for(int i=0; i < rowCount-1; i++) {
			for(int j = i+1; j < rowCount; j++) {
				if(compareFloat(indexes.get(i), indexes.get(j), column) > 0) {
					swap(i,j);
				}
			}
		}
	}
	
	/**
	 * Returns the value is positive if the second value is bigger than the first
	 * @param i the index of the row
	 * @param j the index of the row
	 * @param column the index of column that need sort 
	 * @return the value is positive if the second value is bigger than the first
	 */
	private int compareFloat(int i, int j,int column){
		String is = this.valueAt(i,column).toString();
		String js = this.valueAt(j,column).toString();
		int test = 0;
		int avPoint_is = avantPoint(is);
		int avPoint_js = avantPoint(js);
		int apPoint_is = apresPoint(is);
		int apPoint_js = apresPoint(js);
		if(avPoint_is != avPoint_js)
			test = avPoint_js - avPoint_is;
		else
			test = apPoint_js - apPoint_is;
		return test;
	}
	
	/**
	 * Return the position of the point
	 * @param s the string of a value of float
	 * @return the position of the point
	 */
	private int positionPoint(String s){
		return s.indexOf('.');
	}
	
	/**
	 * Return the value before the point
	 * @param s the string of the value of float
	 * @return the value before the point
	 */
	private int avantPoint(String s){
		int p = positionPoint(s);
		int resultat = Integer.parseInt(s.substring(0,p));
		return resultat;
	}
	/**
	 * Return the value after the point
	 * @param s the string of the value of float
	 * @return the value after the point
	 */
	private int apresPoint(String s){
		int p = positionPoint(s);
		int resultat = Integer.parseInt(s.substring(p+1,s.length()));
		return resultat;
	}
	/**
	 * Returns the index of the data
	 * @return the index of the data
	 */
	public List<Integer> getIndexes(){
		return indexes;
	}
	
	/**
	 * Set the Pid of processus into a list 
	 */
	public void setListPid(){
		for(Processus proc : Processuses.getProcessusList())
			listPid.add(proc.getPid());
	}

	/**
	 * Returns whether the column is sorted
	 * @return whether the column is sorted
	 */
	public boolean getAscending(){
		return ascending;
	}
	
	/**
	 * Sets whether the column is sorted 
	 * @param value true if the column is sorted
	 */
	public void setAscending(boolean value){
		this.ascending = value;
	}
	
	/**
	 * Returns the name of the column that is sorted
	 * @return the name of the column that is sorted
	 */
	public String getSortColumnName(){
		return this.sortColumnName;
	} 
	/**
	 * Set the name of the column
	 * @param columnName
	 */
	public void setSortColumnName(String columnName){
		this.sortColumnName = columnName;
	}
	
	/**
	 * To choose the type of sort by the name of column
	 * @param mc the index of the column
	 */
	public void sort(int mc){
		if(		this.getColumnName(mc)=="Pid"||
				this.getColumnName(mc)=="Tgid"||
				this.getColumnName(mc)=="PPid"||
				this.getColumnName(mc)=="Uid"||
				this.getColumnName(mc)=="Gid"||
				this.getColumnName(mc)=="VmLck(KB)"||
				this.getColumnName(mc)=="VmSize(KB)"||
				this.getColumnName(mc)=="VmRSS(KB)"||
				this.getColumnName(mc)=="VmData(KB)"||
				this.getColumnName(mc)=="VmStk(KB)"||
				this.getColumnName(mc)=="VmExe(KB)"||
				this.getColumnName(mc)=="VmLib(KB)"||
				this.getColumnName(mc)=="VmPte(KB)"||
				this.getColumnName(mc)=="Threads"||
				this.getColumnName(mc)=="TracerPid"
		){
			if(this.getAscending()==true)
				this.sortIntUp(mc);
			else this.sortIntDown(mc);
			}
		else if(this.getColumnName(mc) == "MEM%"||
				this.getColumnName(mc) == "CPU%"){
			if(this.getAscending()==true)
				this.sortFloatUp(mc);
			else this.sortFloatDown(mc);
		}
		else{
			if(this.getAscending()==true)
				this.sortStringUp(mc);
			else this.sortStringDown(mc);
			}
	}
	
	/**
	 * To creat a Renderer of the header of the table
	 * @return the renderer of the header of the table
	 */
    public TableCellRenderer createHeaderRenderer() {
        DefaultTableCellRenderer defaultHeaderRenderer = new MyTableHeaderRenderer();
        defaultHeaderRenderer.setHorizontalAlignment(SwingConstants.CENTER);
        defaultHeaderRenderer.setHorizontalTextPosition(SwingConstants.LEFT);
        return defaultHeaderRenderer;
    } 
	
    
    /**
     * To customize the Renderer of the header of the table
     * @author Ma & Huang
     *
     */
	public  class MyTableHeaderRenderer extends DefaultTableCellRenderer{
		
		@Override
		public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
			setText(value != null ? value.toString() : "");
			int k = table.convertColumnIndexToModel(column);
            if (k == table.getColumnModel().getColumnIndex(sortColumnName)) {
                setIcon(ascending ? upIcon : downIcon);
            } else {
                setIcon(null);
            }
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            return this;
		}

	}
}

⌨️ 快捷键说明

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