View Javadoc

1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2012 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.identify;
25  
26  import java.awt.ComponentOrientation;
27  
28  import javax.swing.Action;
29  import javax.swing.ImageIcon;
30  import javax.swing.JButton;
31  import javax.swing.UIManager;
32  
33  import org.dishevelled.iconbundle.IconBundle;
34  import org.dishevelled.iconbundle.IconSize;
35  import org.dishevelled.iconbundle.IconState;
36  import org.dishevelled.iconbundle.IconTextDirection;
37  
38  /**
39   * An extension of JButton that displays the name property value
40   * and appropriate icon from an icon bundle for an identifiable
41   * action.
42   *
43   * @see IdentifiableAction
44   *
45   * @author  Michael Heuer
46   * @version $Revision: 1059 $ $Date: 2012-01-03 14:03:02 -0600 (Tue, 03 Jan 2012) $
47   */
48  public final class IdButton
49      extends JButton
50  {
51      /** Default icon size. */
52      public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
53  
54      /** Default icon text direction. */
55      private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
56  
57      /** Icon size. */
58      private IconSize iconSize = DEFAULT_ICON_SIZE;
59  
60      /** Icon text direction. */
61      private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
62  
63  
64      /**
65       * Create a new button with the specified identifiable action.
66       *
67       * @param action identifiable action, must not be null
68       */
69      public IdButton(final IdentifiableAction action)
70      {
71          super();
72          // clear gradient for Metal/Ocean L&F
73          UIManager.put("Button.gradient", null);
74          UIManager.getDefaults().put("Button.gradient", null);
75          UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
76  
77          if (action == null)
78          {
79              throw new IllegalArgumentException("action must not be null");
80          }
81          setAction(action);
82      }
83  
84      /**
85       * Create a new button with the specified identifiable action
86       * and icon size.
87       *
88       * @param action identifiable action, must not be null
89       * @param iconSize icon size, must not be null
90       */
91      public IdButton(final IdentifiableAction action, final IconSize iconSize)
92      {
93          super();
94          // clear gradient for Metal/Ocean L&F
95          UIManager.put("Button.gradient", null);
96          UIManager.getDefaults().put("Button.gradient", null);
97          UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
98  
99          if (action == null)
100         {
101             throw new IllegalArgumentException("action must not be null");
102         }
103         setIconSize(iconSize);
104         setAction(action);
105     }
106 
107 
108     /**
109      * Return the icon size for this button.
110      *
111      * @return the icon size for this button
112      */
113     public IconSize getIconSize()
114     {
115         return iconSize;
116     }
117 
118     /**
119      * Set the icon size for this button to <code>iconSize</code>.
120      *
121      * <p>This is a bound property.</p>
122      *
123      * @param iconSize icon size, must not be null
124      */
125     public void setIconSize(final IconSize iconSize)
126     {
127         if (iconSize == null)
128         {
129             throw new IllegalArgumentException("iconSize must not be null");
130         }
131         IconSize oldIconSize = this.iconSize;
132         this.iconSize = iconSize;
133 
134         if (!this.iconSize.equals(oldIconSize))
135         {
136             firePropertyChange("iconSize", oldIconSize, this.iconSize);
137             rebuild();
138         }
139     }
140 
141     /**
142      * Return the icon text direction for this button.
143      *
144      * @return the icon text direction for this button
145      */
146     IconTextDirection getIconTextDirection()
147     {
148         return iconTextDirection;
149     }
150 
151     /**
152      * Display icons only.
153      */
154     public void displayIcon()
155     {
156         //setHideActionText(true);
157         setText(null);
158         rebuild();
159     }
160 
161     /**
162      * Display text only.
163      */
164     public void displayText()
165     {
166         //setHideActionText(false);
167         Action action = getAction();
168         if ((action != null) && (action instanceof IdentifiableAction))
169         {
170             IdentifiableAction identifiableAction = (IdentifiableAction) action;
171             setText(identifiableAction.getName());
172         }
173         setIcon(null);
174         setPressedIcon(null);
175         setSelectedIcon(null);
176         setRolloverIcon(null);
177         setRolloverSelectedIcon(null);
178         setDisabledIcon(null);
179     }
180 
181     /**
182      * Display icon and text.
183      */
184     public void displayIconAndText()
185     {
186         //setHideActionText(false);
187         Action action = getAction();
188         if ((action != null) && (action instanceof IdentifiableAction))
189         {
190             IdentifiableAction identifiableAction = (IdentifiableAction) action;
191             setText(identifiableAction.getName());
192         }
193         rebuild();
194     }
195 
196     /** {@inheritDoc} */
197     public void setComponentOrientation(final ComponentOrientation orientation)
198     {
199         ComponentOrientation oldOrientation = getComponentOrientation();
200 
201         if (!oldOrientation.equals(orientation))
202         {
203             if (orientation != null)
204             {
205                 iconTextDirection = orientation.isLeftToRight()
206                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
207 
208                 rebuild();
209             }
210         }
211 
212         super.setComponentOrientation(orientation);
213     }
214 
215     /** {@inheritDoc} */
216     public void applyComponentOrientation(final ComponentOrientation orientation)
217     {
218         ComponentOrientation oldOrientation = getComponentOrientation();
219 
220         if (!oldOrientation.equals(orientation))
221         {
222             if (orientation != null)
223             {
224                 iconTextDirection = orientation.isLeftToRight()
225                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
226 
227                 rebuild();
228             }
229         }
230 
231         super.applyComponentOrientation(orientation);
232     }
233 
234     /** {@inheritDoc} */
235     protected void configurePropertiesFromAction(final Action action)
236     {
237         super.configurePropertiesFromAction(action);
238         rebuild();
239     }
240 
241     /**
242      * Rebuild the icons for this button from the icon bundle
243      * provided by the identifiable action for this button.
244      */
245     private void rebuild()
246     {
247         Action action = getAction();
248         if ((action != null) && (action instanceof IdentifiableAction))
249         {
250             IdentifiableAction identifiableAction = (IdentifiableAction) action;
251             IconBundle bndl = identifiableAction.getIconBundle();
252             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
253             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
254             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
255             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
256             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
257             //setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
258         }
259     }
260 }