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.ImageIcon;
29  import javax.swing.JMenu;
30  
31  import org.dishevelled.iconbundle.IconBundle;
32  import org.dishevelled.iconbundle.IconSize;
33  import org.dishevelled.iconbundle.IconState;
34  import org.dishevelled.iconbundle.IconTextDirection;
35  
36  /**
37   * An extension of JMenu that displays the name property value
38   * and appropriate icon from an icon bundle for a given bean.
39   *
40   * @author  Michael Heuer
41   * @version $Revision$ $Date$
42   */
43  public final class IdMenu
44      extends JMenu
45  {
46      /** Default icon size. */
47      public static final IconSize DEFAULT_ICON_SIZE = IconSize.DEFAULT_16X16;
48  
49      /** Default icon text direction. */
50      private static final IconTextDirection DEFAULT_ICON_TEXT_DIRECTION = IconTextDirection.LEFT_TO_RIGHT;
51  
52      /** Icon size. */
53      private IconSize iconSize = DEFAULT_ICON_SIZE;
54  
55      /** Icon text direction. */
56      private IconTextDirection iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
57  
58      /** Bound value property. */
59      private Object value;
60  
61  
62      /**
63       * Create a new menu.
64       */
65      public IdMenu()
66      {
67          this(null);
68      }
69  
70      /**
71       * Create a new menu with the specified value.
72       *
73       * @param value value
74       */
75      public IdMenu(final Object value)
76      {
77          super();
78          setValue(value);
79      }
80  
81      /**
82       * Create a new menu item with the specified value and icon size.
83       *
84       * @param value value
85       * @param iconSize icon size, must not be null
86       */
87      public IdMenu(final Object value, final IconSize iconSize)
88      {
89          super();
90          setValue(value);
91          // rebuilds again
92          setIconSize(iconSize);
93      }
94  
95  
96      /**
97       * Return the value for this menu.
98       *
99       * @return the value for this menu
100      */
101     public Object getValue()
102     {
103         return value;
104     }
105 
106     /**
107      * Set the value for this menu to <code>value</code>.
108      *
109      * <p>This is a bound property.</p>
110      *
111      * @param value value for this menu
112      */
113     public void setValue(final Object value)
114     {
115         Object oldValue = this.value;
116         this.value = value;
117         firePropertyChange("value", oldValue, this.value);
118         rebuild();
119     }
120 
121     /**
122      * Return the icon size for this menu.
123      *
124      * @return the icon size for this menu
125      */
126     public IconSize getIconSize()
127     {
128         return iconSize;
129     }
130 
131     /**
132      * Set the icon size for this menu to <code>iconSize</code>.
133      *
134      * <p>This is a bound property.</p>
135      *
136      * @param iconSize icon size, must not be null
137      */
138     public void setIconSize(final IconSize iconSize)
139     {
140         if (iconSize == null)
141         {
142             throw new IllegalArgumentException("iconSize must not be null");
143         }
144         IconSize oldIconSize = this.iconSize;
145         this.iconSize = iconSize;
146 
147         if (!this.iconSize.equals(oldIconSize))
148         {
149             firePropertyChange("iconSize", oldIconSize, this.iconSize);
150             rebuild();
151         }
152     }
153 
154     /**
155      * Return the icon text direction for this menu.
156      *
157      * @return the icon text direction for this menu
158      */
159     public IconTextDirection getIconTextDirection()
160     {
161         return iconTextDirection;
162     }
163 
164     /** {@inheritDoc} */
165     public void setComponentOrientation(final ComponentOrientation orientation)
166     {
167         ComponentOrientation oldOrientation = getComponentOrientation();
168 
169         if (!oldOrientation.equals(orientation))
170         {
171             if (orientation != null)
172             {
173                 iconTextDirection = orientation.isLeftToRight()
174                     ? IconTextDirection.LEFT_TO_RIGHT : IconTextDirection.RIGHT_TO_LEFT;
175 
176                 rebuild();
177             }
178         }
179 
180         super.setComponentOrientation(orientation);
181     }
182 
183     /** {@inheritDoc} */
184     public void applyComponentOrientation(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                 rebuild();
196             }
197         }
198 
199         super.applyComponentOrientation(orientation);
200     }
201 
202     /**
203      * Rebuild the text and icon properties of this menu
204      * with the name property and icon bundle image of
205      * <code>getValue()</code>, respectively.
206      */
207     private void rebuild()
208     {
209         String name = IdentifyUtils.getNameFor(value);
210         setText(name);
211         IconBundle bndl = IdentifyUtils.getIconBundleFor(value);
212 
213         if (bndl == null)
214         {
215             setIcon(null);
216         }
217         else
218         {
219             setIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.NORMAL, iconSize)));
220             setPressedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.ACTIVE, iconSize)));
221             setSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
222             setRolloverIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.MOUSEOVER, iconSize)));
223             setRolloverSelectedIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.SELECTED, iconSize)));
224             setDisabledIcon(new ImageIcon(bndl.getImage(this, iconTextDirection, IconState.DISABLED, iconSize)));
225         }
226     }
227 }