View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.gui.action;
21  
22  import java.beans.PropertyChangeEvent;
23  import java.beans.PropertyChangeListener;
24  import org.xnap.commons.settings.BooleanSetting;
25  
26  /***
27   * Provides a default implementation for a toggle action that monitors 
28   * a {@link org.xnap.commons.settings.BooleanSetting}.
29   */
30  public abstract class AbstractToggleSettingAction extends AbstractToggleAction 
31      implements PropertyChangeListener {
32      
33  	BooleanSetting setting;
34      
35      /***
36       * Constructs a toggle action that monitors a boolean preference key.
37       *
38       * @param setting the setting to monitor
39       */
40      public AbstractToggleSettingAction(BooleanSetting setting)
41      {
42      	if (setting == null) {
43  			throw new IllegalArgumentException();
44  		}
45  		
46  		this.setting = setting;
47  		
48  		setting.addPropertyChangeListener(this);
49  
50  		// initialize
51  		boolean value = setting.getValue();
52  		setSelected(value);
53  		toggled(value);
54      }
55  
56      public void propertyChange(PropertyChangeEvent e) 
57      {
58  		boolean newValue = setting.getValue();
59  		setSelected(newValue);
60      }
61  
62      /***
63       * Updates the setting.
64       */
65      @Override
66      public void toggled(boolean selected)
67      {
68      	setting.setValue(selected);
69      }
70  
71  }