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 import java.util.Enumeration; 022 import java.util.HashSet; 023 import java.util.Iterator; 024 import java.util.Set; 025 026 /** 027 * Manages the state on a Tree:<br /> 028 * 1. selection: selected tree-nodes<br /> 029 * 2. expandState: open/close folder state<br /> 030 * 3. marker: last used action object<br /> 031 */ 032 public class TreeState { 033 034 public static final String SEP = ";"; 035 036 private Set<DefaultMutableTreeNode> selection; 037 private Set<DefaultMutableTreeNode> expandState; 038 private DefaultMutableTreeNode marker; 039 private DefaultMutableTreeNode lastMarker; 040 private String lastCommand; 041 042 public TreeState() { 043 selection = new HashSet<DefaultMutableTreeNode>(); 044 expandState = new HashSet<DefaultMutableTreeNode>(); 045 } 046 047 public void addExpandState(DefaultMutableTreeNode expandStateItem) { 048 expandState.add(expandStateItem); 049 } 050 051 public void addSelection(DefaultMutableTreeNode selectItem) { 052 selection.add(selectItem); 053 } 054 055 public void clearExpandState() { 056 expandState.clear(); 057 } 058 059 public void clearSelection() { 060 selection.clear(); 061 } 062 063 /** 064 * Adds a (external created) node to the actually marked node. 065 */ 066 public void commandNew(DefaultMutableTreeNode newNode) { 067 marker.insert(newNode, 0); 068 setLastMarker(null); 069 setLastCommand(null); 070 } 071 072 public void expand(DefaultMutableTreeNode node, int level) { 073 if (level > 0) { 074 if (!expandState.contains(node)) { 075 expandState.add(node); 076 } 077 for (Enumeration i = node.children(); i.hasMoreElements();) { 078 DefaultMutableTreeNode child = (DefaultMutableTreeNode) i.nextElement(); 079 expand(child, level - 1); 080 } 081 } 082 } 083 084 /** Expands all parents which contains selected children. */ 085 public void expandSelection() { 086 for (Iterator i = selection.iterator(); i.hasNext();) { 087 DefaultMutableTreeNode selected = (DefaultMutableTreeNode) i.next(); 088 expandTo(selected); 089 } 090 } 091 092 public void expandTo(DefaultMutableTreeNode node) { 093 node = (DefaultMutableTreeNode) node.getParent(); 094 while (node != null) { 095 if (!expandState.contains(node)) { 096 expandState.add(node); 097 } 098 node = (DefaultMutableTreeNode) node.getParent(); 099 } 100 } 101 102 public boolean isExpanded(DefaultMutableTreeNode node) { 103 return expandState.contains(node); 104 } 105 106 public boolean isMarked(DefaultMutableTreeNode node) { 107 return node != null && node.equals(marker); 108 } 109 110 public boolean isSelected(DefaultMutableTreeNode node) { 111 return selection.contains(node); 112 } 113 114 public Set<DefaultMutableTreeNode> getExpandState() { 115 return expandState; 116 } 117 118 public void setExpandState(Set<DefaultMutableTreeNode> expandState) { 119 this.expandState = expandState; 120 } 121 122 public String getLastCommand() { 123 return lastCommand; 124 } 125 126 public void setLastCommand(String lastCommand) { 127 this.lastCommand = lastCommand; 128 } 129 130 public DefaultMutableTreeNode getLastMarker() { 131 return lastMarker; 132 } 133 134 public void setLastMarker(DefaultMutableTreeNode lastMarker) { 135 this.lastMarker = lastMarker; 136 } 137 138 public DefaultMutableTreeNode getMarker() { 139 return marker; 140 } 141 142 public void setMarker(DefaultMutableTreeNode marker) { 143 this.marker = marker; 144 } 145 146 public Set<DefaultMutableTreeNode> getSelection() { 147 return selection; 148 } 149 150 public void setSelection(Set<DefaultMutableTreeNode> selection) { 151 this.selection = selection; 152 } 153 } 154