001/* An attribute that produces a custom node controller for resizable icons.
002
003 Copyright (c) 2003-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.icon;
029
030import diva.canvas.interactor.SelectionInteractor;
031import diva.graph.GraphController;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034import ptolemy.kernel.util.NamedObj;
035import ptolemy.vergil.basic.NamedObjController;
036import ptolemy.vergil.basic.NodeControllerFactory;
037import ptolemy.vergil.kernel.AttributeController;
038import ptolemy.vergil.toolbox.AttributeBoundsManipulator;
039
040///////////////////////////////////////////////////////////////////
041//// ResizableAttributeControllerFactory
042
043/**
044 This is attribute that produces a custom node controller for icons that
045 can be interactively resized. It provides a context menu suitable for
046 an attribute as well as an interactor that provides for resizing.
047 To use this class, just insert it as an attribute inside
048 a Ptolemy II attribute, and then right clicking on the icon for
049 that object will result in the use of the controller specified
050 here.  The instance by convention will be named "_controllerFactory",
051 but the only reason to enforce this is that only the first such
052 controller factory found as an attribute will be used.
053 It is a singleton, so placing it any container will replace any
054 previous controller factory with the same name.
055
056 @author Edward A. Lee and Steve Neuendorffer
057 @version $Id$
058 @since Ptolemy II 4.0
059 @Pt.ProposedRating Red (eal)
060 @Pt.AcceptedRating Red (johnr)
061 */
062public class ResizableAttributeControllerFactory extends NodeControllerFactory {
063    /** Construct a new attribute with the given container and name.
064     *  @param container The container.
065     *  @param name The name.
066     *  @exception IllegalActionException If the attribute cannot be contained
067     *   by the proposed container.
068     *  @exception NameDuplicationException If the container already has an
069     *   attribute with this name.
070     */
071    public ResizableAttributeControllerFactory(NamedObj container, String name)
072            throws NameDuplicationException, IllegalActionException {
073        super(container, name);
074    }
075
076    ///////////////////////////////////////////////////////////////////
077    ////                         public methods                    ////
078
079    /** Return a new node controller.  This base class returns an
080     *  instance of IconController.  Derived
081     *  classes can return some other class to customize the
082     *  context menu.
083     *  @param controller The associated graph controller.
084     *  @return A new node controller.
085     */
086    @Override
087    public NamedObjController create(GraphController controller) {
088        return new ResizeAttributeController(controller);
089    }
090
091    ///////////////////////////////////////////////////////////////////
092    ////                         inner classes                     ////
093
094    /** Custom controller that uses a bounds manipulator to allow
095     *  the user to resize the image.
096     */
097    public class ResizeAttributeController extends AttributeController {
098        /** Create a controller associated with the specified graph
099         *  controller.  The attribute controller is given full access.
100         *  @param controller The associated graph controller.
101         */
102        public ResizeAttributeController(GraphController controller) {
103            super(controller);
104
105            SelectionInteractor interactor = (SelectionInteractor) getNodeInteractor();
106
107            // Create and set up the manipulator for connectors.
108            // FIXME: This is only a prototype.  But due to a Diva bug,
109            // the prototype is what gets the mouse events!  I.e., not
110            // the instance decorator that is created from the prototype!
111            NamedObj container = getContainer();
112            _manipulator = new AttributeBoundsManipulator(container);
113
114            interactor.setPrototypeDecorator(_manipulator);
115        }
116
117        /** Specify the snap resolution. The default snap resolution is 5.0.
118         *  This overrides the base class to set the snap resolution on the
119         *  bounds manipulator as well as the drag interactor.
120         *  @param resolution The snap resolution.
121         */
122        @Override
123        public void setSnapResolution(double resolution) {
124            super.setSnapResolution(resolution);
125            _manipulator.setSnapResolution(resolution);
126        }
127
128        private AttributeBoundsManipulator _manipulator;
129    }
130}