📄 filesystemtreescrollpane.java
字号:
package org.net9.oops.jsee;
import javax.swing.*;
import javax.swing.tree.*;
//import javax.swing.table.*;
import java.io.*;
import java.awt.*;
public class FileSystemTreeScrollPane extends JScrollPane {
private JTree tree;
ImageIcon diskIcon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/diskIcon.gif"));
ImageIcon diskOnIcon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/diskOnIcon.gif"));
ImageIcon expandIcon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/expand.gif"));
ImageIcon dirIcon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/dirIcon.gif"));
ImageIcon selectedIcon = new ImageIcon(getClass().getResource("/org/net9/oops/jsee/image/selected.gif"));
public class FolderTreeCellRenderer extends JLabel implements TreeCellRenderer {
// protected boolean selected;
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded,
boolean leaf, int row,
boolean hasFocus) {
Font font;
String s = value.toString(); /* = tree.convertValueToText( value, selected,
expanded, leaf, row, hasFocus);*/
String osName = System.getProperty("os.name",".");
if ( !s.endsWith(":\\") ) // if this is not a drive
{
if ( osName.startsWith("Windows") )
s = s.substring(s.lastIndexOf("\\") + 1);
else if ( !s.equals("/") )// hopefully Unix
s = s.substring(s.lastIndexOf("/") + 1);
}
setIcon(diskIcon);
/* Set the text. */
setText(s);
if (expanded)
if (s.endsWith(":\\"))
if (!selected)
setIcon(diskIcon);
else
setIcon(diskOnIcon);
else if (!selected)
setIcon(expandIcon);
else
setIcon(selectedIcon);
else if (selected)
if (s.endsWith(":\\"))
setIcon(diskOnIcon);
else
setIcon(selectedIcon);
else if (s.endsWith(":\\"))
setIcon(diskIcon);
else
setIcon(dirIcon);
return this;
}
}
public FileSystemTreeScrollPane() {
this( new FileSystemModel() );
}
public FileSystemTreeScrollPane( String startPath ) {
this( new FileSystemModel( startPath ) );
}
public FileSystemTreeScrollPane( FileSystemModel model ) {
tree = new JTree( model ) {
public String convertValueToText(Object value, boolean selected,
boolean expanded, boolean leaf, int row,
boolean hasFocus) {
return ((File)value).getName();
}
};
tree.setCellRenderer(new FolderTreeCellRenderer());
// check operating system
String osName = System.getProperty("os.name",".");
if ( osName.startsWith("Windows") ) /* Win32 */
tree.setRootVisible( false );
else // hopefully Unix for the moment
tree.setRootVisible( true );
tree.setEditable(false);
tree.setShowsRootHandles( true );
tree.putClientProperty( "JTree.lineStyle", "Angled" );
getViewport().setView(tree);
}
public JTree getTree() {
return tree;
}
public void setSelectionPath(File path) {
File temp;
temp = path;
int count = 1;
while (path.getParentFile() != null) {
count++;
path = path.getParentFile();
}
String osName = System.getProperty("os.name",".");
if (osName.startsWith("Windows")) {
File[] fpath = new File[count + 1];
fpath[0] = new File("/");
fpath[count] = temp;
for (int i = count-1; i > 0; i--) {
fpath[i] = fpath[i+1].getParentFile();
}
tree.setSelectionPath(new TreePath(fpath));
} else // Unix
{
File[] fpath = new File[count];
fpath[count-1] = temp;
for (int i = count-2; i >= 0; i--) {
fpath[i] = fpath[i+1].getParentFile();
}
tree.setSelectionPath(new TreePath(fpath));
}
}
public TreePath getSelectionPath() {
return tree.getSelectionPath();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -