KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.help.internal.webapp.data;
12
13 import javax.servlet.*;
14 import javax.servlet.http.*;
15
16 import org.eclipse.help.internal.webapp.servlet.*;
17 import org.eclipse.help.internal.workingset.*;
18
19 /**
20  * This class manages help working sets
21  */

22 public class WorkingSetData extends RequestData {
23     public final static short STATE_UNCHECKED = 0;
24     public final static short STATE_GRAYED = 1;
25     public final static short STATE_CHECKED = 2;
26
27     private WebappWorkingSetManager wsmgr;
28
29     private AdaptableToc[] tocs;
30     private boolean isEditMode;
31
32     public WorkingSetData(ServletContext context, HttpServletRequest request,
33             HttpServletResponse response) {
34         super(context, request, response);
35         wsmgr = new WebappWorkingSetManager(request, response, getLocale());
36         AdaptableTocsArray adaptableTocs = wsmgr.getRoot();
37         tocs = (AdaptableToc[]) adaptableTocs.getChildren();
38         isEditMode = "edit".equals(getOperation()); //$NON-NLS-1$
39
}
40
41     public boolean isEditMode() {
42         return isEditMode;
43     }
44
45     public String JavaDoc getWorkingSetName() {
46         String JavaDoc name = request.getParameter("workingSet"); //$NON-NLS-1$
47
if (name == null)
48             name = ""; //$NON-NLS-1$
49
return name;
50     }
51
52     public WorkingSet getWorkingSet() {
53         String JavaDoc name = getWorkingSetName();
54         if (name != null && name.length() > 0)
55             return wsmgr.getWorkingSet(name);
56         return null;
57     }
58
59     /**
60      * Returns the state of the TOC
61      *
62      * @return boolean
63      */

64     public short getTocState(int toc) {
65         if (!isEditMode())
66             return STATE_UNCHECKED;
67         WorkingSet ws = getWorkingSet();
68         if (ws == null)
69             return STATE_UNCHECKED;
70         if (toc < 0 || toc >= tocs.length)
71             return STATE_UNCHECKED;
72
73         // See if the toc is in the working set
74
AdaptableToc adaptableToc = tocs[toc];
75         AdaptableHelpResource[] elements = ws.getElements();
76         for (int i = 0; i < elements.length; i++) {
77             if (elements[i] == adaptableToc)
78                 return STATE_CHECKED;
79         }
80
81         // Check if it is grayed out
82
int topics = adaptableToc.getChildren().length;
83         boolean allTheSame = true;
84         short baseValue = STATE_UNCHECKED;
85         // base value is that of the first topic
86
if (topics > 0)
87             baseValue = getTopicState(toc, 0);
88         for (int i = 1; allTheSame && i < topics; i++)
89             allTheSame = allTheSame && (getTopicState(toc, i) == baseValue);
90
91         if (!allTheSame)
92             return STATE_GRAYED;
93         return STATE_UNCHECKED;
94     }
95
96     /**
97      * Returns the state of the topic. The state is not dependent on the parent
98      * toc, but only whether it was part of the working set. To get the real
99      * state, the caller must use the parent state as well. This is not done
100      * here for performance reasons. In the JSP, by the time one looks at the
101      * topic, the parent toc has already been processed.
102      *
103      * @param toc
104      * @param topic
105      * @return short
106      */

107     public short getTopicState(int toc, int topic) {
108         if (!isEditMode)
109             return STATE_UNCHECKED;
110         WorkingSet ws = getWorkingSet();
111         if (ws == null)
112             return STATE_UNCHECKED;
113         if (toc < 0 || toc >= tocs.length)
114             return STATE_UNCHECKED;
115
116         AdaptableToc parent = tocs[toc];
117         AdaptableTopic[] topics = (AdaptableTopic[]) parent.getChildren();
118         if (topic < 0 || topic >= topics.length)
119             return STATE_UNCHECKED;
120         AdaptableTopic adaptableTopic = topics[topic];
121         AdaptableHelpResource[] elements = ws.getElements();
122         for (int i = 0; i < elements.length; i++) {
123             if (elements[i] == adaptableTopic)
124                 return STATE_CHECKED;
125         }
126         return STATE_UNCHECKED;
127     }
128
129     public String JavaDoc getOperation() {
130         return request.getParameter("operation"); //$NON-NLS-1$
131
}
132
133     // Accessor methods to avoid exposing help classes directly to JSP.
134
// Note: this seems ok for now, but maybe we need to reconsider this
135
// and allow help classes in JSP's.
136

137     public int getTocCount() {
138         return tocs.length;
139     }
140
141     public String JavaDoc getTocLabel(int i) {
142         return tocs[i].getLabel();
143     }
144
145     public String JavaDoc getTocHref(int i) {
146         return tocs[i].getHref();
147     }
148
149     public int getTopicCount(int toc) {
150         return tocs[toc].getTopics().length;
151     }
152
153     public String JavaDoc getTopicLabel(int toc, int topic) {
154         return tocs[toc].getTopics()[topic].getLabel();
155     }
156 }
157
Popular Tags