Source Code Cross Referenced for BasicButtonUI.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » plaf » basic » 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 » javax package » javax.swing.plaf.basic 
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 Alexander T. Simbirtsev
019:         * @version $Revision$
020:         */package javax.swing.plaf.basic;
021:
022:        import java.awt.Color;
023:        import java.awt.Dimension;
024:        import java.awt.Graphics;
025:        import java.awt.Rectangle;
026:        import java.util.EventListener;
027:
028:        import javax.swing.AbstractButton;
029:        import javax.swing.Icon;
030:        import javax.swing.InputMap;
031:        import javax.swing.JComponent;
032:        import javax.swing.LookAndFeel;
033:        import javax.swing.SwingUtilities;
034:        import javax.swing.UIManager;
035:        import javax.swing.plaf.ButtonUI;
036:        import javax.swing.plaf.ComponentUI;
037:
038:        import org.apache.harmony.x.swing.ButtonCommons;
039:        import org.apache.harmony.x.swing.Utilities;
040:
041:        public class BasicButtonUI extends ButtonUI {
042:
043:            private static final String PROPERTY_PREFIX = "Button.";
044:            private static BasicButtonUI basicButtonUI;
045:
046:            protected int defaultTextIconGap;
047:
048:            protected int defaultTextShiftOffset;
049:
050:            private int textShiftOffset = 0;
051:
052:            private Color focusColor;
053:            private Color disabledTextColor;
054:
055:            private final Rectangle viewR = new Rectangle();
056:            private final Rectangle iconR = new Rectangle();
057:            private final Rectangle textR = new Rectangle();
058:
059:            public static ComponentUI createUI(final JComponent c) {
060:                if (basicButtonUI == null) {
061:                    basicButtonUI = new BasicButtonUI();
062:                }
063:                return basicButtonUI;
064:            }
065:
066:            public void installUI(final JComponent c) {
067:                final AbstractButton button = (AbstractButton) c;
068:                installDefaults(button);
069:                installListeners(button);
070:                installKeyboardActions(button);
071:            }
072:
073:            public void uninstallUI(final JComponent c) {
074:                final AbstractButton button = (AbstractButton) c;
075:                uninstallKeyboardActions(button);
076:                uninstallListeners(button);
077:                uninstallDefaults(button);
078:            }
079:
080:            public Dimension getPreferredSize(final JComponent c) {
081:                return ButtonCommons.getPreferredSize((AbstractButton) c, null);
082:            }
083:
084:            public int getDefaultTextIconGap(final AbstractButton button) {
085:                return defaultTextIconGap;
086:            }
087:
088:            public void paint(final Graphics g, final JComponent c) {
089:                final AbstractButton button = (AbstractButton) c;
090:                viewR.setBounds(0, 0, 0, 0);
091:                String clippedText = ButtonCommons.getPaintingParameters(
092:                        button, viewR, iconR, textR, button.getIcon());
093:
094:                paintButtonPressed(g, button);
095:                paintIcon(g, button, iconR);
096:                paintText(g, button, textR, clippedText);
097:                if (isFocusPainted(button)) {
098:                    paintFocus(g, button, viewR, textR, iconR);
099:                }
100:            }
101:
102:            protected void paintButtonPressed(final Graphics g,
103:                    final AbstractButton button) {
104:            }
105:
106:            protected void paintIcon(final Graphics g, final JComponent c,
107:                    final Rectangle iconRect) {
108:                final Icon icon = ButtonCommons
109:                        .getCurrentIcon((AbstractButton) c);
110:                if (icon != null) {
111:                    icon.paintIcon(c, g, iconRect.x, iconRect.y);
112:                }
113:            }
114:
115:            protected void paintText(final Graphics g, final AbstractButton b,
116:                    final Rectangle textRect, final String text) {
117:                paintText(g, (JComponent) b, textRect, text);
118:            }
119:
120:            protected void paintText(final Graphics g, final JComponent c,
121:                    final Rectangle textRect, final String text) {
122:                final AbstractButton b = (AbstractButton) c;
123:                final Color color = b.isEnabled() ? b.getForeground()
124:                        : disabledTextColor;
125:
126:                final int currentTextShiftOffset = getTextShiftOffset();
127:                textRect.translate(currentTextShiftOffset,
128:                        currentTextShiftOffset);
129:                ButtonCommons.paintText(g, b, textRect, text, color);
130:            }
131:
132:            protected void paintFocus(final Graphics g, final AbstractButton b,
133:                    final Rectangle viewRect, final Rectangle textRect,
134:                    final Rectangle iconRect) {
135:
136:                ButtonCommons.paintFocus(g, ButtonCommons.getFocusRect(
137:                        viewRect, textRect, iconRect), focusColor);
138:            }
139:
140:            protected BasicButtonListener createButtonListener(
141:                    final AbstractButton button) {
142:                return new BasicButtonListener(button);
143:            }
144:
145:            protected void installListeners(final AbstractButton button) {
146:                BasicButtonListener listener = createButtonListener(button);
147:                button.addPropertyChangeListener(listener);
148:                button.addChangeListener(listener);
149:                button.addMouseListener(listener);
150:                button.addMouseMotionListener(listener);
151:                button.addFocusListener(listener);
152:            }
153:
154:            protected void uninstallListeners(final AbstractButton button) {
155:                BasicButtonListener listener = getBasicButtonListener(button);
156:                button.removePropertyChangeListener(listener);
157:                button.removeChangeListener(listener);
158:                button.removeMouseListener(listener);
159:                button.removeMouseMotionListener(listener);
160:                button.removeFocusListener(listener);
161:            }
162:
163:            protected void installKeyboardActions(final AbstractButton button) {
164:                BasicButtonListener listener = getBasicButtonListener(button);
165:                if (listener != null) {
166:                    listener.installKeyboardActions(button);
167:                }
168:                SwingUtilities.replaceUIInputMap(button,
169:                        JComponent.WHEN_FOCUSED, (InputMap) UIManager
170:                                .get(getPropertyPrefix() + "focusInputMap"));
171:            }
172:
173:            protected void uninstallKeyboardActions(final AbstractButton button) {
174:                BasicButtonListener listener = getBasicButtonListener(button);
175:                if (listener != null) {
176:                    listener.uninstallKeyboardActions(button);
177:                }
178:                SwingUtilities.replaceUIInputMap(button,
179:                        JComponent.WHEN_FOCUSED, null);
180:            }
181:
182:            protected void installDefaults(final AbstractButton button) {
183:                LookAndFeel.installColorsAndFont(button, getPropertyPrefix()
184:                        + "background", getPropertyPrefix() + "foreground",
185:                        getPropertyPrefix() + "font");
186:
187:                LookAndFeel.installProperty(button, "opaque", Boolean.TRUE);
188:                LookAndFeel.installBorder(button, getPropertyPrefix()
189:                        + "border");
190:                LookAndFeel.installProperty(button, "alignmentX", new Float(0));
191:                defaultTextIconGap = UIManager.getInt(getPropertyPrefix()
192:                        + "textIconGap");
193:                defaultTextShiftOffset = UIManager.getInt(getPropertyPrefix()
194:                        + "textShiftOffset");
195:
196:                button.setMargin(UIManager.getInsets(getPropertyPrefix()
197:                        + "margin"));
198:
199:                disabledTextColor = button.getBackground().darker();
200:                focusColor = UIManager.getColor("activeCaptionBorder");
201:            }
202:
203:            protected void uninstallDefaults(final AbstractButton button) {
204:                LookAndFeel.uninstallBorder(button);
205:                Utilities.uninstallColorsAndFont(button);
206:                if (Utilities.isUIResource(button.getMargin())) {
207:                    button.setMargin(null);
208:                }
209:            }
210:
211:            protected String getPropertyPrefix() {
212:                return PROPERTY_PREFIX;
213:            }
214:
215:            protected void setTextShiftOffset() {
216:                textShiftOffset = defaultTextShiftOffset;
217:            }
218:
219:            protected void clearTextShiftOffset() {
220:                textShiftOffset = 0;
221:            }
222:
223:            protected int getTextShiftOffset() {
224:                return textShiftOffset;
225:            }
226:
227:            private boolean isFocusPainted(final AbstractButton button) {
228:                return (button.isEnabled() && button.isFocusPainted()
229:                        && button.isFocusOwner() && (button.getIcon() != null || !Utilities
230:                        .isEmptyString(button.getText())));
231:            }
232:
233:            private BasicButtonListener getBasicButtonListener(
234:                    final AbstractButton button) {
235:                EventListener[] listeners = button.getPropertyChangeListeners();
236:                for (int i = 0; i < listeners.length; i++) {
237:                    if (listeners[i] instanceof  BasicButtonListener) {
238:                        return (BasicButtonListener) listeners[i];
239:                    }
240:                }
241:
242:                return null;
243:            }
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.