?? winexplorer.java
字號:
//package winexplorer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DateFormat;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import javax.swing.filechooser.*;
import java.io.*;
import java.util.*;
public class WinExplorer extends JFrame implements ActionListener,TreeSelectionListener{
JPanel contentPane=(JPanel)getContentPane();;
//private static Manager.HardDiskManager HDM=new Manager.HardDiskManager();
Icon iconUp=UIManager.getIcon("FileChooser.upFolderIcon");
static JTextArea SCR_Screen=new JTextArea();
DetailTable detailTable = new DetailTable();
JScrollPane sp = new JScrollPane(detailTable);
JSplitPane split = new JSplitPane();
JSplitPane TEM_split=new JSplitPane();
FileSystemView fileSystemView=FileSystemView.getFileSystemView();
FileNode root=new FileNode(fileSystemView.createFileObject("."));
DefaultTreeModel treeModel=new DefaultTreeModel(root);
JTree tree = new JTree(treeModel);
JPopupMenu HD_contextmenu=new JPopupMenu();
JMenuItem HD_menu1=new JMenuItem("添加進程進虛擬硬盤");
JPopupMenu VHD_contextmenu=new JPopupMenu();
JMenuItem VHD_menu1=new JMenuItem("運行進程");
JLabel VHD_Label1 = new JLabel("虛擬硬盤");
static DefaultMutableTreeNode VHD_root = new DefaultMutableTreeNode("虛擬硬盤");
static JTree VHD_tree = new JTree(VHD_root);
static DefaultTreeModel VHD_treeModel = (DefaultTreeModel)VHD_tree.getModel();
JScrollPane VHD_JS_tree = new JScrollPane(VHD_tree);
String VHD_Columns[]={"ID","名稱","鏈表序號","長度"};
DetailTables VHD_table = new DetailTables(VHD_Columns);
JScrollPane VHD_JS_table = new JScrollPane(VHD_table);
JSplitPane VHD_split = new JSplitPane();
public static void main(String argv[]){
/*try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Font font=new Font("宋體",Font.PLAIN,12);
String names[]={"Label","CheckBox","PopupMenu","TextPane",
"MenuItem","CheckBoxMenuItem","JRadioButtonMenuItem",
"ComboBox","Button","Tree","ScrollPane","TabbedPane",
"EditorPane","TitledBorder","Menu","TextArea","OptionPane",
"MenuBar","ToolBar","ToggleButton","ToolTip","ProgressBar",
"TableHeader","Panel","List","ColorChooser","PasswordField",
"TextField","Table","Label","Viewport","RadioButtonMenuItem",
"RadioButton"};
for(int i=0;i<names.length;i++)UIManager.put(names[i]+".font",font);
UIManager.put("Label.foreground",Color.black);
UIManager.put("Border.foreground",Color.black);
UIManager.put("TitledBorder.titleColor",Color.black);*/
new WinExplorer().show();
/*}catch(Exception e){
e.printStackTrace();
}*/
}
public WinExplorer(){
contentPane.setLayout(new BorderLayout());
Dimension dimension = getToolkit().getScreenSize();
int i = (dimension.width - 320) / 4;
int j = (dimension.height - 240) / 4;
setBounds(i,j,320,240);
setTitle("資源管理器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
VHD_split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,VHD_JS_tree,VHD_JS_table);
VHD_split.setDividerSize(6);
VHD_split.setDividerLocation(80);
VHD_split.setOneTouchExpandable(true);
split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,SCR_Screen,sp);
split.setDividerSize(6);
split.setDividerLocation(80);
split.setOneTouchExpandable(true);
sp.getViewport().setBackground(Color.white);
TEM_split=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,split,VHD_split);
TEM_split.setDividerSize(6);
TEM_split.setDividerLocation(100);
TEM_split.setOneTouchExpandable(true);
contentPane.add(TEM_split, BorderLayout.CENTER);
tree.addTreeExpansionListener(new MyExpandsionListener());
tree.setCellRenderer(new MyTreeCellRenderer());
tree.addTreeSelectionListener(this);
tree.setSelectionRow(0);
tree.setComponentOrientation(ComponentOrientation.UNKNOWN);
HD_contextmenu.add(HD_menu1);
detailTable.add(HD_contextmenu);
HD_menu1.addActionListener(this);
detailTable.addMouseListener
(
new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
HD_contextmenu.show((Component)e.getSource(),e.getX(),e.getY());
}
}
}
);
VHD_contextmenu.add(VHD_menu1);
VHD_table.add(VHD_contextmenu);
VHD_menu1.addActionListener(this);
VHD_table.addMouseListener
(
new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
{
VHD_contextmenu.show((Component)e.getSource(),e.getX(),e.getY());
}
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == HD_menu1)
{
this.HDAddJobToVHD();
}
else if(e.getSource()==VHD_menu1)
{
this.runJobFromVHDTB();
}
}
public void HDAddJobToVHD()
{
DefaultTableModel model = (DefaultTableModel)detailTable.getModel();
int[] selectcount = detailTable.getSelectedRows();
if(selectcount.length!=0)
{
for(int i=0;i<selectcount.length;i++)
{
String path = model.getValueAt(selectcount[i],0).toString();
this.addTextToScreenTextField("添加作業(yè)"+path+"\n");
addJobToVHD(path);
}
}
}
public static void addTextToScreenTextField(String s)
{
SCR_Screen.append(s);
}
private void addJobToVHD(String path)
{
String ss[] = new String[4];
String filepath=path;
ss[1]=filepath;
Random random = new Random();
int id = random.nextInt();
ss[0]=""+id;
DefaultMutableTreeNode pnode = new DefaultMutableTreeNode(""+id);
VHD_treeModel.insertNodeInto(pnode, VHD_root, VHD_root.getChildCount());
VHD_table.addRow(ss);
}
public void runJobFromVHDTB()
{
DetailTableModel model = (DetailTableModel)VHD_table.getModel();
int[] selectcount = VHD_table.getSelectedRows();
if(selectcount.length!=0)
{
for(int i=0;i<selectcount.length;i++)
{
String Sid = model.getValueAt(selectcount[i],1).toString();
this.addTextToScreenTextField("運行作業(yè)"+Sid+"\n");
}
}
}
public void valueChanged(TreeSelectionEvent e){
Object obj=tree.getLastSelectedPathComponent();
if(obj==null)return;
else detailTable.setParent(((FileNode)obj).getFile());
}
class MyTreeCellRenderer extends DefaultTreeCellRenderer {
public MyTreeCellRenderer() {}
public Component getTreeCellRendererComponent(JTree tree,Object value,
boolean sel,boolean expanded,boolean leaf,int row,boolean hasFocus) {
super.getTreeCellRendererComponent(tree,value,sel,expanded,leaf,row,hasFocus);
setIcon(fileSystemView.getSystemIcon(((FileNode)value).getFile()));
return this;
}
}
class MyExpandsionListener implements TreeExpansionListener {
public MyExpandsionListener() {}
public void treeExpanded(TreeExpansionEvent event) {
if(event.getPath() == null){
return;
}
tree.setCursor(new Cursor(Cursor.WAIT_CURSOR));
TreePath path = event.getPath();
FileNode node = (FileNode)path.getLastPathComponent();
node.explore();
treeModel.nodeStructureChanged(node);
tree.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
public void treeCollapsed(TreeExpansionEvent event) {}
}
class FileNode extends DefaultMutableTreeNode {
private boolean explored = false;
public FileNode(File file) { setUserObject(file); }
public boolean getAllowsChildren() { return isDirectory(); }
public boolean isLeaf() { return !isDirectory();}
public File getFile() { return (File)getUserObject(); }
public boolean isExplored() { return explored; }
public void setExplored(boolean b){ explored=b;}
public boolean isDirectory() { return getFile().isDirectory();}
public String toString() {
File file = (File)getUserObject();
String filename = file.toString();
int index = filename.lastIndexOf(File.separator);
return (index != -1 && index != filename.length()-1)
? filename.substring(index+1) : filename;
}
public void explore() {
if(!isExplored()) {
File file = getFile();
File[] children = file.listFiles();
if(children==null||children.length==0)return;
for(int i=0; i < children.length; ++i)
{
File f=children[i];
if(f.isDirectory())add(new FileNode(children[i]));
}
explored = true;
}
}
}
class DetailTable extends JTable{
DetailTableModel model=new DetailTableModel();
public DetailTable(){
setModel(model);
setShowGrid(false);
TableColumnModel colModel = getColumnModel();
for (int i=0;i<3;i++)
colModel.getColumn(i).setCellRenderer(new DetailsTableCellRenderer());
setRowHeight(18);
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2){
for (int i = 0; i <getRowCount(); i++) {
if(getCellRect(i,0,true).contains(e.getPoint())){
openSelect();
break;
}
}
}
}
});
}
public void openSelect(){
Object obj=model.getValueAt(getSelectedRow(),0);
if(obj==null)return;
File f=(File)obj;
if(f.isDirectory()){
//expand tree
}else{
//open select file
}
}
public void setParent(File parent){
model.removeAllRows();
File list[]=parent.listFiles();
if(list==null)return;
Vector vDir=new Vector(),vFile=new Vector();
for (int i = 0; i < list.length; i++) {
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -