KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > ui > repo > component > UILockIcon


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;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.faces.context.FacesContext;
22 import javax.faces.context.ResponseWriter;
23 import javax.faces.el.ValueBinding;
24
25 import org.alfresco.model.ContentModel;
26 import org.alfresco.service.cmr.lock.LockService;
27 import org.alfresco.service.cmr.lock.LockStatus;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.repository.NodeService;
30 import org.alfresco.web.app.Application;
31 import org.alfresco.web.bean.repository.Repository;
32 import org.alfresco.web.ui.common.Utils;
33 import org.alfresco.web.ui.common.component.SelfRenderingComponent;
34 import org.alfresco.web.ui.repo.WebResources;
35
36 /**
37  * @author Kevin Roast
38  */

39 public class UILockIcon extends SelfRenderingComponent
40 {
41    private static final String JavaDoc MSG_LOCKED_YOU = "locked_you";
42    private static final String JavaDoc MSG_LOCKED_USER = "locked_user";
43    
44    // ------------------------------------------------------------------------------
45
// Component implementation
46

47    /**
48     * @see javax.faces.component.UIComponent#getFamily()
49     */

50    public String JavaDoc getFamily()
51    {
52       return "org.alfresco.faces.LockIcon";
53    }
54    
55    /**
56     * @see javax.faces.component.StateHolder#restoreState(javax.faces.context.FacesContext, java.lang.Object)
57     */

58    public void restoreState(FacesContext context, Object JavaDoc state)
59    {
60       Object JavaDoc values[] = (Object JavaDoc[])state;
61       // standard component attributes are restored by the super class
62
super.restoreState(context, values[0]);
63       this.lockImage = (String JavaDoc)values[1];
64       this.lockOwnerImage = (String JavaDoc)values[2];
65       this.align = (String JavaDoc)values[3];
66       this.width = ((Integer JavaDoc)values[4]).intValue();
67       this.height = ((Integer JavaDoc)values[5]).intValue();
68       this.value = values[6];
69    }
70    
71    /**
72     * @see javax.faces.component.StateHolder#saveState(javax.faces.context.FacesContext)
73     */

74    public Object JavaDoc saveState(FacesContext context)
75    {
76       Object JavaDoc values[] = new Object JavaDoc[7];
77       // standard component attributes are saved by the super class
78
values[0] = super.saveState(context);
79       values[1] = this.lockImage;
80       values[2] = this.lockOwnerImage;
81       values[3] = this.align;
82       values[4] = this.width;
83       values[5] = this.height;
84       values[6] = this.value;
85       return (values);
86    }
87    
88    /**
89     * @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
90     */

91    public void encodeBegin(FacesContext context) throws IOException JavaDoc
92    {
93       if (isRendered() == false)
94       {
95          return;
96       }
97       
98       ResponseWriter out = context.getResponseWriter();
99       
100       // get the value and see if the image is locked
101
NodeService nodeService = getNodeService(context);
102       boolean locked = false;
103       boolean lockedOwner = false;
104       
105       Object JavaDoc val = getValue();
106       NodeRef ref = null;
107       if (val instanceof NodeRef)
108       {
109          ref = (NodeRef)val;
110          if (nodeService.exists(ref) && nodeService.hasAspect(ref, ContentModel.ASPECT_LOCKABLE) == true)
111          {
112             LockStatus lockStatus = getLockService(context).getLockStatus(ref);
113             locked = (lockStatus == LockStatus.LOCKED || lockStatus == LockStatus.LOCK_OWNER);
114             lockedOwner = (lockStatus == LockStatus.LOCK_OWNER);
115          }
116       }
117       
118       String JavaDoc msg = null;
119       
120       if (locked == true)
121       {
122          out.write("&nbsp;<img");
123          
124          outputAttribute(out, getAttributes().get("style"), "style");
125          outputAttribute(out, getAttributes().get("styleClass"), "class");
126          
127          outputAttribute(out, getAlign(), "align");
128          outputAttribute(out, getWidth(), "width");
129          outputAttribute(out, getHeight(), "height");
130          
131          out.write("src=\"");
132          out.write(context.getExternalContext().getRequestContextPath());
133          String JavaDoc lockImage = getLockImage();
134          if (lockedOwner == true && getLockOwnerImage() != null)
135          {
136             lockImage = getLockOwnerImage();
137          }
138          out.write(lockImage);
139          out.write("\" border=0");
140          
141          if (lockedOwner == true)
142          {
143             msg = Application.getMessage(context, MSG_LOCKED_YOU);
144             if (getLockedOwnerTooltip() != null)
145             {
146                msg = getLockedOwnerTooltip();
147             }
148          }
149          else
150          {
151             String JavaDoc lockingUser = (String JavaDoc)nodeService.getProperty(ref, ContentModel.PROP_LOCK_OWNER);
152             msg = Application.getMessage(context, MSG_LOCKED_USER);
153             if (getLockedUserTooltip() != null)
154             {
155                msg = getLockedUserTooltip();
156             }
157             StringBuilder JavaDoc buf = new StringBuilder JavaDoc(32);
158             msg = buf.append(msg).append(" '")
159                      .append(lockingUser)
160                      .append("'").toString();
161          }
162          
163          msg = Utils.encode(msg);
164          out.write(" alt=\"");
165          out.write(msg);
166          out.write("\" title=\"");
167          out.write(msg);
168          out.write("\">");
169       }
170    }
171    
172    /**
173     * Use Spring JSF integration to return the Node Service bean instance
174     *
175     * @param context FacesContext
176     *
177     * @return Node Service bean instance or throws exception if not found
178     */

179    private static NodeService getNodeService(FacesContext context)
180    {
181       NodeService service = Repository.getServiceRegistry(context).getNodeService();
182       if (service == null)
183       {
184          throw new IllegalStateException JavaDoc("Unable to obtain NodeService bean reference.");
185       }
186       
187       return service;
188    }
189    
190    /**
191     * Use Spring JSF integration to return the Lock Service bean instance
192     *
193     * @param context FacesContext
194     *
195     * @return Lock Service bean instance or throws exception if not found
196     */

197    private static LockService getLockService(FacesContext context)
198    {
199       LockService service = Repository.getServiceRegistry(context).getLockService();
200       if (service == null)
201       {
202          throw new IllegalStateException JavaDoc("Unable to obtain LockService bean reference.");
203       }
204       
205       return service;
206    }
207    
208    
209    // ------------------------------------------------------------------------------
210
// Strongly typed component property accessors
211

212    /**
213     * @return the image to display as the lock icon. A default is provided if none is set.
214     */

215    public String JavaDoc getLockImage()
216    {
217       ValueBinding vb = getValueBinding("lockImage");
218       if (vb != null)
219       {
220          this.lockImage = (String JavaDoc)vb.getValue(getFacesContext());
221       }
222       
223       return this.lockImage;
224    }
225    
226    /**
227     * @param lockImage the image to display as the lock icon. A default is provided if none is set.
228     */

229    public void setLockImage(String JavaDoc lockImage)
230    {
231       this.lockImage = lockImage;
232    }
233    
234    /**
235     * @return Returns the image to display if the owner has the lock.
236     */

237    public String JavaDoc getLockOwnerImage()
238    {
239       ValueBinding vb = getValueBinding("lockOwnerImage");
240       if (vb != null)
241       {
242          this.lockOwnerImage = (String JavaDoc)vb.getValue(getFacesContext());
243       }
244       
245       return this.lockOwnerImage;
246    }
247
248    /**
249     * @param lockOwnerImage the image to display if the owner has the lock.
250     */

251    public void setLockOwnerImage(String JavaDoc lockOwnerImage)
252    {
253       this.lockOwnerImage = lockOwnerImage;
254    }
255    
256    /**
257     * @return Returns the image alignment value.
258     */

259    public String JavaDoc getAlign()
260    {
261       ValueBinding vb = getValueBinding("align");
262       if (vb != null)
263       {
264          this.align = (String JavaDoc)vb.getValue(getFacesContext());
265       }
266       
267       return this.align;
268    }
269
270    /**
271     * @param align The image alignment value to set.
272     */

273    public void setAlign(String JavaDoc align)
274    {
275       this.align = align;
276    }
277
278    /**
279     * @return Returns the icon height.
280     */

281    public int getHeight()
282    {
283       ValueBinding vb = getValueBinding("height");
284       if (vb != null)
285       {
286          Integer JavaDoc value = (Integer JavaDoc)vb.getValue(getFacesContext());
287          if (value != null)
288          {
289             this.height = value.intValue();
290          }
291       }
292       
293       return this.height;
294    }
295
296    /**
297     * @param height The icon height to set.
298     */

299    public void setHeight(int height)
300    {
301       this.height = height;
302    }
303    
304    /**
305     * @return Returns the icon width.
306     */

307    public int getWidth()
308    {
309       ValueBinding vb = getValueBinding("width");
310       if (vb != null)
311       {
312          Integer JavaDoc value = (Integer JavaDoc)vb.getValue(getFacesContext());
313          if (value != null)
314          {
315             this.width = value.intValue();
316          }
317       }
318       
319       return this.width;
320    }
321
322    /**
323     * @param width The iconwidth to set.
324     */

325    public void setWidth(int width)
326    {
327       this.width = width;
328    }
329
330    /**
331     * @return Returns the lockedOwnerTooltip.
332     */

333    public String JavaDoc getLockedOwnerTooltip()
334    {
335       ValueBinding vb = getValueBinding("lockedOwnerTooltip");
336       if (vb != null)
337       {
338          this.lockedOwnerTooltip = (String JavaDoc)vb.getValue(getFacesContext());
339       }
340       
341       return this.lockedOwnerTooltip;
342    }
343
344    /**
345     * @param lockedOwnerTooltip The lockedOwnerTooltip to set.
346     */

347    public void setLockedOwnerTooltip(String JavaDoc lockedOwnerTooltip)
348    {
349       this.lockedOwnerTooltip = lockedOwnerTooltip;
350    }
351
352    /**
353     * @return Returns the lockedUserTooltip.
354     */

355    public String JavaDoc getLockedUserTooltip()
356    {
357       ValueBinding vb = getValueBinding("lockedUserTooltip");
358       if (vb != null)
359       {
360          this.lockedUserTooltip = (String JavaDoc)vb.getValue(getFacesContext());
361       }
362       
363       return this.lockedUserTooltip;
364    }
365
366    /**
367     * @param lockedUserTooltip The lockedUserTooltip to set.
368     */

369    public void setLockedUserTooltip(String JavaDoc lockedUserTooltip)
370    {
371       this.lockedUserTooltip = lockedUserTooltip;
372    }
373    
374    /**
375     * @return Returns the value (Node or NodeRef)
376     */

377    public Object JavaDoc getValue()
378    {
379       ValueBinding vb = getValueBinding("value");
380       if (vb != null)
381       {
382          this.value = vb.getValue(getFacesContext());
383       }
384       
385       return this.value;
386    }
387
388    /**
389     * @param value The Node or NodeRef value to set.
390     */

391    public void setValue(Object JavaDoc value)
392    {
393       this.value = value;
394    }
395    
396    
397    private String JavaDoc lockImage = WebResources.IMAGE_LOCK;
398    private String JavaDoc lockOwnerImage = WebResources.IMAGE_LOCK_OWNER;
399    private String JavaDoc align = null;
400    private int width = 16;
401    private int height = 16;
402    private String JavaDoc lockedOwnerTooltip = null;
403    private String JavaDoc lockedUserTooltip = null;
404    private Object JavaDoc value = null;
405 }
406
Popular Tags