KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.*;
14 import java.util.*;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18
19 import org.eclipse.help.internal.webapp.servlet.*;
20 import org.eclipse.help.internal.workingset.*;
21
22 /**
23  * This class manages help working sets
24  */

25 public class WorkingSetManagerData extends RequestData {
26     private final static int NONE = 0;
27     private final static int ADD = 1;
28     private final static int REMOVE = 2;
29     private final static int EDIT = 3;
30
31     private String JavaDoc name;
32     private WebappWorkingSetManager wsmgr;
33     // Indicates whether operation specified in the request failed
34
private boolean saved = true;
35
36     public WorkingSetManagerData(ServletContext context,
37             HttpServletRequest request, HttpServletResponse response) {
38         super(context, request, response);
39         wsmgr = new WebappWorkingSetManager(request, response, getLocale());
40         name = request.getParameter("workingSet"); //$NON-NLS-1$
41
try {
42             switch (getOperation()) {
43                 case ADD :
44                     addWorkingSet();
45                     break;
46                 case REMOVE :
47                     removeWorkingSet();
48                     break;
49                 case EDIT :
50                     editWorkingSet();
51                     break;
52                 default :
53                     break;
54             }
55         } catch (IOException ioe) {
56             saved = false;
57         }
58     }
59
60     public void addWorkingSet() throws IOException {
61         if (name != null && name.length() > 0) {
62
63             String JavaDoc[] hrefs = request.getParameterValues("hrefs"); //$NON-NLS-1$
64
if (hrefs == null)
65                 hrefs = new String JavaDoc[0];
66
67             ArrayList selectedElements = new ArrayList(hrefs.length);
68             for (int i = 0; i < hrefs.length; i++) {
69                 AdaptableHelpResource res = getAdaptableHelpResource(hrefs[i]);
70                 if (res != null)
71                     selectedElements.add(res);
72             }
73
74             AdaptableHelpResource[] elements = new AdaptableHelpResource[selectedElements
75                     .size()];
76             selectedElements.toArray(elements);
77             WorkingSet ws = wsmgr.createWorkingSet(name, elements);
78             wsmgr.addWorkingSet(ws);
79         }
80     }
81
82     public void removeWorkingSet() {
83         if (name != null && name.length() > 0) {
84
85             WorkingSet ws = wsmgr.getWorkingSet(name);
86             if (ws != null)
87                 wsmgr.removeWorkingSet(ws);
88         }
89     }
90
91     public void editWorkingSet() throws IOException {
92         if (name != null && name.length() > 0) {
93
94             String JavaDoc oldName = request.getParameter("oldName"); //$NON-NLS-1$
95
if (oldName == null || oldName.length() == 0)
96                 oldName = name;
97             WorkingSet ws = wsmgr.getWorkingSet(oldName);
98             if (ws != null) {
99                 String JavaDoc[] hrefs = request.getParameterValues("hrefs"); //$NON-NLS-1$
100
if (hrefs == null)
101                     hrefs = new String JavaDoc[0];
102
103                 ArrayList selectedElements = new ArrayList(hrefs.length);
104                 for (int i = 0; i < hrefs.length; i++) {
105                     AdaptableHelpResource res = getAdaptableHelpResource(hrefs[i]);
106                     if (res != null)
107                         selectedElements.add(res);
108                 }
109
110                 AdaptableHelpResource[] elements = new AdaptableHelpResource[selectedElements
111                         .size()];
112                 selectedElements.toArray(elements);
113
114                 ws.setElements(elements);
115                 ws.setName(name);
116                 // should also change the name....
117

118                 // We send this notification, so that the manager fires to its
119
// listeners
120
wsmgr.workingSetChanged(ws);
121             }
122         }
123     }
124
125     public String JavaDoc[] getWorkingSets() {
126         WorkingSet[] workingSets = wsmgr.getWorkingSets();
127         String JavaDoc[] sets = new String JavaDoc[workingSets.length];
128         for (int i = 0; i < workingSets.length; i++)
129             sets[i] = workingSets[i].getName();
130
131         return sets;
132     }
133
134     public String JavaDoc getWorkingSetName() {
135         if (name == null || name.length() == 0) {
136             // See if anything is set in the preferences
137
name = wsmgr.getCurrentWorkingSet();
138             if (name == null || name.length() == 0
139                     || wsmgr.getWorkingSet(name) == null)
140                 name = ServletResources.getString("All", request); //$NON-NLS-1$
141
}
142         return name;
143     }
144
145     public WorkingSet getWorkingSet() {
146         if (name != null && name.length() > 0)
147             return wsmgr.getWorkingSet(name);
148         return null;
149     }
150
151     public boolean isCurrentWorkingSet(int i) {
152         WorkingSet[] workingSets = wsmgr.getWorkingSets();
153         return workingSets[i].getName().equals(name);
154     }
155
156     private int getOperation() {
157         String JavaDoc op = request.getParameter("operation"); //$NON-NLS-1$
158
if ("add".equals(op)) //$NON-NLS-1$
159
return ADD;
160         else if ("remove".equals(op)) //$NON-NLS-1$
161
return REMOVE;
162         else if ("edit".equals(op)) //$NON-NLS-1$
163
return EDIT;
164         else
165             return NONE;
166     }
167
168     private AdaptableHelpResource getAdaptableHelpResource(String JavaDoc internalId) {
169         AdaptableHelpResource res = wsmgr.getAdaptableToc(internalId);
170         if (res == null)
171             res = wsmgr.getAdaptableTopic(internalId);
172         return res;
173     }
174     /**
175      * @return null or error message if saving saved
176      */

177     public String JavaDoc getSaveError() {
178         if (saved) {
179             return null;
180         }
181         return UrlUtil.JavaScriptEncode(ServletResources.getString(
182                 "cookieSaveFailed", request)); //$NON-NLS-1$
183
}
184 }
185
Popular Tags