KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > forms > widgets > ScrolledFormText


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.forms.widgets;
12 import java.io.InputStream JavaDoc;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.widgets.*;
16 /**
17  * ScrolledFormText is a control that is capable of scrolling an instance of
18  * the FormText class. It should be created in a parent that will allow it to
19  * use all the available area (for example, a shell, a view or an editor). The
20  * form text can be created by the class itself, or set from outside. In the
21  * later case, the form text instance must be a direct child of the
22  * ScrolledFormText instance.
23  * <p>
24  * The class assumes that text to be rendered contains formatting tags. In case
25  * of a string, it will enclose the text in 'form' root element if missing from
26  * the text as a convinience. For example:
27  *
28  * <pre>
29  * ftext.setText(&quot;&lt;p&gt;Some text here&lt;/&gt;&quot;);
30  * </pre>
31  *
32  * will not cause an error. The same behavior does not exist for content from
33  * the input stream, however - it must be well formed in that case.
34  * </p>
35
36  * @since 3.0
37  * @see FormText
38  */

39 public class ScrolledFormText extends SharedScrolledComposite {
40     private FormText content;
41     private String JavaDoc text;
42     /**
43      * Creates the new scrolled text instance in the provided parent
44      *
45      * @param parent
46      * the parent composite
47      * @param createFormText
48      * if <code>true</code>, enclosing form text instance will be
49      * created in this constructor.
50      */

51     public ScrolledFormText(Composite parent, boolean createFormText) {
52         this(parent, SWT.V_SCROLL | SWT.H_SCROLL, createFormText);
53     }
54     /**
55      * Creates the new scrolled text instance in the provided parent
56      *
57      * @param parent
58      * the parent composite
59      * @param style
60      * the style to pass to the scrolled composite
61      * @param createFormText
62      * if <code>true</code>, enclosing form text instance will be
63      * created in this constructor.
64      */

65     public ScrolledFormText(Composite parent, int style, boolean createFormText) {
66         super(parent, style);
67         if (createFormText)
68             setFormText(new FormText(this, SWT.NULL));
69     }
70     /**
71      * Sets the form text to be managed by this scrolled form text. The
72      * instance must be a direct child of this class. If this method is used,
73      * <code>false</code> must be passed in either of the constructors to
74      * avoid creating form text instance.
75      *
76      * @param formText
77      * the form text instance to use.
78      */

79     public void setFormText(FormText formText) {
80         this.content = formText;
81         super.setContent(content);
82         content.setMenu(getMenu());
83         if (text != null)
84             loadText(text);
85     }
86     /**
87      * Sets the foreground color of the scrolled form text.
88      *
89      * @param fg
90      * the foreground color
91      */

92     public void setForeground(Color fg) {
93         super.setForeground(fg);
94         if (content != null)
95             content.setForeground(fg);
96     }
97     /**
98      * Sets the background color of the scrolled form text.
99      *
100      * @param bg
101      * the background color
102      */

103     public void setBackground(Color bg) {
104         super.setBackground(bg);
105         if (content != null)
106             content.setBackground(bg);
107     }
108     /**
109      * The class sets the content widget. This method should not be called by
110      * classes that instantiate this widget.
111      *
112      * @param c
113      * content control
114      */

115     public final void setContent(Control c) {
116     }
117     /**
118      * Sets the text to be rendered in the scrolled form text. The text must
119      * contain formatting tags.
120      *
121      * @param text
122      * the text to be rendered
123      */

124     public void setText(String JavaDoc text) {
125         this.text = text;
126         loadText(text);
127         reflow(true);
128     }
129     /**
130      * Sets the contents to rendered in the scrolled form text. The stream must
131      * contain formatting tags. The caller is responsible for closing the input
132      * stream. The call may be long running. For best results, call this method
133      * from another thread and call 'reflow' when done (but make both calls
134      * using 'Display.asyncExec' because these calls must be made in the event
135      * dispatching thread).
136      *
137      * @param is
138      * content input stream
139      */

140     public void setContents(InputStream JavaDoc is) {
141         loadContents(is);
142     }
143     /**
144      * Returns the instance of the form text.
145      *
146      * @return the form text instance
147      */

148     public FormText getFormText() {
149         return content;
150     }
151     private void loadText(String JavaDoc text) {
152         if (content != null) {
153             String JavaDoc markup = text;
154             if (!markup.startsWith("<form>")) //$NON-NLS-1$
155
markup = "<form>" + text + "</form>"; //$NON-NLS-1$//$NON-NLS-2$
156
content.setText(markup, true, false);
157         }
158     }
159     private void loadContents(InputStream JavaDoc is) {
160         if (content != null) {
161             content.setContents(is, false);
162         }
163     }
164 }
165
Popular Tags