Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/lib/basiccommand/builder/SwingButtonUpdater.java


1   /*
2   ================================================================================
3   
4     FILE:  SwingButtonUpdater.java
5     
6     PROJECT:
7     
8       Virtuoso Utilities
9     
10    CONTENTS:
11    
12      Utility that updates a swing AbstractButton
13    
14    PROGRAMMERS:
15    
16      Daniel Azuma (DA)  <dazuma@kagi.com>
17    
18    COPYRIGHT:
19    
20      Copyright (C) 2003  Daniel Azuma  (dazuma@kagi.com)
21      
22      This program is free software; you can redistribute it and/or
23      modify it under the terms of the GNU General Public License as
24      published by the Free Software Foundation; either version 2
25      of the License, or (at your option) any later version.
26      
27      This program is distributed in the hope that it will be useful,
28      but WITHOUT ANY WARRANTY; without even the implied warranty of
29      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30      GNU General Public License for more details.
31      
32      You should have received a copy of the GNU General Public
33      License along with this program; if not, write to
34        Free Software Foundation, Inc.
35        59 Temple Place, Suite 330
36        Boston, MA 02111-1307 USA
37  
38  ================================================================================
39  */
40  
41  
42  package com.virtuosotechnologies.lib.basiccommand.builder;
43  
44  
45  import javax.swing.AbstractButton;
46  import javax.swing.Icon;
47  import javax.swing.JMenu;
48  import javax.swing.JMenuItem;
49  import javax.swing.KeyStroke;
50  
51  import com.virtuosotechnologies.lib.basiccommand.BasicCommandNode;
52  import com.virtuosotechnologies.lib.propertyset.PropertySet;
53  import com.virtuosotechnologies.lib.propertyset.PropertySetEvent;
54  import com.virtuosotechnologies.lib.util.ObjectUtils;
55  
56  
57  /**
58   * Utility that updates a swing AbstractButton
59   */
60  public class SwingButtonUpdater
61  {
62    private AbstractButton button_;
63    
64    
65    /**
66     * Constructor
67     */
68    public SwingButtonUpdater(
69      AbstractButton button)
70    {
71      button_ = button;
72    }
73    
74    
75    private void syncSelectedName(
76      PropertySet properties)
77    {
78      String text = (String)properties.getValue(BasicCommandNode.SELECTED_NAME_PROPERTY);
79      if (text == null)
80      {
81        text = (String)properties.getValue(BasicCommandNode.NAME_PROPERTY);
82      }
83      button_.setText(text);
84    }
85    
86    
87    private void syncSelectedIcon(
88      PropertySet properties)
89    {
90      Icon icon = (Icon)properties.getValue(BasicCommandNode.SELECTED_SMALL_ICON_PROPERTY);
91      if (icon == null)
92      {
93        icon = (Icon)properties.getValue(BasicCommandNode.SMALL_ICON_PROPERTY);
94      }
95      button_.setIcon(icon);
96    }
97    
98    
99    private void syncSelectedToolTip(
100     PropertySet properties)
101   {
102     String text = (String)properties.getValue(BasicCommandNode.SELECTED_SHORT_DESCRIPTION_PROPERTY);
103     if (text == null)
104     {
105       text = (String)properties.getValue(BasicCommandNode.SHORT_DESCRIPTION_PROPERTY);
106     }
107     button_.setToolTipText(text);
108   }
109   
110   
111   private void updateSelectionState(
112     PropertySet properties)
113   {
114     boolean isSelected = ObjectUtils.safeEquals(
115       Boolean.TRUE, properties.getValue(BasicCommandNode.SELECTION_STATE_PROPERTY));
116     button_.setSelected(isSelected);
117     if (isSelected)
118     {
119       syncSelectedName(properties);
120       syncSelectedIcon(properties);
121       syncSelectedToolTip(properties);
122     }
123     else
124     {
125       button_.setText((String)
126         properties.getValue(BasicCommandNode.NAME_PROPERTY));
127       button_.setIcon((Icon)
128         properties.getValue(BasicCommandNode.SMALL_ICON_PROPERTY));
129       button_.setToolTipText((String)
130         properties.getValue(BasicCommandNode.SHORT_DESCRIPTION_PROPERTY));
131     }
132   }
133   
134   
135   /**
136    * Update now
137    */
138   public void updateAll(
139     PropertySet properties)
140   {
141     updateSelectionState(properties);
142     Integer i = (Integer)properties.getValue(BasicCommandNode.MNEMONIC_CODE_PROPERTY);
143     if (i != null)
144     {
145       button_.setMnemonic(i.intValue());
146     }
147     if (button_ instanceof JMenuItem && !(button_ instanceof JMenu))
148     {
149       ((JMenuItem)button_).setAccelerator((KeyStroke)
150         properties.getValue(BasicCommandNode.ACCELERATOR_KEYSTROKE_PROPERTY));
151     }
152   }
153   
154   
155   /**
156    * Update now
157    */
158   public void updateIncremental(
159     PropertySetEvent ev)
160   {
161     PropertySet properties = ev.getPropertySet();
162     if (ev.getKey().equals(BasicCommandNode.MNEMONIC_CODE_PROPERTY))
163     {
164       if (ev.getNewValue() != null)
165       {
166         button_.setMnemonic(((Integer)ev.getNewValue()).intValue());
167       }
168     }
169     else if (ev.getKey().equals(BasicCommandNode.ACCELERATOR_KEYSTROKE_PROPERTY))
170     {
171       if (button_ instanceof JMenuItem && !(button_ instanceof JMenu))
172       {
173         ((JMenuItem)button_).setAccelerator((KeyStroke)ev.getNewValue());
174       }
175     }
176     else if (ev.getKey().equals(BasicCommandNode.SELECTION_STATE_PROPERTY))
177     {
178       updateSelectionState(properties);
179     }
180     else
181     {
182       boolean isSelected = ObjectUtils.safeEquals(Boolean.TRUE,
183         properties.getValue(BasicCommandNode.SELECTION_STATE_PROPERTY));
184       if (isSelected)
185       {
186         if (ev.getKey().equals(BasicCommandNode.SELECTED_NAME_PROPERTY) ||
187           ev.getKey().equals(BasicCommandNode.NAME_PROPERTY))
188         {
189           syncSelectedName(properties);
190         }
191         else if (ev.getKey().equals(BasicCommandNode.SELECTED_SMALL_ICON_PROPERTY) ||
192           ev.getKey().equals(BasicCommandNode.SMALL_ICON_PROPERTY))
193         {
194           syncSelectedIcon(properties);
195         }
196         else if (ev.getKey().equals(BasicCommandNode.SELECTED_SHORT_DESCRIPTION_PROPERTY) ||
197           ev.getKey().equals(BasicCommandNode.SHORT_DESCRIPTION_PROPERTY))
198         {
199           syncSelectedToolTip(properties);
200         }
201       }
202       else
203       {
204         if (ev.getKey().equals(BasicCommandNode.NAME_PROPERTY))
205         {
206           button_.setText((String)ev.getNewValue());
207         }
208         else if (ev.getKey().equals(BasicCommandNode.SMALL_ICON_PROPERTY))
209         {
210           button_.setIcon((Icon)ev.getNewValue());
211         }
212         else if (ev.getKey().equals(BasicCommandNode.SHORT_DESCRIPTION_PROPERTY))
213         {
214           button_.setToolTipText((String)ev.getNewValue());
215         }
216       }
217     }
218   }
219 }