Source Code Cross Referenced for PadFormat.java in  » Scripting » Kawa » gnu » text » 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 » Scripting » Kawa » gnu.text 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package gnu.text;
002:
003:        import java.text.*;
004:        import java.text.FieldPosition;
005:        import java.io.Writer;
006:
007:        /** Given a Format, pad the formatted result to a specified width. */
008:
009:        public class PadFormat extends ReportFormat {
010:            /** Minimum width. */
011:            int minWidth;
012:            char padChar;
013:            /** What percentage of padding appears after the data.
014:             * -1 means internal padding (after initial '-' or '+' or '0x' or '0X'). */
015:            int where;
016:
017:            Format fmt;
018:
019:            public PadFormat(Format fmt, int minWidth, char padChar, int where) {
020:                this .fmt = fmt;
021:                this .minWidth = minWidth;
022:                this .padChar = padChar;
023:                this .where = where;
024:            }
025:
026:            public PadFormat(Format fmt, int minWidth) {
027:                this (fmt, minWidth, ' ', 100);
028:            }
029:
030:            public int format(Object[] args, int start, Writer dst,
031:                    FieldPosition fpos) throws java.io.IOException {
032:                return format(fmt, args, start, dst, padChar, minWidth, 1, 0,
033:                        where, fpos);
034:            }
035:
036:            public static int padNeeded(int actualWidth, int minWidth,
037:                    int colInc, int minPad) {
038:                int total = actualWidth + minPad;
039:                if (colInc <= 1)
040:                    colInc = minWidth - total;
041:                while (total < minWidth)
042:                    total += colInc;
043:                return total - actualWidth;
044:            }
045:
046:            public static int format(Format fmt, Object[] args, int start,
047:                    Writer dst, char padChar, int minWidth, int colInc,
048:                    int minPad, int where, FieldPosition fpos)
049:                    throws java.io.IOException {
050:                /*
051:                if (where == 100)
052:                  {
053:                int oldLength = sbuf.length();
054:                fmt.format(obj, sbuf, fpos);
055:                int newLength = sbuf.length();
056:                int pad = padNeeded(newLength - oldLength, minWidth, colInc, minPad);
057:                while (--pad >= 0)
058:                  sbuf.append(padChar);
059:                return start + ?;
060:                  }
061:                 */
062:
063:                StringBuffer tbuf = new StringBuffer(200);
064:                if (fmt instanceof  ReportFormat)
065:                    start = ((ReportFormat) fmt)
066:                            .format(args, start, tbuf, fpos);
067:                else if (fmt instanceof  MessageFormat) {
068:                    // FIXME - only correct if start == 0.
069:                    fmt.format(args, tbuf, fpos);
070:                    start = args.length;
071:                } else {
072:                    fmt.format(args[start], tbuf, fpos);
073:                    start++;
074:                }
075:                int len = tbuf.length();
076:                int pad = padNeeded(len, minWidth, colInc, minPad);
077:                int prefix = 0;
078:                String text = tbuf.toString();
079:                if (pad > 0) {
080:                    if (where == -1) {
081:                        if (len > 0) {
082:                            char ch = text.charAt(0);
083:                            if (ch == '-' || ch == '+') {
084:                                prefix++;
085:                                dst.write(ch);
086:                            }
087:                            if (len - prefix > 2 && text.charAt(prefix) == '0') {
088:                                dst.write('0');
089:                                ch = text.charAt(++prefix);
090:                                if (ch == 'x' || ch == 'X') {
091:                                    prefix++;
092:                                    dst.write(ch);
093:                                }
094:                            }
095:                            if (prefix > 0)
096:                                text = text.substring(prefix);
097:                        }
098:                        where = 0;
099:                    }
100:                    int padAfter = (pad * where) / 100;
101:                    int padBefore = pad - padAfter;
102:                    /*
103:                    if (fpos != null && padBefore > 0)
104:                      {
105:                        // This is still broken in JDK 1.2 beta2.  Check beta3.  FIXME.
106:                        fpos.setBeginIndex(fpos.getBeginIndex() + padBefore);
107:                        fpos.setEndIndex(fpos.getEndIndex() + padBefore);
108:                      }
109:                     */
110:                    while (--padBefore >= 0)
111:                        dst.write(padChar);
112:                    dst.write(text);
113:                    while (--padAfter >= 0)
114:                        dst.write(padChar);
115:                } else {
116:                    dst.write(text);
117:                }
118:                return start;
119:            }
120:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.