Source Code Cross Referenced for BeanDemo.java in  » 6.0-JDK-Modules » j2me » basis » demos » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » basis.demos 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.