1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17package java.beans;
18
19import java.awt.Component;
20import java.awt.Graphics;
21import java.awt.Rectangle;
22import java.util.ArrayList;
23import java.util.Iterator;
24import java.util.List;
25
26import org.apache.harmony.beans.internal.nls.Messages;
27
28public class PropertyEditorSupport implements PropertyEditor {
29
30    Object source = null;
31
32    List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
33
34    Object oldValue = null;
35
36    Object newValue = null;
37
38    public PropertyEditorSupport(Object source) {
39        if (source == null) {
40            throw new NullPointerException(Messages.getString("beans.0C")); //$NON-NLS-1$
41        }
42        this.source = source;
43    }
44
45    public PropertyEditorSupport() {
46        source = this;
47    }
48
49    public void paintValue(Graphics gfx, Rectangle box) {
50    }
51
52    public void setAsText(String text) throws IllegalArgumentException {
53        if (newValue instanceof String) {
54            setValue(text);
55        } else {
56            throw new IllegalArgumentException(text);
57        }
58    }
59
60    public String[] getTags() {
61        return null;
62    }
63
64    public String getJavaInitializationString() {
65        return "???"; //$NON-NLS-1$
66    }
67
68    public String getAsText() {
69        return newValue == null ? "null" : newValue.toString(); //$NON-NLS-1$
70    }
71
72    public void setValue(Object value) {
73        this.oldValue = this.newValue;
74        this.newValue = value;
75        firePropertyChange();
76    }
77
78    public Object getValue() {
79        return newValue;
80    }
81
82    public void setSource(Object source) {
83        if (source == null) {
84            throw new NullPointerException(Messages.getString("beans.0C")); //$NON-NLS-1$
85        }
86        this.source = source;
87    }
88
89    public Object getSource() {
90        return source;
91    }
92
93    public synchronized void removePropertyChangeListener(
94            PropertyChangeListener listener) {
95        if (listeners != null) {
96            listeners.remove(listener);
97        }
98    }
99
100    public synchronized void addPropertyChangeListener(
101            PropertyChangeListener listener) {
102        listeners.add(listener);
103    }
104
105    public Component getCustomEditor() {
106        return null;
107    }
108
109    public boolean supportsCustomEditor() {
110        return false;
111    }
112
113    public boolean isPaintable() {
114        return false;
115    }
116
117    public void firePropertyChange() {
118        if (listeners.size() > 0) {
119            PropertyChangeEvent event = new PropertyChangeEvent(source, null,
120                    oldValue, newValue);
121            Iterator<PropertyChangeListener> iterator = listeners.iterator();
122
123            while (iterator.hasNext()) {
124                PropertyChangeListener listener = iterator.next();
125                listener.propertyChange(event);
126            }
127        }
128    }
129}
130