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: }
|