001: /*
002: * @(#)BeanDemo.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.demos;
027:
028: import java.awt.*;
029: import java.beans.*;
030: import java.io.*;
031: import java.util.EventObject;
032: import basis.DemoButton;
033: import basis.DemoButtonListener;
034: import basis.Bean;
035: import basis.Builder;
036:
037: public class BeanDemo extends Demo implements PropertyChangeListener,
038: VetoableChangeListener {
039: private Bean bean = new Bean();
040: private DemoButton textButton = new DemoButton("Text");
041: private DemoButton colorButton = new DemoButton("Color");
042: private DemoButton dimensionButton = new DemoButton("Dimension");
043: private DemoButton saveButton = new DemoButton("save");
044: private DemoButton loadButton = new DemoButton("load");
045: private static String[] texts = { "Pumpkin", "Broccoli", "Tomato",
046: "Peas", "Lettuce", "Onion", "Potato", "Radish", "Carrot",
047: "Cabbage", "Cauliflower", "Cucumber", "Celery", "Corn" };
048:
049: public BeanDemo() {
050: setLayout(new FlowLayout());
051: add(textButton);
052: add(colorButton);
053: add(dimensionButton);
054: add(saveButton);
055: add(loadButton);
056: bean.addPropertyChangeListener(this );
057: bean.addVetoableChangeListener(this );
058: bean.setLocation(20, 20);
059: add(bean);
060: ButtonListener listener = new ButtonListener();
061: textButton.addDemoButtonListener(listener);
062: colorButton.addDemoButtonListener(listener);
063: dimensionButton.addDemoButtonListener(listener);
064: saveButton.addDemoButtonListener(listener);
065: loadButton.addDemoButtonListener(listener);
066: }
067:
068: private String message(PropertyChangeEvent event) {
069: String name = event.getPropertyName();
070: Object oldValue = null;
071: Object newValue = null;
072: for (int i = 0; i < 2; i++) {
073: Object object = null;
074: object = (i == 0) ? event.getOldValue() : event
075: .getNewValue();
076: if (object instanceof Color) {
077: Color color = (Color) object;
078: int r = color.getRed();
079: int g = color.getGreen();
080: int b = color.getBlue();
081: object = "[" + r + "," + g + "," + b + "]";
082: }
083: if (object instanceof Dimension) {
084: Dimension d = (Dimension) object;
085: object = "[" + d.width + "," + d.height + "]";
086: }
087: if (object instanceof Point) {
088: Point p = (Point) object;
089: object = "[" + p.x + "," + p.y + "]";
090: }
091: if (i == 0) {
092: oldValue = object;
093: } else {
094: newValue = object;
095: }
096: }
097: return name + ": " + oldValue + " => " + newValue;
098: }
099:
100: class ButtonListener implements DemoButtonListener, Serializable {
101: public void buttonPressed(EventObject event) {
102: DemoButton button = (DemoButton) event.getSource();
103: try {
104: if (button == textButton) {
105: int index = (int) (Math.random() * texts.length);
106: bean.setText(texts[index]);
107: bean.repaint();
108: }
109: if (button == colorButton) {
110: bean.setColor(new Color(
111: (int) (Math.random() * 255 * 255 * 255)));
112: bean.repaint();
113: }
114: if (button == dimensionButton) {
115: Dimension size = getSize();
116: int width = 20 + (int) (Math.random() * size.width);
117: int height = 5 + (int) (Math.random() * size.height);
118: bean.setDimension(new Dimension(width, height));
119: bean.repaint();
120: }
121: if (button == saveButton) {
122: try {
123: FileOutputStream fos = new FileOutputStream(
124: "Bean.ser");
125: ObjectOutputStream oos = new ObjectOutputStream(
126: fos);
127: oos.writeObject(bean);
128: oos.close();
129: setStatus("Bean saved.");
130: } catch (IOException ioe) {
131: System.err.println("Error saving Bean: " + ioe);
132: setStatus("Error saving bean!");
133: }
134: }
135: if (button == loadButton) {
136: try {
137: FileInputStream fis = new FileInputStream(
138: "Bean.ser");
139: ObjectInputStream ois = new ObjectInputStream(
140: fis);
141: Bean oldBean = bean;
142: bean = (Bean) ois.readObject();
143: bean.addPropertyChangeListener(BeanDemo.this );
144: bean.addVetoableChangeListener(BeanDemo.this );
145: remove(oldBean);
146: add(bean);
147: BeanDemo.this .repaint();
148: ois.close();
149: setStatus("Bean loaded.");
150: } catch (Exception e) {
151: System.err.println("Error loading Bean: " + e);
152: setStatus("Error loading bean!");
153: }
154: }
155: } catch (PropertyVetoException pve) {
156: PropertyChangeEvent pce = pve.getPropertyChangeEvent();
157: setStatus(message(pce) + " VETOED!");
158: }
159: }
160: }
161:
162: public void propertyChange(PropertyChangeEvent event) {
163: setStatus(message(event));
164: String name = event.getPropertyName();
165: if (name.equals("Dimension")) {
166: validate();
167: }
168: }
169:
170: public void vetoableChange(PropertyChangeEvent event)
171: throws PropertyVetoException {
172: String name = event.getPropertyName();
173: if (name.equals("Text")) {
174: String oldText = (String) event.getOldValue();
175: String newText = (String) event.getNewValue();
176: if (newText.startsWith("C")) {
177: throw new PropertyVetoException(
178: "Don't like vegetables beginning with C!",
179: event);
180: }
181: }
182: if (name.equals("Color")) {
183: Color color = (Color) event.getNewValue();
184: if (color.getRed() + color.getGreen() + color.getBlue() < 300) {
185: throw new PropertyVetoException("Too dark!", event);
186: }
187: }
188: if (name.equals("Dimension")) {
189: Dimension dimension = (Dimension) event.getNewValue();
190: Dimension size = getSize();
191: Point point = BeanDemo.this .bean.getLocation();
192: if (dimension.width < 40 || dimension.height < 10) {
193: throw new PropertyVetoException("Too small!", event);
194: }
195: if (dimension.width > size.width
196: || dimension.height > size.height) {
197: throw new PropertyVetoException("Too big!", event);
198: }
199: }
200: }
201: }
|