Source Code Cross Referenced for ButtonStateController.java in  » Apache-Harmony-Java-SE » org-package » org » apache » harmony » awt » 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 » Apache Harmony Java SE » org package » org.apache.harmony.awt 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:         * @author Dmitry A. Durnev
019:         * @version $Revision$
020:         */package org.apache.harmony.awt;
021:
022:        import java.awt.Component;
023:        import java.awt.event.FocusEvent;
024:        import java.awt.event.FocusListener;
025:        import java.awt.event.KeyEvent;
026:        import java.awt.event.KeyListener;
027:        import java.awt.event.MouseEvent;
028:        import java.awt.event.MouseListener;
029:
030:        import org.apache.harmony.awt.internal.nls.Messages;
031:
032:        /**
033:         * ButtonStateController.
034:         * Changes Component state and fires [action] events in response to
035:         * user input(key, mouse, focus events) and current state.
036:         * Repaints component when necessary.
037:         * Is typically used by Components such as Button, Checkbox, etc
038:         * as input event listener. Such components also query their state
039:         * properties, which are not stored in the Component-derived class,
040:         * for example isPressed(), with state controller.
041:         */
042:        public abstract class ButtonStateController implements  MouseListener,
043:                FocusListener, KeyListener {
044:
045:            private static Component activeComponent;
046:
047:            private boolean mousePressed = false;
048:            private boolean keyPressed = false;
049:            private boolean mouseInside = false;
050:            private boolean focused = false;
051:            private final Component component;
052:
053:            /**
054:             * Store input event properties
055:             * to be able to retrieve them later
056:             * inside fireEvent() implementation
057:             * which typically uses them to 
058:             * fire action/item event
059:             */
060:            private long when;
061:            private int mod;
062:
063:            public ButtonStateController(Component comp) {
064:                component = comp;
065:            }
066:
067:            public boolean isPressed() {
068:                return ((mousePressed && mouseInside) || keyPressed);
069:            }
070:
071:            public void mousePressed(MouseEvent me) {
072:                if (mousePressed || keyPressed
073:                        || (me.getButton() != MouseEvent.BUTTON1) || !lock()) {
074:                    return;
075:                }
076:
077:                mousePressed = true;
078:
079:                if (mouseInside) {
080:                    component.repaint();
081:                }
082:
083:                if (component.isFocusable()) {
084:                    component.requestFocus();
085:                }
086:            }
087:
088:            public void mouseReleased(MouseEvent me) {
089:                if (!mousePressed || (me.getButton() != MouseEvent.BUTTON1)
090:                        || !unlock()) {
091:                    return;
092:                }
093:
094:                mousePressed = false;
095:                component.repaint();
096:
097:                if (mouseInside) {
098:                    when = me.getWhen();
099:                    mod = me.getModifiers();
100:                    fireEvent();
101:                }
102:            }
103:
104:            public void keyPressed(KeyEvent ke) {
105:                // awt.54=Key event for unfocused component
106:                assert focused == true : Messages.getString("awt.54"); //$NON-NLS-1$
107:
108:                if (mousePressed || keyPressed
109:                        || (ke.getKeyCode() != KeyEvent.VK_SPACE) || !lock()) {
110:                    return;
111:                }
112:
113:                keyPressed = true;
114:                component.repaint();
115:            }
116:
117:            public void keyReleased(KeyEvent ke) {
118:                // awt.54=Key event for unfocused component
119:                assert focused == true : Messages.getString("awt.54"); //$NON-NLS-1$
120:
121:                if (!keyPressed || (ke.getKeyCode() != KeyEvent.VK_SPACE)
122:                        || !unlock()) {
123:                    return;
124:                }
125:
126:                keyPressed = false;
127:                component.repaint();
128:                when = ke.getWhen();
129:                mod = ke.getModifiers();
130:                fireEvent();
131:            }
132:
133:            public void mouseEntered(MouseEvent me) {
134:                // awt.55=Double mouse enter event for component
135:                assert mouseInside == false : Messages.getString("awt.55"); //$NON-NLS-1$
136:                mouseCrossed(true);
137:            }
138:
139:            public void mouseExited(MouseEvent me) {
140:                // awt.56=Double mouse exit event for component
141:                assert mouseInside == true : Messages.getString("awt.56"); //$NON-NLS-1$
142:                mouseCrossed(false);
143:            }
144:
145:            public void focusGained(FocusEvent fe) {
146:                // awt.57=Double focus gained event for component
147:                assert focused == false : Messages.getString("awt.57"); //$NON-NLS-1$
148:
149:                focused = true;
150:                component.repaint();
151:            }
152:
153:            public void focusLost(FocusEvent fe) {
154:                // awt.58=Double focus lost event for component
155:                assert focused == true : Messages.getString("awt.58"); //$NON-NLS-1$
156:
157:                unlock();
158:                focused = false;
159:                keyPressed = false;
160:                mousePressed = false;
161:                component.repaint();
162:            }
163:
164:            //Ignored
165:            public void keyTyped(KeyEvent ke) {
166:            }
167:
168:            public void mouseClicked(MouseEvent me) {
169:            }
170:
171:            private void mouseCrossed(boolean inside) {
172:                mouseInside = inside;
173:                if (mousePressed && focused) {
174:                    component.repaint();
175:                }
176:            }
177:
178:            public final long getWhen() {
179:                return when;
180:            }
181:
182:            public final int getMod() {
183:                return mod;
184:            }
185:
186:            protected abstract void fireEvent();
187:
188:            /**
189:             * Acquires the lock for this button. If the lock is acquired no other
190:             * buttons could be pressed until the lock is released.
191:             * 
192:             * @return true if the lock has been successfully acquired, otherwise
193:             *         returns false.
194:             */
195:            private boolean lock() {
196:                if (activeComponent != null) {
197:                    return false;
198:                }
199:
200:                activeComponent = component;
201:                return true;
202:            }
203:
204:            /**
205:             * Releases the lock.
206:             * 
207:             * @return true if the lock has been released, otherwise returns false.
208:             */
209:            private boolean unlock() {
210:                if (activeComponent != component) {
211:                    return false;
212:                }
213:
214:                activeComponent = null;
215:                return true;
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.