📄 virtualfolders.java
字号:
VirtualFolderNode node = new VirtualFolderNode(content, true); treeModel.insertNodeInto(node, parent, parent.getChildCount()); return node; } public static boolean folderExists(VirtualFolderNode parent, String name) { boolean exists = false; Enumeration enum = parent.children(); while ((enum.hasMoreElements()) && !exists) { VirtualFolderNode child = (VirtualFolderNode) enum.nextElement(); exists = child.toString().equals(name); } return exists; } private void newFolder() { TreePath[] paths = tree.getSelectionPaths(); VirtualFolderNode parentNode = null; if ((paths == null) || (paths.length == 0)) { parentNode = root; } else { parentNode = (VirtualFolderNode) paths[0].getLastPathComponent(); } if (parentNode.isLeaf()) { if (!parentNode.isRoot()) { parentNode = (VirtualFolderNode) parentNode.getParent(); } } newFolder(parentNode); } private void newFolder(VirtualFolderNode parentNode) { String response = JOptionPane.showInputDialog(parent, Jext.getProperty("vf.add.input.msg"), Jext.getProperty("vf.add.input.title"), JOptionPane.QUESTION_MESSAGE); if (response != null && response.length() > 0) { if (createFolder(response, true, parentNode) == null) GUIUtilities.message(parent, "vf.folder.exists", null); } } private void removeItem() { TreePath[] paths = tree.getSelectionPaths(); if (paths != null) { for (int i = 0; i < paths.length; i++) { VirtualFolderNode node = (VirtualFolderNode) paths[i].getLastPathComponent(); treeModel.removeNodeFromParent(node); } } int index = root.getChildCount() - 1; if (index >= 0) { VirtualFolderNode _node_ = (VirtualFolderNode) root.getChildAt(index); tree.setSelectionPath(new TreePath(_node_.getPath())); } } private void addFile() { JextTextArea textArea = parent.getNSTextArea(); if (textArea.isNew()) return; addFile(textArea.getCurrentFile()); } private void addFile(String fileName) { TreePath selection = tree.getSelectionPath(); VirtualFolderNode node = null; if (selection == null) { node = root; } else { node = (VirtualFolderNode) selection.getLastPathComponent(); if (node.isLeaf()) node = (VirtualFolderNode) node.getParent(); } if (createLeaf(node, fileName) == null) GUIUtilities.message(parent, "vf.item.exists", null); } private void addAllFiles() { TreePath selection = tree.getSelectionPath(); VirtualFolderNode node = null; if (selection == null) { node = root; } else { node = (VirtualFolderNode) selection.getLastPathComponent(); if (node.isLeaf()) node = (VirtualFolderNode) node.getParent(); } JextTextArea[] textAreas = parent.getTextAreas(); for (int i = 0; i < textAreas.length; i++) { if (textAreas[i].isNew()) continue; if (createLeaf(node, textAreas[i].getCurrentFile()) == null) GUIUtilities.message(parent, "vf.item.exists", null); } } private void openSelection(boolean fromMenu) { TreePath[] paths = tree.getSelectionPaths(); if (paths != null) { for (int i = 0; i < paths.length; i++) { VirtualFolderNode node = (VirtualFolderNode) paths[i].getLastPathComponent(); openNode(node, fromMenu); } } } public void openNode(VirtualFolderNode node, boolean fromMenu) { if (node.isLeaf()) { parent.open(node.getFilePath()); } else { if (fromMenu) { Enumeration enum = node.children(); while (enum.hasMoreElements()) { VirtualFolderNode child = (VirtualFolderNode) enum.nextElement(); openNode(child, fromMenu); } } } } public void notifyChanges() { ArrayList instances = Jext.getInstances(); for (int i = 0; i < instances.size(); i++) { JextFrame instance = (JextFrame) instances.get(i); if (instance != parent) instance.getVirtualFolders().notify(treeModel); } } public void notify(DefaultTreeModel model) { this.treeModel = model; tree.setModel(treeModel); } public void actionPerformed(ActionEvent evt) { Object o = evt.getSource(); if ((o == newFolder) || (o == newFolderM)) { newFolder(); notifyChanges(); } else if ((o == addFile) || (o == addFileM)) { addFile(); notifyChanges(); } else if ((o == addAllFiles) || (o == addAllFilesM)) { addAllFiles(); notifyChanges(); } else if ((o == deleteItem) || (o == deleteM)) { removeItem(); notifyChanges(); } else if ((o == openFile) || (o == openFileM)) { openSelection(true); } } public void valueChanged(TreeSelectionEvent e) { TreePath[] paths = tree.getSelectionPaths(); boolean alsoFolder = false; if (paths != null) { openFileM.setEnabled(true); openFile.setEnabled(true); deleteM.setEnabled(true); deleteItem.setEnabled(true); for (int i = 0; i < paths.length; i++) { VirtualFolderNode node = (VirtualFolderNode) paths[i].getLastPathComponent(); if (!node.isLeaf()) alsoFolder = true; } } else { openFileM.setEnabled(false); openFile.setEnabled(false); deleteM.setEnabled(false); deleteItem.setEnabled(false); } } class MouseHandler extends MouseAdapter { public void mousePressed(MouseEvent e) { if (e.getModifiers() == e.BUTTON3_MASK) { popup.show(tree, e.getX(), e.getY()); } else { TreePath path = tree.getPathForLocation(e.getX(), e.getY()); if (path == null) tree.clearSelection(); if (e.getClickCount() == 2) openSelection(false); } } } class VirtualFoldersHandler extends HandlerBase { VirtualFolderNode parent = null; String folderName = null; String fileName = null; boolean isVisible = false; public void startElement(String elname) throws java.lang.Exception { if (parent == null) parent = root; if (elname.equalsIgnoreCase("folder")) parent = createFolder(folderName, false, parent); if (elname.equalsIgnoreCase("file")) { VirtualFolderNode node = createLeaf(parent, fileName); if (isVisible) node.isVisible = isVisible; isVisible = false; } } public void endElement(String elname) throws java.lang.Exception { if (elname.equalsIgnoreCase("folder")) { if (parent != null) parent = (VirtualFolderNode) parent.getParent(); if (parent == null) parent = root; } } public void attribute(String aname, String value, boolean isSpecified) { if (aname.equalsIgnoreCase("path")) fileName = value; if (aname.equalsIgnoreCase("name")) folderName = value; if (aname.equalsIgnoreCase("visible")) isVisible = value.equalsIgnoreCase("yes") ? true : false; } } class VirtualFolderNode extends DefaultMutableTreeNode { private boolean isLeaf; private String filePath, label; private boolean isVisible = false; VirtualFolderNode(String filePath) { this(filePath, true); } VirtualFolderNode(String filePath, boolean isLeaf) { super(); this.filePath = filePath; this.isLeaf = isLeaf; int index = filePath.lastIndexOf(java.io.File.separator); if (index != -1) label = filePath.substring(index + 1); else label = filePath; } public void ensureVisible(boolean isVisible) { this.isVisible = isVisible; } public boolean shouldBeVisible() { return isVisible; } public String getFilePath() { return filePath; } public boolean isLeaf() { return isLeaf; } public String toString() { return label; } } class KeyHandler extends KeyAdapter { public void keyPressed(KeyEvent evt) { if (evt.getKeyCode() == KeyEvent.VK_ENTER) openSelection(false); } } class DnDHandler implements DropTargetListener { public void dragEnter(DropTargetDragEvent evt) { } public void dragOver(DropTargetDragEvent evt) { Point p = evt.getLocation(); TreePath path = tree.getPathForLocation(p.x, p.y); tree.setSelectionPath(path); tree.expandPath(path); } public void dragExit(DropTargetEvent evt) { } public void dragScroll(DropTargetDragEvent evt) { } public void dropActionChanged(DropTargetDragEvent evt) { } public void drop(DropTargetDropEvent evt) { DataFlavor[] flavors = evt.getCurrentDataFlavors(); if (flavors == null) return; boolean dropCompleted = false; for (int i = flavors.length - 1; i >= 0; i--) { if (flavors[i].isFlavorJavaFileListType()) { evt.acceptDrop(DnDConstants.ACTION_COPY); Transferable transferable = evt.getTransferable(); try { Iterator iterator = ((java.util.List) transferable.getTransferData(flavors[i])).iterator(); while (iterator.hasNext()) addFile(((File) iterator.next()).getPath()); dropCompleted = true; } catch (Exception e) { } } } evt.dropComplete(dropCompleted); } }}// End of VirtualFolders.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -