KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > webapp > data > IndexData


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 Intel Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Intel Corporation - initial API and implementation
10  * IBM Corporation - 122967 [Help] Remote help system
11  * 163558 Dynamic content support for all UA
12  *******************************************************************************/

13 package org.eclipse.help.internal.webapp.data;
14
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17
18 import javax.servlet.ServletContext JavaDoc;
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.eclipse.help.IIndex;
23 import org.eclipse.help.IIndexEntry;
24 import org.eclipse.help.ITopic;
25 import org.eclipse.help.internal.HelpPlugin;
26
27 /**
28  * Helper class for Index view initialization
29  */

30 public class IndexData extends ActivitiesData {
31     private IIndex index;
32
33     // images directory
34
private String JavaDoc imagesDirectory;
35
36     // plus/minus image file name
37
private String JavaDoc plusMinusImage;
38
39     // name of expand/collapse class for IMG, UL tags
40
private String JavaDoc expandedCollapsed;
41
42     // use or not expand/collapse feature
43
private boolean usePlusMinus;
44
45     // expand all by default flag
46
private boolean expandAll;
47
48     // flag right-to-left direction of text
49
private boolean isRTL;
50
51     // global writer for private generate...() methods
52
private Writer JavaDoc out;
53
54     /**
55      * Constructs the data for the index page.
56      * @param context
57      * @param request
58      */

59     public IndexData(ServletContext JavaDoc context, HttpServletRequest JavaDoc request,
60             HttpServletResponse JavaDoc response) {
61         super(context, request, response);
62
63         imagesDirectory = preferences.getImagesDirectory();
64         usePlusMinus = preferences.isIndexPlusMinus();
65         expandAll = preferences.isIndexExpandAll();
66         plusMinusImage = expandAll ? "/minus.gif" : "/plus.gif"; //$NON-NLS-1$ //$NON-NLS-2$
67
expandedCollapsed = expandAll ? "expanded" : "collapsed"; //$NON-NLS-1$ //$NON-NLS-2$
68
isRTL = UrlUtil.isRTL(request, response);
69         index = HelpPlugin.getIndexManager().getIndex(getLocale());
70     }
71
72     /**
73      * Generates values for array of ids of list items
74      * avaliable to be navigated through typein feature.
75      *
76      * Currently only first level items can be navigated.
77      *
78      * @param out
79      * @throws IOException
80      */

81     public void generateIds(Writer JavaDoc out) throws IOException JavaDoc {
82         boolean first = true;
83         IIndexEntry[] entries = index.getEntries();
84         for (int i=0;i<entries.length;++i) {
85             IIndexEntry entry = entries[i];
86             if (entry != null && entry.getKeyword() != null && entry.getKeyword().length() > 0) {
87                 if (first) {
88                     first = false;
89                 } else {
90                     out.write(",\n"); //$NON-NLS-1$
91
}
92                 out.write("\""); //$NON-NLS-1$
93
out.write(UrlUtil.JavaScriptEncode(entry.getKeyword()));
94                 out.write("\""); //$NON-NLS-1$
95
}
96         }
97     }
98
99     /**
100      * Generates the HTML code (a list) for the index.
101      *
102      * @param out
103      * @throws IOException
104      */

105     public void generateIndex(Writer JavaDoc out) throws IOException JavaDoc {
106         this.out = out;
107         IIndexEntry[] entries = index.getEntries();
108         for (int i=0;i<entries.length;++i) {
109             if (EnabledTopicUtils.isEnabled(entries[i])) {
110                 generateEntry(entries[i], 0);
111             }
112         }
113     }
114
115     /**
116      * Generates the HTML code for an index entry.
117      *
118      * @param entry
119      * @param level
120      * @throws IOException
121      */

122     /*
123      * For advanced UI:
124      * <li>[ plus_image ]<a ...>...</a>
125      * [<ul>list of topics</ul>]
126      * [<ul>nested entries</ul>]
127      * </li>
128      *
129      * For basic UI:
130      * <li><a ...>...</a>
131      * [<ul>
132      * list of topics
133      * nested entries
134      * </ul>]
135      * </li>
136      */

137     private void generateEntry(IIndexEntry entry, int level) throws IOException JavaDoc {
138         if (entry.getKeyword() != null && entry.getKeyword().length() > 0) {
139             ITopic[] topics = EnabledTopicUtils.getEnabled(entry.getTopics());
140             IIndexEntry[] subentries = EnabledTopicUtils.getEnabled(entry.getSubentries());
141             boolean multipleTopics = topics.length > 1;
142             boolean singleTopic = topics.length == 1;
143     
144             out.write("<li>"); //$NON-NLS-1$
145
if (usePlusMinus && advancedUI) generatePlusImage(multipleTopics);
146             generateAnchor(singleTopic, entry, level);
147             if (multipleTopics || subentries.length > 0) {
148                 if (!advancedUI) {
149                     out.write("<ul>\n"); //$NON-NLS-1$
150
}
151                 if (multipleTopics) generateTopicList(entry);
152                 generateSubentries(entry, level + 1);
153                 if (!advancedUI) {
154                     out.write("</ul>\n"); //$NON-NLS-1$
155
}
156             }
157             out.write("</li>\n"); //$NON-NLS-1$
158
}
159     }
160
161     /**
162      * Generates the HTML code (a list) for the index.
163      * Basic UI version.
164      *
165      * @param out
166      * @throws IOException
167      */

168     public void generateBasicIndex(Writer JavaDoc out) throws IOException JavaDoc {
169         this.out = out;
170         IIndexEntry[] entries = index.getEntries();
171         for (int i=0;i<entries.length;++i) {
172             generateBasicEntry(entries[i], 0);
173         }
174     }
175
176     /**
177      * Generates the HTML code for an index entry.
178      * Basic UI version.
179      *
180      * @param entry
181      * @param level
182      * @throws IOException
183      */

184     /*
185      * <tr><td align={ "left" | "right" } nowrap>
186      * <a ...>...</a>
187      * </td></tr>
188      * [<tr><td align={ "left" | "right" } nowrap><ul>
189      * list of topics
190      * nested entries
191      * </ul></td></tr>]
192      */

193     private void generateBasicEntry(IIndexEntry entry, int level) throws IOException JavaDoc {
194         ITopic[] topics = entry.getTopics();
195         IIndexEntry[] subentries = entry.getSubentries();
196         boolean multipleTopics = topics.length > 1;
197         boolean singleTopic = topics.length == 1;
198
199         out.write("<tr><td align=\""); //$NON-NLS-1$
200
out.write(isRTL ? "right" : "left"); //$NON-NLS-1$ //$NON-NLS-2$
201
out.write("\" nowrap>\n"); //$NON-NLS-1$
202
generateAnchor(singleTopic, entry, level);
203         out.write("</td></tr>\n"); //$NON-NLS-1$
204
if (multipleTopics || subentries.length > 0) {
205             out.write("<tr><td align=\""); //$NON-NLS-1$
206
out.write(isRTL ? "right" : "left"); //$NON-NLS-1$ //$NON-NLS-2$
207
out.write("\" nowrap><ul>\n"); //$NON-NLS-1$
208
if (multipleTopics) generateTopicList(entry);
209             generateSubentries(entry, level + 1);
210             out.write("</ul></td></tr>\n"); //$NON-NLS-1$
211
}
212     }
213
214     /**
215      * Generates the HTML code for the plus/minus image.
216      *
217      * @param multipleTopics
218      * @throws IOException
219      */

220     /*
221      * <img scr="images/plus.gif" class={ "collapsed" | "expanded" | "h" } alt="...">
222      */

223     private void generatePlusImage(boolean multipleTopics) throws IOException JavaDoc {
224         out.write("<img SRC=\""); //$NON-NLS-1$
225
out.write(imagesDirectory);
226         out.write(plusMinusImage);
227         out.write("\" class=\""); //$NON-NLS-1$
228
if (multipleTopics) {
229             out.write(expandedCollapsed);
230         } else {
231             out.write("h"); //$NON-NLS-1$
232
}
233         out.write("\" alt=\""); //$NON-NLS-1$
234
if (multipleTopics) {
235             if (expandAll) {
236                 out.write(ServletResources.getString("collapseTopicTitles", request)); //$NON-NLS-1$
237
} else {
238                 out.write(ServletResources.getString("expandTopicTitles", request)); //$NON-NLS-1$
239
}
240         }
241         out.write("\">"); //$NON-NLS-1$
242
}
243
244     /**
245      * Generates the HTML code for an index entry anchor tag.
246      *
247      * @param singleTopic
248      * @param entry
249      * @param level
250      * @throws IOException
251      */

252     /*
253      * For advanced UI:
254      * <a [ id="..." ] [ class="nolink" ] HREF="...">...</a>
255      *
256      * For basic UI:
257      * <a HREF="...">...</a>
258      */

259     private void generateAnchor(boolean singleTopic, IIndexEntry entry, int level) throws IOException JavaDoc {
260         out.write("<a "); //$NON-NLS-1$
261
if (level == 0 && advancedUI) {
262             out.write("id=\""); //$NON-NLS-1$
263
out.write(entry.getKeyword());
264             out.write("\" "); //$NON-NLS-1$
265
}
266         if (singleTopic) {
267             out.write("href=\""); //$NON-NLS-1$
268
out.write(UrlUtil.getHelpURL((entry.getTopics()[0]).getHref()));
269             out.write("\">"); //$NON-NLS-1$
270
} else {
271             if (advancedUI) {
272                 out.write("class=\"nolink\" "); //$NON-NLS-1$
273
}
274             out.write("href=\"about:blank\">"); //$NON-NLS-1$
275
}
276         out.write(UrlUtil.htmlEncode(entry.getKeyword()));
277         out.write("</a>\n"); //$NON-NLS-1$
278
}
279
280     /**
281      * Generates the HTML code for a list of topics.
282      *
283      * @param entry
284      * @throws IOException
285      */

286     /*
287      * For advanced UI:
288      * <ul class={"collapsed" | "expanded"}>
289      * <li><img class="h" SRC="images/plus.gif" alt=""><a HREF="..."><img SRC="images/topic.gif" alt="">...</a></li>
290      * <li>...
291      * </ul>
292      *
293      * For basic UI:
294      * <li><a HREF="..."><img SRC="images/topic.gif" border=0 alt="">...</a></li>
295      * <li>...
296      */

297     private void generateTopicList(IIndexEntry entry) throws IOException JavaDoc {
298         ITopic[] topics = entry.getTopics();
299
300         if (advancedUI) {
301             out.write("\n<ul class=\""); //$NON-NLS-1$
302
out.write(expandedCollapsed);
303             out.write("\">\n"); //$NON-NLS-1$
304
}
305         for (int i = 0; i < topics.length; ++i) {
306             ITopic topic = (ITopic)topics[i];
307
308             out.write("<li>"); //$NON-NLS-1$
309
if (usePlusMinus && advancedUI) {
310                 out.write("<img class=\"h\" SRC=\""); //$NON-NLS-1$
311
out.write(imagesDirectory);
312                 out.write(plusMinusImage);
313                 out.write("\" alt=\"\">"); //$NON-NLS-1$
314
}
315             out.write("<a HREF=\""); //$NON-NLS-1$
316
out.write(UrlUtil.getHelpURL(topic.getHref()));
317             out.write("\"><img SRC=\""); //$NON-NLS-1$
318
out.write(imagesDirectory);
319             out.write("/topic.gif\" "); //$NON-NLS-1$
320
if (!advancedUI) {
321                 out.write("border=0 "); //$NON-NLS-1$
322
}
323             out.write("alt=\"\">"); //$NON-NLS-1$
324
out.write(UrlUtil.htmlEncode(topic.getLabel()));
325             out.write("</a></li>\n"); //$NON-NLS-1$
326
}
327         if (advancedUI) {
328             out.write("</ul>\n"); //$NON-NLS-1$
329
}
330     }
331
332     /**
333      * Generates the HTML for nested index entries.
334      *
335      * @param entry
336      * @param level
337      * @throws IOException
338      */

339     /*
340      * For advanced UI:
341      * <ul class="expanded">
342      * entries...
343      * </ul>
344      *
345      * For basic UI:
346      * entries...
347      */

348     private void generateSubentries(IIndexEntry entry, int level) throws IOException JavaDoc {
349         if (advancedUI) {
350             out.write("<ul class=\"expanded\">\n"); //$NON-NLS-1$
351
}
352         IIndexEntry[] subentries = entry.getSubentries();
353         for (int i=0;i<subentries.length;++i) {
354             generateEntry(subentries[i], level);
355         }
356         if (advancedUI) {
357             out.write("</ul>\n"); //$NON-NLS-1$
358
}
359     }
360 }
361
Popular Tags