/* * Moonlight|3D Copyright (C) 2005 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Jul 14, 2005 */ package ml.ui.core; import java.util.ArrayList; import ml.core.exceptions.Exception; public class MenuNode implements Cloneable { public enum Type { Menu, Entry, Generator, ExtensionPoint, Contribution, Separator } private ArrayList children; private Type type; private String name; private String actionName; /** * This is the name of the extension point that this node * belongs to if it is a contribution. */ private String parentExtensionPoint; /** * * @param type the type of this menu node * @param name the name of this menu node, overrides the display name for the associated action * @param actionName the name of the associated action or the parent extension point if this is a contribution */ public MenuNode(Type type, String name, String actionName) { this.type=type; this.name=name; if(type==Type.Entry) { this.actionName=actionName; } else if(type==Type.Contribution) { this.parentExtensionPoint=actionName; } children=new ArrayList(); } public void add(MenuNode child) { children.add(child); } public void insertBefore(MenuNode child, MenuNode successor) { for(int i=0;i getChildren() { return (ArrayList) children.clone(); } public MenuNode getChildByName(String name) { for(MenuNode node : children) { if(node.getName().equals(name)) { return node; } } return null; } public String getName() { return name; } public Type getType() { return type; } public String getActionName() { return actionName; } public void addContribution(MenuNode menuContribution) throws Exception { if(menuContribution.type!=Type.Contribution) { throw new ml.core.exceptions.Exception("passed menu node is not a contribution"); } if(type==Type.Menu) { // descend tree for(MenuNode child : children) { child.addContribution(menuContribution); } } else if(type==Type.ExtensionPoint) { // add contribution at proper position so that lexicographical sorting is maintained if(name.equals(menuContribution.getParentExtensionPoint())) { for(int i=0;i0) { children.add(i,menuContribution); return; } } children.add(menuContribution); } else { // descend contributed menu trees for(MenuNode contribution : children) { for(MenuNode child : contribution.getChildren()) { child.addContribution(menuContribution); } } } } } public String getParentExtensionPoint() { return parentExtensionPoint; } public void setParentExtensionPoint(String parentExtensionPoint) { this.parentExtensionPoint = parentExtensionPoint; } public MenuNode deepCopy() throws CloneNotSupportedException { MenuNode copy; copy=(MenuNode) clone(); copy.children=new ArrayList(); for(int i=0;i