001: /*
002: * @(#)Bean.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis;
027:
028: import java.awt.*;
029: import java.beans.*;
030: import java.io.Serializable;
031:
032: public class Bean extends Component implements Serializable {
033: private static final long serialVersionUID = 8683961066421525018L;
034: private PropertyChangeSupport pcs;
035: private VetoableChangeSupport vcs;
036:
037: public Bean() {
038: super .setName("Bean");
039: super .setBackground(Builder.SUN_RED);
040: super .setForeground(Color.black);
041: super .setSize(60, 20);
042: pcs = new PropertyChangeSupport(this );
043: vcs = new VetoableChangeSupport(this );
044: }
045:
046: public void addPropertyChangeListener(
047: PropertyChangeListener listener) {
048: pcs.addPropertyChangeListener(listener);
049: }
050:
051: public void removePropertyChangeListener(
052: PropertyChangeListener listener) {
053: pcs.removePropertyChangeListener(listener);
054: }
055:
056: public void addVetoableChangeListener(
057: VetoableChangeListener listener) {
058: vcs.addVetoableChangeListener(listener);
059: }
060:
061: public void removeVetoableChangeListener(
062: VetoableChangeListener listener) {
063: vcs.removeVetoableChangeListener(listener);
064: }
065:
066: public void setText(String text) throws PropertyVetoException {
067: String oldText = getName();
068: // NOTE: The vetoableChange method of the listeners will not be called
069: // if the old and new values are the same...
070: vcs.fireVetoableChange("Text", oldText, text);
071: super .setName(text);
072: pcs.firePropertyChange("Text", oldText, text);
073: }
074:
075: public void setColor(Color color) throws PropertyVetoException {
076: Color oldColor = getBackground();
077: vcs.fireVetoableChange("Color", oldColor, color);
078: super .setBackground(color);
079: pcs.firePropertyChange("Color", oldColor, color);
080: }
081:
082: public void setDimension(Dimension dimension)
083: throws PropertyVetoException {
084: Dimension oldDimension = getSize();
085: vcs.fireVetoableChange("Dimension", oldDimension, dimension);
086: super .setSize(dimension);
087: pcs.firePropertyChange("Dimension", oldDimension, dimension);
088: }
089:
090: public void setPosition(Point position)
091: throws PropertyVetoException {
092: Point oldPosition = getLocation();
093: vcs.fireVetoableChange("Position", oldPosition, position);
094: super .setLocation(position);
095: pcs.firePropertyChange("Position", oldPosition, position);
096: }
097:
098: public Dimension getPreferredSize() {
099: return getSize();
100: }
101:
102: public Dimension getMinimumSize() {
103: return getSize();
104: }
105:
106: public Dimension getMaximumSize() {
107: return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
108: }
109:
110: public void paint(Graphics g) {
111: String text = getName();
112: Color color = getBackground();
113: Dimension dimension = getSize();
114: g.setColor(color);
115: g.fillRect(0, 0, dimension.width, dimension.height);
116: g.setColor(Color.black);
117: Font font = new Font("sanserif", Font.PLAIN, 12);
118: g.setFont(font);
119: FontMetrics fm = g.getFontMetrics(font);
120: int w = fm.stringWidth(text);
121: int h = fm.getHeight();
122: g.drawString(text, (dimension.width - w) / 2, h
123: + (dimension.height - h) / 2 - 3);
124: }
125: }
|