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 org.apache.harmony.beans.editors;
019:
020: import java.awt.Component;
021: import java.awt.Font;
022: import java.awt.Graphics;
023: import java.awt.Panel;
024: import java.awt.Rectangle;
025: import java.beans.PropertyChangeEvent;
026: import java.beans.PropertyChangeListener;
027: import java.beans.PropertyEditor;
028: import java.util.ArrayList;
029: import java.util.Iterator;
030: import java.util.List;
031:
032: @SuppressWarnings("serial")
033: public class FontEditor extends Panel implements PropertyEditor {
034:
035: List<PropertyChangeListener> listeners = new ArrayList<PropertyChangeListener>();
036:
037: private Font value;
038:
039: private Object source;
040:
041: public FontEditor(Object source) {
042: if (source == null) {
043: throw new NullPointerException();
044: }
045: this .source = source;
046: }
047:
048: public FontEditor() {
049: super ();
050: }
051:
052: public Component getCustomEditor() {
053: return this ;
054: }
055:
056: public boolean supportsCustomEditor() {
057: return true;
058: }
059:
060: public String getJavaInitializationString() {
061: String result = null;
062: if (value != null) {
063: String name = value.getName();
064: int style = value.getStyle();
065: int size = value.getSize();
066: result = "new Font(" + name + "," + style + "," + size + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
067: }
068: return result;
069: }
070:
071: public String[] getTags() {
072: return null;
073: }
074:
075: public void setValue(Object newValue) {
076: Object oldValue = value;
077: value = (Font) newValue;
078: PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(
079: this , "value", oldValue, value); //$NON-NLS-1$
080: PropertyChangeListener[] copy = new PropertyChangeListener[listeners
081: .size()];
082: listeners.toArray(copy);
083: for (PropertyChangeListener listener : copy) {
084: listener.propertyChange(changeAllEvent);
085: }
086: }
087:
088: public boolean isPaintable() {
089: return true;
090: }
091:
092: public void paintValue(Graphics gfx, Rectangle box) {
093: Font font = (Font) getValue();
094: if (font != null) {
095: gfx.setFont(font);
096: gfx
097: .drawBytes(
098: "Hello".getBytes(), box.x, box.y, box.x + box.width, //$NON-NLS-1$
099: box.y + box.height);
100: }
101: }
102:
103: public String getAsText() {
104: return null;
105: }
106:
107: public Object getValue() {
108: return value;
109: }
110:
111: public void setAsText(String text) throws IllegalArgumentException {
112: throw new IllegalArgumentException(text == null ? text : value
113: .toString());
114: }
115:
116: @Override
117: public synchronized void removePropertyChangeListener(
118: PropertyChangeListener listener) {
119: if (listeners != null) {
120: listeners.remove(listener);
121: }
122: }
123:
124: @Override
125: public synchronized void addPropertyChangeListener(
126: PropertyChangeListener listener) {
127: listeners.add(listener);
128: }
129:
130: public void firePropertyChange() {
131: if (listeners.isEmpty()) {
132: return;
133: }
134:
135: List<PropertyChangeListener> copy = new ArrayList<PropertyChangeListener>(
136: listeners.size());
137: synchronized (listeners) {
138: copy.addAll(listeners);
139: }
140:
141: PropertyChangeEvent changeAllEvent = new PropertyChangeEvent(
142: source, null, null, null);
143: for (Iterator<PropertyChangeListener> listenersItr = copy
144: .iterator(); listenersItr.hasNext();) {
145: PropertyChangeListener listna = listenersItr.next();
146: listna.propertyChange(changeAllEvent);
147: }
148: }
149: }
|