Source Code Cross Referenced for JGraphpadRichTextValue.java in  » Graphic-Library » jgraphpad » com » jgraph » pad » graph » 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 » Graphic Library » jgraphpad » com.jgraph.pad.graph 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * $Id: JGraphpadRichTextValue.java,v 1.2 2006/01/30 15:33:27 gaudenz Exp $
003:         * Copyright (c) 2001-2005, Gaudenz Alder
004:         * 
005:         * All rights reserved.
006:         * 
007:         * See LICENSE file for license details. If you are unable to locate
008:         * this file please contact info (at) jgraph (dot) com.
009:         */
010:        package com.jgraph.pad.graph;
011:
012:        import java.io.ByteArrayInputStream;
013:        import java.io.ByteArrayOutputStream;
014:        import java.io.Serializable;
015:
016:        import javax.swing.text.BadLocationException;
017:        import javax.swing.text.Document;
018:        import javax.swing.text.rtf.RTFEditorKit;
019:
020:        /**
021:         * Rich text replacement for string values in {@link JGraphpadBusinessObject}.
022:         * This requirs the {@link JGraphpadVertexView} to provide a rich text editor
023:         * and renderer.
024:         * 
025:         * @see JGraphpadGraphModel
026:         * @see JGraphpadVertexRenderer
027:         * @see RTFEditorKit
028:         */
029:        public class JGraphpadRichTextValue implements  Serializable, Cloneable {
030:
031:            /**
032:             * Holds the shared editor kit for creating new documents.
033:             */
034:            public static RTFEditorKit editorKit = new RTFEditorKit();
035:
036:            /**
037:             * Holds the rich text as an RTF encoded text.
038:             */
039:            protected String richText;
040:
041:            /**
042:             * A plain-text representation of the rich text is always keps along with
043:             * the rich text value to speedup the {@link #toString()} method.
044:             */
045:            protected String plainText;
046:
047:            /**
048:             * Constructs a new empty rich text value.
049:             */
050:            public JGraphpadRichTextValue() {
051:                // empty
052:            }
053:
054:            /**
055:             * Constructs a new rich text value using the specified document.
056:             * 
057:             * @param document
058:             *            The document to obtain the rich text from.
059:             */
060:            public JGraphpadRichTextValue(Document document) {
061:                setRichText(getRichText(document));
062:            }
063:
064:            /**
065:             * Constructs a new rich text document using the string text.
066:             * 
067:             * @param stringValue
068:             *            The string to use as the initial value.
069:             */
070:            public JGraphpadRichTextValue(String stringValue) {
071:                this (createDefaultDocument(stringValue));
072:            }
073:
074:            /**
075:             * Inserts this rich text into the specified component. This implementation
076:             * silently ignores all exceptions.
077:             * 
078:             * @param document
079:             *            The document to insert the rich text into.
080:             */
081:            public void insertInto(Document document) {
082:                ByteArrayInputStream bin = new ByteArrayInputStream(richText
083:                        .getBytes());
084:                try {
085:                    document.remove(0, document.getLength());
086:                    editorKit.read(bin, document, 0);
087:                    bin.close();
088:                } catch (Exception e) {
089:                    // ignore
090:                }
091:            }
092:
093:            /**
094:             * Returns the richt text value as an RTF encoded string.
095:             * 
096:             * @return Returns the rich text.
097:             */
098:            public String getRichText() {
099:                return richText;
100:            }
101:
102:            /**
103:             * Sets the richt text value as an RTF encoded string and updates
104:             * {@link #plainText}.
105:             * 
106:             * @param richText
107:             *            The rich text to set.
108:             */
109:            public void setRichText(String richText) {
110:                this .richText = richText;
111:                // Updates the plain text version of the rich text.
112:                plainText = getPlainText(this );
113:            }
114:
115:            /**
116:             * Returns the plain text representation of this rich text value.
117:             * 
118:             * @return Returns {@link #plainText}.
119:             */
120:            public String toString() {
121:                return plainText;
122:            }
123:
124:            /**
125:             * Returns the rich text encoded RTF string from the specified document.
126:             * 
127:             * @param document
128:             *            The document to be converted.
129:             * @return Returns the RTF encoded document.
130:             */
131:            public static String getRichText(Document document) {
132:                ByteArrayOutputStream bos = new ByteArrayOutputStream();
133:                try {
134:                    editorKit.write(bos, document, 0, document.getLength());
135:                    bos.flush();
136:                } catch (Exception e) {
137:                    // ignore
138:                }
139:                return new String(bos.toByteArray());
140:            }
141:
142:            /**
143:             * Returns a plain text representation of the specified rich text value. If
144:             * an exception occurs during conversion then the RTF encoded string is
145:             * returned instead.
146:             * 
147:             * @param richText
148:             *            The rich text value to be converted.
149:             * @return Returns the plain text representation.
150:             */
151:            public static String getPlainText(JGraphpadRichTextValue richText) {
152:                Document doc = createDefaultDocument();
153:                richText.insertInto(doc);
154:                try {
155:                    return doc.getText(0, doc.getLength()).trim();
156:                } catch (BadLocationException e) {
157:                    // ignore
158:                }
159:                return richText.getRichText();
160:            }
161:
162:            /**
163:             * Hook for subclassers to create a default document. This implementation
164:             * uses {@link #createDefaultDocument(String)} with a value of null.
165:             * 
166:             * @return Returns a new empty default document.
167:             */
168:            protected static Document createDefaultDocument() {
169:                return createDefaultDocument(null);
170:            }
171:
172:            /**
173:             * Hook for subclassers to create a default document. This implementation
174:             * uses {@link #editorKit} to create the document and sets its value.
175:             * 
176:             * @return Returns a new empty default document.
177:             */
178:            public static Document createDefaultDocument(String value) {
179:                Document document = editorKit.createDefaultDocument();
180:                if (value != null) {
181:                    try {
182:                        // FIXME: Use font for "" (GraphConstants.DEFAULTFONT)
183:                        document.insertString(0, value, null);
184:                    } catch (BadLocationException e1) {
185:                        // ignore
186:                    }
187:                }
188:                return document;
189:            }
190:
191:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.