1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package Torello.HTML.Tools.JavaDoc;

import Torello.HTML.*;
import Torello.HTML.NodeSearch.*;

import Torello.Java.Additional.EffectivelyFinal;

import java.util.*;
import java.util.stream.*;

/**
 * <B STYLE='color:darkred;'>Process Java Doc Web-Page:</B>
 * 
 * Add several buttons to the Navigation Menu, along with a Detail-Element drop-down menu.
 * 
 * <EMBED CLASS="external-html" DATA-FILE-ID=PKG_PRIVATE_MSG>
 * <EMBED CLASS="external-html" DATA-FILE-ID=NAV_BUTTONS>
 */
@StaticFunctional
public class NavButtons
{
    private NavButtons() { }

    private static final TextNode NL = new TextNode("\n");

    private static final String MEMBERS_BUTTON_START =
        "<LI>&nbsp;|&nbsp;</LI>\n" +
        "<LI CLASS=DetailsPopupButton onmouseover=" +
            "'document.getElementById(\"PopUpDIV\").style.display=\"block\";'>" +
            "<SPAN STYLE='color: #4D7A97;'>MENU</SPAN>\n" +

        // NOTE: The "<NOSCRIPT>" thing actually looks very respectable.  You have to actually
        //       deactivate JavaScript in Chrome to see this popup.  To do that, Open up 
        //       "Chrome Dev Tools" and Press "Control-Shift P" (on Windows), then start typing
        //       the word "JavaScript" into the Run-Search-Bar that pops up.  It will display 
        //       a button that allows to disable JavaScript for the window you are on

        "<NOSCRIPT>\n" +
        "<DIV CLASS=NoScriptDetailsPopup>JavaScript is disabled on your browser.</DIV>\n" +
        "</NOSCRIPT>\n" +

        // The Pop-Up <DIV> is just an "undisplayable" <DIV> inside the <LI>.  It cannot be
        // displayed unless the CONTAINING/SURROUNDING <LI> gets a "MouseOver" event.
        //
        // NOTE: That the actual hiding of this PopUp Menu is done right here.  Whenever this
        //       <DIV> gets a "MouseClick" or a "MouseOut" event, it hides this DIV again, by
        //       setting it's CSS-'display' property back to "none"

        "<DIV ID=PopUpDIV CLASS=DetailsPopup onclick='this.style.display=\"none\"' " +
            "onmouseout='this.style.display=\"none\"'>\n" +
        "<TABLE>\n";

    private static final String MEMBERS_BUTTON_END = "\n</TABLE>\n</DIV>\n</LI>\n";


    // NOTE: The "unchecked" eventually became necessary because r.html was somehow converted
    //       into a "rawtype" as well, and that cause

    //@SuppressWarnings({"rawtypes", "unchecked"})
    static boolean addButtonsToTop(JavaDocHTMLFile jdhf)
    {
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Build the Top NavBar Buttons: "TOP", "SRC", "HILITED"
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        int LN = (jdhf.jdStartLineNumber != -1)
            ? jdhf.jdStartLineNumber
            : jdhf.startLineNumber;

        String firstLinksMenuStr =
            "<UL CLASS=subNavList>\n" +
            "\t<LI>PAGE:&nbsp;</LI>\n" +
            "\t<LI><A HREF='#top'>TOP</A></LI>\n" +
            ((jdhf.srcAsHTMLFileURL != null)
                ? ("\t<LI>&nbsp;|&nbsp;</LI>\n" +
                    "\t<LI><A HREF='" + jdhf.srcAsHTMLFileURL +
                    (jdhf.isInner ? ("#line." + LN) : "") +
                    "'>SRC</A></LI>\n")
                : "") +
            ((jdhf.hiLitedSrcFileURL != null)
                ? ("\t<LI>&nbsp;|&nbsp;</LI>\n" +
                    "<LI><A HREF='" + jdhf.hiLitedSrcFileURL +
                    (jdhf.isInner ? ("#L" + LN) : "") + "'>HILITED" +
                    "</A></LI>\n")
                : "");


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Build the Nav-Bar "Member's List" Drop-Down Menu
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        // The output HTML is stored here
        StringBuilder               sb      = new StringBuilder();
        EffectivelyFinal<Boolean>   first   = new EffectivelyFinal<>(true);

        // *** *** NOTE *** ***
        // Annotation-Elements & Enum-Constants are FIRST
        // THEY SHOULD BE LISTED FIRST IN THE MENU IS **WHY** ==> They are "special"


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Annotation Elements
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        first.f = true;
        if (jdhf.ciet == CIET.ANNOTATION) jdhf  // (ReflHTML<AnnotationElem> r) ->
            .allAEDetails()
            .filter(r -> r.htmlID != null)
            .sorted((r1, r2) -> r1.entity.compareTo(r2.entity))
            .forEachOrdered(r ->
            {
                sb.append(
                    (first.f
                        ? ("\t<TR CLASS=DPL><TH COLSPAN=2><DIV>Annotation Elements" +
                            "</DIV></TH></TR>\n")
                        : "") +
                    "\t<TR CLASS=DPI onclick='document.location=\"#" + r.htmlID + "\";'>\n" +
                    "\t\t<TD>" + r.entity.typeJOW + "</TD>\n" +
                    "\t\t<TD><SPAN>" + r.entity.name + " &nbsp; </SPAN></TD>\n" +
                    "\t</TR>\n"
                );

                first.f = false;
            });


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Enum Constants
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        first.f = true;
        if (jdhf.ciet == CIET.ENUM) jdhf  // (ReflHTML<EnumConstant> r) ->
            .allECDetails()
            .filter(r -> r.htmlID != null)
            .sorted((r1, r2) -> r1.entity.compareTo(r2.entity))
            .forEachOrdered(r ->
            {
                sb.append(
                    (first.f
                        ? "\t<TR CLASS=DPL><TH COLSPAN=2><DIV>Enum Constants</DIV></TH></TR>\n"
                        : "") +
                    "\t<TR CLASS=DPI onclick='document.location=\"#" + r.htmlID + "\";'>\n" +
                        "\t\t<TD COLSPAN=2><SPAN>" + r.entity.name + "</SPAN> &nbsp; </TD>\n" +
                    "\t</TR>\n"
                );

                first.f = false;
            });


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Fields
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        first.f = true;
        jdhf.allFieldDetails() // (ReflHTML<Field> r) ->
            .filter(r -> r.htmlID != null)
            .sorted((r1, r2) -> r1.entity.compareTo(r2.entity))
            .forEachOrdered(r ->
            {
                sb.append(
                    (first.f
                        ? "\t<TR CLASS=DPL><TH COLSPAN=2><DIV>Fields</DIV></TH></TR>\n"
                        : "") +
                    "\t<TR CLASS=DPI onclick='document.location=\"#" + r.htmlID + "\";'>\n" +
                    "\t\t<TD>" + r.entity.typeJOW + "</TD>\n" +
                    "\t\t<TD><SPAN>" + r.entity.name + "</SPAN> &nbsp; </TD>\n" +
                    "\t</TR>\n"
                );

                first.f = false;
            });


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Constructors
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        first.f = true;
        jdhf.allConstructorDetails() // (ReflHTML<Constructor> r) ->
            .filter(r -> r.htmlID != null)
            .sorted((r1, r2) -> r1.entity.compareTo(r2.entity))
            .forEachOrdered(r ->
            {
                Constructor c = r.entity;
                int         n = c.numParameters();

                sb.append(
                    (first.f
                        ? "\t<TR CLASS=DPL><TH COLSPAN=2><DIV>Constructors</DIV></TH></TR>\n"
                        : "") +
                    "\t<TR CLASS=DPI onclick='document.location=\"#" + r.htmlID + "\";'>\n" +
                        "\t\t<TD COLSPAN=2><SPAN>" +
                        c.name + "</SPAN>("
                );

                first.f = false;

                // Constructors have parameters, and those need to be printed to the output
                for (int i=1; i <= n; i++) sb.append(
                    c.getParameterTypeJOW(i) + "&nbsp;" + c.getParameterName(i) + 
                    ((i < n) ? ", " : "")
                );

                sb.append(") &nbsp; </TD>\n\t</TR>\n");
            });


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Methods
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        first.f = true;
        jdhf.allMethodDetails()  // (ReflHTML<Method> r) ->
            .filter(r -> r.htmlID != null)
            .sorted((r1, r2) -> r1.entity.compareTo(r2.entity))
            .forEachOrdered(r ->
            {
                Method  m = r.entity;
                int     n = m.numParameters();

                sb.append(
                    (first.f
                        ? "\t<TR CLASS=DPL><TH COLSPAN=2><DIV>Methods</DIV></TH></TR>\n"
                        : "") +
                    "\t<TR CLASS=DPI onclick='document.location=\"#" + r.htmlID + "\";'>\n" +
                    "\t\t<TD>" + m.returnTypeJOW + "</TD>\n" +
                    "\t\t<TD><SPAN>" + m.name + "</SPAN>("
                );

                first.f = false;

                // Methods have parameters, and those need to be printed to the output
                for (int i=1; i <= n; i++) sb.append(
                    m.getParameterTypeJOW(i) + "&nbsp;" + m.getParameterName(i) + 
                    ((i < n) ? ", " : "")
                );

                sb.append(") &nbsp; </TD>\n\t</TR>\n");
            });


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Close off the Menus, and Finish the StringBuilder
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

        // There are classes whose Details are completely removed.  If they were, they certainly
        // don't need the opening & closing Menu <LI>'s and <DIV>'s

        if (sb.length() > 0)
        {
            sb.insert(0, MEMBERS_BUTTON_START); // Opening DetailsMenuPopup 
            sb.append(MEMBERS_BUTTON_END);      // Closing detailsMenuPopup
        }

        // Now insert the Initial-Links at the beginning of the StringBuilder.  This is only
        // done in the last step because the sb.length() is checked to see if there even was a
        // "Details Members" Menu built in the first place.  It isn't "costly" to do an insert
        // on a StringBuilder, that's kind of the whole point of the class.

        sb.insert(0, firstLinksMenuStr);
        sb.append("</UL>\n");

        // Now that everything is built, insert the items into the Java Doc Page.
        Vector<HTMLNode> topNavBar = jdhf.headerFooter.topNavBar();

        // Debugging Println
        // System.out.println(Util.pageToString(topNavBar)); Torello.Java.Q.BP();

        // INSERT THIS INTO THE SECOND <UL CLASS=subNavList> on the page
        int insertLocation = InnerTagFindInclusive.nth
            (topNavBar, 2, "ul", "class", TextComparitor.C, "subNavList").end + 1;


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Leave this here for a remembrance...  It's balanced, and this saves time.
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        //
        // NORMAL VERSION:
        // pageTop.addAll(insertLocation, HTMLPage.getPageTokens(sb.toString(), false));

        // CHEATING VERSION:
        topNavBar.insertElementAt(new TextNode(sb.toString()), insertLocation);


        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        // Now do the the links
        // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
        //
        // *NOTE* Order here *DOES NOT* matter - as long as it is the same in **BOTH** the "links"
        // retrieval / String-Array builder **AND** in the method that inserts the links

        Stream.Builder<String> b = Stream.builder();

        if (jdhf.ciet == CIET.ANNOTATION)
            jdhf.allAEDetails().forEachOrdered(ae -> b.accept(ae.htmlID));

        if (jdhf.ciet == CIET.ENUM)
            jdhf.allECDetails().forEachOrdered(ec -> b.accept(ec.htmlID));

        jdhf.allFieldDetails()          .forEachOrdered(f -> b.accept(f.htmlID));
        jdhf.allConstructorDetails()    .forEachOrdered(c -> b.accept(c.htmlID));
        jdhf.allMethodDetails()         .forEachOrdered(m -> b.accept(m.htmlID));

        final String[] links = b.build().toArray(String[]::new);

        addButtonsToDetails(jdhf, links);

        return true;
    }

    static boolean addButtonsToDetails
        (JavaDocHTMLFile jdhf, String[] links)
    {
        int divID = 0;

        if (jdhf.ciet == CIET.ANNOTATION)
        {
            Iterator<ReflHTML<AnnotationElem>> iterAE = jdhf.aeDetailsIterator();
            while (iterAE.hasNext()) insertNavBar(jdhf, iterAE.next(), links, divID++);
        }

        if (jdhf.ciet == CIET.ENUM)
        {
            Iterator<ReflHTML<EnumConstant>> iterEC = jdhf.ecDetailsIterator();
            while (iterEC.hasNext()) insertNavBar(jdhf, iterEC.next(), links, divID++);
        }

        Iterator<ReflHTML<Field>> iterF = jdhf.fieldDetailsIterator();
        while (iterF.hasNext()) insertNavBar(jdhf, iterF.next(), links, divID++);

        Iterator<ReflHTML<Constructor>> iterC = jdhf.constructorDetailsIterator();
        while (iterC.hasNext()) insertNavBar(jdhf, iterC.next(), links, divID++);

        Iterator<ReflHTML<Method>> iterM = jdhf.methodDetailsIterator();
        while (iterM.hasNext()) insertNavBar(jdhf, iterM.next(), links, divID++);

        return true;
    }

    static void insertNavBar
        (JavaDocHTMLFile jdhf, ReflHTML<? extends Declaration> r, String[] links, int divID)
    {
        if (links.length == 0) return;

        TextNode    DIV             = null;
        String      srcLink         = null;
        boolean     srcWasHiLited   = jdhf.hiLitedSrcFileURL != null;

        int lineNum = (r.entity.jdStartLineNumber != -1)
            ? r.entity.jdStartLineNumber
            : r.entity.signatureLineNumber;

        // REMEBER: For "Auto-Generated" constructors, and for "Synthetic Methods" (also auto
        //          generated), there is no source-code for the constructor/method.  In these cases
        //          class Declaration assigns -1 to the line number.

        if (lineNum == -1) srcWasHiLited = false;

        if (srcWasHiLited) srcLink =
            "<A HREF='" + jdhf.hiLitedSrcFileURL + "#L" + lineNum + "'>" +
            "&#11179;</A>";


        // If there is *ONLY 1* Detail on the entire page, there is no need to add any of the
        // arrow navigation buttons at all.  Just add the Source-Code Link, and the arrow to
        // the Summary-Table <TR>  (the "double-up-arrow")

        if ((links.length == 1) && srcWasHiLited) DIV = new TextNode(
            "\n<DIV CLASS=ANAV>\n\t" + 
            "<A HREF='#" + r.entityAsEnum.abbrev + r.entity.id +
                "'>&#8648;</A>\n\t&nbsp;" + // Double-Arrow Up ⇈
            (srcWasHiLited ? srcLink : "") +
            "\n</DIV>\n"
        );

        // This is TRUE when this is the *FIRST DETAIL* on the page.  It cannot have an
        // "Up Arrow", just a "Down Arrow' and a Source-Code Link

        else if (divID == 0) DIV = new TextNode(
            "\n<DIV CLASS=ANAV>\n\t" +
            "<A HREF='#" + links[1] + "'>&#129095;</A>\n\t&nbsp;" + // Down 🡇
            "<A HREF='#" + r.entityAsEnum.abbrev + r.entity.id +
                "'>&#8648;</A>\n\t&nbsp;" + // Double-Arrow Up ⇈
            (srcWasHiLited ? srcLink : "") +
            "\n</DIV>\n"
        );

        // This is TRUE when this is the *LAST DETAIL* on the page.  It cannot have a
        // "Down Arrow", just an "Up Arrow' and a Source-Code Link

        else if (divID == (links.length - 1)) DIV = new TextNode(
            "\n<DIV CLASS=ANAV>\n\t" + 
            "<A HREF='#" + links[links.length - 2] + "'>&#129093;</A>\n\t&nbsp;" + // Up 🡅
            "<A HREF='#" + r.entityAsEnum.abbrev + r.entity.id +
                "'>&#8648;</A>\n\t&nbsp;" + // Double-Arrow Up ⇈
            (srcWasHiLited ? srcLink : "") +
            "\n</DIV>\n"
        );

        // Up Arrow, Down Arrow, and Src-Link
        else DIV = new TextNode(
            "\n<DIV CLASS=ANAV>\n\t" +
            "<A HREF='#" + links[divID - 1] + "'>&#129093;</A>\n\t&nbsp;" + // Up 🡅
            "<A HREF='#" + links[divID + 1] + "'>&#129095;</A>\n\t&nbsp;" + // Down 🡇
            "<A HREF='#" + r.entityAsEnum.abbrev + r.entity.id +
                "'>&#8648;</A>\n\t&nbsp;" + // Double-Arrow Up ⇈
            (srcWasHiLited ? srcLink : "") +
            "\n</DIV>\n"
        );

        // Add into the "Name" vector, immediately afer the <H4> .. it is fine...
        // trust me, i'm a professional
        if (DIV != null) r.name().add(DIV);
    }
}