KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > shelf > UIClipboardShelfItem


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.web.ui.repo.component.shelf;
18
19 import java.io.IOException JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.ResourceBundle JavaDoc;
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 /**
45  * @author Kevin Roast
46  */

47 public class UIClipboardShelfItem extends UIShelfItem
48 {
49    // ------------------------------------------------------------------------------
50
// Component Impl
51

52    /**
53     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
54     */

55    public void restoreState(FacesContext context, Object JavaDoc state)
56    {
57       Object JavaDoc values[] = (Object JavaDoc[])state;
58       // standard component attributes are restored by the super class
59
super.restoreState(context, values[0]);
60       this.collections = (List JavaDoc)values[1];
61       this.pasteActionListener = (MethodBinding)values[2];
62    }
63    
64    /**
65     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
66     */

67    public Object JavaDoc saveState(FacesContext context)
68    {
69       Object JavaDoc values[] = new Object JavaDoc[3];
70       // standard component attributes are saved by the super class
71
values[0] = super.saveState(context);
72       values[1] = this.collections;
73       values[2] = this.pasteActionListener;
74       return values;
75    }
76    
77    /**
78     * @see javax.faces.component.UIComponentBase#decode(javax.faces.context.FacesContext)
79     */

80    public void decode(FacesContext context)
81    {
82       Map JavaDoc requestMap = context.getExternalContext().getRequestParameterMap();
83       String JavaDoc fieldId = getHiddenFieldName();
84       String JavaDoc value = (String JavaDoc)requestMap.get(fieldId);
85       
86       if (value != null && value.length() != 0)
87       {
88          // decode the values - we are expecting an action identifier and an index
89
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          // raise an event to process the action later in the lifecycle
94
ClipboardEvent event = new ClipboardEvent(this, action, index);
95          this.queueEvent(event);
96       }
97    }
98    
99    /**
100     * @see javax.faces.component.UIComponentBase#broadcast(javax.faces.event.FacesEvent)
101     */

102    public void broadcast(FacesEvent event) throws AbortProcessingException
103    {
104       if (event instanceof ClipboardEvent)
105       {
106          // found an event we should handle
107
ClipboardEvent clipEvent = (ClipboardEvent)event;
108          
109          List JavaDoc<ClipboardItem> items = getCollections();
110          if (items.size() > clipEvent.Index)
111          {
112             // process the action
113
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    /**
137     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
138     */

139    public void encodeBegin(FacesContext context) throws IOException JavaDoc
140    {
141       if (isRendered() == false)
142       {
143          return;
144       }
145       
146       ResponseWriter out = context.getResponseWriter();
147       
148       List JavaDoc<ClipboardItem> items = getCollections();
149       out.write(SHELF_START);
150       if (items.size() != 0)
151       {
152          DictionaryService dd = Repository.getServiceRegistry(context).getDictionaryService();
153          
154          ResourceBundle JavaDoc bundle = Application.getBundle(context);
155          
156          for (int i=0; i<items.size(); i++)
157          {
158             ClipboardItem item = items.get(i);
159             
160             // start row with cut/copy state icon
161
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                // start row with Space icon
175
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 JavaDoc image = Utils.getFileTypeImage(item.Node.getName(), true);
180                out.write(Utils.buildImageTag(context, image, 16, 16, null, null, "absmiddle"));
181             }
182             
183             // output cropped item label - we also output with no breaks, this is ok
184
// as the copped label will ensure a sensible maximum width
185
out.write("</td><td width=100%><nobr>&nbsp;");
186             out.write(Utils.cropEncode(item.Node.getName()));
187             
188             // output actions
189
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("&nbsp;");
192             out.write(buildActionLink(ACTION_PASTE_ITEM, i, bundle.getString(MSG_PASTE_ITEM), WebResources.IMAGE_PASTE));
193             
194             // end actions cell and end row
195
out.write("</nobr></td></tr>");
196          }
197          
198          // output general actions if any clipboard items are present
199
out.write("<tr><td colspan=3><nobr>");
200          out.write(buildActionLink(ACTION_PASTE_ALL, -1, bundle.getString(MSG_PASTE_ALL), null));
201          out.write("&nbsp;");
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    // ------------------------------------------------------------------------------
211
// Strongly typed component property accessors
212

213    /**
214     * @param collections Set the clipboard item collections to use
215     */

216    public void setCollections(List JavaDoc<ClipboardItem> collections)
217    {
218       this.collections = collections;
219    }
220    
221    /**
222     * @return The clipboard item collections to use
223     */

224    public List JavaDoc<ClipboardItem> getCollections()
225    {
226       ValueBinding vb = getValueBinding("collections");
227       if (vb != null)
228       {
229          this.collections = (List JavaDoc<ClipboardItem>)vb.getValue(getFacesContext());
230       }
231       
232       return this.collections;
233    }
234    
235    /**
236     * @param binding The MethodBinding to call when Paste is selected by the user
237     */

238    public void setPasteActionListener(MethodBinding binding)
239    {
240       this.pasteActionListener = binding;
241    }
242    
243    /**
244     * @return The MethodBinding to call when Paste is selected by the user
245     */

246    public MethodBinding getPasteActionListener()
247    {
248       return this.pasteActionListener;
249    }
250    
251    
252    // ------------------------------------------------------------------------------
253
// Private helpers
254

255    /**
256     * We use a hidden field name on the assumption that only one clipboard shelf item instance
257     * is present on a single page.
258     *
259     * @return hidden field name
260     */

261    private String JavaDoc getHiddenFieldName()
262    {
263       return getClientId(getFacesContext());
264    }
265    
266    /**
267     * Encode the specified values for output to a hidden field
268     *
269     * @param action Action identifer
270     * @param index Index of the clipboard item the action is for
271     *
272     * @return encoded values
273     */

274    private static String JavaDoc encodeValues(int action, int index)
275    {
276       return Integer.toString(action) + NamingContainer.SEPARATOR_CHAR + Integer.toString(index);
277    }
278    
279    /**
280     * Build HTML for an link representing a clipboard action
281     *
282     * @param action action indentifier to represent
283     * @param index index of the clipboard item this action relates too
284     * @param text of the action to display
285     * @param image image icon to display
286     *
287     * @return HTML for action link
288     */

289    private String JavaDoc buildActionLink(int action, int index, String JavaDoc text, String JavaDoc image)
290    {
291       FacesContext context = getFacesContext();
292       
293       StringBuilder JavaDoc buf = new StringBuilder JavaDoc(256);
294       
295       buf.append("<a HREF='#' onclick=\"");
296       // generate JavaScript to set a hidden form field and submit
297
// a form which request attributes that we can decode
298
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    // ------------------------------------------------------------------------------
317
// Inner classes
318

319    /**
320     * Class representing the an action relevant to the Clipboard element.
321     */

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    // ------------------------------------------------------------------------------
337
// Private data
338

339    /** I18N messages */
340    private static final String JavaDoc MSG_REMOVE_ALL = "remove_all";
341    private static final String JavaDoc MSG_PASTE_ALL = "paste_all";
342    private static final String JavaDoc MSG_PASTE_ITEM = "paste_item";
343    private static final String JavaDoc MSG_REMOVE_ITEM = "remove_item";
344    private static final String JavaDoc MSG_CUT = "cut";
345    private static final String JavaDoc 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    /** the current list of clipboard items */
353    private List JavaDoc<ClipboardItem> collections;
354    
355    /** action listener called when a paste action occurs */
356    private MethodBinding pasteActionListener;
357 }
358
Popular Tags