1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.Collection ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import org.eclipse.core.commands.common.NotDefinedException; 20 21 40 class SPartMenuLayout { 41 42 53 public static final String PATH_DELIMITERS = "/"; 55 74 protected static final void insertElementIntoNode( 75 final MenuElement element, final SLocation location, 76 final LeafLocationElement locationElement, LayoutNode node) 77 throws NotDefinedException { 78 final ILocationElementTokenizer tokenizer = locationElement 79 .getTokenizer(); 80 while (tokenizer.hasMoreTokens()) { 81 final LocationElementToken token = tokenizer.nextToken(); 82 node = node.getChildNode(token); 83 } 84 85 node.createChildNode(element, location); 86 } 87 88 93 private final Map barsByType; 94 95 100 private final Map popupsById; 101 102 105 protected SPartMenuLayout() { 106 this.barsByType = new HashMap (); 107 this.popupsById = new HashMap (); 108 } 109 110 125 protected final void addBar(final MenuElement element, 126 final SLocation location, final SBar bar) 127 throws NotDefinedException { 128 final String type = bar.getType(); 129 LayoutNode node = (LayoutNode) barsByType.get(type); 130 if (node == null) { 131 node = new LayoutNode(); 132 barsByType.put(type, node); 133 } 134 insertElementIntoNode(element, location, bar, node); 135 } 136 137 151 protected final void addPopup(final MenuElement element, 152 final SLocation location, final SPopup popup) 153 throws NotDefinedException { 154 final String popupId = popup.getId(); 155 LayoutNode node = (LayoutNode) popupsById.get(popupId); 156 if (node == null) { 157 node = new LayoutNode(); 158 popupsById.put(popupId, node); 159 } 160 insertElementIntoNode(element, location, popup, node); 161 } 162 163 177 public final ILayoutNode getBar(final String type) { 178 return (ILayoutNode) barsByType.get(type); 179 } 180 181 189 public final ILayoutNode getMenuBar() { 190 return (ILayoutNode) barsByType.get(SBar.TYPE_MENU); 191 } 192 193 200 protected final Map getBarsByType() { 201 return barsByType; 202 } 203 204 210 protected final Map getPopupsById() { 211 return popupsById; 212 } 213 214 226 protected final void printNode(final LayoutNode node, 227 final StringBuffer buffer, final int indent) { 228 for (int i = 0; i < indent; i++) { 229 buffer.append(' '); 230 } 231 buffer.append(node.getMenuElement()); 232 buffer.append('\n'); 233 final Collection children = node.getChildrenSorted(); 234 final Iterator childItr = children.iterator(); 235 while (childItr.hasNext()) { 236 final LayoutNode childNode = (LayoutNode) childItr.next(); 237 printNode(childNode, buffer, indent + 2); 238 } 239 } 240 241 245 public String toString() { 246 final StringBuffer buffer = new StringBuffer (); 247 Iterator entryItr; 248 249 buffer.append(" ___ top-level bars ___ \n"); entryItr = getBarsByType().entrySet().iterator(); 252 while (entryItr.hasNext()) { 253 final Map.Entry entry = (Map.Entry ) entryItr.next(); 254 final String type = (String ) entry.getKey(); 255 buffer.append(' '); 256 buffer.append(' '); 257 buffer.append(type); 258 buffer.append('\n'); 259 LayoutNode node = (LayoutNode) entry.getValue(); 260 printNode(node, buffer, 4); 261 } 262 263 buffer.append(" ___ context menus ___ \n"); entryItr = getPopupsById().entrySet().iterator(); 266 while (entryItr.hasNext()) { 267 final Map.Entry entry = (Map.Entry ) entryItr.next(); 268 final String id = (String ) entry.getKey(); 269 buffer.append(' '); 270 buffer.append(' '); 271 buffer.append(id); 272 buffer.append('\n'); 273 LayoutNode node = (LayoutNode) entry.getValue(); 274 printNode(node, buffer, 4); 275 } 276 277 return buffer.toString(); 278 } 279 } 280 | Popular Tags |