📄 htmltree.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 java.io.IOException;import java.util.*;import javax.faces.component.UIViewRoot;import javax.faces.component.html.HtmlPanelGroup;import javax.faces.context.FacesContext;import javax.faces.el.ValueBinding;import org.apache.myfaces.component.html.util.AddResource;import org.apache.myfaces.custom.tree.event.TreeSelectionEvent;import org.apache.myfaces.custom.tree.event.TreeSelectionListener;import org.apache.myfaces.custom.tree.model.TreeModel;import org.apache.myfaces.custom.tree.model.TreeModelEvent;import org.apache.myfaces.custom.tree.model.TreeModelListener;import org.apache.myfaces.custom.tree.model.TreePath;/** * <p/> * Tree implementation based on javax.swing.JTree. * </p> * <p/> * The tree model is assigned by using a value binding named <code>model</code> * and is not stored in view state. * </p> * <p/> * A hierarchy of {@link HtmlTreeNode}objects is used to represent the current * expanded state of the tree. The root node is held as a faces named * <code>rootNode</code>. * </p> * * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller </a> * @version $Revision: 1.31 $ $Date: 2005/03/30 07:41:58 $ * <p/> * $Log: HtmlTree.java,v $ * Revision 1.31 2005/03/30 07:41:58 matzew * closed MYFACES-139. Thanks to Mathias Broekelmann * * Revision 1.30 2005/02/11 00:54:02 svieujot * Revert changes commited to the wrong branch. * * Revision 1.28 2004/12/24 14:13:16 svieujot * Upgrade the tree component to use the Extensions filter. * * Revision 1.27 2004/11/26 12:46:38 oros * cleanup: removed unused iconChild attribute * * Revision 1.26 2004/11/26 12:33:54 oros * MYFACES-9: store iconChildMiddle in component state * * Revision 1.25 2004/11/26 12:14:10 oros * MYFACES-8: applied tree table patch by David Le Strat * */public class HtmlTree extends HtmlPanelGroup implements TreeModelListener{ public static final int DEFAULT_EXPIRE_LISTENERS = 8 * 60 * 60 * 1000; // 8 hours private static final String FACET_ROOTNODE = "rootNode"; private static final String PREVIOUS_VIEW_ROOT = HtmlTree.class.getName() + ".PREVIOUS_VIEW_ROOT"; private static final int EVENT_CHANGED = 0; private static final int EVENT_INSERTED = 1; private static final int EVENT_REMOVED = 2; private static final int EVENT_STRUCTURE_CHANGED = 3; private static int counter = 0; // Defaut images private static final String DEFAULT_IMAGE_ICON_LINE = "images/line.gif"; private static final String DEFAULT_IMAGE_ICON_NOLINE = "images/noline.gif"; private static final String DEFAULT_IMAGE_ICON_CHILD_FIRST = "images/line_first.gif"; private static final String DEFAULT_IMAGE_ICON_CHILD_MIDDLE = "images/line_middle.gif"; private static final String DEFAULT_IMAGE_ICON_CHILD_LAST = "images/line_last.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_OPEN = "images/node_open.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_OPEN_FIRST = "images/node_open_first.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_OPEN_MIDDLE = "images/node_open_middle.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_OPEN_LAST = "images/node_open_last.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_CLOSE = "images/node_close.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_CLOSE_FIRST = "images/node_close_first.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_CLOSE_MIDDLE = "images/node_close_middle.gif"; private static final String DEFAULT_IMAGE_ICON_NODE_CLOSE_LAST = "images/node_close_last.gif"; private IconProvider iconProvider; private boolean itemStatesRestored = false; private String var; private String styleClass; private String nodeClass; private String rowClasses; private String columnClasses; private String selectedNodeClass; private String iconClass; private String iconLine; private String iconNoline; private String iconChildFirst; private String iconChildMiddle; private String iconChildLast; private String iconNodeOpen; private String iconNodeOpenFirst; private String iconNodeOpenMiddle; private String iconNodeOpenLast; private String iconNodeClose; private String iconNodeCloseFirst; private String iconNodeCloseMiddle; private String iconNodeCloseLast; private int uniqueIdCounter = 0; private int[] selectedPath; private int internalId; private long expireListeners = DEFAULT_EXPIRE_LISTENERS; /** * <p/> * Default constructor. * </p> */ public HtmlTree() { internalId = counter++; } public TreeModel getModel(FacesContext context) { ValueBinding binding = getValueBinding("model"); if (binding != null) { TreeModel model = (TreeModel) binding.getValue(context); if (model != null) { return model; } } return null; } public String createUniqueId(FacesContext context) { return getClientId(context).replaceAll(":", "_") + "_node_" + uniqueIdCounter++; } public void addTreeSelectionListener(TreeSelectionListener listener) { addFacesListener(listener); } public IconProvider getIconProvider() { return iconProvider; } public void setIconProvider(IconProvider iconProvider) { this.iconProvider = iconProvider; } /** * @return Returns the var. */ public String getVar() { return var; } /** * @param var The var to set. */ public void setVar(String var) { this.var = var; } public String getIconLine() { if( iconLine != null ) return iconLine; return getDefaultImagePath( DEFAULT_IMAGE_ICON_LINE ); } public void setIconLine(String iconLine) { this.iconLine = iconLine; } public String getIconNoline() { if( iconNoline != null ) return iconNoline; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NOLINE ); } public void setIconNoline(String iconNoline) { this.iconNoline = iconNoline; } public String getIconChildFirst() { if( iconChildFirst != null ) return iconChildFirst; return getDefaultImagePath( DEFAULT_IMAGE_ICON_CHILD_FIRST ); } public void setIconChildFirst(String iconChildFirst) { this.iconChildFirst = iconChildFirst; } public String getIconChildMiddle() { if( iconChildMiddle != null ) return iconChildMiddle; return getDefaultImagePath( DEFAULT_IMAGE_ICON_CHILD_MIDDLE ); } public void setIconChildMiddle(String iconChildMiddle) { this.iconChildMiddle = iconChildMiddle; } public String getIconChildLast() { if( iconChildLast != null ) return iconChildLast; return getDefaultImagePath( DEFAULT_IMAGE_ICON_CHILD_LAST ); } public void setIconChildLast(String iconChildLast) { this.iconChildLast = iconChildLast; } public String getIconNodeOpen() { if( iconNodeOpen != null ) return iconNodeOpen; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_OPEN ); } public void setIconNodeOpen(String iconNodeOpen) { this.iconNodeOpen = iconNodeOpen; } public String getIconNodeOpenFirst() { if( iconNodeOpenFirst != null ) return iconNodeOpenFirst; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_OPEN_FIRST ); } public void setIconNodeOpenFirst(String iconNodeOpenFirst) { this.iconNodeOpenFirst = iconNodeOpenFirst; } public String getIconNodeOpenMiddle() { if( iconNodeOpenMiddle != null ) return iconNodeOpenMiddle; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_OPEN_MIDDLE ); } public void setIconNodeOpenMiddle(String iconNodeOpenMiddle) { this.iconNodeOpenMiddle = iconNodeOpenMiddle; } public String getIconNodeOpenLast() { if( iconNodeOpenLast != null ) return iconNodeOpenLast; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_OPEN_LAST ); } public void setIconNodeOpenLast(String iconNodeOpenLast) { this.iconNodeOpenLast = iconNodeOpenLast; } public String getIconNodeClose() { if( iconNodeClose != null ) return iconNodeClose; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_CLOSE ); } public void setIconNodeClose(String iconNodeClose) { this.iconNodeClose = iconNodeClose; } public String getIconNodeCloseFirst() { if( iconNodeCloseFirst != null ) return iconNodeCloseFirst; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_CLOSE_FIRST ); } public void setIconNodeCloseFirst(String iconNodeCloseFirst) { this.iconNodeCloseFirst = iconNodeCloseFirst; } public String getIconNodeCloseMiddle() { if( iconNodeCloseMiddle != null ) return iconNodeCloseMiddle; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_CLOSE_MIDDLE ); } public void setIconNodeCloseMiddle(String iconNodeCloseMiddle) { this.iconNodeCloseMiddle = iconNodeCloseMiddle; } public String getIconNodeCloseLast() { if( iconNodeCloseLast != null ) return iconNodeCloseLast; return getDefaultImagePath( DEFAULT_IMAGE_ICON_NODE_CLOSE_LAST ); } public void setIconNodeCloseLast(String iconNodeCloseLast) { this.iconNodeCloseLast = iconNodeCloseLast; } public String getStyleClass() { return styleClass; } public void setStyleClass(String styleClass) { this.styleClass = styleClass; } public String getNodeClass() { return nodeClass; } public void setNodeClass(String nodeClass) { this.nodeClass = nodeClass; } /** * @return Returns the rowClasses. */ public String getRowClasses() { return rowClasses; } /** * @param rowClasses The rowClasses to set. */ public void setRowClasses(String rowClasses) { this.rowClasses = rowClasses; } /** * @return Returns the columnClasses. */ public String getColumnClasses() { return columnClasses; } /** * @param columnClasses The columnClasses to set. */ public void setColumnClasses(String columnClasses) { this.columnClasses = columnClasses; } /** * @return Returns the selectedNodeClass. */ public String getSelectedNodeClass() { return selectedNodeClass; } /** * @param selectedNodeClass The selectedNodeClass to set. */ public void setSelectedNodeClass(String selectedNodeClass) { this.selectedNodeClass = selectedNodeClass; } public String getIconClass() { return iconClass; } public void setIconClass(String iconClass) { this.iconClass = iconClass; } public long getExpireListeners() { return expireListeners; } public void setExpireListeners(long expireListeners) { this.expireListeners = expireListeners; } public String getFamily() { return "org.apache.myfaces.HtmlTree"; } /** * Ensures that the node identified by the specified path is expanded and * viewable. If the last item in the path is a leaf, this will have no * effect. * * @param path the <code>TreePath</code> identifying a node */ public void expandPath(TreePath path, FacesContext context) { // Only expand if not leaf! TreeModel model = getModel(context); if (path != null && model != null && !model.isLeaf(path.getLastPathComponent())) { int[] translatedPath = HtmlTreeNode.translatePath(path, getModel(context)); HtmlTreeNode rootNode = getRootNode(); if (rootNode == null) { createRootNode(context); rootNode = getRootNode(); } if (!rootNode.isExpanded()) { rootNode.setExpanded(true); } rootNode.expandPath(translatedPath, 0); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -