001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.pretty;
010:
011: import java.util.StringTokenizer;
012:
013: /**
014: * Store a portion of a javadoc item
015: *
016: *@author Chris Seguin
017: *@created April 15, 1999
018: */
019: public class JavaDocComponent {
020: private int longestLength = 0;
021: private String description;
022: private boolean printed;
023: private boolean required;
024: // Instance Variable
025: private String type;
026:
027: /**
028: * Create an instance of this java doc object
029: */
030: public JavaDocComponent() {
031: type = "";
032: description = "";
033: printed = false;
034: required = false;
035: }
036:
037: /**
038: * Set the description
039: *
040: *@param newDescription the new description
041: */
042: public void setDescription(String newDescription) {
043: if (newDescription != null) {
044: description = newDescription;
045: }
046: }
047:
048: /**
049: * Set the longestLength
050: *
051: *@param newLongestLength the new longestLength
052: */
053: public void setLongestLength(int newLongestLength) {
054: longestLength = newLongestLength;
055: }
056:
057: /**
058: * Note that this node is required
059: *
060: *@param req true if it is required
061: */
062: public void setRequired(boolean req) {
063: required = req;
064: }
065:
066: /**
067: * Set the type
068: *
069: *@param newType the new type
070: */
071: public void setType(String newType) {
072: if (newType != null) {
073: type = newType;
074: setLongestLength(type.length() + 2);
075: }
076: }
077:
078: /**
079: * Return the description
080: *
081: *@return the description
082: */
083: public String getDescription() {
084: return description;
085: }
086:
087: /**
088: * Return the longestLength
089: *
090: *@return the longestLength
091: */
092: public int getLongestLength() {
093: return longestLength;
094: }
095:
096: /**
097: * Return the type
098: *
099: *@return the type
100: */
101: public String getType() {
102: return type;
103: }
104:
105: /**
106: * Return whether this node has been printed
107: *
108: *@return true if it was printed
109: */
110: public boolean isPrinted() {
111: return printed;
112: }
113:
114: /**
115: * Return whether this node is required
116: *
117: *@return true if it is required
118: */
119: public boolean isRequired() {
120: return required;
121: }
122:
123: /**
124: * Print this tag
125: *
126: *@param printData printData
127: */
128: public void print(PrintData printData) {
129: // We are now printing it
130: setPrinted(true);
131:
132: // Start the line
133: if (!printData.isCurrentSingle()) {
134: printData.indent();
135: if (!printData.isStarsAlignedWithSlash()) {
136: printData.space();
137: }
138: printData.appendComment("*", PrintData.JAVADOC_COMMENT);
139: }
140:
141: if (printData.isSpaceBeforeAt() && !isDescription()) {
142: printData.appendComment(" ", PrintData.JAVADOC_COMMENT);
143: }
144:
145: // Print the type
146: if (!isDescription()) {
147: printData.appendComment(getType(),
148: PrintData.JAVADOC_COMMENT);
149: }
150:
151: // Pad extra spaces after the ID
152: if (!isDescription() && printData.isJavadocLinedUp()) {
153: int extra = getLongestLength() - getType().length();
154: for (int ndx = 0; ndx < extra; ndx++) {
155: printData.appendComment(" ", PrintData.JAVADOC_COMMENT);
156: }
157: }
158:
159: // Pad any extra spaces after the stars and before the text
160: if (printData.isReformatComments() || !isDescription()) {
161: for (int i = 0; i < printData.getJavadocIndent(); ++i) {
162: printData.appendComment(" ", PrintData.JAVADOC_COMMENT);
163: }
164: }
165:
166: // Print the description
167: printDescription(printData);
168:
169: if (!printData.isCurrentSingle()) {
170: printData.newline();
171: }
172: }
173:
174: /**
175: * Note that this node has been printed
176: *
177: *@param prn Description of Parameter
178: */
179: protected void setPrinted(boolean prn) {
180: printed = prn;
181: }
182:
183: /**
184: * Print the drescription
185: *
186: *@param printData the print data
187: */
188: protected void leaveDescription(PrintData printData) {
189: StringBuffer sb = new StringBuffer(printData.getJavadocIndent());
190: for (int i = 0; i < printData.getJavadocIndent(); ++i) {
191: sb.append(" ");
192: }
193: String indent = sb.toString();
194:
195: StringTokenizer tok = new StringTokenizer(getDescription(),
196: "\n\r");
197: boolean first = true;
198: while (tok.hasMoreTokens()) {
199: String nextToken = tok.nextToken();
200: if (!first) {
201: printData.indent();
202: if (!printData.isStarsAlignedWithSlash()) {
203: printData.space();
204: }
205: printData.appendComment("*", PrintData.JAVADOC_COMMENT);
206: printData.appendComment(indent,
207: PrintData.JAVADOC_COMMENT);
208: }
209: printData.appendComment(nextToken,
210: PrintData.JAVADOC_COMMENT);
211: first = false;
212: }
213: }
214:
215: /**
216: * Print the drescription
217: *
218: *@param printData the print data
219: */
220: protected void printDescription(PrintData printData) {
221: int indent = 0;
222:
223: if (getType().length() == 0) {
224: indent = printData.getJavadocIndent();
225: } else {
226: indent = printData.getTaggedJavadocDescription();
227: }
228:
229: wordwrapDescription(printData, indent);
230: }
231:
232: /**
233: * Print the drescription
234: *
235: *@param printData the print data
236: *@param indent the number of spaces to indent
237: */
238: protected void wordwrapDescription(PrintData printData, int indent) {
239: JavadocDescriptionPrinter jdp = new JavadocDescriptionPrinter(
240: printData, getDescription(), indent);
241:
242: jdp.run();
243: }
244:
245: /**
246: * returns true if this is a description
247: *
248: *@return true if it is a description
249: */
250: boolean isDescription() {
251: return (getType().length() == 0);
252: }
253: }
254: /*******\
255: \*******/
|