1 17 package org.alfresco.web.ui.repo.component.shelf; 18 19 import java.io.IOException ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.ResourceBundle ; 23 24 import javax.faces.component.NamingContainer; 25 import javax.faces.component.UIComponent; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseWriter; 28 import javax.faces.el.MethodBinding; 29 import javax.faces.el.ValueBinding; 30 import javax.faces.event.AbortProcessingException; 31 import javax.faces.event.ActionEvent; 32 import javax.faces.event.FacesEvent; 33 34 import org.alfresco.model.ContentModel; 35 import org.alfresco.service.cmr.dictionary.DictionaryService; 36 import org.alfresco.web.app.Application; 37 import org.alfresco.web.bean.clipboard.ClipboardItem; 38 import org.alfresco.web.bean.clipboard.ClipboardStatus; 39 import org.alfresco.web.bean.repository.Repository; 40 import org.alfresco.web.ui.common.Utils; 41 import org.alfresco.web.ui.repo.WebResources; 42 43 44 47 public class UIClipboardShelfItem extends UIShelfItem 48 { 49 52 55 public void restoreState(FacesContext context, Object state) 56 { 57 Object values[] = (Object [])state; 58 super.restoreState(context, values[0]); 60 this.collections = (List )values[1]; 61 this.pasteActionListener = (MethodBinding)values[2]; 62 } 63 64 67 public Object saveState(FacesContext context) 68 { 69 Object values[] = new Object [3]; 70 values[0] = super.saveState(context); 72 values[1] = this.collections; 73 values[2] = this.pasteActionListener; 74 return values; 75 } 76 77 80 public void decode(FacesContext context) 81 { 82 Map requestMap = context.getExternalContext().getRequestParameterMap(); 83 String fieldId = getHiddenFieldName(); 84 String value = (String )requestMap.get(fieldId); 85 86 if (value != null && value.length() != 0) 87 { 88 int sepIndex = value.indexOf(NamingContainer.SEPARATOR_CHAR); 90 int action = Integer.parseInt(value.substring(0, sepIndex)); 91 int index = Integer.parseInt(value.substring(sepIndex + 1)); 92 93 ClipboardEvent event = new ClipboardEvent(this, action, index); 95 this.queueEvent(event); 96 } 97 } 98 99 102 public void broadcast(FacesEvent event) throws AbortProcessingException 103 { 104 if (event instanceof ClipboardEvent) 105 { 106 ClipboardEvent clipEvent = (ClipboardEvent)event; 108 109 List <ClipboardItem> items = getCollections(); 110 if (items.size() > clipEvent.Index) 111 { 112 switch (clipEvent.Action) 114 { 115 case ACTION_REMOVE_ALL: 116 items.clear(); 117 break; 118 119 case ACTION_REMOVE_ITEM: 120 items.remove(clipEvent.Index); 121 break; 122 123 case ACTION_PASTE_ALL: 124 case ACTION_PASTE_ITEM: 125 Utils.processActionMethod(getFacesContext(), getPasteActionListener(), clipEvent); 126 break; 127 } 128 } 129 } 130 else 131 { 132 super.broadcast(event); 133 } 134 } 135 136 139 public void encodeBegin(FacesContext context) throws IOException 140 { 141 if (isRendered() == false) 142 { 143 return; 144 } 145 146 ResponseWriter out = context.getResponseWriter(); 147 148 List <ClipboardItem> items = getCollections(); 149 out.write(SHELF_START); 150 if (items.size() != 0) 151 { 152 DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService(); 153 154 ResourceBundle bundle = Application.getBundle(context); 155 156 for (int i=0; i<items.size(); i++) 157 { 158 ClipboardItem item = items.get(i); 159 160 out.write("<tr><td>"); 162 if (item.Mode == ClipboardStatus.COPY) 163 { 164 out.write(Utils.buildImageTag(context, WebResources.IMAGE_COPY, 14, 16, bundle.getString(MSG_COPY), null, "absmiddle")); 165 } 166 else 167 { 168 out.write(Utils.buildImageTag(context, WebResources.IMAGE_CUT, 13, 16, bundle.getString(MSG_CUT), null, "absmiddle")); 169 } 170 out.write("</td><td>"); 171 172 if (dd.isSubClass(item.Node.getType(), ContentModel.TYPE_FOLDER)) 173 { 174 out.write(Utils.buildImageTag(context, WebResources.IMAGE_SPACE, 16, 16, null, null, "absmiddle")); 176 } 177 else if (dd.isSubClass(item.Node.getType(), ContentModel.TYPE_CONTENT)) 178 { 179 String image = Utils.getFileTypeImage(item.Node.getName(), true); 180 out.write(Utils.buildImageTag(context, image, 16, 16, null, null, "absmiddle")); 181 } 182 183 out.write("</td><td width=100%><nobr> "); 186 out.write(Utils.cropEncode(item.Node.getName())); 187 188 out.write("</nobr></td><td align=right><nobr>"); 190 out.write(buildActionLink(ACTION_REMOVE_ITEM, i, bundle.getString(MSG_REMOVE_ITEM), WebResources.IMAGE_REMOVE)); 191 out.write(" "); 192 out.write(buildActionLink(ACTION_PASTE_ITEM, i, bundle.getString(MSG_PASTE_ITEM), WebResources.IMAGE_PASTE)); 193 194 out.write("</nobr></td></tr>"); 196 } 197 198 out.write("<tr><td colspan=3><nobr>"); 200 out.write(buildActionLink(ACTION_PASTE_ALL, -1, bundle.getString(MSG_PASTE_ALL), null)); 201 out.write(" "); 202 out.write(buildActionLink(ACTION_REMOVE_ALL, -1, bundle.getString(MSG_REMOVE_ALL), null)); 203 out.write("</nobr></td><td></td></tr>"); 204 } 205 206 out.write(SHELF_END); 207 } 208 209 210 213 216 public void setCollections(List <ClipboardItem> collections) 217 { 218 this.collections = collections; 219 } 220 221 224 public List <ClipboardItem> getCollections() 225 { 226 ValueBinding vb = getValueBinding("collections"); 227 if (vb != null) 228 { 229 this.collections = (List <ClipboardItem>)vb.getValue(getFacesContext()); 230 } 231 232 return this.collections; 233 } 234 235 238 public void setPasteActionListener(MethodBinding binding) 239 { 240 this.pasteActionListener = binding; 241 } 242 243 246 public MethodBinding getPasteActionListener() 247 { 248 return this.pasteActionListener; 249 } 250 251 252 255 261 private String getHiddenFieldName() 262 { 263 return getClientId(getFacesContext()); 264 } 265 266 274 private static String encodeValues(int action, int index) 275 { 276 return Integer.toString(action) + NamingContainer.SEPARATOR_CHAR + Integer.toString(index); 277 } 278 279 289 private String buildActionLink(int action, int index, String text, String image) 290 { 291 FacesContext context = getFacesContext(); 292 293 StringBuilder buf = new StringBuilder (256); 294 295 buf.append("<a HREF='#' onclick=\""); 296 buf.append(Utils.generateFormSubmit(context, this, getHiddenFieldName(), encodeValues(action, index))); 299 buf.append("\">"); 300 301 if (image != null) 302 { 303 buf.append(Utils.buildImageTag(context, image, text)); 304 } 305 else 306 { 307 buf.append(Utils.encode(text)); 308 } 309 310 buf.append("</a>"); 311 312 return buf.toString(); 313 } 314 315 316 319 322 public static class ClipboardEvent extends ActionEvent 323 { 324 public ClipboardEvent(UIComponent component, int action, int index) 325 { 326 super(component); 327 Action = action; 328 Index = index; 329 } 330 331 public int Action; 332 public int Index; 333 } 334 335 336 339 340 private static final String MSG_REMOVE_ALL = "remove_all"; 341 private static final String MSG_PASTE_ALL = "paste_all"; 342 private static final String MSG_PASTE_ITEM = "paste_item"; 343 private static final String MSG_REMOVE_ITEM = "remove_item"; 344 private static final String MSG_CUT = "cut"; 345 private static final String MSG_COPY = "copy"; 346 347 private final static int ACTION_REMOVE_ITEM = 0; 348 private final static int ACTION_REMOVE_ALL = 1; 349 private final static int ACTION_PASTE_ITEM = 2; 350 private final static int ACTION_PASTE_ALL = 3; 351 352 353 private List <ClipboardItem> collections; 354 355 356 private MethodBinding pasteActionListener; 357 } 358 | Popular Tags |