001/* A GUI property that encloses a JButton component. 002 003 Copyright (c) 2008-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.actor.gui.properties; 029 030import java.awt.event.ActionEvent; 031import java.awt.event.ActionListener; 032import java.net.URL; 033 034import javax.swing.ImageIcon; 035import javax.swing.JButton; 036import javax.swing.JComponent; 037 038import ptolemy.data.expr.FileParameter; 039import ptolemy.data.expr.StringParameter; 040import ptolemy.kernel.util.Attribute; 041import ptolemy.kernel.util.IllegalActionException; 042import ptolemy.kernel.util.NameDuplicationException; 043import ptolemy.kernel.util.NamedObj; 044 045////////////////////////////////////////////////////////////////////////// 046//// Button 047 048/** 049 A GUI property that encloses a JButton component. 050 051 @author Thomas Huining Feng 052 @version $Id$ 053 @since Ptolemy II 8.0 054 @Pt.ProposedRating Red (tfeng) 055 @Pt.AcceptedRating Red (tfeng) 056 */ 057public class Button extends ActionGUIProperty { 058 059 /** Construct a GUI property with the given name contained by the specified 060 * entity. The container argument must not be null, or a 061 * NullPointerException will be thrown. This attribute will use the 062 * workspace of the container for synchronization and version counts. 063 * If the name argument is null, then the name is set to the empty string. 064 * Increment the version of the workspace. 065 * @param container The container. 066 * @param name The name of this attribute. 067 * @exception IllegalActionException If the attribute is not of an 068 * acceptable class for the container, or if the name contains a period. 069 * @exception NameDuplicationException If the name coincides with 070 * an attribute already in the container. 071 */ 072 public Button(NamedObj container, String name) 073 throws IllegalActionException, NameDuplicationException { 074 super(container, name); 075 icon = new FileParameter(this, "icon"); 076 tooltip = new StringParameter(this, "tooltip"); 077 } 078 079 /** Set a name to present to the user. 080 * @param name A name to present to the user. 081 * @see #getDisplayName() 082 */ 083 @Override 084 public void setDisplayName(String name) { 085 super.setDisplayName(name); 086 ((JButton) getComponent()).setText(name); 087 } 088 089 /** React to a change in an attribute. This method is called by 090 * a contained attribute when its value changes. If the changed attribute 091 * is {@link #preferredSize}, then the preferred size of the Swing 092 * component in this GUI property is adjusted accordingly. 093 * @param attribute The attribute that changed. 094 * @exception IllegalActionException If the change is not acceptable 095 * to this container (not thrown in this base class). 096 */ 097 @Override 098 public void attributeChanged(Attribute attribute) 099 throws IllegalActionException { 100 super.attributeChanged(attribute); 101 102 if (attribute == icon) { 103 URL url = icon.asURL(); 104 if (url != null) { 105 ImageIcon imageIcon = new ImageIcon(url); 106 JButton button = (JButton) getComponent(); 107 button.setIcon(imageIcon); 108 } 109 } 110 111 if (attribute == tooltip) { 112 String tooltipString = tooltip.stringValue(); 113 JButton button = (JButton) getComponent(); 114 button.setToolTipText(tooltipString); 115 } 116 117 } 118 119 /** Create a new Java Swing component. 120 * 121 * @return A Swing component that can be enclosed in this GUI property. 122 * @exception IllegalActionException Not thrown in this base class. 123 */ 124 @Override 125 protected JComponent _createComponent() throws IllegalActionException { 126 JButton button = new JButton(getDisplayName()); 127 button.addActionListener(new ActionListener() { 128 @Override 129 public void actionPerformed(ActionEvent e) { 130 perform(); 131 } 132 }); 133 return button; 134 } 135 136 /** Icon for the button. Set an empty string to remove the icon. 137 */ 138 public FileParameter icon; 139 140 /** Tooltip for the button. 141 */ 142 public StringParameter tooltip; 143}