001/* An abstract menu factory that creates context menus using item factories 002 003 Copyright (c) 2000-2016 The Regents of the University of California. 004 All rights reserved. 005 Permission is hereby granted, without written agreement and without 006 license or royalty fees, to use, copy, modify, and distribute this 007 software and its documentation for any purpose, provided that the above 008 copyright notice and the following two paragraphs appear in all copies 009 of this software. 010 011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 015 SUCH DAMAGE. 016 017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 022 ENHANCEMENTS, OR MODIFICATIONS. 023 024 PT_COPYRIGHT_VERSION_2 025 COPYRIGHTENDKEY 026 027 */ 028package ptolemy.vergil.toolbox; 029 030import java.util.Collections; 031import java.util.Iterator; 032import java.util.LinkedList; 033import java.util.List; 034 035import diva.canvas.Figure; 036import diva.graph.GraphController; 037import diva.gui.toolbox.JContextMenu; 038import diva.gui.toolbox.MenuFactory; 039import ptolemy.kernel.util.NamedObj; 040 041/////////////////////////////////////////////////////////////////// 042//// PtolemyMenuFactory 043 044/** 045 A menu factory that contains a list of item factories. 046 When asked to create a context menu, This class first 047 takes the figure and finds the ptolemy object associated with it. Then it 048 passes the ptolemy object to each menu item factory that it contains to add 049 the menu items. Lastly, it returns the resulting menu. This seems simple, 050 except for the fact that for different types of figures, and different 051 visual notations, the mapping between figure and the interesting ptolemy 052 object is different. Hence, Node and Edge Controllers will often need 053 subclasses of this factory to get the correct ptolemy object. 054 055 @author Steve Neuendorffer 056 @version $Id$ 057 @since Ptolemy II 1.0 058 @Pt.ProposedRating Red (eal) 059 @Pt.AcceptedRating Red (johnr) 060 */ 061public class PtolemyMenuFactory implements MenuFactory { 062 063 /** Create a new menu factory that contains no menu item factories. 064 * @param controller The controller. 065 */ 066 public PtolemyMenuFactory(GraphController controller) { 067 _factoryList = new LinkedList(); 068 _controller = controller; 069 } 070 071 /////////////////////////////////////////////////////////////////// 072 //// public methods //// 073 074 /** Add a menu item factory to this creator. 075 * @param factory The menu item factory to add. 076 */ 077 public void addMenuItemFactory(MenuItemFactory factory) { 078 _factoryList.add(factory); 079 } 080 081 /** Create an instance of the menu associated with this factory. 082 * @param figure The figure for which to create a context menu. 083 * @return The instance of the menu. 084 */ 085 @Override 086 public JContextMenu create(Figure figure) { 087 NamedObj object = _getObjectFromFigure(figure); 088 089 if (object == null) { 090 return null; 091 } 092 093 JContextMenu menu = new JContextMenu(object, object.getFullName()); 094 Iterator i = menuItemFactoryList().iterator(); 095 096 while (i.hasNext()) { 097 MenuItemFactory factory = (MenuItemFactory) i.next(); 098 factory.create(menu, object); 099 } 100 101 return menu; 102 } 103 104 /** Return the graph controller that created this menu factory. 105 * @return The controller. 106 */ 107 public GraphController getController() { 108 return _controller; 109 } 110 111 /** Return the list of menu item factories. 112 * @return An unmodifiable list. 113 */ 114 public List menuItemFactoryList() { 115 return Collections.unmodifiableList(_factoryList); 116 } 117 118 /** Remove the given menu item factory from the factory list. 119 * @param factory The factory to be removed. 120 */ 121 public void removeMenuItemFactory(MenuItemFactory factory) { 122 _factoryList.remove(factory); 123 } 124 125 /////////////////////////////////////////////////////////////////// 126 //// protected methods //// 127 128 /** Return the Ptolemy object that the given figure represents. 129 * In this base class, we assume that the figure is attached to a 130 * a diva.graph.model object, and that object is attached to the 131 * correct ptolemy object. In many cases, this is not the case, 132 * and you will have to override this function. 133 * @param figure The figure. 134 * @return The Ptolemy object that the given figure represents. 135 */ 136 protected NamedObj _getObjectFromFigure(Figure figure) { 137 Object object = figure.getUserObject(); 138 return (NamedObj) _controller.getGraphModel().getSemanticObject(object); 139 } 140 141 /////////////////////////////////////////////////////////////////// 142 //// private members //// 143 144 /** The graph controller. 145 */ 146 private GraphController _controller; 147 148 /** The menu item factories. 149 */ 150 private List _factoryList; 151}