001/* An attribute that creates an editor dialog to edit the value of a parameter. 002 003 Copyright (c) 1998-2014 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.awt.Component; 031import java.awt.Frame; 032 033import javax.swing.JLabel; 034import javax.swing.SwingUtilities; 035 036import ptolemy.actor.gui.EditorFactory; 037import ptolemy.actor.gui.PtolemyQuery; 038import ptolemy.gui.ComponentDialog; 039import ptolemy.kernel.util.IllegalActionException; 040import ptolemy.kernel.util.NameDuplicationException; 041import ptolemy.kernel.util.NamedObj; 042import ptolemy.kernel.util.Settable; 043import ptolemy.moml.MoMLChangeRequest; 044import ptolemy.util.StringUtilities; 045 046/////////////////////////////////////////////////////////////////// 047//// VisibleParameterEditorFactory 048 049/** 050 If this class is contained by a settable attribute, then double 051 clicking on that attribute will invoke an editor for only that 052 parameter. This is contrary to the the double-click action for most 053 objects, which edits the parameters they contain.. This class is 054 contained by visible parameters in the Vergil utilities library. 055 056 @author Steve Neuendorffer 057 @version $Id$ 058 @since Ptolemy II 2.0 059 @Pt.ProposedRating Red (eal) 060 @Pt.AcceptedRating Red (johnr) 061 */ 062public class VisibleParameterEditorFactory extends EditorFactory { 063 /** Construct a factory with the specified container and name. 064 * @param container The container. 065 * @param name The name of the factory. 066 * @exception IllegalActionException If the factory is not of an 067 * acceptable attribute for the container. 068 * @exception NameDuplicationException If the name coincides with 069 * an attribute already in the container. 070 */ 071 public VisibleParameterEditorFactory(NamedObj container, String name) 072 throws IllegalActionException, NameDuplicationException { 073 super(container, name); 074 } 075 076 /////////////////////////////////////////////////////////////////// 077 //// public methods //// 078 079 /** Create an editor for configuring the specified object. 080 */ 081 @Override 082 public void createEditor(final NamedObj object, Frame parent) { 083 ComponentDialog dialog = new ComponentDialog(parent, 084 "Edit Parameter " + object.getName(), createEditorPane()); 085 086 // If we were canceled, then restore the old value. 087 if (dialog.buttonPressed().equals("Cancel")) { 088 SwingUtilities.invokeLater(new Runnable() { 089 @Override 090 public void run() { 091 NamedObj parent = object.getContainer(); 092 String moml = "<property name=\"" + object.getName() 093 + "\" value=\"" 094 + StringUtilities.escapeForXML(_oldExpression) 095 + "\"/>"; 096 MoMLChangeRequest request = new MoMLChangeRequest(this, // originator 097 parent, // context 098 moml, // MoML code 099 null); // base 100 object.requestChange(request); 101 } 102 }); 103 } 104 } 105 106 /////////////////////////////////////////////////////////////////// 107 //// public methods //// 108 109 /** Return a new widget for configuring the container. This 110 * class returns a new PtolemyQuery that references the container, 111 * assuming that that container is a settable attribute. 112 * @return A new widget for configuring the container. 113 */ 114 public Component createEditorPane() { 115 NamedObj object = getContainer(); 116 PtolemyQuery query = new PtolemyQuery(object); 117 query.setTextWidth(25); 118 119 if (object instanceof Settable) { 120 Settable parameter = (Settable) object; 121 _oldExpression = parameter.getExpression(); 122 query.addStyledEntry(parameter); 123 return query; 124 } else { 125 return new JLabel( 126 object.getName() + " is not a settable attribute!"); 127 } 128 } 129 130 String _oldExpression = ""; 131}