1 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 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 name; 32 private WebappWorkingSetManager wsmgr; 33 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"); 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 [] hrefs = request.getParameterValues("hrefs"); if (hrefs == null) 65 hrefs = new String [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 oldName = request.getParameter("oldName"); if (oldName == null || oldName.length() == 0) 96 oldName = name; 97 WorkingSet ws = wsmgr.getWorkingSet(oldName); 98 if (ws != null) { 99 String [] hrefs = request.getParameterValues("hrefs"); if (hrefs == null) 101 hrefs = new String [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 118 wsmgr.workingSetChanged(ws); 121 } 122 } 123 } 124 125 public String [] getWorkingSets() { 126 WorkingSet[] workingSets = wsmgr.getWorkingSets(); 127 String [] sets = new String [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 getWorkingSetName() { 135 if (name == null || name.length() == 0) { 136 name = wsmgr.getCurrentWorkingSet(); 138 if (name == null || name.length() == 0 139 || wsmgr.getWorkingSet(name) == null) 140 name = ServletResources.getString("All", request); } 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 op = request.getParameter("operation"); if ("add".equals(op)) return ADD; 160 else if ("remove".equals(op)) return REMOVE; 162 else if ("edit".equals(op)) return EDIT; 164 else 165 return NONE; 166 } 167 168 private AdaptableHelpResource getAdaptableHelpResource(String internalId) { 169 AdaptableHelpResource res = wsmgr.getAdaptableToc(internalId); 170 if (res == null) 171 res = wsmgr.getAdaptableTopic(internalId); 172 return res; 173 } 174 177 public String getSaveError() { 178 if (saved) { 179 return null; 180 } 181 return UrlUtil.JavaScriptEncode(ServletResources.getString( 182 "cookieSaveFailed", request)); } 184 } 185 | Popular Tags |