KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > builder > CommentSetImpl


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.java.source.builder;
20
21 import org.netbeans.api.java.source.Comment;
22 import org.netbeans.api.java.source.query.CommentSet;
23 import org.netbeans.api.java.source.query.Query;
24 import com.sun.source.tree.Tree;
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29  * Class that associates the before and after comments to a tree.
30  */

31 public class CommentSetImpl implements Cloneable JavaDoc, CommentSet {
32     Tree tree;
33     List JavaDoc<Comment> precedingComments = new ArrayList JavaDoc<Comment>();
34     List JavaDoc<Comment> trailingComments = new ArrayList JavaDoc<Comment>();
35
36     /**
37      * Add the specified comment string to the list of preceding comments.
38      */

39     public void addPrecedingComment(String JavaDoc s) {
40         addPrecedingComment(Comment.create(s));
41     }
42
43     /**
44      * Add the specified comment to the list of preceding comments.
45      */

46     public void addPrecedingComment(Comment c) {
47         precedingComments.add(c);
48     }
49
50     /**
51      * Add a list of comments to the list of preceding comments.
52      */

53     public void addPrecedingComments(List JavaDoc<Comment> comments) {
54         precedingComments.addAll(comments);
55     }
56     
57     /**
58      * Add the specified comment string to the list of trailing comments.
59      */

60     public void addTrailingComment(String JavaDoc s) {
61         addTrailingComment(Comment.create(s));
62     }
63
64     /**
65      * Add the specified comment to the list of trailing comments.
66      */

67     public void addTrailingComment(Comment c) {
68         trailingComments.add(c);
69     }
70
71     /**
72      * Add a list of comments to the list of preceding comments.
73      */

74     public void addTrailingComments(List JavaDoc<Comment> comments) {
75         trailingComments.addAll(comments);
76     }
77     
78     public List JavaDoc<Comment> getPrecedingComments() {
79         return precedingComments;
80     }
81     
82     public List JavaDoc<Comment> getTrailingComments() {
83         return trailingComments;
84     }
85     
86     public boolean hasComments() {
87         return precedingComments.size() > 0 || trailingComments.size() > 0;
88     }
89     
90     /**
91      * Returns the first character position, which is either the initial
92      * position of the first preceding comment, or NOPOS if there are no comments.
93      *
94      * @see org.netbeans.api.java.source.query.Query#NOPOS
95      */

96     public int pos() {
97         return precedingComments.size() > 0 ?
98             precedingComments.get(0).pos() : Query.NOPOS;
99     }
100         
101     void setTree(Tree newTree) {
102         tree = newTree;
103     }
104     
105     public boolean hasChanges() {
106         for (Comment c : precedingComments)
107             if (c.isNew())
108                 return true;
109         for (Comment c : trailingComments)
110             if (c.isNew())
111                 return true;
112         return false;
113     }
114     
115     public Object JavaDoc clone() {
116         try {
117             return super.clone();
118         } catch (CloneNotSupportedException JavaDoc e) {
119         throw new InternalError JavaDoc("Unexpected " + e);
120         }
121     }
122     
123     public String JavaDoc toString() {
124         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
125         boolean first = true;
126         sb.append('{');
127         for (Comment c : precedingComments) {
128             if (!first)
129                 sb.append(',');
130             sb.append(c.getText());
131             first = false;
132         }
133         for (Comment c : trailingComments) {
134             if (!first)
135                 sb.append(',');
136             sb.append(c.getText());
137             first = false;
138         }
139         sb.append('}');
140         return sb.toString();
141     }
142 }
143
Popular Tags