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.JCheckBoxMenuItem;
31  
32  import org.dishevelled.iconbundle.IconBundle;
33  import org.dishevelled.iconbundle.IconSize;
34  import org.dishevelled.iconbundle.IconState;
35  import org.dishevelled.iconbundle.IconTextDirection;
36  
37  /**
38   * An extension of JCheckBoxMenuItem that displays the name property value
39   * and appropriate icon from an icon bundle for an identifiable
40   * action.
41   *
42   * @see IdentifiableAction
43   *
44   * @author  Michael Heuer
45   */
46  public final class IdCheckBoxMenuItem
47      extends JCheckBoxMenuItem
48  {
49      /** Default icon size. */
50      public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
51  
52      /** Default icon text direction. */
53      private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
54  
55      /** Icon size. */
56      private IconSize iconSize = DEFAULT_ICON_SIZE;
57  
58      /** Icon text direction. */
59      private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
60  
61      /** Dirty flag. */
62      private transient boolean dirty = true;
63  
64  
65      /**
66       * Create a new check box menu item with the specified identifiable action.
67       *
68       * @param action identifiable action, must not be null
69       */
70      public IdCheckBoxMenuItem(final IdentifiableAction action)
71      {
72          super();
73  
74          if (action == null)
75          {
76              throw new IllegalArgumentException("action must not be null");
77          }
78          setAction(action);
79      }
80  
81      /**
82       * Create a new check box menu item with the specified identifiable action and state.
83       *
84       * @param action identifiable action, must not be null
85       * @param state true if this check box menu item is selected
86       */
87      public IdCheckBoxMenuItem(final IdentifiableAction action, final boolean state)
88      {
89          super();
90  
91          if (action == null)
92          {
93              throw new IllegalArgumentException("action must not be null");
94          }
95          setAction(action);
96          setState(state);
97      }
98  
99      /**
100      * Create a new check box menu item with the specified identifiable action
101      * and icon size.
102      *
103      * @param action identifiable action, must not be null
104      * @param iconSize icon size, must not be null
105      */
106     public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize)
107     {
108         super();
109 
110         if (action == null)
111         {
112             throw new IllegalArgumentException("action must not be null");
113         }
114         setIconSize(iconSize);
115         setAction(action);
116     }
117 
118     /**
119      * Create a new check box menu item with the specified identifiable action,
120      * icon size, and state.
121      *
122      * @param action identifiable action, must not be null
123      * @param iconSize icon size, must not be null
124      * @param state true if this check box menu item is selected
125      */
126     public IdCheckBoxMenuItem(final IdentifiableAction action, final IconSize iconSize, final boolean state)
127     {
128         super();
129 
130         if (action == null)
131         {
132             throw new IllegalArgumentException("action must not be null");
133         }
134         setIconSize(iconSize);
135         setAction(action);
136         setState(state);
137     }
138 
139 
140     /**
141      * Return the icon size for this check box menu item.
142      *
143      * @return the icon size for this check box menu item
144      */
145     public IconSize getIconSize()
146     {
147         return iconSize;
148     }
149 
150     /**
151      * Set the icon size for this check box menu item to <code>iconSize</code>.
152      *
153      * <p>This is a bound property.</p>
154      *
155      * @param iconSize icon size, must not be null
156      */
157     public void setIconSize(final IconSize iconSize)
158     {
159         if (iconSize == null)
160         {
161             throw new IllegalArgumentException("iconSize must not be null");
162         }
163         IconSize oldIconSize = this.iconSize;
164         this.iconSize = iconSize;
165 
166         if (!this.iconSize.equals(oldIconSize))
167         {
168             firePropertyChange("iconSize", oldIconSize, this.iconSize);
169             setDirty(true);
170         }
171     }
172 
173     /**
174      * Return the icon text direction for this check box menu item.
175      *
176      * @return the icon text direction for this check box menu item
177      */
178     public IconTextDirection getIconTextDirection()
179     {
180         return iconTextDirection;
181     }
182 
183     @Override
184     public void setComponentOrientation(final ComponentOrientation orientation)
185     {
186         ComponentOrientation oldOrientation = getComponentOrientation();
187 
188         if (!oldOrientation.equals(orientation))
189         {
190             if (orientation != null)
191             {
192                 iconTextDirection = orientation.isLeftToRight()
193                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
194 
195                 setDirty(true);
196             }
197         }
198 
199         super.setComponentOrientation(orientation);
200     }
201 
202     @Override
203     public void applyComponentOrientation(final ComponentOrientation orientation)
204     {
205         ComponentOrientation oldOrientation = getComponentOrientation();
206 
207         if (!oldOrientation.equals(orientation))
208         {
209             if (orientation != null)
210             {
211                 iconTextDirection = orientation.isLeftToRight()
212                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
213 
214                 setDirty(true);
215             }
216         }
217 
218         super.applyComponentOrientation(orientation);
219     }
220 
221     @Override
222     protected void configurePropertiesFromAction(final Action action)
223     {
224         super.configurePropertiesFromAction(action);
225 
226         if (isDirty())
227         {
228             rebuild();
229         }
230     }
231 
232     /**
233      * Set the dirty flag to the logical OR of <code>dirty</code>
234      * and the previous dirty state.
235      *
236      * @param dirty dirty flag
237      */
238     private void setDirty(final boolean dirty)
239     {
240         this.dirty = (this.dirty || dirty);
241     }
242 
243     /**
244      * Return true if a rebuild of the icons for this check box menu item is necessary.
245      *
246      * @return true if a rebuild of the icons for this check box menu item is necessary
247      */
248     private boolean isDirty()
249     {
250         return dirty;
251     }
252 
253     /**
254      * Rebuild the icons for this check box menu item from the icon bundle
255      * provided by the identifiable action for this check box menu item.
256      */
257     private void rebuild()
258     {
259         Action action = getAction();
260         if ((action != null) && (action instanceof IdentifiableAction))
261         {
262             IdentifiableAction/identify/IdentifiableAction.html#IdentifiableAction">IdentifiableAction identifiableAction = (IdentifiableAction) action;
263             IconBundle bndl = identifiableAction.getIconBundle();
264             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
265             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
266             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
267             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
268             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
269             //setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
270         }
271         dirty = false;
272     }
273 }