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.shell.smart;
015:
016: import org.wings.*;
017: import org.osbl.client.wings.action.NoAction;
018:
019: import javax.swing.tree.*;
020: import javax.swing.*;
021: import javax.swing.event.TreeModelListener;
022: import javax.swing.event.TreeModelEvent;
023:
024: /**
025: * @author hengels
026: * @version $Revision: 1.3 $
027: */
028: public class Menu extends SMenuBar {
029: public static final SURLIcon NO_ICON = new SURLIcon(
030: "../icon/16x16/noicon.gif", 16, 16);
031: private TreeModel model;
032: private final TreeModelListener l = new TreeModelListener() {
033: public void treeNodesChanged(TreeModelEvent e) {
034: reinitialize();
035: }
036:
037: public void treeNodesInserted(TreeModelEvent e) {
038: reinitialize();
039: }
040:
041: public void treeNodesRemoved(TreeModelEvent e) {
042: reinitialize();
043: }
044:
045: public void treeStructureChanged(TreeModelEvent e) {
046: reinitialize();
047: }
048: };
049:
050: public Menu() {
051: }
052:
053: public Menu(final TreeModel model) {
054: setModel(model);
055: }
056:
057: public void setModel(TreeModel model) {
058: if (this .model != null)
059: this .model.removeTreeModelListener(l);
060:
061: this .model = model;
062:
063: if (this .model != null)
064: this .model.addTreeModelListener(l);
065:
066: initialize();
067: }
068:
069: private void reinitialize() {
070: removeAll();
071: initialize();
072: }
073:
074: private void initialize() {
075: DefaultMutableTreeNode root = (DefaultMutableTreeNode) model
076: .getRoot();
077: for (int i = 0; i < root.getChildCount(); i++) {
078: DefaultMutableTreeNode node = (DefaultMutableTreeNode) root
079: .getChildAt(i);
080: if (node.isLeaf()) {
081: //add(createMenuItem(node));
082: } else {
083: SMenu menu = createMenu(node);
084: if (menu != null) {
085: if (i == 0) {
086: String existingStyle = menu.getStyle();
087: menu
088: .setStyle(existingStyle != null ? existingStyle
089: + " "
090: + ProcessClientStyles.FIRST_MENU
091: : ProcessClientStyles.FIRST_MENU);
092: }
093:
094: add(menu);
095: }
096: }
097: }
098: }
099:
100: SMenuItem createMenuItem(DefaultMutableTreeNode node) {
101: Action action = (Action) node.getUserObject();
102:
103: if (action.getValue(Action.SMALL_ICON) == null)
104: action.putValue(Action.SMALL_ICON, NO_ICON);
105: if (action.getValue(Action.NAME) == null)
106: action.putValue(Action.NAME, action
107: .getValue(Action.ACTION_COMMAND_KEY));
108:
109: SMenuItem item = new SMenuItem(action);
110: item.setToolTipText(node.toString());
111: KeyStroke accellerator = (KeyStroke) action
112: .getValue(Action.ACCELERATOR_KEY);
113: if (accellerator != null)
114: item.setAccelerator(accellerator);
115:
116: return item;
117: }
118:
119: SMenu createMenu(DefaultMutableTreeNode root) {
120: SMenu menu;
121: if (root.getUserObject() instanceof Action) {
122: Action action = (Action) root.getUserObject();
123:
124: if (action.getValue(Action.SMALL_ICON) == null)
125: action.putValue(Action.SMALL_ICON, NO_ICON);
126: if (action.getValue(Action.NAME) == null)
127: action.putValue(Action.NAME, action
128: .getValue(Action.ACTION_COMMAND_KEY));
129:
130: menu = new SMenu((String) action.getValue(Action.NAME),
131: (SIcon) action.getValue(Action.SMALL_ICON));
132: //menu.add(createMenuItem(root));
133: } else
134: menu = new SMenu(root.toString());
135:
136: menu.setWidthScaleFactor(1.0);
137:
138: for (int i = 0; i < root.getChildCount(); i++) {
139: DefaultMutableTreeNode node = (DefaultMutableTreeNode) root
140: .getChildAt(i);
141: Action action = (Action) node.getUserObject();
142: if (node.isLeaf()) {
143: if (action != null && !(action instanceof NoAction))
144: menu.add(createMenuItem(node));
145: } else {
146: SMenu m = createMenu(node);
147: if (m != null)
148: menu.add(m);
149: }
150: }
151:
152: // only return a menu if it has child menu items
153: return menu.getChildrenCount() > 0 ? menu : null;
154: }
155: }
|