📄 test.java
字号:
return;
}
if (ghostImage != null) {
Graphics2D g2 = (Graphics2D) getGraphics();
// If a drag image is not supported by the platform, then draw my
// own drag image
if (!DragSource.isDragImageSupported()) {
paintImmediately(ghostRect.getBounds()); // Rub out the last
// ghost image and cue
// line
// And remember where we are about to draw the new ghost image
ghostRect.setRect(pt.x - ptOffset.x, pt.y - ptOffset.y,
ghostImage.getWidth(), ghostImage.getHeight());
g2.drawImage( (ghostImage), AffineTransform
.getTranslateInstance(ghostRect.getX(), ghostRect
.getY()), null);
}
}
TreePath path = getClosestPathForLocation(pt.x, pt.y);
if (! (path == lastPath)) {
lastPath = path;
hoverTimer.restart();
}
}
public void dropActionChanged(DropTargetDragEvent dtde) {
}
public void drop(DropTargetDropEvent e) {
try {
DataFlavor stringFlavor = DataFlavor.stringFlavor;
Transferable tr = e.getTransferable();
TreePath path = this.getPathForLocation(e.getLocation().x, e
.getLocation().y);
if (path == null) {
e.rejectDrop();
return;
}
FileNode node = (FileNode) path.getLastPathComponent();
if (e.isDataFlavorSupported(DataFlavor.javaFileListFlavor)
&& node.isDirectory()) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
System.out.println("file cp");
List list = (List) (e.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor));
Iterator iterator = list.iterator();
File parent = node.getFile();
while (iterator.hasNext()) {
File f = (File) iterator.next();
cp(f, new File(parent, f.getName()));
}
node.reexplore();
e.dropComplete(true);
this.updateUI();
}
else if (e.isDataFlavorSupported(stringFlavor)
&& node.isDirectory()) {
String filename = (String) tr.getTransferData(stringFlavor);
if (filename.endsWith(".txt") || filename.endsWith(".java")
|| filename.endsWith(".jsp")
|| filename.endsWith(".html")
|| filename.endsWith(".htm")) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
File f = new File(filename);
if (f.exists()) {
f.renameTo(new File(node.getFile(), f.getName()));
node.reexplore();
( (FileNode) sourceNode.getParent()).remove(sourceNode);
e.dropComplete(true);
this.updateUI();
}
else {
e.rejectDrop();
}
}
else {
e.rejectDrop();
}
}
else {
e.rejectDrop();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
}
finally {
ghostImage = null;
this.repaint();
}
}
private void cp(File src, File dest) throws IOException {
if (src.isDirectory()) {
if (!dest.exists()) {
boolean ret = dest.mkdir();
if (ret == false) {
return;
}
}
File[] fs = src.listFiles();
for (int i = 0; i < fs.length; i++) {
cp(fs[i], new File(dest, fs[i].getName()));
}
return;
}
byte[] buf = new byte[1024];
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dest);
int len;
try {
while ( (len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
finally {
in.close();
out.close();
}
}
public void dragExit(DropTargetEvent dte) {
}
}
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 boolean isDirectory() {
File file = getFile();
return file.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 (!isDirectory()) {
return;
}
if (!isExplored()) {
File file = getFile();
File[] children = file.listFiles();
for (int i = 0; i < children.length; ++i) {
if (children[i].isDirectory()) {
add(new FileNode(children[i]));
}
}
for (int i = 0; i < children.length; ++i) {
if (!children[i].isDirectory()) {
add(new FileNode(children[i]));
}
}
explored = true;
}
}
public void reexplore() {
this.removeAllChildren();
explored = false;
explore();
}
}
public class Test
extends JFrame implements DropTargetListener {
private JEditorPane textPane = new JEditorPane();
public Test() {
super("Drag and Drop With Swing");
new DropTarget(textPane, DnDConstants.ACTION_COPY_OR_MOVE, this);
JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
createTreePanel(), createTextPanel());
splitPane.setDividerLocation(250);
splitPane.setOneTouchExpandable(true);
getContentPane().add(splitPane, BorderLayout.CENTER);
}
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e1) {
e1.printStackTrace();
}
Test test = new Test();
test.setBounds(300, 300, 850, 350);
test.setVisible(true);
test.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
test.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
private JPanel createTreePanel() {
JPanel treePanel = new JPanel();
DragTree tree = new DragTree();
treePanel.setLayout(new BorderLayout());
treePanel.add(new JScrollPane(tree), BorderLayout.CENTER);
treePanel.setBorder(BorderFactory
.createTitledBorder("Drag source for filenames"));
return treePanel;
}
private JPanel createTextPanel() {
JPanel textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.add(new JScrollPane(textPane), BorderLayout.CENTER);
textPanel.setMinimumSize(new Dimension(375, 0));
textPanel.setBorder(BorderFactory
.createTitledBorder("Drop target for filenames"));
return textPanel;
}
private void readFile(final String filename) {
try {
textPane.setPage(new File(filename).toURL());
}
catch (IOException e) {
EditorKit kit = textPane.getEditorKit();
Document document = textPane.getDocument();
try {
document.remove(0, document.getLength());
kit.read(new FileReader(filename), document, 0);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void drop(DropTargetDropEvent e) {
try {
DataFlavor stringFlavor = DataFlavor.stringFlavor;
Transferable tr = e.getTransferable();
if (e.isDataFlavorSupported(stringFlavor)) {
String filename = (String) tr.getTransferData(stringFlavor);
if (filename.endsWith(".txt")
|| filename.endsWith(".java") ||
filename.endsWith(".jsp") || filename.endsWith(".html") ||
filename.endsWith(".htm")) {
e.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
readFile(filename);
textPane.setCaretPosition(0);
e.dropComplete(true);
}
else {
e.rejectDrop();
}
}
else {
e.rejectDrop();
}
}
catch (IOException ioe) {
ioe.printStackTrace();
}
catch (UnsupportedFlavorException ufe) {
ufe.printStackTrace();
}
}
public void dragEnter(DropTargetDragEvent e) {
}
public void dragExit(DropTargetEvent e) {
}
public void dragOver(DropTargetDragEvent e) {
}
public void dropActionChanged(DropTargetDragEvent e) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -