001    /*
002     * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    
026    package com.sun.tools.doclets.formats.html;
027    
028    import com.sun.tools.doclets.internal.toolkit.*;
029    import com.sun.tools.doclets.internal.toolkit.util.*;
030    import com.sun.javadoc.*;
031    import java.io.*;
032    import java.util.*;
033    
034    /**
035     * Write the Constants Summary Page in HTML format.
036     *
037     * @author Jamie Ho
038     * @since 1.4
039     */
040    public class ConstantsSummaryWriterImpl extends HtmlDocletWriter
041            implements ConstantsSummaryWriter {
042    
043        /**
044         * The configuration used in this run of the standard doclet.
045         */
046        ConfigurationImpl configuration;
047    
048        /**
049         * The current class being documented.
050         */
051        private ClassDoc currentClassDoc;
052    
053        /**
054         * Construct a ConstantsSummaryWriter.
055         * @param configuration the configuration used in this run
056         *        of the standard doclet.
057         */
058        public ConstantsSummaryWriterImpl(ConfigurationImpl configuration)
059                throws IOException {
060            super(configuration, ConfigurationImpl.CONSTANTS_FILE_NAME);
061            this.configuration = configuration;
062        }
063    
064        /**
065         * {@inheritDoc}
066         */
067        public void writeHeader() {
068            printHtmlHeader(configuration.getText("doclet.Constants_Summary"),
069                null, true);
070            printTop();
071            navLinks(true);
072            hr();
073    
074            center();
075            h1(); printText("doclet.Constants_Summary"); h1End();
076            centerEnd();
077    
078            hr(4, "noshade");
079        }
080    
081        /**
082         * {@inheritDoc}
083         */
084        public void writeFooter() {
085            hr();
086            navLinks(false);
087            printBottom();
088            printBodyHtmlEnd();
089        }
090    
091        /**
092         * {@inheritDoc}
093         */
094        public void writeContentsHeader() {
095            strong(configuration.getText("doclet.Contents"));
096            ul();
097        }
098    
099        /**
100         * {@inheritDoc}
101         */
102        public void writeContentsFooter() {
103            ulEnd();
104            println();
105        }
106    
107        /**
108         * {@inheritDoc}
109         */
110        public void writeLinkToPackageContent(PackageDoc pkg, String parsedPackageName, Set<String> printedPackageHeaders) {
111            String packageName = pkg.name();
112            //add link to summary
113            li();
114            if (packageName.length() == 0) {
115                printHyperLink("#" + DocletConstants.UNNAMED_PACKAGE_ANCHOR,
116                               DocletConstants.DEFAULT_PACKAGE_NAME);
117            } else {
118                printHyperLink("#" + parsedPackageName, parsedPackageName + ".*");
119                printedPackageHeaders.add(parsedPackageName);
120            }
121            println();
122        }
123    
124        /**
125         * {@inheritDoc}
126         */
127        public void writeConstantMembersHeader(ClassDoc cd) {
128            //generate links backward only to public classes.
129            String classlink = (cd.isPublic() || cd.isProtected())?
130                getLink(new LinkInfoImpl(LinkInfoImpl.CONTEXT_CONSTANT_SUMMARY, cd,
131                    false)) :
132                cd.qualifiedName();
133            String name = cd.containingPackage().name();
134            if (name.length() > 0) {
135                writeClassName(name + "." + classlink);
136            } else {
137                writeClassName(classlink);
138            }
139        }
140    
141        /**
142         * {@inheritDoc}
143         */
144        public void writeConstantMembersFooter(ClassDoc cd) {
145            tableFooter(false);
146            p();
147        }
148    
149        /**
150         * Print the class name in the table heading.
151         * @param classStr the heading to print.
152         */
153        protected void writeClassName(String classStr) {
154            table(1, 3, 0);
155            trBgcolorStyle("#EEEEFF", "TableSubHeadingColor");
156            thAlignColspan("left", 3);
157            write(classStr);
158            thEnd();
159            trEnd();
160        }
161    
162        private void tableFooter(boolean isHeader) {
163            fontEnd();
164            if (isHeader) {
165                thEnd();
166            } else {
167                tdEnd();
168            }
169            trEnd();
170            tableEnd();
171            p();
172        }
173    
174        /**
175         * {@inheritDoc}
176         */
177        public void writePackageName(PackageDoc pkg, String parsedPackageName) {
178            String pkgname;
179            if (parsedPackageName.length() == 0) {
180                anchor(DocletConstants.UNNAMED_PACKAGE_ANCHOR);
181                pkgname = DocletConstants.DEFAULT_PACKAGE_NAME;
182            } else {
183                anchor(parsedPackageName);
184                pkgname = parsedPackageName;
185            }
186            table(1, "100%", 3, 0);
187            trBgcolorStyle("#CCCCFF", "TableHeadingColor");
188            thAlign("left");
189            font("+2");
190            write(pkgname + ".*");
191            tableFooter(true);
192        }
193    
194        /**
195         * {@inheritDoc}
196         */
197        public void writeConstantMembers(ClassDoc cd, List<FieldDoc> fields) {
198            currentClassDoc = cd;
199            for (int i = 0; i < fields.size(); ++i) {
200                writeConstantMember(fields.get(i));
201            }
202        }
203    
204        private void writeConstantMember(FieldDoc member) {
205            trBgcolorStyle("white", "TableRowColor");
206            anchor(currentClassDoc.qualifiedName() + "." + member.name());
207            writeTypeColumn(member);
208            writeNameColumn(member);
209            writeValue(member);
210            trEnd();
211        }
212    
213        private void writeTypeColumn(FieldDoc member) {
214            tdAlign("right");
215            font("-1");
216            code();
217            StringTokenizer mods = new StringTokenizer(member.modifiers());
218            while(mods.hasMoreTokens()) {
219                print(mods.nextToken() + "&nbsp;");
220            }
221            printLink(new LinkInfoImpl(LinkInfoImpl.CONTEXT_CONSTANT_SUMMARY,
222                member.type()));
223            codeEnd();
224            fontEnd();
225            tdEnd();
226        }
227    
228        private void writeNameColumn(FieldDoc member) {
229            tdAlign("left");
230            code();
231            printDocLink(LinkInfoImpl.CONTEXT_CONSTANT_SUMMARY, member,
232                member.name(), false);
233            codeEnd();
234            tdEnd();
235        }
236    
237        private void writeValue(FieldDoc member) {
238            tdAlign("right");
239            code();
240            print(Util.escapeHtmlChars(member.constantValueExpression()));
241            codeEnd();
242            tdEnd();
243        }
244    }