KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > web > bean > RecentSpacesBean


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.bean;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.faces.context.FacesContext;
24 import javax.faces.event.ActionEvent;
25
26 import org.alfresco.service.cmr.repository.InvalidNodeRefException;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.web.app.Application;
29 import org.alfresco.web.app.context.IContextListener;
30 import org.alfresco.web.app.context.UIContextService;
31 import org.alfresco.web.bean.repository.Node;
32 import org.alfresco.web.bean.repository.Repository;
33 import org.alfresco.web.config.ClientConfigElement;
34 import org.alfresco.web.ui.common.Utils;
35 import org.alfresco.web.ui.repo.component.shelf.UIRecentSpacesShelfItem;
36 import org.apache.log4j.Logger;
37
38 /**
39  * This bean manages the real-time updated list of Recent Spaces in the Shelf component.
40  * <p>
41  * Registers itself as a UI Context Listener so it is informed as to when the current Node ID
42  * has changed in the NavigationBeans. This is used to keep the list of spaces up-to-date.
43  *
44  * @author Kevin Roast
45  */

46 public class RecentSpacesBean implements IContextListener
47 {
48    private static Logger logger = Logger.getLogger(RecentSpacesBean.class);
49    
50    /** The NavigationBean reference */
51    protected NavigationBean navigator;
52    
53    /** The BrowseBean reference */
54    protected BrowseBean browseBean;
55    
56    /** Maximum number of recent spaces to show */
57    private Integer JavaDoc maxRecentSpaces = null;
58    
59    /** List of recent space nodes */
60    private List JavaDoc<Node> recentSpaces = new LinkedList JavaDoc<Node>();
61    
62    
63    // ------------------------------------------------------------------------------
64
// Construction
65

66    /**
67     * Default Constructor
68     */

69    public RecentSpacesBean()
70    {
71       UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this);
72    }
73
74
75    // ------------------------------------------------------------------------------
76
// Bean property getters and setters
77

78    /**
79     * @param navigator The NavigationBean to set.
80     */

81    public void setNavigator(NavigationBean navigator)
82    {
83       this.navigator = navigator;
84    }
85
86    /**
87     * @param browseBean The BrowseBean to set.
88     */

89    public void setBrowseBean(BrowseBean browseBean)
90    {
91       this.browseBean = browseBean;
92    }
93    
94    /**
95     * @return the List of recent spaces
96     */

97    public List JavaDoc<Node> getRecentSpaces()
98    {
99       return this.recentSpaces;
100    }
101    
102    /**
103     * @param spaces List of Nodes
104     */

105    public void setRecentSpaces(List JavaDoc<Node> spaces)
106    {
107       this.recentSpaces = spaces;
108    }
109    
110    
111    // ------------------------------------------------------------------------------
112
// Action method handlers
113

114    /**
115     * Action handler bound to the recent spaces Shelf component called when a Space is clicked
116     */

117    public void navigate(ActionEvent event)
118    {
119       // work out which node was clicked from the event data
120
UIRecentSpacesShelfItem.RecentSpacesEvent spaceEvent = (UIRecentSpacesShelfItem.RecentSpacesEvent)event;
121       Node selectedNode = this.recentSpaces.get(spaceEvent.Index);
122       NodeRef nodeRef = selectedNode.getNodeRef();
123       try
124       {
125          // then navigate to the appropriate node in UI
126
// use browse bean functionality for this as it will update the breadcrumb for us
127
this.browseBean.updateUILocation(nodeRef);
128       }
129       catch (InvalidNodeRefException refErr)
130       {
131          Utils.addErrorMessage(MessageFormat.format(Application.getMessage(
132                FacesContext.getCurrentInstance(), Repository.ERROR_NODEREF), new Object JavaDoc[] {nodeRef.getId()}) );
133          
134          // remove invalid node from recent spaces list
135
this.recentSpaces.remove(spaceEvent.Index);
136       }
137    }
138    
139    
140    // ------------------------------------------------------------------------------
141
// IContextListener implementation
142

143    /**
144     * @see org.alfresco.web.app.context.IContextListener#contextUpdated()
145     */

146    public void contextUpdated()
147    {
148       // We use this listener handler to refresh the recent spaces list. At the point
149
// where this method is called, the current node Id in UI will probably have changed.
150
Node node = this.navigator.getCurrentNode();
151       
152       // search for this node - if it's already in the list remove it so
153
// that it appears at the top for us
154
for (int i=0; i<this.recentSpaces.size(); i++)
155       {
156          if (node.getId().equals(this.recentSpaces.get(i).getId()))
157          {
158             // found same node already in the list - remove it
159
this.recentSpaces.remove(i);
160             break;
161          }
162       }
163       
164       // remove an item if the list is at the maximum length
165
int maxItems = getMaxRecentSpaces();
166       if (this.recentSpaces.size() == maxItems)
167       {
168          this.recentSpaces.remove(maxItems - 1);
169       }
170       
171       if (logger.isDebugEnabled())
172          logger.debug("Inserting node: " + node.getName() + " at top of recent spaces list.");
173       
174       // insert our Node at the top of the list so it's most relevent
175
this.recentSpaces.add(0, node);
176    }
177    
178    /**
179     * @return the max number of recent spaces to show, retrieved from client config
180     */

181    private int getMaxRecentSpaces()
182    {
183       if (maxRecentSpaces == null)
184       {
185          ClientConfigElement config = Application.getClientConfig(FacesContext.getCurrentInstance());
186          maxRecentSpaces = Integer.valueOf(config.getRecentSpacesItems());
187       }
188       
189       return maxRecentSpaces.intValue();
190    }
191 }
192
Popular Tags