001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.swing;
019:
020: import java.beans.PropertyChangeListener;
021: import java.io.Serializable;
022: import java.util.HashMap;
023: import javax.swing.event.SwingPropertyChangeSupport;
024: import org.apache.harmony.x.swing.StringConstants;
025:
026: public abstract class AbstractAction implements Action, Cloneable,
027: Serializable {
028: protected boolean enabled = true;
029:
030: protected SwingPropertyChangeSupport changeSupport = new SwingPropertyChangeSupport(
031: this );
032:
033: private HashMap<String, Object> properties;
034:
035: public AbstractAction() {
036: super ();
037: }
038:
039: public AbstractAction(final String name) {
040: super ();
041: properties = new HashMap<String, Object>();
042: properties.put(Action.NAME, name);
043: }
044:
045: public AbstractAction(final String name, final Icon icon) {
046: super ();
047: properties = new HashMap<String, Object>();
048: properties.put(Action.NAME, name);
049: properties.put(Action.SMALL_ICON, icon);
050: }
051:
052: public void putValue(final String name, final Object value) {
053: if (properties == null) {
054: properties = new HashMap<String, Object>();
055: }
056: Object oldValue = properties.get(name);
057: if (value != oldValue) {
058: properties.put(name, value);
059: firePropertyChange(name, oldValue, value);
060: }
061: }
062:
063: public Object getValue(final String name) {
064: return (properties != null) ? properties.get(name) : null;
065: }
066:
067: public Object[] getKeys() {
068: return (properties != null) ? properties.keySet().toArray()
069: : new Object[0];
070: }
071:
072: @SuppressWarnings("unchecked")
073: @Override
074: protected Object clone() throws CloneNotSupportedException {
075: AbstractAction cloned = (AbstractAction) super .clone();
076: if (properties != null) {
077: cloned.properties = (HashMap<String, Object>) properties
078: .clone();
079: }
080: return cloned;
081: }
082:
083: public synchronized void removePropertyChangeListener(
084: final PropertyChangeListener listener) {
085: changeSupport.removePropertyChangeListener(listener);
086: }
087:
088: public synchronized void addPropertyChangeListener(
089: final PropertyChangeListener listener) {
090: changeSupport.addPropertyChangeListener(listener);
091: }
092:
093: public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
094: return changeSupport.getPropertyChangeListeners();
095: }
096:
097: protected void firePropertyChange(final String propertyName,
098: final Object oldValue, final Object newValue) {
099: changeSupport.firePropertyChange(propertyName, oldValue,
100: newValue);
101: }
102:
103: public void setEnabled(final boolean enabled) {
104: boolean oldValue = this .enabled;
105: this .enabled = enabled;
106: firePropertyChange(StringConstants.ENABLED_PROPERTY_CHANGED,
107: Boolean.valueOf(oldValue), Boolean.valueOf(enabled));
108: }
109:
110: public boolean isEnabled() {
111: return enabled;
112: }
113: }
|