1 11 package org.eclipse.jface.preference; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.StringTokenizer ; 16 17 import org.eclipse.core.runtime.Assert; 18 19 23 public class PreferenceManager { 24 28 public static final int PRE_ORDER = 0; 29 30 33 public static final int POST_ORDER = 1; 34 35 41 PreferenceNode root = new PreferenceNode(""); 43 46 String separator; 47 48 51 public PreferenceManager() { 52 this('.'); 53 } 54 55 61 public PreferenceManager(char separatorChar) { 62 separator = new String (new char[] { separatorChar }); 63 } 64 65 75 public boolean addTo(String path, IPreferenceNode node) { 76 IPreferenceNode target = find(path); 77 if (target == null) { 78 return false; 79 } 80 target.add(node); 81 return true; 82 } 83 84 91 public void addToRoot(IPreferenceNode node) { 92 Assert.isNotNull(node); 93 root.add(node); 94 } 95 96 107 protected void buildSequence(IPreferenceNode node, List sequence, int order) { 108 if (order == PRE_ORDER) { 109 sequence.add(node); 110 } 111 IPreferenceNode[] subnodes = node.getSubNodes(); 112 for (int i = 0; i < subnodes.length; i++) { 113 buildSequence(subnodes[i], sequence, order); 114 } 115 if (order == POST_ORDER) { 116 sequence.add(node); 117 } 118 } 119 120 126 public IPreferenceNode find(String path) { 127 return find(path,root); 128 } 129 130 140 protected IPreferenceNode find(String path,IPreferenceNode top){ 141 Assert.isNotNull(path); 142 StringTokenizer stok = new StringTokenizer (path, separator); 143 IPreferenceNode node = top; 144 while (stok.hasMoreTokens()) { 145 String id = stok.nextToken(); 146 node = node.findSubNode(id); 147 if (node == null) { 148 return null; 149 } 150 } 151 if (node == top) { 152 return null; 153 } 154 return node; 155 } 156 157 167 public List getElements(int order) { 168 Assert.isTrue(order == PRE_ORDER || order == POST_ORDER, 169 "invalid traversal order"); ArrayList sequence = new ArrayList (); 171 IPreferenceNode[] subnodes = getRoot().getSubNodes(); 172 for (int i = 0; i < subnodes.length; i++) { 173 buildSequence(subnodes[i], sequence, order); 174 } 175 return sequence; 176 } 177 178 186 protected IPreferenceNode getRoot() { 187 return root; 188 } 189 190 196 public final IPreferenceNode[] getRootSubNodes() { 197 return getRoot().getSubNodes(); 198 } 199 200 208 public IPreferenceNode remove(String path) { 209 Assert.isNotNull(path); 210 int index = path.lastIndexOf(separator); 211 if (index == -1) { 212 return root.remove(path); 213 } 214 Assert.isTrue(index < path.length() - 1, "Path can not end with a dot"); String parentPath = path.substring(0, index); 217 String id = path.substring(index + 1); 218 IPreferenceNode parentNode = find(parentPath); 219 if (parentNode == null) { 220 return null; 221 } 222 return parentNode.remove(id); 223 } 224 225 233 public boolean remove(IPreferenceNode node) { 234 Assert.isNotNull(node); 235 236 return root.remove(node); 237 } 238 239 242 public void removeAll() { 243 root = new PreferenceNode(""); } 245 } 246 | Popular Tags |