KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jface.text.Position;
15
16 import org.eclipse.jdt.internal.ui.text.javadoc.IHtmlTagConstants;
17
18 /**
19  * Range in a comment region in comment region coordinates.
20  *
21  * @since 3.0
22  */

23 public class CommentRange extends Position implements ICommentAttributes, IHtmlTagConstants {
24
25     /** The attributes of this range */
26     private int fAttributes= 0;
27
28     /**
29      * Creates a new comment range.
30      *
31      * @param position
32      * Offset of the range
33      * @param count
34      * Length of the range
35      */

36     public CommentRange(final int position, final int count) {
37         super(position, count);
38     }
39
40     /**
41      * Is the attribute <code>attribute</code> true?
42      *
43      * @param attribute
44      * The attribute to get.
45      * @return <code>true</code> iff this attribute is <code>true</code>,
46      * <code>false</code> otherwise.
47      */

48     protected final boolean hasAttribute(final int attribute) {
49         return (fAttributes & attribute) == attribute;
50     }
51
52     /**
53      * Does this comment range contain a closing HTML tag?
54      *
55      * @param token
56      * Token belonging to the comment range
57      * @param tag
58      * The HTML tag to check
59      * @return <code>true</code> iff this comment range contains a closing
60      * html tag, <code>false</code> otherwise
61      */

62     protected final boolean isClosingTag(final String JavaDoc token, final String JavaDoc tag) {
63
64         boolean result= token.startsWith(HTML_CLOSE_PREFIX) && token.charAt(token.length() - 1) == HTML_TAG_POSTFIX;
65         if (result) {
66
67             setAttribute(COMMENT_CLOSE);
68             result= token.substring(HTML_CLOSE_PREFIX.length(), token.length() - 1).equals(tag);
69         }
70         return result;
71     }
72
73     /**
74      * Does this comment range contain an opening HTML tag?
75      *
76      * @param token
77      * Token belonging to the comment range
78      * @param tag
79      * The HTML tag to check
80      * @return <code>true</code> iff this comment range contains an opening
81      * html tag, <code>false</code> otherwise
82      */

83     protected final boolean isOpeningTag(final String JavaDoc token, final String JavaDoc tag) {
84
85         boolean result= token.length() > 0 && token.charAt(0) == HTML_TAG_PREFIX && !token.startsWith(HTML_CLOSE_PREFIX) && token.charAt(token.length() - 1) == HTML_TAG_POSTFIX;
86         if (result) {
87
88             setAttribute(COMMENT_OPEN);
89             result= token.startsWith(tag, 1);
90         }
91         return result;
92     }
93
94     /**
95      * Mark the comment range with the occurred HTML tags.
96      *
97      * @param tags
98      * The HTML tags to test for their occurrence
99      * @param token
100      * Token belonging to the comment range
101      * @param attribute
102      * Attribute to set if a HTML tag is present
103      * @param open
104      * <code>true</code> iff opening tags should be marked, <code>false</code>
105      * otherwise.
106      * @param close
107      * <code>true</code> iff closing tags should be marked, <code>false</code>
108      * otherwise.
109      */

110     protected final void markHtmlTag(final String JavaDoc[] tags, final String JavaDoc token, final int attribute, final boolean open, final boolean close) {
111
112         if (token.charAt(0) == HTML_TAG_PREFIX && token.charAt(token.length() - 1) == HTML_TAG_POSTFIX) {
113
114             String JavaDoc tag= null;
115             boolean isOpen= false;
116             boolean isClose= false;
117
118             for (int index= 0; index < tags.length; index++) {
119
120                 tag= tags[index];
121
122                 isOpen= isOpeningTag(token, tag);
123                 isClose= isClosingTag(token, tag);
124
125                 if ((open && isOpen) || (close && isClose)) {
126
127                     setAttribute(attribute);
128                     break;
129                 }
130             }
131         }
132     }
133
134     /**
135      * Mark the comment range with the occurred tags.
136      *
137      * @param tags the tags to test for their occurrence
138      * @param prefix the prefix which is common to all the tags to test
139      * @param token the token belonging to the comment range
140      * @param attribute
141      * Attribute to set if a tag is present
142      */

143     protected final void markPrefixTag(final String JavaDoc[] tags, final char prefix, final String JavaDoc token, final int attribute) {
144
145         if (token.charAt(0) == prefix) {
146
147             String JavaDoc tag= null;
148             for (int index= 0; index < tags.length; index++) {
149
150                 tag= tags[index];
151                 if (token.equals(tag)) {
152
153                     setAttribute(attribute);
154                     break;
155                 }
156             }
157         }
158     }
159
160     /**
161      * Marks the comment range with the HTML range tag.
162      *
163      * @param token the token belonging to the comment range
164      * @param tag the HTML tag which confines the HTML range
165      * @param level the nesting level of the current HTML range
166      * @param key the key of the attribute to set if the comment range is in the HTML range
167      * @param html <code>true</code> iff the HTML tags in this HTML range
168      * should be marked too, <code>false</code> otherwise
169      * @return The new nesting level of the HTML range
170      */

171     protected final int markTagRange(final String JavaDoc token, final String JavaDoc tag, int level, final int key, final boolean html) {
172
173         if (isOpeningTag(token, tag)) {
174             if (level++ > 0)
175                 setAttribute(key);
176         } else if (isClosingTag(token, tag)) {
177             if (--level > 0)
178                 setAttribute(key);
179         } else if (level > 0) {
180             if (html || !hasAttribute(COMMENT_HTML))
181                 setAttribute(key);
182         }
183         return level;
184     }
185
186     /**
187      * Moves this comment range.
188      *
189      * @param delta
190      * The delta to move the range
191      */

192     public final void move(final int delta) {
193         offset += delta;
194     }
195
196     /**
197      * Set the attribute <code>attribute</code> to true.
198      *
199      * @param attribute
200      * The attribute to set.
201      */

202     protected final void setAttribute(final int attribute) {
203         fAttributes |= attribute;
204     }
205
206     /**
207      * Trims this comment range at the beginning.
208      *
209      * @param delta
210      * Amount to trim the range
211      */

212     public final void trimBegin(final int delta) {
213         offset += delta;
214         length -= delta;
215     }
216
217     /**
218      * Trims this comment range at the end.
219      *
220      * @param delta
221      * Amount to trim the range
222      */

223     public final void trimEnd(final int delta) {
224         length += delta;
225     }
226 }
227
Popular Tags