KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > comment > CommentLine


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

11
12 package org.eclipse.jdt.internal.ui.text.comment;
13
14 import java.util.LinkedList JavaDoc;
15
16 /**
17  * General comment line in a comment region.
18  *
19  * @since 3.0
20  */

21 public abstract class CommentLine implements IBorderAttributes {
22
23     /** Prefix of non-formattable comment lines */
24     protected static final String JavaDoc NON_FORMAT_START_PREFIX= "/*-"; //$NON-NLS-1$
25

26     /** The attributes of this line */
27     private int fAttributes= 0;
28
29     /** The parent region of this line */
30     private final CommentRegion fParent;
31
32     /** The comment ranges in this line */
33     private final LinkedList JavaDoc fRanges= new LinkedList JavaDoc();
34
35     /**
36      * Creates a new comment line.
37      *
38      * @param parent
39      * Comment region to create the comment line for
40      */

41     protected CommentLine(final CommentRegion parent) {
42         fParent= parent;
43     }
44
45     /**
46      * Adapts the line attributes from the previous line in the comment region.
47      *
48      * @param previous
49      * The previous comment line in the comment region
50      */

51     protected abstract void adapt(final CommentLine previous);
52
53     /**
54      * Appends the specified comment range to this comment line.
55      *
56      * @param range
57      * Comment range to append to this line
58      */

59     protected void append(final CommentRange range) {
60         fRanges.add(range);
61     }
62
63     /**
64      * Formats this comment line as content line.
65      *
66      * @param predecessor
67      * The predecessor comment line in the comment region
68      * @param last
69      * The most recently processed comment range
70      * @param indentation
71      * The indentation of the comment region
72      * @param line
73      * The index of this comment line in the comment region
74      * @return The first comment range in this comment line
75      */

76     protected CommentRange formatLine(final CommentLine predecessor, final CommentRange last, final String JavaDoc indentation, final int line) {
77
78         int offset= 0;
79         int length= 0;
80
81         CommentRange next= last;
82         CommentRange previous= null;
83
84         final int stop= fRanges.size() - 1;
85         final int end= fParent.getSize() - 1;
86
87         for (int index= stop; index >= 0; index--) {
88
89             previous= next;
90             next= (CommentRange)fRanges.get(index);
91
92             if (fParent.canFormat(previous, next)) {
93
94                 offset= next.getOffset() + next.getLength();
95                 length= previous.getOffset() - offset;
96
97                 if (index == stop && line != end)
98                     fParent.logEdit(fParent.getDelimiter(predecessor, this, previous, next, indentation), offset, length);
99                 else
100                     fParent.logEdit(fParent.getDelimiter(previous, next), offset, length);
101             }
102         }
103         return next;
104     }
105
106     /**
107      * Formats this comment line as end line having a lower border consisting
108      * of content line prefixes.
109      *
110      * @param range
111      * Last comment range of the last comment line in the comment
112      * region
113      * @param indentation
114      * The indentation of the comment region
115      * @param length
116      * The maximal length of text in this comment region measured in
117      * average character widths
118      */

119     protected void formatLowerBorder(final CommentRange range, final String JavaDoc indentation, final int length) {
120
121         final int offset= range.getOffset() + range.getLength();
122
123         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(length);
124         final String JavaDoc end= getEndingPrefix();
125         final String JavaDoc delimiter= fParent.getDelimiter();
126
127         if (fParent.isSingleLine() && fParent.getSize() == 1)
128             buffer.append(end);
129         else {
130
131             final String JavaDoc filler= getContentPrefix().trim();
132
133             buffer.append(delimiter);
134             buffer.append(indentation);
135
136             if (fParent.hasBorder(BORDER_LOWER)) {
137
138                 buffer.append(' ');
139                 for (int character= 0; character < length; character++)
140                     buffer.append(filler);
141
142                 buffer.append(end.trim());
143
144             } else
145                 buffer.append(end);
146         }
147         fParent.logEdit(buffer.toString(), offset, fParent.getLength() - offset);
148     }
149
150     /**
151      * Formats this comment line as start line having an upper border
152      * consisting of content line prefixes.
153      *
154      * @param range
155      * The first comment range in the comment region
156      * @param indentation
157      * The indentation of the comment region
158      * @param length
159      * The maximal length of text in this comment region measured in
160      * average character widths
161      */

162     protected void formatUpperBorder(final CommentRange range, final String JavaDoc indentation, final int length) {
163
164         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(length);
165         final String JavaDoc start= getStartingPrefix();
166         final String JavaDoc content= getContentPrefix();
167
168         if (fParent.isSingleLine() && fParent.getSize() == 1)
169             buffer.append(start);
170         else {
171
172             final String JavaDoc trimmed= start.trim();
173             final String JavaDoc filler= content.trim();
174
175             buffer.append(trimmed);
176
177             if (fParent.hasBorder(BORDER_UPPER)) {
178
179                 for (int character= 0; character < length - trimmed.length() + start.length(); character++)
180                     buffer.append(filler);
181             }
182
183             buffer.append(fParent.getDelimiter());
184             buffer.append(indentation);
185             buffer.append(content);
186         }
187         fParent.logEdit(buffer.toString(), 0, range.getOffset());
188     }
189
190     /**
191      * Returns the line prefix of content lines.
192      *
193      * @return Line prefix of content lines
194      */

195     protected abstract String JavaDoc getContentPrefix();
196
197     /**
198      * Returns the line prefix of end lines.
199      *
200      * @return Line prefix of end lines
201      */

202     protected abstract String JavaDoc getEndingPrefix();
203
204     /**
205      * Returns the first comment range in this comment line.
206      *
207      * @return The first comment range
208      */

209     protected final CommentRange getFirst() {
210         return (CommentRange)fRanges.getFirst();
211     }
212
213     /**
214      * Returns the indentation reference string for this line.
215      *
216      * @return The indentation reference string for this line
217      */

218     protected String JavaDoc getIndentationReference() {
219         return ""; //$NON-NLS-1$
220
}
221
222     /**
223      * Returns the last comment range in this comment line.
224      *
225      * @return The last comment range
226      */

227     protected final CommentRange getLast() {
228         return (CommentRange)fRanges.getLast();
229     }
230
231     /**
232      * Returns the parent comment region of this comment line.
233      *
234      * @return The parent comment region
235      */

236     protected final CommentRegion getParent() {
237         return fParent;
238     }
239
240     /**
241      * Returns the number of comment ranges in this comment line.
242      *
243      * @return The number of ranges in this line
244      */

245     protected final int getSize() {
246         return fRanges.size();
247     }
248
249     /**
250      * Returns the line prefix of start lines.
251      *
252      * @return Line prefix of start lines
253      */

254     protected abstract String JavaDoc getStartingPrefix();
255
256     /**
257      * Is the attribute <code>attribute</code> true?
258      *
259      * @param attribute
260      * The attribute to get.
261      * @return <code>true</code> iff this attribute is <code>true</code>,
262      * <code>false</code> otherwise.
263      */

264     protected final boolean hasAttribute(final int attribute) {
265         return (fAttributes & attribute) == attribute;
266     }
267
268     /**
269      * Scans this comment line for comment range boundaries.
270      *
271      * @param line
272      * The index of this line in the comment region
273      */

274     protected abstract void scanLine(final int line);
275
276     /**
277      * Set the attribute <code>attribute</code> to true.
278      *
279      * @param attribute
280      * The attribute to set.
281      */

282     protected final void setAttribute(final int attribute) {
283         fAttributes |= attribute;
284     }
285
286     /**
287      * Tokenizes this comment line into comment ranges
288      *
289      * @param line
290      * The index of this line in the comment region
291      */

292     protected void tokenizeLine(final int line) {
293
294         int offset= 0;
295         int index= offset;
296
297         final CommentRange range= (CommentRange)fRanges.get(0);
298         final int begin= range.getOffset();
299
300         final String JavaDoc content= fParent.getText(begin, range.getLength());
301         final int length= content.length();
302
303         while (offset < length) {
304
305             while (offset < length && Character.isWhitespace(content.charAt(offset)))
306                 offset++;
307
308             index= offset;
309
310             while (index < length && !Character.isWhitespace(content.charAt(index)))
311                 index++;
312
313             if (index - offset > 0) {
314                 fParent.append(new CommentRange(begin + offset, index - offset));
315
316                 offset= index;
317             }
318         }
319     }
320 }
321
Popular Tags