View Javadoc
1   /*
2   
3       dsh-identify  Lightweight components for identifiable beans.
4       Copyright (c) 2003-2019 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   */
47  public final class IdButton
48      extends JButton
49  {
50      /** Default icon size. */
51      public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
52  
53      /** Default icon text direction. */
54      private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
55  
56      /** Icon size. */
57      private IconSize iconSize = DEFAULT_ICON_SIZE;
58  
59      /** Icon text direction. */
60      private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
61  
62  
63      /**
64       * Create a new button with the specified identifiable action.
65       *
66       * @param action identifiable action, must not be null
67       */
68      public IdButton(final IdentifiableAction action)
69      {
70          super();
71          // clear gradient for Metal/Ocean L&F
72          UIManager.put("Button.gradient", null);
73          UIManager.getDefaults().put("Button.gradient", null);
74          UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
75  
76          if (action == null)
77          {
78              throw new IllegalArgumentException("action must not be null");
79          }
80          setAction(action);
81      }
82  
83      /**
84       * Create a new button with the specified identifiable action
85       * and icon size.
86       *
87       * @param action identifiable action, must not be null
88       * @param iconSize icon size, must not be null
89       */
90      public IdButton(final IdentifiableAction action, final IconSize iconSize)
91      {
92          super();
93          // clear gradient for Metal/Ocean L&F
94          UIManager.put("Button.gradient", null);
95          UIManager.getDefaults().put("Button.gradient", null);
96          UIManager.getLookAndFeelDefaults().put("Button.gradient", null);
97  
98          if (action == null)
99          {
100             throw new IllegalArgumentException("action must not be null");
101         }
102         setIconSize(iconSize);
103         setAction(action);
104     }
105 
106 
107     /**
108      * Return the icon size for this button.
109      *
110      * @return the icon size for this button
111      */
112     public IconSize getIconSize()
113     {
114         return iconSize;
115     }
116 
117     /**
118      * Set the icon size for this button to <code>iconSize</code>.
119      *
120      * <p>This is a bound property.</p>
121      *
122      * @param iconSize icon size, must not be null
123      */
124     public void setIconSize(final IconSize iconSize)
125     {
126         if (iconSize == null)
127         {
128             throw new IllegalArgumentException("iconSize must not be null");
129         }
130         IconSize oldIconSize = this.iconSize;
131         this.iconSize = iconSize;
132 
133         if (!this.iconSize.equals(oldIconSize))
134         {
135             firePropertyChange("iconSize", oldIconSize, this.iconSize);
136             rebuild();
137         }
138     }
139 
140     /**
141      * Return the icon text direction for this button.
142      *
143      * @return the icon text direction for this button
144      */
145     IconTextDirection getIconTextDirection()
146     {
147         return iconTextDirection;
148     }
149 
150     /**
151      * Display icons only.
152      */
153     public void displayIcon()
154     {
155         //setHideActionText(true);
156         setText(null);
157         rebuild();
158     }
159 
160     /**
161      * Display text only.
162      */
163     public void displayText()
164     {
165         //setHideActionText(false);
166         Action action = getAction();
167         if ((action != null) && (action instanceof IdentifiableAction))
168         {
169             IdentifiableAction/identify/IdentifiableAction.html#IdentifiableAction">IdentifiableAction identifiableAction = (IdentifiableAction) action;
170             setText(identifiableAction.getName());
171         }
172         setIcon(null);
173         setPressedIcon(null);
174         setSelectedIcon(null);
175         setRolloverIcon(null);
176         setRolloverSelectedIcon(null);
177         setDisabledIcon(null);
178     }
179 
180     /**
181      * Display icon and text.
182      */
183     public void displayIconAndText()
184     {
185         //setHideActionText(false);
186         Action action = getAction();
187         if ((action != null) && (action instanceof IdentifiableAction))
188         {
189             IdentifiableAction/identify/IdentifiableAction.html#IdentifiableAction">IdentifiableAction identifiableAction = (IdentifiableAction) action;
190             setText(identifiableAction.getName());
191         }
192         rebuild();
193     }
194 
195     @Override
196     public void setComponentOrientation(final ComponentOrientation orientation)
197     {
198         ComponentOrientation oldOrientation = getComponentOrientation();
199 
200         if (!oldOrientation.equals(orientation))
201         {
202             if (orientation != null)
203             {
204                 iconTextDirection = orientation.isLeftToRight()
205                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
206 
207                 rebuild();
208             }
209         }
210 
211         super.setComponentOrientation(orientation);
212     }
213 
214     @Override
215     public void applyComponentOrientation(final ComponentOrientation orientation)
216     {
217         ComponentOrientation oldOrientation = getComponentOrientation();
218 
219         if (!oldOrientation.equals(orientation))
220         {
221             if (orientation != null)
222             {
223                 iconTextDirection = orientation.isLeftToRight()
224                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
225 
226                 rebuild();
227             }
228         }
229 
230         super.applyComponentOrientation(orientation);
231     }
232 
233     @Override
234     protected void configurePropertiesFromAction(final Action action)
235     {
236         super.configurePropertiesFromAction(action);
237         rebuild();
238     }
239 
240     /**
241      * Rebuild the icons for this button from the icon bundle
242      * provided by the identifiable action for this button.
243      */
244     private void rebuild()
245     {
246         Action action = getAction();
247         if ((action != null) && (action instanceof IdentifiableAction))
248         {
249             IdentifiableAction/identify/IdentifiableAction.html#IdentifiableAction">IdentifiableAction identifiableAction = (IdentifiableAction) action;
250             IconBundle bndl = identifiableAction.getIconBundle();
251             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
252             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
253             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
254             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
255             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
256             //setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
257         }
258     }
259 }