📄 fileviewimpl.java
字号:
* 窗口布局,左边是Tree右边是Table
*/
JPanel southBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
southBtnPanel.setBorder(new TitledBorder(""));
southBtnPanel.add(addFolderBtn);
southBtnPanel.add(addFileBtn);
southBtnPanel.add(deleteBtn);
southBtnPanel.add(exitBtn);
this.getContentPane().add(southBtnPanel,BorderLayout.SOUTH);
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createTitledBorder("Folder Tree"));
JPanel rightPanel = new JPanel(new BorderLayout());
rightPanel.setBorder(new TitledBorder("File Resorceses"));
JPanel upBtnPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
upBtnPanel.add(upBtn);
upBtnPanel.add(addressLabel);
JPanel rightTopTextFieldPanel = new JPanel(new BorderLayout());
rightTopTextFieldPanel.setBorder(new TitledBorder(""));
rightTopTextFieldPanel.add(upBtnPanel,BorderLayout.WEST);
rightTopTextFieldPanel.add(addressTextField);
leftPanel.add(new JScrollPane(tree));
JScrollPane rightScrollPane = new JScrollPane(table);
rightScrollPane.setBorder(new TitledBorder("File Detail"));
rightPanel.add(rightTopTextFieldPanel,BorderLayout.NORTH);
rightPanel.add(rightScrollPane);
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
jsp.setDividerSize(3);
jsp.setDividerLocation(200);
this.getContentPane().add(jsp);
tree.addTreeSelectionListener(new TreeSelectionProcesser());
tree.addTreeWillExpandListener(new TreeWillExpandProcessor());
table.addMouseListener(new mouseClickedProcessor());
tree.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e){
if(e.getButton() == e.BUTTON3)
popMenu.show(tree,e.getX(),e.getY());
}
});
addFolderBtn.addActionListener(this);
addFolderMenu.addActionListener(this);
addFileBtn.addActionListener(this);
addFileMenu.addActionListener(this);
deleteBtn.addActionListener(this);
deleteMenu.addActionListener(this);
exitBtn.addActionListener(this);
upBtn.addActionListener(this);
this.setSize(700,450);
this.setLocationRelativeTo(this);
this.setVisible(true);
}
/**
* Append nodes to the tree
* @param node,parent node that will appended other nodes
* @param vct,nodes file sets that you want to append
*/
public void appendNodes(DefaultMutableTreeNode node,Collection vct){
Vector newVct = new Vector();
if(vct != null){
newVct = (Vector)vct;
node.removeAllChildren();
}
else{
node.removeAllChildren();
return;
}
for(int i=0;i<vct.size();i++){
File file = (File)newVct.get(i);
TNode tnode = new TNode(file);
tnode.add(new DefaultMutableTreeNode(""));
node.add(tnode);
}
}
/**
* Rsize the table columnWidth
*/
public void setColumnWidth(int pColumn, int pWidth){
TableColumnModel colModel = table.getColumnModel();
colModel.getColumn(pColumn).setPreferredWidth(pWidth);
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == upBtn){
try{
curPath = curPath.getParentPath();
if(curPath != null){
tree.setSelectionPath(curPath);
}
}
catch(Exception ee){
ee.printStackTrace();
}
}
else if(e.getSource() == addFolderBtn || e.getSource() == addFolderMenu){
try{
String buf = JOptionPane.showInputDialog(this,"Pls Input FileName: ","new Folder");
createFile("d",buf);
}
catch(Exception ee){
ee.printStackTrace();
}
}
else if(e.getSource() == addFileBtn || e.getSource() == addFileMenu){
try{
String buf = JOptionPane.showInputDialog(this,"Pls Input FileName: ","new File");
createFile("f",buf);
}
catch(Exception ee){
ee.printStackTrace();;
}
}
else if(e.getSource() == deleteBtn || e.getSource() == deleteMenu){
deleteFile();
}
else if(e.getSource() == exitBtn){
System.exit(0);
}
}
/**
* Get all root nodes with the system root files
*/
public Collection handleGetRootNodes() throws Exception{
return model.handleGetRootNodes();
}
/**
* Get all root files of system
*/
public Collection handleGetRootFiles() throws Exception {
// TODO Auto-generated method stub
return model.handleGetRootFiles();
}
/**
* This method invoke by controller,return a collection sets of the file with you supplied file
* @param file,this is you supplied file that you want to get all files under it
*/
public Collection handleGetAll(File file) throws Exception {
// TODO Auto-generated method stub
return model.handleGetAll(file);
}
/**
* This method invoke by controller,return a collection sets of the directories with you supplied file
* @param file,this is you supplied file that you want to get the directories under it
*/
public Collection handleGetAllDirectories(File file) throws Exception {
// TODO Auto-generated method stub
return model.handleGetAllDirectories(file);
}
/**
* This method invoke by model when file deleted
* the end refresh the table and tree
*/
public void fireDeleteFile() throws Exception {
// TODO Auto-generated method stub
try{
if(isTable){
refreshNode();
}
else{
curPath = curPath.getParentPath();
tree.setSelectionPath(curPath);
refreshNode();
}
}
catch(Exception ee){
JOptionPane.showMessageDialog(this,"Delete failure,folder not empty or file protected!");
ee.printStackTrace();
}
}
/**
* This method invoke by model when a new file created,the "file" is the new file created,
* the end refresh the table and tree
*/
public void fireCreateFile(File file) throws Exception {
// TODO Auto-generated method stub
refreshNode();
if(!tree.isExpanded(curPath))
tree.expandPath(curPath);
}
/* (non-Javadoc)
* @see FileView#fireModifyFile(java.io.File)
*/
public void fireModifyFileName(File file) throws Exception {
// TODO Auto-generated method stub
}
/**
* Create a new file or folder
* @param f this is the file type, "f" to create a file,"d" to create a folder
* @param file
*/
public void createFile(String f,String file){
try{
if(file != null && f.equals("f")){
file = file.trim();
String newFile = "";
if(currentFile.isDirectory())
newFile = currentFile.getAbsolutePath()+"\\"+file;
else
newFile = currentNode.getFile().getAbsolutePath()+"\\"+file;
control.createFile("f",newFile);
}
else if(file != null && f.equals("d")){
file = file.trim();
String newFile = "";
if(currentFile.isDirectory())
newFile = currentFile.getAbsolutePath()+"\\"+file;
else
newFile = currentNode.getFile().getAbsolutePath()+"\\"+file;
control.createFile("d",newFile);
}
}
catch(Exception e){
e.printStackTrace();
}
}
/**
* Deleted the all files that you selected,but the file can not be deleted
* that contain the another file under or sub-folder
*/
public void deleteFile(){
try{
if(isTable){
int[] indeics = table.getSelectedRows();
if(indeics != null && indeics.length>0){
File[] buf = new File[indeics.length];
for(int i=0;i<indeics.length;i++){
buf[i] = tableModel.getFile(indeics[i]);
}
control.deleteFile(buf);
}
}
else{
File[] buf = {currentNode.getFile()};
control.deleteFile(buf);
}
}
catch(Exception ee){
JOptionPane.showMessageDialog(this,"Delete failure,folder not empty or file protected!");
ee.printStackTrace();
}
}
/**
* Refresh the Nodes and table when deleted or created a file
*/
public void refreshNode(){
try{
Collection files = control.handleGetAll(currentNode.getFile());
tableModel.addAllFiles(files);
appendNodes(currentNode,control.handleGetAllDirectories(currentNode.getFile()));
treeModel.nodeStructureChanged(currentNode);
}
catch(Exception e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -