📄 treeupdater.java
字号:
/*
Copyright 2004 Jenkov Development
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 com.jenkov.prizetags.tree.impl;
import com.jenkov.prizetags.tree.itf.ITree;
import javax.servlet.http.HttpServletRequest;
/**
* The TreeUpdater updates an ITree instance based on the request parameters in
* the current request. If your tree parameters are prefixed remember to set the
* parameter prefix on the updater instance.
*
* <br/><br/>
* If some of your tree request parameters
* does not have standard names
* remember to change the name of the request parameter by calling the appropriate
* setExpand / setCollapse / setSelect / setUnSelect etc. methods with the name.
* For instance if you use "expandNode" instead of "expand", call setExpand("expandNode")
* on the TreeUpdater before calling update(). Do not include the tree parameter
* prefix in this name.
*
*
*
* @author Jakob Jenkov - Copyright 2005 Jenkov Development
*/
public class TreeUpdater {
protected HttpServletRequest request = null;
protected ITree tree = null;
protected String paramPrefix = "";
protected String expandParam = "expand";
protected String expandAllParam = "all";
protected String expandAncestorsParam = "expandAncestors";
protected String expandAncestorsAndSelfParam = "expandAncestorsAndSelf";
protected String expandDescendantsParam = "expandDescendants";
protected String expandDescendantsAndSelfParam = "expandDescendantsAndSelf";
protected String expandParentParam = "expandParent";
protected String expandParentAndSelfParam = "expandParentAndSelf";
protected String expandChildrenParam = "expandChildren";
protected String expandChildrenAndSelfParam = "expandChildrenAndSelf";
protected String collapseParam = "collapse";
protected String collapseAllParam = "all";
protected String collapseAncestorsParam = "collapseAncestors";
protected String collapseAncestorsAndSelfParam = "collapseAncestorsAndSelf";
protected String collapseDescendantsParam = "collapseDescendants";
protected String collapseDescendantsAndSelfParam = "collapseDescendantsAndSelf";
protected String collapseParentParam = "collapseParent";
protected String collapseParentAndSelfParam = "collapseParentAndSelf";
protected String collapseChildrenParam = "collapseChildren";
protected String collapseChildrenAndSelfParam = "collapseChildrenAndSelf";
protected String selectParam = "select";
protected String selectOnlyParam = "selectOnly";
protected String selectAncestorsParam = "selectAncestors";
protected String selectAncestorsAndSelfParam = "selectAncestorsAndSelf";
protected String selectDescendantsParam = "selectDescendants";
protected String selectDescendantsAndSelfParam = "selectDescendantsAndSelf";
protected String selectParentParam = "selectParent";
protected String selectParentAndSelfParam = "selectParentAndSelf";
protected String selectChildrenParam = "selectChildren";
protected String selectChildrenAndSelfParam = "selectChildrenAndSelf";
protected String unSelectParam = "unSelect";
protected String unSelectAncestorsParam = "unSelectAncestors";
protected String unSelectAncestorsAndSelfParam = "unSelectAncestorsAndSelf";
protected String unSelectDescendantsParam = "unSelectDescendants";
protected String unSelectDescendantsAndSelfParam = "unSelectDescendantsAndSelf";
protected String unSelectParentParam = "unSelectParent";
protected String unSelectParentAndSelfParam = "unSelectParentAndSelf";
protected String unSelectChildrenParam = "unSelectChildren";
protected String unSelectChildrenAndSelfParam = "unSelectChildrenAndSelf";
public TreeUpdater(HttpServletRequest request, ITree tree) {
this.request = request;
this.tree = tree;
}
public TreeUpdater(HttpServletRequest request, ITree tree, String paramPrefix) {
this.request = request;
this.tree = tree;
this.paramPrefix = paramPrefix;
}
/**
* Updates the expand, collapse, select and unselect status of the tree depending on the
* request parameters.
*
* <br/><br/>
* The updater attaches itself to the request as a request attribute
* so the tree tag knows not to repeat the update in case this update was
* executed in a Struts action, Spring controller, Servlet or other (model 2) component
* that executes before the JSP with the tree tag does. The key used to attach the
* updater to the request attributes is the tree param prefix. That
* way if you have several trees on a page their updaters will not conflict. Each
* tree will have it's own tree param prefix anyways.
*/
public void update(){
expandNodes();
collapseNodes();
selectUnselectNodes();
unSelectNodes();
request.setAttribute(getParamPrefix() + ".TreeUpdater", this);
}
protected void expandNodes() {
String[] expandId = request.getParameterValues(getParamPrefix() + expandParam);
if(expandId != null){
for(int i=0; i< expandId.length; i++){
if ((getExpandAllParam()).equals(expandId[i])) tree.expandAll();
else tree.expand(expandId[i]);
}
}
String expandAncestorIds[] = request.getParameterValues(getParamPrefix() + expandAncestorsParam);
if(expandAncestorIds!= null){
for(int i=0; i< expandAncestorIds.length; i++){
tree.expandAncestors(expandAncestorIds[i]);
}
}
String expandAncestorAndSelfIds[] = request.getParameterValues(getParamPrefix() + expandAncestorsAndSelfParam);
if(expandAncestorAndSelfIds != null){
for(int i=0; i< expandAncestorAndSelfIds.length; i++){
tree.expandAncestorsAndSelf(expandAncestorAndSelfIds[i]);
}
}
String expandDescendantsIds[] = request.getParameterValues(getParamPrefix() + expandDescendantsParam);
if(expandDescendantsIds != null){
for(int i=0; i< expandDescendantsIds.length; i++){
tree.expandDescendants(expandDescendantsIds[i]);
}
}
String expandDescendantsAndSelfIds[] = request.getParameterValues(getParamPrefix() + expandDescendantsAndSelfParam);
if(expandDescendantsAndSelfIds != null){
for(int i=0; i< expandDescendantsAndSelfIds.length; i++){
tree.expandDescendantsAndSelf(expandDescendantsAndSelfIds[i]);
}
}
String expandParentIds[] = request.getParameterValues(getParamPrefix() + expandParentParam);
if(expandParentIds != null){
for(int i=0; i< expandParentIds.length; i++){
tree.expandParent(expandParentIds[i]);
}
}
String expandParentAndSelfIds[] = request.getParameterValues(getParamPrefix() + expandParentAndSelfParam);
if(expandParentAndSelfIds != null){
for(int i=0; i< expandParentAndSelfIds.length; i++){
tree.expandParentAndSelf(expandParentAndSelfIds[i]);
}
}
String expandChildrenIds[] = request.getParameterValues(getParamPrefix() + expandChildrenParam);
if(expandChildrenIds != null){
for(int i=0; i< expandChildrenIds.length; i++){
tree.expandChildren(expandChildrenIds[i]);
}
}
String expandChildrenAndSelfIds[] = request.getParameterValues(getParamPrefix() + expandChildrenAndSelfParam);
if(expandChildrenAndSelfIds != null){
for(int i=0; i< expandChildrenAndSelfIds.length; i++){
tree.expandChildrenAndSelf(expandChildrenAndSelfIds[i]);
}
}
}
private void collapseNodes() {
String collapseId[] = request.getParameterValues(getParamPrefix() + collapseParam);
if(collapseId != null){
for(int i=0; i<collapseId.length; i++){
if((getCollapseAllParam()).equals(collapseId[i])) tree.collapseAll();
else tree.collapse(collapseId[i]);
}
}
String collapseAncestorIds[] = request.getParameterValues(getParamPrefix() + collapseAncestorsParam);
if(collapseAncestorIds!= null){
for(int i=0; i< collapseAncestorIds.length; i++){
tree.collapseAncestors(collapseAncestorIds[i]);
}
}
String collapseAncestorAndSelfIds[] = request.getParameterValues(getParamPrefix() + collapseAncestorsAndSelfParam);
if(collapseAncestorAndSelfIds != null){
for(int i=0; i< collapseAncestorAndSelfIds.length; i++){
tree.collapseAncestorsAndSelf(collapseAncestorAndSelfIds[i]);
}
}
String collapseDescendantsIds[] = request.getParameterValues(getParamPrefix() + collapseDescendantsParam);
if(collapseDescendantsIds != null){
for(int i=0; i< collapseDescendantsIds.length; i++){
tree.collapseDescendants(collapseDescendantsIds[i]);
}
}
String collapseDescendantsAndSelfIds[] = request.getParameterValues(getParamPrefix() + collapseDescendantsAndSelfParam);
if(collapseDescendantsAndSelfIds != null){
for(int i=0; i< collapseDescendantsAndSelfIds.length; i++){
tree.collapseDescendantsAndSelf(collapseDescendantsAndSelfIds[i]);
}
}
String collapseParentIds[] = request.getParameterValues(getParamPrefix() + collapseParentParam);
if(collapseParentIds != null){
for(int i=0; i< collapseParentIds.length; i++){
tree.collapseParent(collapseParentIds[i]);
}
}
String collapseParentAndSelfIds[] = request.getParameterValues(getParamPrefix() + collapseParentAndSelfParam);
if(collapseParentAndSelfIds != null){
for(int i=0; i< collapseParentAndSelfIds.length; i++){
tree.collapseParentAndSelf(collapseParentAndSelfIds[i]);
}
}
String collapseChildrenIds[] = request.getParameterValues(getParamPrefix() + collapseChildrenParam);
if(collapseChildrenIds != null){
for(int i=0; i< collapseChildrenIds.length; i++){
tree.collapseChildren(collapseChildrenIds[i]);
}
}
String collapseChildrenAndSelfIds[] = request.getParameterValues(getParamPrefix() + collapseChildrenAndSelfParam);
if(collapseChildrenAndSelfIds != null){
for(int i=0; i< collapseChildrenAndSelfIds.length; i++){
tree.collapseChildrenAndSelf(collapseChildrenAndSelfIds[i]);
}
}
}
protected void selectUnselectNodes() {
String[] selectIds = request.getParameterValues(getParamPrefix() + selectParam);
String[] selectOnlyIds = request.getParameterValues(getParamPrefix() + selectOnlyParam );
if(selectOnlyIds != null){
tree.selectOnly(selectOnlyIds);
}
if(selectIds != null){
tree.select(selectIds);
}
String selectAncestorsIds[] = request.getParameterValues(getParamPrefix() + selectAncestorsParam);
if(selectAncestorsIds != null){
for(int i=0; i< selectAncestorsIds.length; i++){
tree.selectAncestors(selectAncestorsIds[i]);
}
}
String selectAncestorsAndSelfIds[] = request.getParameterValues(getParamPrefix() + selectAncestorsAndSelfParam);
if(selectAncestorsAndSelfIds != null){
for(int i=0; i< selectAncestorsAndSelfIds.length; i++){
tree.selectAncestorsAndSelf(selectAncestorsAndSelfIds[i]);
}
}
String selectDescendantsIds[] = request.getParameterValues(getParamPrefix() + selectDescendantsParam);
if(selectDescendantsIds != null){
for(int i=0; i< selectDescendantsIds.length; i++){
tree.selectDescendants(selectDescendantsIds[i]);
}
}
String selectDescendantsAndSelfIds[] = request.getParameterValues(getParamPrefix() + selectDescendantsAndSelfParam);
if(selectDescendantsAndSelfIds != null){
for(int i=0; i< selectDescendantsAndSelfIds.length; i++){
tree.selectDescendantsAndSelf(selectDescendantsAndSelfIds[i]);
}
}
String selectParentIds[] = request.getParameterValues(getParamPrefix() + selectParentParam);
if(selectParentIds != null){
for(int i=0; i< selectParentIds.length; i++){
tree.selectParent(selectParentIds[i]);
}
}
String selectParentAndSelfIds[] = request.getParameterValues(getParamPrefix() + selectParentAndSelfParam);
if(selectParentAndSelfIds != null){
for(int i=0; i< selectParentAndSelfIds.length; i++){
tree.selectParentAndSelf(selectParentAndSelfIds[i]);
}
}
String selectChildrenIds[] = request.getParameterValues(getParamPrefix() + selectChildrenParam);
if(selectChildrenIds != null){
for(int i=0; i< selectChildrenIds.length; i++){
tree.selectChildren(selectChildrenIds[i]);
}
}
String selectChildrenAndSelfIds[] = request.getParameterValues(getParamPrefix() + selectChildrenAndSelfParam);
if(selectChildrenAndSelfIds != null){
for(int i=0; i< selectChildrenAndSelfIds.length; i++){
tree.selectChildrenAndSelf(selectChildrenAndSelfIds[i]);
}
}
}
private void unSelectNodes() {
String unselectId = request.getParameter (getParamPrefix() + unSelectParam);
if(unselectId != null){
tree.unSelect(unselectId);
}
String unSelectAncestorsIds[] = request.getParameterValues(getParamPrefix() + unSelectAncestorsParam);
if(unSelectAncestorsIds != null){
for(int i=0; i< unSelectAncestorsIds.length; i++){
tree.unSelectAncestors(unSelectAncestorsIds[i]);
}
}
String unSelectAncestorsAndSelfIds[] = request.getParameterValues(getParamPrefix() + unSelectAncestorsAndSelfParam);
if(unSelectAncestorsAndSelfIds != null){
for(int i=0; i< unSelectAncestorsAndSelfIds.length; i++){
tree.unSelectAncestorsAndSelf(unSelectAncestorsAndSelfIds[i]);
}
}
String unSelectDescendantsIds[] = request.getParameterValues(getParamPrefix() + unSelectDescendantsParam);
if(unSelectDescendantsIds != null){
for(int i=0; i< unSelectDescendantsIds.length; i++){
tree.unSelectDescendants(unSelectDescendantsIds[i]);
}
}
String unSelectDescendantsAndSelfIds[] = request.getParameterValues(getParamPrefix() + unSelectDescendantsAndSelfParam);
if(unSelectDescendantsAndSelfIds != null){
for(int i=0; i< unSelectDescendantsAndSelfIds.length; i++){
tree.unSelectDescendantsAndSelf(unSelectDescendantsAndSelfIds[i]);
}
}
String unSelectParentIds[] = request.getParameterValues(getParamPrefix() + unSelectParentParam);
if(unSelectParentIds != null){
for(int i=0; i< unSelectParentIds.length; i++){
tree.unSelectParent(unSelectParentIds[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -