Helle,我想查看JTree中存储库的内容。为了获得更好的性能,我只是在扩展节点时才懒惰地加载子节点。这是有效的,唯一的问题是甚至"文件"显示为"文件夹"。
节点是文件(叶子)或文件夹(可扩展节点)的标准是
o.getType().getBaseTypeId().toString().equals("CMIS_FOLDER")
整个代码:
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.Session;
import at.sviss.cmis.SessionHelper;
import at.sviss.gui.dialogs.OutputDialogs;
public class LazyLoadingTree extends DefaultMutableTreeNode {
private static final long serialVersionUID = 1L;
private Email mail = new Email();
private OutputDialogs out = new OutputDialogs();
private boolean loaded = false;
private static SessionHelper sessionHelper;
private static String currentDirectory = "/";
public LazyLoadingTree(String name, boolean hasChildren) {
add(new DefaultMutableTreeNode("Lade ...", false));
setAllowsChildren(hasChildren);
setUserObject(name);
}
public void setSession (SessionHelper sessionHelper) {
LazyLoadingTree.sessionHelper = sessionHelper;
}
private void setChildren(List<LazyLoadingTree> children) {
removeAllChildren();
setAllowsChildren(getAllowsChildren());
for (MutableTreeNode node : children) {
add(node);
}
loaded = true;
}
@Override
public boolean isLeaf() {
return false;
}
public void loadChildren(final DefaultTreeModel model, final PropertyChangeListener progressListener) {
if (loaded) {
return;
}
SwingWorker<List<LazyLoadingTree>, Void> worker = new SwingWorker<List<LazyLoadingTree>, Void>() {
@Override
protected List<LazyLoadingTree> doInBackground() throws Exception {
Session session = sessionHelper.getSession();
CmisObject cmisObject = session.getObjectByPath(LazyLoadingTree.currentDirectory);
Folder folder = (Folder)cmisObject;
ItemIterable<CmisObject> items = folder.getChildren();
List<LazyLoadingTree> children = new ArrayList<LazyLoadingTree>();
for (CmisObject o : items)
children.add(new LazyLoadingTree(o.getName(), o.getType().getBaseTypeId().toString().equals("CMIS_FOLDER")));
return children;
}
@Override
protected void done() {
try {
setChildren(get());
model.nodeStructureChanged(LazyLoadingTree.this);
} catch (Exception e) {
e.printStackTrace();
}
super.done();
}
};
if (progressListener != null) {
worker.getPropertyChangeSupport().addPropertyChangeListener("progress", progressListener);
}
worker.execute();
}
public JTree init() {
LazyLoadingTree root = new LazyLoadingTree("Alfresco", true);
final DefaultTreeModel model = new DefaultTreeModel(root);
final JProgressBar bar = new JProgressBar();
final PropertyChangeListener progressListener = new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
bar.setValue((Integer) evt.getNewValue());
}
};
final JTree tree = new JTree();
tree.setShowsRootHandles(true);
tree.addTreeWillExpandListener(new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent event) throws ExpandVetoException {
TreePath path = event.getPath();
if (path.getLastPathComponent() instanceof LazyLoadingTree) {
LazyLoadingTree.currentDirectory = mod(path.toString());
LazyLoadingTree node = (LazyLoadingTree) path.getLastPathComponent();
node.loadChildren(model, progressListener);
}
}
@Override
public void treeWillCollapse(TreeExpansionEvent event) throws ExpandVetoException { }
});
tree.setModel(model);
tree.setRootVisible(false);
try {
ImageIcon folderIcon = new ImageIcon(getClass().getClassLoader().getResource("images/folder-16.png"));
ImageIcon documentIcon = new ImageIcon(getClass().getClassLoader().getResource("images/doclib-16.png"));
if (folderIcon != null && documentIcon != null) {
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setClosedIcon(folderIcon);
renderer.setOpenIcon(folderIcon);
renderer.setLeafIcon(documentIcon);
tree.setCellRenderer(renderer);
}
} catch (Exception x) {
x.printStackTrace();
}
MouseListener ml = ( new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
if (SwingUtilities.isRightMouseButton(e))
{
final TreePath treepath = tree.getPathForLocation ( e.getX (), e.getY () );
Rectangle pathBounds = tree.getUI ().getPathBounds ( tree, treepath );
final TreePath path = tree.getSelectionPath();
if (path != null) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
if (node.isLeaf()) {
if (pathBounds != null && pathBounds.contains(e.getX(), e.getY())) {
JPopupMenu menu = new JPopupMenu ();
JMenuItem menuItemEmail = new JMenuItem("E-Mail");
menuItemEmail.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
mail.attache(sessionHelper, mod(path.toString()));
}
});
JMenuItem menuItemDetails = new JMenuItem("Details");
menuItemDetails.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
out.nodeDetails(sessionHelper.getDetails(mod(path.toString())), null);
}
});
menu.add(menuItemEmail);
menu.add(menuItemDetails);
menu.show(tree,pathBounds.x, pathBounds.y + pathBounds.height );
}
} else {
}
}
}
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
});
tree.addMouseListener(ml);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
root.loadChildren(model, progressListener);
return tree;
}
private String mod(String str) {
str = str.replace(", ", "/");
str = str.replace("[", "");
str = str.replace("]", "");
str = str.replace("Alfresco", "");
return str;
}
}