comparison org.eclipse.ui.forms/src/org/eclipse/ui/internal/forms/widgets/Paragraph.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module org.eclipse.ui.internal.forms.widgets.Paragraph;
14
15 import org.eclipse.ui.internal.forms.widgets.ParagraphSegment;
16 import org.eclipse.ui.internal.forms.widgets.Locator;
17 import org.eclipse.ui.internal.forms.widgets.IHyperlinkSegment;
18 import org.eclipse.ui.internal.forms.widgets.SelectionData;
19 import org.eclipse.ui.internal.forms.widgets.TextSegment;
20 import org.eclipse.ui.internal.forms.widgets.TextHyperlinkSegment;
21
22 import org.eclipse.swt.graphics.GC;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.ui.forms.HyperlinkSettings;
25
26 import java.lang.all;
27 import java.util.ArrayList;
28 import java.util.Set;
29 import tango.io.model.IFile;
30
31 import tango.text.Text;
32
33 /**
34 * @version 1.0
35 * @author
36 */
37 public class Paragraph {
38 public static const String HTTP = "http://"; //$NON-NLS-1$
39
40 private Vector segments;
41
42 private bool addVerticalSpace = true;
43
44 public this(bool addVerticalSpace) {
45 this.addVerticalSpace = addVerticalSpace;
46 }
47
48 public int getIndent() {
49 return 0;
50 }
51
52 public bool getAddVerticalSpace() {
53 return addVerticalSpace;
54 }
55
56 /*
57 * @see IParagraph#getSegments()
58 */
59 public ParagraphSegment[] getSegments() {
60 if (segments is null)
61 return null;
62 return arraycast!(ParagraphSegment)(segments
63 .toArray());
64 }
65
66 public void addSegment(ParagraphSegment segment) {
67 if (segments is null)
68 segments = new Vector;
69 segments.add(segment);
70 }
71
72 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed,
73 HyperlinkSettings settings, String fontId) {
74 parseRegularText(text, expandURLs, wrapAllowed, settings, fontId, null);
75 }
76
77 public void parseRegularText(String text, bool expandURLs, bool wrapAllowed,
78 HyperlinkSettings settings, String fontId, String colorId) {
79 if (text.length is 0)
80 return;
81 if (expandURLs) {
82 int loc = text.indexOf(HTTP);
83
84 if (loc is -1)
85 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
86 else {
87 int textLoc = 0;
88 while (loc !is -1) {
89 addSegment(new TextSegment(text.substring(textLoc, loc),
90 fontId, colorId, wrapAllowed));
91 bool added = false;
92 for (textLoc = loc; textLoc < text.length; textLoc++) {
93 char c = text.charAt(textLoc);
94 if (CharacterIsWhitespace(c)) {
95 addHyperlinkSegment(text.substring(loc, textLoc),
96 settings, fontId);
97 added = true;
98 break;
99 }
100 }
101 if (!added) {
102 // there was no space - just end of text
103 addHyperlinkSegment(text.substring(loc), settings,
104 fontId);
105 break;
106 }
107 loc = text.indexOf(HTTP, textLoc);
108 }
109 if (textLoc < text.length) {
110 addSegment(new TextSegment(text.substring(textLoc), fontId,
111 colorId, wrapAllowed));
112 }
113 }
114 } else {
115 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed));
116 }
117 }
118
119 private void addHyperlinkSegment(String text, HyperlinkSettings settings,
120 String fontId) {
121 TextHyperlinkSegment hs = new TextHyperlinkSegment(text, settings,
122 fontId);
123 hs.setWordWrapAllowed(false);
124 hs.setHref(text);
125 addSegment(hs);
126 }
127
128 protected void computeRowHeights(GC gc, int width, Locator loc,
129 int lineHeight, Hashtable resourceTable) {
130 ParagraphSegment[] segments = getSegments();
131 // compute heights
132 Locator hloc = loc.create();
133 ArrayList heights = new ArrayList();
134 hloc.heights = heights;
135 hloc.rowCounter = 0;
136 int innerWidth = width - loc.marginWidth*2;
137 for (int j = 0; j < segments.length; j++) {
138 ParagraphSegment segment = segments[j];
139 segment.advanceLocator(gc, innerWidth, hloc, resourceTable, true);
140 }
141 hloc.collectHeights();
142 loc.heights = heights;
143 loc.rowCounter = 0;
144 }
145
146 public void layout(GC gc, int width, Locator loc, int lineHeight,
147 Hashtable resourceTable, IHyperlinkSegment selectedLink) {
148 ParagraphSegment[] segments = getSegments();
149 //int height;
150 if (segments.length > 0) {
151 /*
152 if (segments[0] instanceof TextSegment
153 && ((TextSegment) segments[0]).isSelectable())
154 loc.x += 1;
155 */
156 // compute heights
157 if (loc.heights is null)
158 computeRowHeights(gc, width, loc, lineHeight, resourceTable);
159 for (int j = 0; j < segments.length; j++) {
160 ParagraphSegment segment = segments[j];
161 bool doSelect = false;
162 if (selectedLink !is null && segment.opEquals(cast(Object)selectedLink))
163 doSelect = true;
164 segment.layout(gc, width, loc, resourceTable, doSelect);
165 }
166 loc.heights = null;
167 loc.y += loc.rowHeight;
168 } else {
169 loc.y += lineHeight;
170 }
171 }
172
173 public void paint(GC gc, Rectangle repaintRegion,
174 Hashtable resourceTable, IHyperlinkSegment selectedLink,
175 SelectionData selData) {
176 ParagraphSegment[] segments = getSegments();
177
178 for (int i = 0; i < segments.length; i++) {
179 ParagraphSegment segment = segments[i];
180 if (!segment.intersects(repaintRegion))
181 continue;
182 bool doSelect = false;
183 if (selectedLink !is null && segment.opEquals(cast(Object)selectedLink))
184 doSelect = true;
185 segment.paint(gc, false, resourceTable, doSelect, selData, repaintRegion);
186 }
187 }
188
189 public void computeSelection(GC gc, Hashtable resourceTable, IHyperlinkSegment selectedLink,
190 SelectionData selData) {
191 ParagraphSegment[] segments = getSegments();
192
193 for (int i = 0; i < segments.length; i++) {
194 ParagraphSegment segment = segments[i];
195 //bool doSelect = false;
196 //if (selectedLink !is null && segment.equals(selectedLink))
197 //doSelect = true;
198 segment.computeSelection(gc, resourceTable, selData);
199 }
200 }
201
202 public String getAccessibleText() {
203 ParagraphSegment[] segments = getSegments();
204 auto txt = new tango.text.Text.Text!(char);
205 for (int i = 0; i < segments.length; i++) {
206 ParagraphSegment segment = segments[i];
207 if ( auto ts = cast(TextSegment)segment ) {
208 String text = ts.getText();
209 txt.append(text);
210 }
211 }
212 txt.append( FileConst.NewlineString );
213 return txt.toString();
214 }
215
216 public ParagraphSegment findSegmentAt(int x, int y) {
217 if (segments !is null) {
218 for (int i = 0; i < segments.size(); i++) {
219 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i);
220 if (segment.contains(x, y))
221 return segment;
222 }
223 }
224 return null;
225 }
226 public void clearCache(String fontId) {
227 if (segments !is null) {
228 for (int i = 0; i < segments.size(); i++) {
229 ParagraphSegment segment = cast(ParagraphSegment) segments.get(i);
230 segment.clearCache(fontId);
231 }
232 }
233 }
234 }