KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM 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  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - fix for Bug 182466
11  *******************************************************************************/

12 package org.eclipse.help.internal.webapp.data;
13 import javax.servlet.*;
14 import javax.servlet.http.*;
15
16 import org.eclipse.help.*;
17 import org.eclipse.help.internal.*;
18
19 /**
20  * Helper class for linksView.jsp initialization
21  */

22 public class LinksData extends RequestData {
23
24     // Request parameters
25
private String JavaDoc topicHref;
26     private String JavaDoc selectedTopicId = ""; //$NON-NLS-1$
27

28     // list of related links
29
private IHelpResource[] links;
30
31     /**
32      * Constructs data for the links page.
33      *
34      * @param context
35      * @param request
36      */

37     public LinksData(ServletContext context, HttpServletRequest request,
38             HttpServletResponse response) {
39         super(context, request, response);
40         this.topicHref = request.getParameter("topic"); //$NON-NLS-1$
41
if (topicHref != null && topicHref.length() == 0)
42             topicHref = null;
43
44         if (isLinksRequest())
45             loadLinks();
46     }
47
48     /**
49      * Returns true when there is a search request
50      *
51      * @return boolean
52      */

53     public boolean isLinksRequest() {
54         return (request.getParameter("contextId") != null); //$NON-NLS-1$
55
}
56
57     /**
58      * Return the number of links
59      *
60      * @return int
61      */

62     public int getLinksCount() {
63         return links.length;
64     }
65
66     public String JavaDoc getSelectedTopicId() {
67         return selectedTopicId;
68     }
69
70     public String JavaDoc getTopicHref(int i) {
71         return UrlUtil.getHelpURL(links[i].getHref());
72     }
73
74     public String JavaDoc getTopicLabel(int i) {
75         return UrlUtil.htmlEncode(links[i].getLabel());
76     }
77
78     public String JavaDoc getTopicTocLabel(int i) {
79         IToc toc = findTocForTopic(links[i].getHref());
80         if (toc != null)
81             return UrlUtil.htmlEncode(toc.getLabel());
82         return ""; //$NON-NLS-1$
83
}
84
85     /**
86      * Finds a topic in a toc or within a scope if specified
87      */

88     private IToc findTocForTopic(String JavaDoc href) {
89         IToc[] tocs = HelpPlugin.getTocManager().getTocs(getLocale());
90         for (int i = 0; i < tocs.length; i++) {
91             ITopic topic = tocs[i].getTopic(href);
92             if (topic != null)
93                 return tocs[i];
94         }
95         return null;
96     }
97
98     private void loadLinks() {
99         String JavaDoc contextId = request.getParameter("contextId"); //$NON-NLS-1$
100
if (contextId == null) {
101             links = new IHelpResource[0];
102                     return;
103         }
104         IContext context = HelpSystem.getContext(contextId);
105         if (context == null) {
106             links = new IHelpResource[0];
107             return;
108         }
109         links = context.getRelatedTopics();
110         if (links == null) {
111             links = new IHelpResource[0];
112             return;
113         }
114
115         for (int i = 0; i < links.length; i++) {
116             // the following assume topic numbering as in linksView.jsp
117
if (links[i].getHref().equals(topicHref)) {
118                 selectedTopicId = "a" + i; //$NON-NLS-1$
119
break;
120             }
121         }
122     }
123
124 }
125
Popular Tags