KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > Paragraph


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.forms.widgets;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.graphics.GC;
20 import org.eclipse.ui.forms.HyperlinkSettings;
21
22 /**
23  * @version 1.0
24  * @author
25  */

26 public class Paragraph {
27     public static final String JavaDoc HTTP = "http://"; //$NON-NLS-1$
28

29     private Vector JavaDoc segments;
30
31     private boolean addVerticalSpace = true;
32
33     public Paragraph(boolean addVerticalSpace) {
34         this.addVerticalSpace = addVerticalSpace;
35     }
36
37     public int getIndent() {
38         return 0;
39     }
40
41     public boolean getAddVerticalSpace() {
42         return addVerticalSpace;
43     }
44
45     /*
46      * @see IParagraph#getSegments()
47      */

48     public ParagraphSegment[] getSegments() {
49         if (segments == null)
50             return new ParagraphSegment[0];
51         return (ParagraphSegment[]) segments
52                 .toArray(new ParagraphSegment[segments.size()]);
53     }
54
55     public void addSegment(ParagraphSegment segment) {
56         if (segments == null)
57             segments = new Vector JavaDoc();
58         segments.add(segment);
59     }
60
61     public void parseRegularText(String JavaDoc text, boolean expandURLs, boolean wrapAllowed,
62             HyperlinkSettings settings, String JavaDoc fontId) {
63         parseRegularText(text, expandURLs, wrapAllowed, settings, fontId, null);
64     }
65
66     public void parseRegularText(String JavaDoc text, boolean expandURLs, boolean wrapAllowed,
67             HyperlinkSettings settings, String JavaDoc fontId, String JavaDoc colorId) {
68         if (text.length() == 0)
69             return;
70         if (expandURLs) {
71             int loc = text.indexOf(HTTP);
72
73             if (loc == -1)
74                 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
75             else {
76                 int textLoc = 0;
77                 while (loc != -1) {
78                     addSegment(new TextSegment(text.substring(textLoc, loc),
79                             fontId, colorId, wrapAllowed));
80                     boolean added = false;
81                     for (textLoc = loc; textLoc < text.length(); textLoc++) {
82                         char c = text.charAt(textLoc);
83                         if (Character.isSpaceChar(c)) {
84                             addHyperlinkSegment(text.substring(loc, textLoc),
85                                     settings, fontId);
86                             added = true;
87                             break;
88                         }
89                     }
90                     if (!added) {
91                         // there was no space - just end of text
92
addHyperlinkSegment(text.substring(loc), settings,
93                                 fontId);
94                         break;
95                     }
96                     loc = text.indexOf(HTTP, textLoc);
97                 }
98                 if (textLoc < text.length()) {
99                     addSegment(new TextSegment(text.substring(textLoc), fontId,
100                             colorId, wrapAllowed));
101                 }
102             }
103         } else {
104             addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
105         }
106     }
107
108     private void addHyperlinkSegment(String JavaDoc text, HyperlinkSettings settings,
109             String JavaDoc fontId) {
110         TextHyperlinkSegment hs = new TextHyperlinkSegment(text, settings,
111                 fontId);
112         hs.setWordWrapAllowed(false);
113         hs.setHref(text);
114         addSegment(hs);
115     }
116
117     protected void computeRowHeights(GC gc, int width, Locator loc,
118             int lineHeight, Hashtable JavaDoc resourceTable) {
119         ParagraphSegment[] segments = getSegments();
120         // compute heights
121
Locator hloc = loc.create();
122         ArrayList JavaDoc heights = new ArrayList JavaDoc();
123         hloc.heights = heights;
124         hloc.rowCounter = 0;
125         int innerWidth = width - loc.marginWidth*2;
126         for (int j = 0; j < segments.length; j++) {
127             ParagraphSegment segment = segments[j];
128             segment.advanceLocator(gc, innerWidth, hloc, resourceTable, true);
129         }
130         hloc.collectHeights();
131         loc.heights = heights;
132         loc.rowCounter = 0;
133     }
134
135     public void layout(GC gc, int width, Locator loc, int lineHeight,
136             Hashtable JavaDoc resourceTable, IHyperlinkSegment selectedLink) {
137         ParagraphSegment[] segments = getSegments();
138         //int height;
139
if (segments.length > 0) {
140             /*
141             if (segments[0] instanceof TextSegment
142                     && ((TextSegment) segments[0]).isSelectable())
143                 loc.x += 1;
144             */

145             // compute heights
146
if (loc.heights == null)
147                 computeRowHeights(gc, width, loc, lineHeight, resourceTable);
148             for (int j = 0; j < segments.length; j++) {
149                 ParagraphSegment segment = segments[j];
150                 boolean doSelect = false;
151                 if (selectedLink != null && segment.equals(selectedLink))
152                     doSelect = true;
153                 segment.layout(gc, width, loc, resourceTable, doSelect);
154             }
155             loc.heights = null;
156             loc.y += loc.rowHeight;
157         } else {
158             loc.y += lineHeight;
159         }
160     }
161
162     public void paint(GC gc, Rectangle repaintRegion,
163             Hashtable JavaDoc resourceTable, IHyperlinkSegment selectedLink,
164             SelectionData selData) {
165         ParagraphSegment[] segments = getSegments();
166
167         for (int i = 0; i < segments.length; i++) {
168             ParagraphSegment segment = segments[i];
169             if (!segment.intersects(repaintRegion))
170                 continue;
171             boolean doSelect = false;
172             if (selectedLink != null && segment.equals(selectedLink))
173                 doSelect = true;
174             segment.paint(gc, false, resourceTable, doSelect, selData, repaintRegion);
175         }
176     }
177     
178     public void computeSelection(GC gc, Hashtable JavaDoc resourceTable, IHyperlinkSegment selectedLink,
179             SelectionData selData) {
180         ParagraphSegment[] segments = getSegments();
181
182         for (int i = 0; i < segments.length; i++) {
183             ParagraphSegment segment = segments[i];
184             //boolean doSelect = false;
185
//if (selectedLink != null && segment.equals(selectedLink))
186
//doSelect = true;
187
segment.computeSelection(gc, resourceTable, selData);
188         }
189     }
190
191     public String JavaDoc getAccessibleText() {
192         ParagraphSegment[] segments = getSegments();
193         StringWriter swriter = new StringWriter();
194         PrintWriter writer = new PrintWriter(swriter);
195         for (int i = 0; i < segments.length; i++) {
196             ParagraphSegment segment = segments[i];
197             if (segment instanceof TextSegment) {
198                 String JavaDoc text = ((TextSegment) segment).getText();
199                 writer.print(text);
200             }
201         }
202         writer.println();
203         swriter.flush();
204         return swriter.toString();
205     }
206
207     public ParagraphSegment findSegmentAt(int x, int y) {
208         if (segments != null) {
209             for (int i = 0; i < segments.size(); i++) {
210                 ParagraphSegment segment = (ParagraphSegment) segments.get(i);
211                 if (segment.contains(x, y))
212                     return segment;
213             }
214         }
215         return null;
216     }
217     public void clearCache(String JavaDoc fontId) {
218         if (segments != null) {
219             for (int i = 0; i < segments.size(); i++) {
220                 ParagraphSegment segment = (ParagraphSegment) segments.get(i);
221                 segment.clearCache(fontId);
222             }
223         }
224     }
225 }
226
Popular Tags