Mercurial > projects > dwt-addons
annotate dwtx/jface/internal/text/link/contentassist/LineBreakingReader.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | 6dcb0baaa031 |
children | 1a5b8f8129df |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2006 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 dwtx.jface.internal.text.link.contentassist.LineBreakingReader; | |
14 | |
131 | 15 import dwtx.jface.internal.text.link.contentassist.IProposalListener; // packageimport |
16 import dwtx.jface.internal.text.link.contentassist.CompletionProposalPopup2; // packageimport | |
17 import dwtx.jface.internal.text.link.contentassist.ContextInformationPopup2; // packageimport | |
18 import dwtx.jface.internal.text.link.contentassist.ContentAssistMessages; // packageimport | |
19 import dwtx.jface.internal.text.link.contentassist.Helper2; // packageimport | |
20 import dwtx.jface.internal.text.link.contentassist.PopupCloser2; // packageimport | |
21 import dwtx.jface.internal.text.link.contentassist.IContentAssistListener2; // packageimport | |
22 import dwtx.jface.internal.text.link.contentassist.ContentAssistant2; // packageimport | |
23 import dwtx.jface.internal.text.link.contentassist.AdditionalInfoController2; // packageimport | |
24 | |
25 | |
129 | 26 import dwt.dwthelper.utils; |
158 | 27 import dwtx.dwtxhelper.BufferedReader; |
28 import dwtx.dwtxhelper.mangoicu.UBreakIterator; | |
129 | 29 |
30 import dwt.graphics.GC; | |
31 | |
32 /* | |
33 * Not a real reader. Could change if requested | |
34 */ | |
35 public class LineBreakingReader { | |
36 | |
37 private BufferedReader fReader; | |
38 private GC fGC; | |
39 private int fMaxWidth; | |
40 | |
41 private String fLine; | |
42 private int fOffset; | |
43 | |
158 | 44 private UBreakIterator fLineBreakIterator; |
129 | 45 private bool fBreakWords; |
46 | |
47 /** | |
48 * Creates a reader that breaks an input text to fit in a given width. | |
158 | 49 * |
129 | 50 * @param reader Reader of the input text |
51 * @param gc The graphic context that defines the currently used font sizes | |
52 * @param maxLineWidth The max width (pixels) where the text has to fit in | |
53 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
54 public this(Reader reader, GC gc, int maxLineWidth) { |
129 | 55 fReader= new BufferedReader(reader); |
56 fGC= gc; | |
57 fMaxWidth= maxLineWidth; | |
58 fOffset= 0; | |
59 fLine= null; | |
158 | 60 fLineBreakIterator= UBreakIterator.getLineInstance(); |
129 | 61 fBreakWords= true; |
62 } | |
63 | |
64 public bool isFormattedLine() { | |
65 return fLine !is null; | |
66 } | |
67 | |
68 /** | |
69 * Reads the next line. The lengths of the line will not exceed the given maximum | |
70 * width. | |
158 | 71 * |
72 * @return the next line | |
73 * @throws IOException | |
129 | 74 */ |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
75 public String readLine() { |
129 | 76 if (fLine is null) { |
77 String line= fReader.readLine(); | |
78 if (line is null) | |
79 return null; | |
80 | |
81 int lineLen= fGC.textExtent(line).x; | |
82 if (lineLen < fMaxWidth) { | |
83 return line; | |
84 } | |
85 fLine= line; | |
86 fLineBreakIterator.setText(line); | |
87 fOffset= 0; | |
88 } | |
89 int breakOffset= findNextBreakOffset(fOffset); | |
90 String res; | |
158 | 91 if (breakOffset !is UBreakIterator.DONE) { |
129 | 92 res= fLine.substring(fOffset, breakOffset); |
93 fOffset= findWordBegin(breakOffset); | |
94 if (fOffset is fLine.length()) { | |
95 fLine= null; | |
96 } | |
97 } else { | |
98 res= fLine.substring(fOffset); | |
99 fLine= null; | |
100 } | |
101 return res; | |
102 } | |
103 | |
104 private int findNextBreakOffset(int currOffset) { | |
105 int currWidth= 0; | |
106 int nextOffset= fLineBreakIterator.following(currOffset); | |
158 | 107 while (nextOffset !is UBreakIterator.DONE) { |
129 | 108 String word= fLine.substring(currOffset, nextOffset); |
109 int wordWidth= fGC.textExtent(word).x; | |
110 int nextWidth= wordWidth + currWidth; | |
111 if (nextWidth > fMaxWidth) { | |
112 if (currWidth > 0) | |
113 return currOffset; | |
114 | |
115 if (!fBreakWords) | |
116 return nextOffset; | |
117 | |
118 // need to fit into fMaxWidth | |
119 int length= word.length(); | |
120 while (length >= 0) { | |
121 length--; | |
122 word= word.substring(0, length); | |
123 wordWidth= fGC.textExtent(word).x; | |
124 if (wordWidth + currWidth < fMaxWidth) | |
125 return currOffset + length; | |
126 } | |
127 return nextOffset; | |
128 } | |
129 currWidth= nextWidth; | |
130 currOffset= nextOffset; | |
131 nextOffset= fLineBreakIterator.next(); | |
132 } | |
133 return nextOffset; | |
134 } | |
135 | |
136 private int findWordBegin(int idx) { | |
137 while (idx < fLine.length() && Character.isWhitespace(fLine.charAt(idx))) { | |
138 idx++; | |
139 } | |
140 return idx; | |
141 } | |
142 } |