001: /*
002: *
003: * (c) Copyright 2004 - 2007 osbl development team.
004: *
005: * This file is part of the osbl (http://osbl.wilken.de).
006: *
007: * the osbl is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2.1
010: * of the License, or (at your option) any later version.
011: *
012: * Please see COPYING for the complete licence.
013: */
014: package org.osbl.client.wings.navigation;
015:
016: import javax.swing.tree.*;
017: import javax.swing.*;
018:
019: import org.osbl.client.wings.shell.Client;
020: import org.osbl.client.action.ActionProvider;
021: import org.osbl.client.ClientResourceProvider;
022:
023: import java.util.*;
024:
025: /**
026: * @author hengels
027: * @version $Revision: 1.6 $
028: */
029: public class NavigationStructure {
030: private static NavigationStructure INSTANCE = new NavigationStructure();
031:
032: Map<String, List<String>> categories = new LinkedHashMap<String, List<String>>();
033:
034: public static NavigationStructure getInstance() {
035: return INSTANCE;
036: }
037:
038: private NavigationStructure() {
039: }
040:
041: public DefaultMutableTreeNode generateTree() {
042: ActionProvider actionProvider = Client.getInstance()
043: .getActionProvider();
044: ClientResourceProvider resourceProvider = Client.getInstance()
045: .getResourceProvider();
046:
047: DefaultMutableTreeNode top = new DefaultMutableTreeNode(
048: "Navigation");
049:
050: Map<String, DefaultMutableTreeNode> categoryNodes = new HashMap<String, DefaultMutableTreeNode>();
051: categoryNodes.put("", top);
052:
053: for (Iterator<Map.Entry<String, List<String>>> iterator = categories
054: .entrySet().iterator(); iterator.hasNext();) {
055: Map.Entry<String, List<String>> entry = iterator.next();
056: String categoryKey = entry.getKey();
057: String categoryName = resourceProvider
058: .getMessage("category." + categoryKey);
059: DefaultMutableTreeNode categoryNode = new DefaultMutableTreeNode(
060: categoryName);
061:
062: for (Iterator<String> iterator1 = entry.getValue()
063: .iterator(); iterator1.hasNext();) {
064: String actionKey = iterator1.next();
065: Action action = actionProvider.getAction(actionKey);
066:
067: if (!action.isEnabled())
068: continue;
069:
070: DefaultMutableTreeNode actionNode = new DefaultMutableTreeNode(
071: action);
072: categoryNode.add(actionNode);
073: }
074:
075: categoryNodes.put(categoryKey, categoryNode);
076: String parentKey;
077: int pos = categoryKey.lastIndexOf('.');
078: if (pos != -1)
079: parentKey = categoryKey.substring(0, pos);
080: else
081: parentKey = "";
082:
083: DefaultMutableTreeNode parentNode = categoryNodes
084: .get(parentKey);
085: parentNode.add(categoryNode);
086: }
087:
088: List<DefaultMutableTreeNode> emptyNodes = new ArrayList<DefaultMutableTreeNode>(
089: categoryNodes.size());
090: Enumeration enumeration = top.depthFirstEnumeration();
091: outer: while (enumeration.hasMoreElements()) {
092: DefaultMutableTreeNode node = (DefaultMutableTreeNode) enumeration
093: .nextElement();
094: if (node.getUserObject() instanceof Action)
095: continue;
096: if (node.getChildCount() == 0)
097: emptyNodes.add(node);
098: else {
099: Enumeration enumeration2 = node.children();
100: while (enumeration2.hasMoreElements()) {
101: DefaultMutableTreeNode child = (DefaultMutableTreeNode) enumeration2
102: .nextElement();
103: if (!emptyNodes.contains(child))
104: continue outer;
105: }
106: emptyNodes.add(node);
107: }
108: }
109: for (DefaultMutableTreeNode emptyNode : emptyNodes)
110: emptyNode.removeFromParent();
111:
112: return top;
113: }
114:
115: public void addAction(String categoryKey, String actionKey) {
116: if (!categories.containsKey(categoryKey)) {
117: String path = "";
118: for (StringTokenizer stringTokenizer = new StringTokenizer(
119: categoryKey, "."); stringTokenizer.hasMoreTokens();) {
120: if (path.length() == 0)
121: path += stringTokenizer.nextToken();
122: else
123: path += ("." + stringTokenizer.nextToken());
124:
125: if (categories.containsKey(path))
126: continue;
127:
128: categories.put(path, new LinkedList<String>());
129: }
130: }
131: categories.get(categoryKey).add(actionKey);
132: }
133: }
|