View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.tree;
18  
19  import java.util.MissingResourceException;
20  
21  import javax.jcr.Node;
22  
23  import org.exoplatform.ecm.webui.utils.Utils;
24  import org.exoplatform.services.jcr.util.Text;
25  import org.exoplatform.services.log.ExoLogger;
26  import org.exoplatform.services.log.Log;
27  import org.exoplatform.webui.config.annotation.ComponentConfig;
28  import org.exoplatform.webui.config.annotation.EventConfig;
29  import org.exoplatform.webui.core.UIRightClickPopupMenu;
30  import org.exoplatform.webui.core.UITree;
31  
32  /**
33   * Created by The eXo Platform SAS
34   * @author : Hoa.Pham
35   *          hoa.pham@exoplatform.com
36   * Jun 23, 2008
37   */
38  
39  @ComponentConfig(
40      template = "system:/groovy/webui/core/UITree.gtmpl" ,
41      events = @EventConfig(listeners = UITree.ChangeNodeActionListener.class)
42    )
43    /**
44     * This class extend <code>org.exoplatform.webui.core.UITree</code>
45     * to render node tree for <code>javax.jcr.Node</code>
46     *
47     * */
48  public class UINodeTree extends UITree {
49  
50    /** The log. */
51    private static final Log LOG = ExoLogger.getLogger(UINodeTree.class.getName());
52    
53    private String rootPath = "";
54  
55    private boolean isTaxonomyLocalize;
56  
57    public boolean isTaxonomyLocalize() {
58      return isTaxonomyLocalize;
59    }
60  
61    public void setTaxonomyLocalize(boolean isTaxonomyLocalize) {
62      this.isTaxonomyLocalize = isTaxonomyLocalize;
63    }
64  
65    public String getRootPath() {
66      return rootPath;
67    }
68  
69    public void setRootPath(String rootPath) {
70      this.rootPath = rootPath;
71    }
72  
73    /*
74     * render nodetype icon for node in tree
75     * @see org.exoplatform.webui.core.UITree#renderNode(java.lang.Object)
76     */
77    public String renderNode(Object obj) throws Exception {
78      Node node = (Node) obj;
79      String nodeTypeIcon = Utils.getNodeTypeIcon(node,"uiIconEcms");
80  
81      String nodeIcon = this.getColapseIcon();
82      String iconGroup = this.getIcon();
83      String note = "" ;
84      if(isSelected(obj)) {
85        nodeIcon = getExpandIcon();
86        iconGroup = getSelectedIcon();
87        note = " nodeSelected" ;
88      }
89      String beanIconField = getBeanIconField();
90      if(beanIconField != null && beanIconField.length() > 0) {
91        if(getFieldValue(obj, beanIconField) != null)
92          iconGroup = (String)getFieldValue(obj, beanIconField);
93      }
94      String objId = Utils.formatNodeName(String.valueOf(getId(obj)));
95      String actionLink = event("ChangeNode", objId);
96      StringBuilder builder = new StringBuilder();
97      if(nodeIcon.equals(getColapseIcon())) {
98        builder.append(" <a class=\"")
99               .append(nodeIcon)
100              .append(" ")
101              .append(nodeTypeIcon)
102              .append("\" href=\"javascript:void(0);\" onclick=\"")
103              .append(actionLink)
104              .append("\">");
105     } else {
106       builder.append(" <a class=\"")
107              .append(nodeIcon)
108              .append(" ")
109              .append(nodeTypeIcon)
110              .append("\" onclick=\"eXo.portal.UIPortalControl.collapseTree(this)")
111              .append("\">");
112     }
113     UIRightClickPopupMenu popupMenu = getUiPopupMenu();
114     String beanFieldValue = getDisplayFieldValue(obj);
115     beanFieldValue = Text.unescapeIllegalJcrChars(Utils.getTitle(node));
116     String className="uiIconFileMini uiIconLightGray";
117     boolean flgSymlink = false;
118     if (Utils.isSymLink(node)) {
119       flgSymlink = true;
120       className = "uiIconNodeLink";
121     }
122     if(popupMenu == null) {
123       builder.append(" <i class=\"").append(className).append(" ").append(iconGroup).append(" ").append(nodeTypeIcon)
124           .append(note).append("\"").append(" title=\"").append(beanFieldValue)
125           .append("\"").append(">");
126       if (flgSymlink) {
127         builder.append("  <i class=\"linkSmall\">")          
128           .append("</i>").append(beanFieldValue);
129       } 
130       builder.append("</i>");
131       builder.append(beanFieldValue);
132     } else {
133       builder.append(" <i class=\"").append(className).append(" ").append(iconGroup).append(" ").append(nodeTypeIcon)
134           .append(note).append("\" ").append(popupMenu.getJSOnclickShowPopup(objId, null)).append(
135               " title=\"").append(beanFieldValue).append("\"").append(">");
136       if (flgSymlink) {
137         builder.append("  <i class=\"linkSmall\">")
138           .append(beanFieldValue)
139           .append("</i>");
140       } 
141       builder.append("</i>");
142       builder.append(beanFieldValue);
143     }
144     builder.append(" </a>");
145     return builder.toString();
146   }
147 
148   private String getDisplayFieldValue(Object bean) throws Exception{
149     if (isTaxonomyLocalize && Node.class.isInstance(bean)) {
150       String path = ((Node)bean).getPath();
151       String taxonomyTreeName = rootPath.substring(rootPath.lastIndexOf("/") + 1);
152       try {
153         String display = taxonomyTreeName.concat(path.replace(rootPath, "")).replaceAll("/", ".");
154         return Utils.getResourceBundle(("eXoTaxonomies.").concat(display).concat(".label"));
155       } catch (MissingResourceException me) {
156         if (LOG.isWarnEnabled()) {
157           LOG.warn(me.getMessage());
158         }
159       }
160     }
161     return String.valueOf(getFieldValue(bean, getBeanLabelField()));
162   }
163 
164   public boolean isSelected(Object obj) throws Exception {
165     Node selectedNode = this.getSelected();
166     Node node = (Node) obj;
167     if(selectedNode == null) return false;
168     return selectedNode.getPath().equals(node.getPath());
169   }
170 }