001 package org.apache.myfaces.tobago.model; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import javax.swing.tree.DefaultMutableTreeNode; 021 022 import org.apache.commons.lang.StringUtils; 023 024 import java.util.Enumeration; 025 import java.util.HashSet; 026 import java.util.Set; 027 028 /** 029 * Manages the state on a Tree:<br /> 030 * 1. selection: selected tree-nodes<br /> 031 * 2. expandState: open/close folder state<br /> 032 * 3. marker: last used action object<br /> 033 */ 034 public class TreeState { 035 036 public static final String SEP = ";"; 037 038 private Set<DefaultMutableTreeNode> selection; 039 private Set<DefaultMutableTreeNode> expandState; 040 private DefaultMutableTreeNode marker; 041 private DefaultMutableTreeNode lastMarker; 042 private String lastCommand; 043 private Integer[] scrollPosition; 044 045 public TreeState() { 046 selection = new HashSet<DefaultMutableTreeNode>(); 047 expandState = new HashSet<DefaultMutableTreeNode>(); 048 } 049 050 public void addExpandState(DefaultMutableTreeNode expandStateItem) { 051 expandState.add(expandStateItem); 052 } 053 054 public void addSelection(DefaultMutableTreeNode selectItem) { 055 selection.add(selectItem); 056 } 057 058 public void clearExpandState() { 059 expandState.clear(); 060 } 061 062 public void clearSelection() { 063 selection.clear(); 064 } 065 066 /** 067 * Adds a (external created) node to the actually marked node. 068 */ 069 public void commandNew(DefaultMutableTreeNode newNode) { 070 marker.insert(newNode, 0); 071 setLastMarker(null); 072 setLastCommand(null); 073 } 074 075 public void expand(DefaultMutableTreeNode node, int level) { 076 if (level > 0) { 077 if (!expandState.contains(node)) { 078 expandState.add(node); 079 } 080 for (Enumeration i = node.children(); i.hasMoreElements();) { 081 DefaultMutableTreeNode child = (DefaultMutableTreeNode) i.nextElement(); 082 expand(child, level - 1); 083 } 084 } 085 } 086 087 /** Expands all parents which contains selected children. */ 088 public void expandSelection() { 089 for (DefaultMutableTreeNode treeNode : selection) { 090 expandTo(treeNode); 091 } 092 } 093 094 public void expandTo(DefaultMutableTreeNode node) { 095 node = (DefaultMutableTreeNode) node.getParent(); 096 while (node != null) { 097 if (!expandState.contains(node)) { 098 expandState.add(node); 099 } 100 node = (DefaultMutableTreeNode) node.getParent(); 101 } 102 } 103 104 public boolean isExpanded(DefaultMutableTreeNode node) { 105 return expandState.contains(node); 106 } 107 108 public boolean isMarked(DefaultMutableTreeNode node) { 109 return node != null && node.equals(marker); 110 } 111 112 public boolean isSelected(DefaultMutableTreeNode node) { 113 return selection.contains(node); 114 } 115 116 public Set<DefaultMutableTreeNode> getExpandState() { 117 return expandState; 118 } 119 120 public void setExpandState(Set<DefaultMutableTreeNode> expandState) { 121 this.expandState = expandState; 122 } 123 124 public String getLastCommand() { 125 return lastCommand; 126 } 127 128 public void setLastCommand(String lastCommand) { 129 this.lastCommand = lastCommand; 130 } 131 132 public DefaultMutableTreeNode getLastMarker() { 133 return lastMarker; 134 } 135 136 public void setLastMarker(DefaultMutableTreeNode lastMarker) { 137 this.lastMarker = lastMarker; 138 } 139 140 public DefaultMutableTreeNode getMarker() { 141 return marker; 142 } 143 144 public void setMarker(DefaultMutableTreeNode marker) { 145 this.marker = marker; 146 } 147 148 public Set<DefaultMutableTreeNode> getSelection() { 149 return selection; 150 } 151 152 public void setSelection(Set<DefaultMutableTreeNode> selection) { 153 this.selection = selection; 154 } 155 156 public Integer[] getScrollPosition() { 157 return scrollPosition; 158 } 159 160 public void setScrollPosition(Integer[] scrollPosition) { 161 this.scrollPosition = scrollPosition; 162 } 163 164 public static Integer[] parseScrollPosition(String value) { 165 Integer[] position = null; 166 if (!StringUtils.isBlank(value)) { 167 int sep = value.indexOf(";"); 168 if (sep == -1) { 169 throw new NumberFormatException(value); 170 } 171 int left = Integer.parseInt(value.substring(0, sep)); 172 int top = Integer.parseInt(value.substring(sep + 1)); 173 position = new Integer[2]; 174 position[0] = left; 175 position[1] = top; 176 } 177 return position; 178 } 179 180 } 181