contenttree.java

来自「用Swing实现的CHM制作工具」· Java 代码 · 共 91 行

JAVA
91
字号
package g2w.app.gchm.gui;

import java.util.Iterator;
import java.util.List;


import javax.swing.JTree;
import javax.swing.tree.TreePath;

/**
 * The tree to display the content of the chm document.
 * 
 * @author GreatGhoul
 * @version 030 2009-3-20 15:13:15
 */
public class ContentTree extends JTree {
	
	/**
	 * Constructs a tree using a xml document.
	 * @param model 
	 */
	public ContentTree(ContentTreeModel model) {
		super(model);
	}
	
	public void updateModel(ContentTreeNode parent) {
		ContentElement parentElem = parent.getUserObject();
		List<ContentElement> childElems = parentElem.getChildren();
		Iterator<ContentElement> iterator = childElems.iterator();
		while (iterator.hasNext()) {
			ContentElement childElem = iterator.next();
			ContentTreeNode child = new ContentTreeNode(childElem);
			parent.add(child);
			updateModel(child);
		}
	}
	
	/**
	 * Returns the selection node.If no selection found, returns null.
	 * 
	 * @return The selection node.
	 */
	public ContentTreeNode getSelectionNode() {
		try {
			return (ContentTreeNode) getSelectionPath().getLastPathComponent();
		} catch (NullPointerException e) {
			return null;
		}
	}
	
	/**
	 * Create a new topic node with the name "新标题".
	 * 
	 * @return A new topic node.
	 */
	public static ContentTreeNode createNewTopicNode() {
		return new ContentTreeNode(ContentElement.createTopic("新标题", ""));
	}
	
	/**
	 * Set the selection.
	 * 
	 * @param node The node to select.
	 */
	public void setSelection(ContentTreeNode node) {
		try {
			ContentTreeModel model = (ContentTreeModel) getModel();
			TreePath path = new TreePath(model.getPathToRoot(node));
			setSelectionPath(path);
		} catch (Exception e) {}
	}
	
	@Override
	public void collapsePath(TreePath path) {
		try {
			ContentTreeNode node = (ContentTreeNode) path.getLastPathComponent();
			if (node.isRoot()) return;
			super.collapsePath(path);
		} catch (Exception e) {}
	}

	@Override
	public void collapseRow(int row)
	{
		TreePath path = getPathForRow(row);
		ContentTreeNode node = (ContentTreeNode) path.getLastPathComponent();
		if (node.isRoot()) return;
		super.collapseRow(row);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?