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

📄 tabbedpane.java

📁 这是一个在linux环境下
💻 JAVA
字号:
package fr.umlv.projet.fenetre;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumnModel;

import fr.umlv.projet.fenetre.net.PanelReseau;
import fr.umlv.projet.fenetre.preformance.Preformance;
import fr.umlv.projet.fenetre.table.ConverData;
import fr.umlv.projet.fenetre.table.MyTableModel;
import fr.umlv.projet.fenetre.table.MyTableRenderer;
import fr.umlv.projet.procfs.KillProcessus;
import fr.umlv.projet.procfs.Processuses;

/**
 * To creat a JTabbedPane in which there are these JPanel :  Processus,Performance,Reseau
 * @author Ma & Huang
 *
 */
public class TabbedPane {
	private static String[] defaultColomn = new String[]{"User Name","Name","Pid","CPU%","MEM%"};
	private static JTabbedPane jtp = new JTabbedPane();	
	private static Preformance preformance = new Preformance();
	private static PanelReseau reseau = new PanelReseau();
	private static MyTableModel procTableModel = new MyTableModel(defaultColomn);
	private static JTable ptable = new JTable();
	private static List<Integer> indexSelected = new ArrayList<Integer>();
	private static JPopupMenu popupMenu = new JPopupMenu();
	
	
	/**
	 * To customize a JTabbedPane
	 * @param frame the current container that contain the JTabbedPane
	 * @return
	 */
	public static JTabbedPane creatTabbedPane(JFrame frame){
		
		JPanel p1 = new JPanel();
		p1.setLayout(new BorderLayout());
		JButton kill = new JButton("Terminer le processus");
		kill.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				killAction();
			}			
		});
		p1.add(kill);
		ptable.getTableHeader().setDefaultRenderer(procTableModel.createHeaderRenderer());
		ptable.setModel(makeMyProcTableModel(procTableModel));
		ptable.setDefaultRenderer(Number.class,new MyTableRenderer());
		ptable.add(creatPopupMenu());
		ptable.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				this_mousePressed(e);
			}
		});
		
		/**************************************************************/
		JPanel pButton2 = new JPanel();
		JPanel PB2 = new JPanel();
		pButton2.setLayout(new BorderLayout());
		PB2.setLayout(new BorderLayout());
		pButton2.add(kill,BorderLayout.EAST);
		PB2.add(new JPanel(),BorderLayout.EAST);
		PB2.add(new JPanel(),BorderLayout.WEST);
		PB2.add(new JPanel(),BorderLayout.NORTH);
		PB2.add(new JPanel(),BorderLayout.SOUTH);
		PB2.add(pButton2,BorderLayout.CENTER);
		
		p1.add(new JPanel(),BorderLayout.NORTH);
		p1.add(new JPanel(),BorderLayout.EAST);
		p1.add(new JPanel(),BorderLayout.WEST);
		p1.add(new JScrollPane(ptable),BorderLayout.CENTER);
		p1.add(PB2,BorderLayout.SOUTH);
		/**************************************************************/	
		
		jtp.setBorder(new BevelBorder(BevelBorder.LOWERED));
		jtp.addTab("Processus",null,p1);
		jtp.addTab("Performances",null,preformance);
		jtp.addTab("Mise en Reseau",null,reseau);
		//jtp.add(creatPopupMenu());
		return jtp;
	}
	
	/**
	 *  the action of kill a processuce
	 *
	 */
	public static void killAction(){
		int[] tmp = ptable.getSelectedRows();
		for(int i:tmp){
			int pid = Processuses.getProcessusList().get(procTableModel.getIndexes().get(i)).getPid();
			String str = KillProcessus.killAProc(pid);
			if(str.equals("ok")){
				procTableModel.fireTableRowsDeleted(i,i);
				ptable.setRowSelectionInterval(0,0);
				indexSelected.clear();
				indexSelected.add(procTableModel.getIndexes().get(0));
			}
			else{
				 JOptionPane.showMessageDialog(null, "Failed to kill pid("+pid+"): "+str, "Failed to kill", JOptionPane.ERROR_MESSAGE);
			}
		}
		update();
	}
	
	/**
	 * To customize a TableModel for display the infomation of all the processuses
	 * @param procTableModel the model that need customize
	 * @return the TableModel after customize
	 * @see fr.umlv.projet.fenetre.table.MyTableModel
	 */
	public static MyTableModel makeMyProcTableModel(final MyTableModel procTableModel){
		procTableModel.setData(ConverData.covert(Processuses.getProcessusList(),TabbedPane.getProcTableModel().getColumnName()));
		JTableHeader hdr = ptable.getTableHeader();
		hdr.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				saveTableSelected();
				TableColumnModel tcm = ptable.getColumnModel();
				int vc = tcm.getColumnIndexAtX(e.getX());
				int mc = ptable.convertColumnIndexToModel(vc);
				String sortColomnName = procTableModel.getSortColumnName();
				if(mc == ptable.getColumnModel().getColumnIndex(sortColomnName))
					procTableModel.setAscending(!procTableModel.getAscending());
				else{
					procTableModel.setAscending(true);
					procTableModel.setSortColumnName(procTableModel.getColumnName(mc));
				}
				procTableModel.sort(mc);
				procTableModel.fireTableDataChanged();
				loadTableSelected();
			}
		});
		return procTableModel;
	}
	
	/**
	 * Save these indexes of the row of the table that the user choose before the table update
	 * 
	 */
	public static void saveTableSelected(){
		indexSelected.clear();
		int tmp[] = ptable.getSelectedRows();
		for(int i : tmp)
			indexSelected.add(procTableModel.getIndexes().get(i));
	}
	/**
	 * Load these indexes of the row of the table that the user choose before the table update
	 *
	 */
	public static void loadTableSelected(){
		//System.out.println(indexSelected);
		if(indexSelected.size()!=0){
			for(int i : indexSelected){
				//System.out.println("index"+i);
				ptable.addRowSelectionInterval(procTableModel.getIndexes().indexOf(i),procTableModel.getIndexes().indexOf(i));
			}
		}
	}
	
	/**
	 * Returns the customized JTabbedPane
	 * @return the customized JTabbedPane
	 */
	public static JTabbedPane getJtp() {
		return jtp;
	}
	
	/**
	 * Return the model of table that can display the infomation of all the processuses
	 * @return  the model of table that can display the infomation of all the processuses
	 * @see fr.umlv.projet.fenetre.table.MyTableModel
	 */
	public static MyTableModel getProcTableModel() {
		return procTableModel;
	}
	
	/**
	 * Return the table of the processues
	 * @return the table of the processues
	 */
	public static JTable getPtable() {
		return ptable;
	}
	
	/**
	 * To customize the JPopupMenu that display when the user click the right button of the 
	 * mouse in the table
	 * @return the menu that display when the user click the right button of the 
	 * mouse in the table
	 */
	public static JPopupMenu creatPopupMenu(){
	
		JMenuItem menuItem1 = new JMenuItem("kill processus");
		menuItem1.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				killAction();
			}
			
		});
		JMenuItem menuItem2 = new JMenuItem("refresh");
		menuItem2.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				update();
			}
	
		});
		popupMenu.add(menuItem1); popupMenu.add(menuItem2);
		return popupMenu;
	}
	
	/**
	 * the action of when the mouse pressed
	 * @param e the MouseEvent
	 */
	static void this_mousePressed(MouseEvent e) {
		int mods=e.getModifiers();
		if((mods&InputEvent.BUTTON3_MASK)!=0){
			popupMenu.show (ptable,e.getX(),e.getY());
		}
	}
	
	/**
	 * Return the JPanel of the performance
	 * @return the JPanel of the performance
	 */
	public static Preformance getPreformance(){
		return preformance;
	}
	
	/**
	 * Return the JPanel of the reseau
	 * @return the JPanel of the reseau
	 */
	public static PanelReseau getPanelReseau(){
		return reseau;
	}

	/**
	 * the action of update the table of all the processuses
	 *
	 */
	public synchronized static void update(){
		Processuses.loadProcess();
		TabbedPane.getProcTableModel().tableChanged(null);
		TabbedPane.saveTableSelected();
		TabbedPane.getProcTableModel().setData(ConverData.covert(Processuses.getProcessusList(),TabbedPane.getProcTableModel().getColumnName()));
		TabbedPane.getProcTableModel().sort(ptable.getColumnModel().getColumnIndex(procTableModel.getSortColumnName()));
		TabbedPane.getProcTableModel().fireTableDataChanged();
		TabbedPane.getJtp().repaint();
		TabbedPane.loadTableSelected();
	}
	
	
}

⌨️ 快捷键说明

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