KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > formatter > comment > CommentLine


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  *******************************************************************************/

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

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

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

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

52     protected abstract void adapt(final CommentLine previous);
53
54     /**
55      * Appends the specified comment range to this comment line.
56      *
57      * @param range 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 the predecessor comment line in the comment region
67      * @param last the most recently processed comment range
68      * @param indentation the indentation of the comment region
69      * @param line the index of this comment line in the comment region
70      * @return the first comment range in this comment line
71      */

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

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

152     protected void formatUpperBorder(final CommentRange range, final String JavaDoc indentation, final int length) {
153
154         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(length);
155         final String JavaDoc start= getStartingPrefix();
156         final String JavaDoc content= getContentPrefix();
157
158         if (fParent.isSingleLine() && fParent.getSize() == 1)
159             buffer.append(start);
160         else {
161
162             final String JavaDoc trimmed= start.trim();
163             final String JavaDoc filler= content.trim();
164
165             buffer.append(trimmed);
166
167             if (fParent.hasBorder(BORDER_UPPER)) {
168
169                 for (int character= 0; character < length - trimmed.length() + start.length(); character++)
170                     buffer.append(filler);
171             }
172
173             buffer.append(fParent.getDelimiter());
174             buffer.append(indentation);
175             buffer.append(content);
176         }
177         fParent.logEdit(buffer.toString(), 0, range.getOffset());
178     }
179
180     /**
181      * Returns the line prefix of content lines.
182      *
183      * @return line prefix of content lines
184      */

185     protected abstract String JavaDoc getContentPrefix();
186
187     /**
188      * Returns the line prefix of end lines.
189      *
190      * @return line prefix of end lines
191      */

192     protected abstract String JavaDoc getEndingPrefix();
193
194     /**
195      * Returns the first comment range in this comment line.
196      *
197      * @return the first comment range
198      */

199     protected final CommentRange getFirst() {
200         return (CommentRange)fRanges.getFirst();
201     }
202
203     /**
204      * Returns the indentation reference string for this line.
205      *
206      * @return the indentation reference string for this line
207      */

208     protected String JavaDoc getIndentationReference() {
209         return ""; //$NON-NLS-1$
210
}
211
212     /**
213      * Returns the last comment range in this comment line.
214      *
215      * @return the last comment range
216      */

217     protected final CommentRange getLast() {
218         return (CommentRange)fRanges.getLast();
219     }
220
221     /**
222      * Returns the parent comment region of this comment line.
223      *
224      * @return the parent comment region
225      */

226     protected final CommentRegion getParent() {
227         return fParent;
228     }
229
230     /**
231      * Returns the number of comment ranges in this comment line.
232      *
233      * @return the number of ranges in this line
234      */

235     protected final int getSize() {
236         return fRanges.size();
237     }
238
239     /**
240      * Returns the line prefix of start lines.
241      *
242      * @return line prefix of start lines
243      */

244     protected abstract String JavaDoc getStartingPrefix();
245
246     /**
247      * Is the attribute <code>attribute</code> true?
248      *
249      * @param attribute the attribute to get.
250      * @return <code>true</code> iff this attribute is <code>true</code>,
251      * <code>false</code> otherwise.
252      */

253     protected final boolean hasAttribute(final int attribute) {
254         return (fAttributes & attribute) == attribute;
255     }
256
257     /**
258      * Scans this comment line for comment range boundaries.
259      *
260      * @param line the index of this line in the comment region
261      */

262     protected abstract void scanLine(final int line);
263
264     /**
265      * Set the attribute <code>attribute</code> to true.
266      *
267      * @param attribute the attribute to set.
268      */

269     protected final void setAttribute(final int attribute) {
270         fAttributes |= attribute;
271     }
272
273     /**
274      * Tokenizes this comment line into comment ranges
275      *
276      * @param line the index of this line in the comment region
277      */

278     protected void tokenizeLine(final int line) {
279
280         int offset= 0;
281         int index= offset;
282
283         final CommentRange range= (CommentRange)fRanges.get(0);
284         final int begin= range.getOffset();
285
286         final String JavaDoc content= fParent.getText(begin, range.getLength());
287         final int length= content.length();
288
289         while (offset < length) {
290
291             while (offset < length && ScannerHelper.isWhitespace(content.charAt(offset)))
292                 offset++;
293
294             index= offset;
295
296             while (index < length && !ScannerHelper.isWhitespace(content.charAt(index)))
297                 index++;
298
299             if (index - offset > 0) {
300                 fParent.append(new CommentRange(begin + offset, index - offset));
301
302                 offset= index;
303             }
304         }
305     }
306     
307     public String JavaDoc toString() {
308         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
309         final int size = this.fRanges.size();
310         for (int i = 0; i < size; i++) {
311             buffer.append(this.fRanges.get(i)).append("\n"); //$NON-NLS-1$
312
}
313         return String.valueOf(buffer);
314     }
315 }
316
Popular Tags