View Javadoc

1   // Copyright (C) 2003 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.console.swingui;
23  
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.util.HashSet;
27  import java.util.Set;
28  import javax.swing.AbstractAction;
29  import javax.swing.AbstractButton;
30  import javax.swing.Action;
31  import javax.swing.ImageIcon;
32  
33  import net.grinder.console.common.Resources;
34  
35  
36  /**
37   * Customised Action.
38   *
39   * @author Philip Aston
40   */
41  abstract class CustomAction extends AbstractAction {
42  
43    /** Property key used to indicate a set action event. */
44    protected static final String SET_ACTION_PROPERTY = "setAction";
45  
46    /** Property key for rollover icon value. */
47    public static final String ROLLOVER_ICON = "RolloverIcon";
48  
49    /**
50     * Property key for Boolean value indicating whether action is relevant
51     * to the selected context.
52     */
53    public static final String RELEVANT_TO_SELECTION = "RelevantToSelection";
54  
55    private final String m_key;
56    private final Set<AbstractButton> m_buttonsWithRegisteredListeners =
57      new HashSet<AbstractButton>();
58  
59    protected CustomAction(Resources resources, String key) {
60      this(resources, key, false);
61    }
62  
63    protected CustomAction(Resources resources, String key,
64                           boolean isDialogAction) {
65      super();
66  
67      m_key = key;
68  
69      final String label = resources.getString(m_key + ".label", false);
70  
71      if (label != null) {
72        if (isDialogAction) {
73          putValue(Action.NAME, label + "...");
74        }
75        else {
76          putValue(Action.NAME, label);
77        }
78      }
79  
80      final String tip = resources.getString(m_key + ".tip", false);
81  
82      if (tip != null) {
83        putValue(Action.SHORT_DESCRIPTION, tip);
84      }
85  
86      final ImageIcon imageIcon = resources.getImageIcon(m_key + ".image");
87  
88      if (imageIcon != null) {
89        putValue(Action.SMALL_ICON, imageIcon);
90      }
91  
92      final ImageIcon rolloverImageIcon =
93        resources.getImageIcon(m_key + ".rollover-image");
94  
95      if (rolloverImageIcon != null) {
96        putValue(CustomAction.ROLLOVER_ICON, rolloverImageIcon);
97      }
98    }
99  
100   public final String getKey() {
101     return m_key;
102   }
103 
104   public final void registerButton(final AbstractButton button) {
105     if (!m_buttonsWithRegisteredListeners.contains(button)) {
106       addPropertyChangeListener(
107         new PropertyChangeListener() {
108           public void propertyChange(PropertyChangeEvent e) {
109             if (e.getPropertyName().equals(SET_ACTION_PROPERTY)) {
110 
111               final CustomAction newAction = (CustomAction)e.getNewValue();
112 
113               button.setAction(newAction);
114               newAction.registerButton(button);
115             }
116           }
117         }
118         );
119 
120       m_buttonsWithRegisteredListeners.add(button);
121     }
122   }
123 
124   public void setRelevantToSelection(boolean b) {
125     putValue(RELEVANT_TO_SELECTION, Boolean.valueOf(b));
126   }
127 
128   public boolean isRelevantToSelection() {
129     final Boolean b = (Boolean) getValue(RELEVANT_TO_SELECTION);
130     return b != null && b.booleanValue();
131   }
132 }