📄 htmltreenode.java
字号:
/* * Copyright 2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.myfaces.custom.tree;import org.apache.myfaces.custom.tree.model.TreeModel;import org.apache.myfaces.custom.tree.model.TreePath;import javax.faces.component.html.HtmlCommandLink;import javax.faces.context.FacesContext;import java.util.Iterator;import java.util.List;/** * Represents a single node of a three. A custom html link component representing the expand/collapse icon * is held as a facet named <code>expandCollapse</code>. * * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a> * @version $Revision: 1.12 $ $Date: 2004/11/26 12:46:38 $ * * $Log: HtmlTreeNode.java,v $ * Revision 1.12 2004/11/26 12:46:38 oros * cleanup: removed unused iconChild attribute * * Revision 1.11 2004/11/26 12:14:10 oros * MYFACES-8: applied tree table patch by David Le Strat * */public class HtmlTreeNode extends HtmlCommandLink{ private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.HtmlTreeNode"; public static final String COMPONENT_TYPE = "org.apache.myfaces.HtmlTreeNode"; public static final String EXPAND_COLLAPSE_FACET = "expandCollapse"; public static final int OPEN = 0; public static final int OPEN_FIRST = 1; public static final int OPEN_LAST = 2; public static final int OPEN_SINGLE = 3; public static final int CLOSED = 10; public static final int CLOSED_FIRST = 11; public static final int CLOSED_LAST = 12; public static final int CLOSED_SINGLE = 13; public static final int CHILD = 20; public static final int CHILD_FIRST = 21; public static final int CHILD_LAST = 22; public static final int LINE = 30; public static final int EMPTY = 40; private static final int OFFSET = 10; private static final int MOD_FIRST = 1; private static final int MOD_LAST = 2; private transient TreePath path; private int[] translatedPath; private transient Object userObject; private boolean expanded = false; private boolean selected = false; private int[] layout; public HtmlTreeNode() { setRendererType(DEFAULT_RENDERER_TYPE); } public int getLevel() { return layout.length - 1; } public int getMaxChildLevel() { if (getChildCount() == 0) { return getLevel(); } int max = getLevel(); for (Iterator iterator = getChildren().iterator(); iterator.hasNext();) { HtmlTreeNode child = (HtmlTreeNode) iterator.next(); max = Math.max(max, child.getMaxChildLevel()); } return max; } public TreePath getPath() { if (path == null) { if (translatedPath == null) { throw new IllegalStateException("No path and no translated path available"); } path = translatePath(translatedPath, getTreeModel(FacesContext.getCurrentInstance())); } return path; } public void setPath(TreePath path) { this.path = path; } int[] getTranslatedPath() { if (translatedPath != null) { return translatedPath; } ; return translatePath(getPath(), getTreeModel(FacesContext.getCurrentInstance())); } public Object getUserObject() { if (userObject == null) { userObject = getPath().getLastPathComponent(); } return userObject; } public void setUserObject(Object userObject) { this.userObject = userObject; setValue(userObject.toString()); } public boolean isExpanded() { return expanded; } void childrenAdded(int[] children, FacesContext context) { if (getChildCount() == 0 && children.length > 0) { // change to CLOSED_* layout[layout.length - 1] -= OFFSET; } if (!expanded) { // nothing to do return; } TreeModel model = getTreeModel(context); int childCount = model.getChildCount(getUserObject()); int pathUpdateIndex = getTranslatedPath().length; for (int i = 0; i < children.length; i++) { int index = children[i]; translateChildrenPath(pathUpdateIndex, index, 1); Object userObject = model.getChild(getUserObject(), index); addNode(model, userObject, index, childCount, context); } } void childrenChanged(int[] children, FacesContext context) { if (!expanded) { // nothing to do return; } TreeModel model = getTreeModel(context); for (int i = 0; i < children.length; i++) { int index = children[i]; Object userObject = model.getChild(getUserObject(), index); HtmlTreeNode node = (HtmlTreeNode) getChildren().get(index); node.setUserObject(userObject); // todo: modify path???? } } private void addNode(TreeModel model, Object userObject, int index, int childCount, FacesContext context) { HtmlTreeNode node = createNode(model, userObject, childCount, index, context); List children = getChildren(); if (!children.isEmpty()) { if (index == 0) { HtmlTreeNode first = (HtmlTreeNode) getChildren().get(0); int[] layout = first.getLayout(); if (layout[layout.length - 1] % OFFSET == MOD_FIRST) { // change from *_FIRST to * layout[layout.length - 1]--; } } else if (index == childCount - 1) { HtmlTreeNode last = (HtmlTreeNode) getChildren().get(childCount - 2); int[] layout = last.getLayout(); if (layout[layout.length - 1] % OFFSET == MOD_LAST) { // change from *_LAST to * layout[layout.length - 1] -= 2; } } } children.add(index, node); } void childrenRemoved(int[] children) { if (!expanded) { // nothing to do return; } List childNodes = getChildren(); int pathUpdateIndex = getTranslatedPath().length; for (int i = children.length - 1; i >= 0; i--) { translateChildrenPath(pathUpdateIndex, children[i], -1); HtmlTreeNode child = (HtmlTreeNode) childNodes.get(children[i]); if (child.isSelected()) { child.setSelected(false); if (children[i] < childNodes.size() - 1) { ((HtmlTreeNode) childNodes.get(children[i] + 1)).setSelected(true); } else { if (children[i] > 0) { ((HtmlTreeNode) childNodes.get(children[i] - 1)).setSelected(true); } else { setSelected(true); } } } childNodes.remove(children[i]); } } /** * Update the translatedPath of all child nodes so the path points to the same object in the model * after adding/removing a node * * @param pathUpdateIndex * @param startIndex * @param offset */ private void translateChildrenPath(int pathUpdateIndex, int startIndex, int offset) { List children = getChildren(); int childrenSize = children.size(); for (int i = startIndex; i < childrenSize; i++) { HtmlTreeNode node = (HtmlTreeNode) children.get(i); node.updateTranslatedPath(pathUpdateIndex, offset); } } private void updateTranslatedPath(int pathUpdateIndex, int offset) { translatedPath[pathUpdateIndex] += offset; // reset path and userObject so the values are acquired from the model path = null; userObject = null; if (isExpanded()) { // continue with the children of this node
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -