1 17 package org.alfresco.web.bean; 18 19 import java.text.MessageFormat ; 20 import java.util.LinkedList ; 21 import java.util.List ; 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 46 public class RecentSpacesBean implements IContextListener 47 { 48 private static Logger logger = Logger.getLogger(RecentSpacesBean.class); 49 50 51 protected NavigationBean navigator; 52 53 54 protected BrowseBean browseBean; 55 56 57 private Integer maxRecentSpaces = null; 58 59 60 private List <Node> recentSpaces = new LinkedList <Node>(); 61 62 63 66 69 public RecentSpacesBean() 70 { 71 UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this); 72 } 73 74 75 78 81 public void setNavigator(NavigationBean navigator) 82 { 83 this.navigator = navigator; 84 } 85 86 89 public void setBrowseBean(BrowseBean browseBean) 90 { 91 this.browseBean = browseBean; 92 } 93 94 97 public List <Node> getRecentSpaces() 98 { 99 return this.recentSpaces; 100 } 101 102 105 public void setRecentSpaces(List <Node> spaces) 106 { 107 this.recentSpaces = spaces; 108 } 109 110 111 114 117 public void navigate(ActionEvent event) 118 { 119 UIRecentSpacesShelfItem.RecentSpacesEvent spaceEvent = (UIRecentSpacesShelfItem.RecentSpacesEvent)event; 121 Node selectedNode = this.recentSpaces.get(spaceEvent.Index); 122 NodeRef nodeRef = selectedNode.getNodeRef(); 123 try 124 { 125 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 [] {nodeRef.getId()}) ); 133 134 this.recentSpaces.remove(spaceEvent.Index); 136 } 137 } 138 139 140 143 146 public void contextUpdated() 147 { 148 Node node = this.navigator.getCurrentNode(); 151 152 for (int i=0; i<this.recentSpaces.size(); i++) 155 { 156 if (node.getId().equals(this.recentSpaces.get(i).getId())) 157 { 158 this.recentSpaces.remove(i); 160 break; 161 } 162 } 163 164 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 this.recentSpaces.add(0, node); 176 } 177 178 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 |