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: package java.beans;
018:
019: import java.awt.Component;
020: import java.awt.Graphics;
021: import java.awt.Rectangle;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.harmony.beans.internal.nls.Messages;
027:
028: public class PropertyEditorSupport implements PropertyEditor {
029:
030: Object source = null;
031:
032: List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
033:
034: Object oldValue = null;
035:
036: Object newValue = null;
037:
038: public PropertyEditorSupport(Object source) {
039: if (source == null) {
040: throw new NullPointerException(Messages
041: .getString("beans.0C")); //$NON-NLS-1$
042: }
043: this .source = source;
044: }
045:
046: public PropertyEditorSupport() {
047: source = this ;
048: }
049:
050: public void paintValue(Graphics gfx, Rectangle box) {
051: // expected
052: }
053:
054: public void setAsText(String text) throws IllegalArgumentException {
055: if (newValue instanceof String) {
056: setValue(text);
057: } else {
058: throw new IllegalArgumentException(text);
059: }
060: }
061:
062: public String[] getTags() {
063: return null;
064: }
065:
066: public String getJavaInitializationString() {
067: return "???"; //$NON-NLS-1$
068: }
069:
070: public String getAsText() {
071: return newValue == null ? "null" : newValue.toString(); //$NON-NLS-1$
072: }
073:
074: public void setValue(Object value) {
075: this .oldValue = this .newValue;
076: this .newValue = value;
077: firePropertyChange();
078: }
079:
080: public Object getValue() {
081: return newValue;
082: }
083:
084: public void setSource(Object source) {
085: this .source = source;
086: }
087:
088: public Object getSource() {
089: return source;
090: }
091:
092: public synchronized void removePropertyChangeListener(
093: PropertyChangeListener listener) {
094: if (listeners != null) {
095: listeners.remove(listener);
096: }
097: }
098:
099: public synchronized void addPropertyChangeListener(
100: PropertyChangeListener listener) {
101: listeners.add(listener);
102: }
103:
104: public Component getCustomEditor() {
105: return null;
106: }
107:
108: public boolean supportsCustomEditor() {
109: return false;
110: }
111:
112: public boolean isPaintable() {
113: return false;
114: }
115:
116: public void firePropertyChange() {
117: if (listeners.isEmpty()) {
118: return;
119: }
120:
121: List<PropertyChangeListener> copy = new ArrayList<PropertyChangeListener>(
122: listeners.size());
123: synchronized (listeners) {
124: copy.addAll(listeners);
125: }
126:
127: PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(
128: source, null, null, null);
129: for (Iterator<PropertyChangeListener> listenersItr = copy
130: .iterator(); listenersItr.hasNext();) {
131: PropertyChangeListener listna = listenersItr.next();
132: listna.propertyChange(changeAllEvent);
133: }
134: }
135: }
|