1 11 package org.eclipse.help.internal.webapp.servlet; 12 13 import java.io.*; 14 import java.util.*; 15 16 import javax.servlet.http.*; 17 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.help.internal.*; 20 import org.eclipse.help.internal.util.*; 21 import org.eclipse.help.internal.webapp.*; 22 import org.eclipse.help.internal.workingset.*; 23 24 30 public class InfocenterWorkingSetManager implements IHelpWorkingSetManager { 31 private static final String COOKIE_NAME = "wset"; private static final int MAX_COOKIES = 15; 33 private HttpServletRequest request; 34 private HttpServletResponse response; 35 36 private String currentWorkingSet = ""; private SortedSet workingSets = new TreeSet(new WorkingSetComparator()); 39 private String locale; 40 private AdaptableTocsArray root; 41 42 47 public InfocenterWorkingSetManager(HttpServletRequest request, 48 HttpServletResponse response, String locale) { 49 this.request = request; 50 this.response = response; 51 this.locale = locale; 52 restoreState(); 53 } 54 55 public AdaptableTocsArray getRoot() { 56 if (root == null) 57 root = new AdaptableTocsArray(HelpPlugin.getTocManager().getTocs( 58 locale)); 59 return root; 60 } 61 62 65 public void addWorkingSet(WorkingSet workingSet) throws IOException { 66 if (workingSet == null || workingSets.contains(workingSet)) 67 return; 68 workingSets.add(workingSet); 69 saveState(); 70 } 71 72 75 public WorkingSet createWorkingSet(String name, 76 AdaptableHelpResource[] elements) { 77 return new WorkingSet(name, elements); 78 } 79 80 84 public WorkingSet getWorkingSet(String name) { 85 if (name == null || workingSets == null) 86 return null; 87 88 Iterator iter = workingSets.iterator(); 89 while (iter.hasNext()) { 90 WorkingSet workingSet = (WorkingSet) iter.next(); 91 if (name.equals(workingSet.getName())) 92 return workingSet; 93 } 94 return null; 95 } 96 97 102 public WorkingSet[] getWorkingSets() { 103 return (WorkingSet[]) workingSets.toArray(new WorkingSet[workingSets 104 .size()]); 105 } 106 107 110 public void removeWorkingSet(WorkingSet workingSet) { 111 workingSets.remove(workingSet); 112 try { 113 saveState(); 114 } catch (IOException ioe) { 115 } 116 } 117 118 private void restoreState() { 119 String data = CookieUtil.restoreString(COOKIE_NAME, request); 120 if (data == null) { 121 return; 122 } 123 124 String [] values = data.split("\\|", -1); if (values.length < 1) { 126 return; 127 } 128 currentWorkingSet = URLCoder.decode(values[0] 129 ); 130 i : for (int i = 1; i < values.length; i++) { 131 String [] nameAndHrefs = values[i].split("&", -1); 133 String name = URLCoder.decode(nameAndHrefs[0] 134 ); 135 136 AdaptableHelpResource[] elements = new AdaptableHelpResource[nameAndHrefs.length - 1]; 137 for (int e = 0; e < nameAndHrefs.length - 1; e++) { 139 int h = e + 1; 140 elements[e] = getAdaptableToc(URLCoder.decode(nameAndHrefs[h] 141 142 )); 143 if (elements[e] == null) { 144 elements[e] = getAdaptableTopic(URLCoder 145 .decode(nameAndHrefs[h] 146 147 )); 148 } 149 if (elements[e] == null) { 150 continue i; 152 } 153 } 154 WorkingSet ws = createWorkingSet(name, elements); 155 workingSets.add(ws); 156 } 157 } 158 159 164 private void saveState() throws IOException { 165 StringBuffer data = new StringBuffer (); 166 data.append(URLCoder.encode(currentWorkingSet 167 )); 168 169 for (Iterator i = workingSets.iterator(); i.hasNext();) { 170 data.append('|'); 171 WorkingSet ws = (WorkingSet) i.next(); 172 data.append(URLCoder.encode(ws.getName() 173 )); 174 175 AdaptableHelpResource[] resources = ws.getElements(); 176 for (int j = 0; j < resources.length; j++) { 177 data.append('&'); 178 179 IAdaptable parent = resources[j].getParent(); 180 if (parent == getRoot()) { 181 data.append(URLCoder.encode(resources[j].getHref() 183 184 )); 185 } else { 186 AdaptableToc toc = (AdaptableToc) parent; 188 AdaptableHelpResource[] siblings = (toc).getChildren(); 189 for (int t = 0; t < siblings.length; t++) { 190 if (siblings[t] == resources[j]) { 191 data.append(URLCoder.encode(toc.getHref() 192 193 )); 194 data.append('_'); 195 data.append(t); 196 data.append('_'); 197 break; 198 } 199 } 200 } 201 } 202 } 203 204 try { 205 CookieUtil.saveString(COOKIE_NAME, data.toString(), MAX_COOKIES, 206 request, response); 207 } catch (IOException ioe) { 208 if (HelpWebappPlugin.DEBUG_WORKINGSETS) { 209 System.out 210 .println("InfocenterWorkingSetManager.saveState(): Too much data to save: " + data.toString()); 212 } 213 throw ioe; 214 } 215 } 216 217 223 public void workingSetChanged(WorkingSet changedWorkingSet) 224 throws IOException { 225 saveState(); 226 } 227 228 public AdaptableToc getAdaptableToc(String href) { 229 return getRoot().getAdaptableToc(href); 230 } 231 232 public AdaptableTopic getAdaptableTopic(String id) { 233 234 if (id == null || id.length() == 0) 235 return null; 236 237 int len = id.length(); 241 if (id.charAt(len - 1) == '_') { 242 String indexStr = id.substring(id.lastIndexOf('_', len - 2) + 1, 244 len - 1); 245 int index = 0; 246 try { 247 index = Integer.parseInt(indexStr); 248 } catch (Exception e) { 249 } 250 251 String tocStr = id.substring(0, id.lastIndexOf('_', len - 2)); 252 AdaptableToc toc = getAdaptableToc(tocStr); 253 if (toc == null) 254 return null; 255 IAdaptable[] topics = toc.getChildren(); 256 if (index < 0 || index >= topics.length) 257 return null; 258 return (AdaptableTopic) topics[index]; 259 } 260 261 return null; 262 } 263 264 public String getCurrentWorkingSet() { 265 return currentWorkingSet; 266 } 267 268 public void setCurrentWorkingSet(String workingSet) { 269 currentWorkingSet = workingSet; 270 try { 271 saveState(); 272 } catch (IOException ioe) { 273 } 274 } 275 276 } 277 | Popular Tags |