Source Code Cross Referenced for ImageButton.java in  » Web-Server » Jigsaw » org » w3c » tools » widgets » 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 » Web Server » Jigsaw » org.w3c.tools.widgets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // ImageButton.java
002:        // $Id: ImageButton.java,v 1.13 2000/08/16 21:37:56 ylafon Exp $
003:        // (c) COPYRIGHT MIT and INRIA, 1997.
004:        // Please first read the full copyright statement in file COPYRIGHT.html
005:
006:        package org.w3c.tools.widgets;
007:
008:        import java.awt.AWTEventMulticaster;
009:        import java.awt.Canvas;
010:        import java.awt.Color;
011:        import java.awt.Container;
012:        import java.awt.Dimension;
013:        import java.awt.Graphics;
014:        import java.awt.Image;
015:        import java.awt.Shape;
016:        import java.awt.event.ActionEvent;
017:        import java.awt.event.ActionListener;
018:        import java.awt.event.MouseAdapter;
019:        import java.awt.event.MouseEvent;
020:
021:        public class ImageButton extends Canvas {
022:
023:            /**
024:             * This MouseListener is used to do all the paint operations
025:             * and to generate ActionEvents when a click occured 
026:             */
027:
028:            private class ImageButtonListener extends MouseAdapter {
029:
030:                public void mousePressed(MouseEvent me) {
031:                    // paint down
032:                    paintShadow(false);
033:                }
034:
035:                public void mouseReleased(MouseEvent me) {
036:                    // paint up
037:                    paintShadow(true);
038:                }
039:
040:                public void mouseClicked(MouseEvent me) {
041:                    // fire a new ActionEvent
042:                    fireActionEvent();
043:                }
044:            }
045:
046:            protected boolean switchable = false;
047:            protected Image img1 = null;
048:            protected Image img2 = null;
049:            protected Image currentImg = null;
050:            private int width = -1;
051:            private int height = -1;
052:            private String command;
053:
054:            transient ActionListener actionListener;
055:
056:            int max(int a, int b) {
057:                return ((a < b) ? b : a);
058:            }
059:
060:            /**
061:             * Gets the size of the Image to calculate the minimum size of the 
062:             * Button
063:             */
064:
065:            protected void initSize() {
066:                if (switchable) {
067:                    width = max(img1.getWidth(this ), img2.getWidth(this ));
068:                    height = max(img1.getHeight(this ), img2.getHeight(this ));
069:                } else {
070:                    width = currentImg.getWidth(this );
071:                    height = currentImg.getHeight(this );
072:                }
073:            }
074:
075:            public void switchImage() {
076:                if (switchable) {
077:                    if (currentImg == img1)
078:                        currentImg = img2;
079:                    else
080:                        currentImg = img1;
081:                }
082:                paintShadow(true);
083:            }
084:
085:            /**
086:             * paint the ImageButton in its initial shape
087:             * @param g A Graphics
088:             */
089:
090:            public void paint(Graphics g) {
091:                paintShadow(true);
092:            }
093:
094:            /**
095:             * paints the ImageButton using double buffering
096:             * @param raised A boolean which shows the state of the button
097:             */
098:
099:            protected void paintShadow(boolean raised) {
100:                Graphics g = getGraphics();
101:                Shape s = g.getClip();
102:                Image dbi;
103:                Graphics dbg;
104:                Color bg = getBackground();
105:                Dimension d = getSize();
106:                int dx;
107:                int dy;
108:
109:                dbi = ImageCache.getImage(this , d.width, d.height);
110:                dbg = dbi.getGraphics();
111:                dbg.setClip(s);
112:                dbg.setColor(bg);
113:                dx = d.width - width;
114:                dy = d.height - height;
115:                dbg.clearRect(0, 0, d.width, d.height);
116:                dbg.fill3DRect(1, 1, d.width - 2, d.height - 2, raised);
117:                dbg.drawImage(currentImg, dx / 2, dy / 2, this );
118:                g.drawImage(dbi, 0, 0, this );
119:            }
120:
121:            /**
122:             * called when more informations about the image are available.
123:             * When the size is available, the ImageButton notifies its container
124:             * that the size may have changed.
125:             * @see java.awt.image.ImageObserver
126:             */
127:
128:            public boolean imageUpdate(Image img, int flaginfo, int x, int y,
129:                    int width, int height) {
130:                // 	if ((flaginfo & (HEIGHT|WIDTH)) != 0) {
131:                // 	    this.width = width;
132:                // 	    this.height = height;
133:                // 	    Container parent = getParent();
134:                // 	    if(parent != null) 
135:                // 		parent.doLayout();
136:                // 	}
137:                initSize();
138:                Container parent = getParent();
139:                if (parent != null)
140:                    parent.doLayout();
141:                return super .imageUpdate(img, flaginfo, x, y, width, height);
142:            }
143:
144:            /**
145:             * Returns the minimum size of the ImageButton
146:             */
147:
148:            public Dimension getMinimumSize() {
149:                return new Dimension(width + 8, height + 8);
150:            }
151:
152:            /**
153:             * Returns the preferred size of the ImageButton
154:             */
155:
156:            public Dimension getPreferredSize() {
157:                return new Dimension(width + 8, height + 8);
158:            }
159:
160:            /**
161:             * Sets the action command String used when an ActionEvent is fired
162:             * @param command The command String
163:             */
164:
165:            public void setActionCommand(String command) {
166:                this .command = command;
167:            }
168:
169:            /**
170:             * Returns the action command String
171:             */
172:
173:            public String getActionCommand() {
174:                return command;
175:            }
176:
177:            /**
178:             * Adds an action listener to this ImageButton
179:             * @param al The ActionListener
180:             */
181:
182:            public synchronized void addActionListener(ActionListener al) {
183:                actionListener = AWTEventMulticaster.add(actionListener, al);
184:            }
185:
186:            /**
187:             * Removes an action listener to this ImageButton
188:             * @param al The ActionListener
189:             */
190:
191:            public synchronized void removeActionListener(ActionListener al) {
192:                actionListener = AWTEventMulticaster.remove(actionListener, al);
193:            }
194:
195:            /**
196:             * fire a new ActionEvent and process it, if some listeners are listening
197:             */
198:
199:            protected void fireActionEvent() {
200:                if (actionListener != null) {
201:                    ActionEvent ae = new ActionEvent(this ,
202:                            ActionEvent.ACTION_PERFORMED, command);
203:                    actionListener.actionPerformed(ae);
204:                }
205:            }
206:
207:            /**
208:             * Construct an ImageButton with the specified action command
209:             * @param img1 The image of this ImageButton
210:             * @param img2 The image of this ImageButton
211:             * @param command The action command String
212:             */
213:
214:            public ImageButton(Image img1, Image img2, String command) {
215:                this .switchable = true;
216:                this .img1 = img1;
217:                this .img2 = img2;
218:                this .currentImg = img1;
219:                this .command = command;
220:                addMouseListener(new ImageButtonListener());
221:                prepareImage(img1, this );
222:                prepareImage(img2, this );
223:                initSize();
224:            }
225:
226:            /**
227:             * Construct an ImageButton with the specified action command
228:             * @param img1 The image of this ImageButton
229:             * @param realesed The image of this ImageButton
230:             */
231:
232:            public ImageButton(Image img1, Image img2) {
233:                this (img1, img2, "");
234:            }
235:
236:            /**
237:             * Construct an ImageButton with the specified action command
238:             * @param img The image of this ImageButton
239:             * @param command The action command String
240:             */
241:
242:            public ImageButton(Image img, String command) {
243:                this .switchable = false;
244:                this .currentImg = img;
245:                this .command = command;
246:                addMouseListener(new ImageButtonListener());
247:                prepareImage(currentImg, this );
248:                initSize();
249:            }
250:
251:            /**
252:             * Construct an ImageButton with no action command
253:             * @param img The image of this ImageButton
254:             */
255:
256:            public ImageButton(Image img) {
257:                this (img, "");
258:            }
259:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.