View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Felix Berger
5    *  Copyright (C) 2005  Steffen Pingel
6    *
7    *  This library is free software; you can redistribute it and/or
8    *  modify it under the terms of the GNU Lesser General Public
9    *  License as published by the Free Software Foundation; either
10   *  version 2.1 of the License, or (at your option) any later version.
11   *
12   *  This library is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   *  Lesser General Public License for more details.
16   *
17   *  You should have received a copy of the GNU Lesser General Public
18   *  License along with this library; if not, write to the Free Software
19   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package org.xnap.commons.gui.shortcut;
22  
23  import java.beans.PropertyChangeListener;
24  import java.util.HashMap;
25  import javax.swing.KeyStroke;
26  import javax.swing.event.SwingPropertyChangeSupport;
27  
28  /***
29   * 
30   */
31  public abstract class AbstractShortcut implements Shortcut
32  {
33  
34  	private SwingPropertyChangeSupport changeSupport =
35  		new SwingPropertyChangeSupport(this);
36  	
37  	private HashMap<String, Object> values = new HashMap<String, Object>();
38  	
39  	/*
40  	 * @see org.xnap.commons.gui.shortcut.Shortcut#putValue(java.lang.String, java.lang.Object)
41  	 */
42  	public void putValue(String key, Object value)
43  	{
44  		Object oldValue = null;
45  		if (values.containsKey(key)) {
46  			oldValue = values.get(key);
47  		}
48  		values.put(key, value);
49  		firePropertChange(key, oldValue, value);
50  	}
51  
52  	protected void firePropertChange(String key, Object oldValue, Object newValue)
53  	{
54  		if (oldValue != newValue 
55  				&& (oldValue == null || newValue == null || !oldValue.equals(newValue))) {
56  			changeSupport.firePropertyChange(key, oldValue, newValue);
57  		}
58  	}
59  
60  	/*
61  	 * @see org.xnap.commons.gui.shortcut.Shortcut#getValue(java.lang.String)
62  	 */
63  	public Object getValue(String key)
64  	{
65  		return  values.get(key);
66  	}
67  
68  	/*
69  	 * @see org.xnap.commons.gui.shortcut.Shortcut#addPropertyChangeListener(java.beans.PropertyChangeListener)
70  	 */
71  	public synchronized void addPropertyChangeListener(PropertyChangeListener l)
72  	{
73  		changeSupport.addPropertyChangeListener(l);
74  	}
75  
76  	/*
77  	 * @see org.xnap.commons.gui.shortcut.Shortcut#removePropertyChangeListener(java.beans.PropertyChangeListener)
78  	 */
79  	public synchronized void removePropertyChangeListener(PropertyChangeListener l)
80  	{
81  		changeSupport.removePropertyChangeListener(l);
82  	}
83  
84  	/*
85  	 * @see org.xnap.commons.gui.shortcut.Shortcut#getKeyStroke()
86  	 */
87  	public KeyStroke getKeyStroke()
88  	{
89  		return (KeyStroke)values.get(KEYSTROKE);
90  	}
91  
92  	/*
93  	 * @see org.xnap.commons.gui.shortcut.Shortcut#setKeyStroke(javax.swing.KeyStroke)
94  	 */
95  	public void setKeyStroke(KeyStroke stroke)
96  	{
97  		putValue(KEYSTROKE, stroke);
98  	}
99  }