Source Code Cross Referenced for TextMetrics.java in  » Ajax » MyGWT » net » mygwt » ui » client » util » 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 » Ajax » MyGWT » net.mygwt.ui.client.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * MyGWT Widget Library
003:         * Copyright(c) 2007, MyGWT.
004:         * licensing@mygwt.net
005:         * 
006:         * http://mygwt.net/license
007:         */
008:        package net.mygwt.ui.client.util;
009:
010:        import net.mygwt.ui.client.MyDOM;
011:
012:        import com.google.gwt.user.client.DOM;
013:        import com.google.gwt.user.client.Element;
014:
015:        /**
016:         * Provides precise pixel measurements for blocks of text so that you can
017:         * determine exactly how high and wide, in pixels, a given block of text will
018:         * be.
019:         */
020:        public class TextMetrics {
021:
022:            private static TextMetrics instance;
023:
024:            static {
025:                instance = new TextMetrics();
026:            }
027:
028:            /**
029:             * Returns the singleton instance.
030:             * 
031:             * @return the text metrics instance
032:             */
033:            public static TextMetrics get() {
034:                return instance;
035:            }
036:
037:            private Element elem;
038:
039:            private TextMetrics() {
040:                elem = DOM.createDiv();
041:                DOM.appendChild(MyDOM.getBody(), elem);
042:                DOM.setStyleAttribute(elem, "position", "absolute");
043:                MyDOM.setLeftTop(elem, -10000, -1000);
044:                MyDOM.setVisibility(elem, false);
045:            }
046:
047:            /**
048:             * Binds this TextMetrics instance to an element from which to copy existing
049:             * CSS styles that can affect the size of the rendered text.
050:             * 
051:             * @param el the element
052:             */
053:            public void bind(Element el) {
054:                DOM.setStyleAttribute(elem, "fontSize", DOM.getStyleAttribute(
055:                        el, "fontSize"));
056:                DOM.setStyleAttribute(elem, "fontStyle", DOM.getStyleAttribute(
057:                        el, "fontStyle"));
058:                DOM.setStyleAttribute(elem, "fontWeight", DOM
059:                        .getStyleAttribute(el, "fontWeight"));
060:            }
061:
062:            /**
063:             * Returns the measured height of the specified text. For multiline text, be
064:             * sure to call {@link #setFixedWidth} if necessary.
065:             * 
066:             * @param text the text to be measured
067:             * @return the height in pixels
068:             */
069:            public int getHeight(String text) {
070:                return getSize(text).height;
071:            }
072:
073:            /**
074:             * Returns the size of the specified text based on the internal element's
075:             * style and width properties.
076:             * 
077:             * @param text the text to measure
078:             * @return the size
079:             */
080:            public Size getSize(String text) {
081:                MyDOM.setInnerHTML(elem, text);
082:                Size size = MyDOM.getSize(elem);
083:                MyDOM.setInnerHTML(elem, "");
084:                return size;
085:            }
086:
087:            /**
088:             * Returns the measured width of the specified text.
089:             * 
090:             * @param text the text to measure
091:             * @return the width in pixels
092:             */
093:            public int getWidth(String text) {
094:                DOM.setStyleAttribute(elem, "width", "auto");
095:                return getSize(text).width;
096:            }
097:
098:            /**
099:             * Sets a fixed width on the internal measurement element. If the text will be
100:             * multiline, you have to set a fixed width in order to accurately measure the
101:             * text height.
102:             * 
103:             * @param width the width to set on the element
104:             */
105:            public void setFixedWidth(int width) {
106:                DOM.setIntStyleAttribute(elem, "width", width);
107:            }
108:
109:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.