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