001/*
002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *
016 * $Id$
017 */
018
019package org.nuxeo.ecm.webapp.tree.nav;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.HashMap;
024import java.util.Map;
025
026import org.nuxeo.common.xmap.annotation.XNode;
027import org.nuxeo.common.xmap.annotation.XObject;
028import org.nuxeo.ecm.platform.actions.Action;
029import org.nuxeo.ecm.platform.actions.ActionPropertiesDescriptor;
030import org.nuxeo.ecm.webapp.directory.DirectoryTreeDescriptor;
031
032/**
033 * Descriptor for navigation tree contributions.
034 * <p>
035 * Moved from module nuxeo-platform-virtual-navigation-web, originally added in 5.6.
036 *
037 * @since 6.0
038 */
039@XObject("navTree")
040public class NavTreeDescriptor implements Serializable, Comparable<NavTreeDescriptor> {
041
042    /**
043     * @since 6.0
044     */
045    public static final String ACTION_ID_PREFIX = "navtree_";
046
047    private static final long serialVersionUID = 1L;
048
049    @XNode("@treeId")
050    private String treeId;
051
052    @XNode("@treeLabel")
053    private String treeLabel;
054
055    @XNode("@xhtmlview")
056    private String xhtmlview;
057
058    @XNode("@directoryTreeBased")
059    private boolean directoryTreeBased = false;
060
061    @XNode("@order")
062    private Integer order = Integer.valueOf(100);
063
064    @XNode("@enabled")
065    private boolean enabled = true;
066
067    public boolean isDirectoryTreeBased() {
068        return directoryTreeBased;
069    }
070
071    public void setDirectoryTreeBased(boolean directoryTreeBased) {
072        this.directoryTreeBased = directoryTreeBased;
073    }
074
075    public String getXhtmlview() {
076        return xhtmlview;
077    }
078
079    public void setXhtmlview(String xhtmlview) {
080        this.xhtmlview = xhtmlview;
081    }
082
083    public NavTreeDescriptor() {
084    }
085
086    public NavTreeDescriptor(String treeId, String treeLabel) {
087        this(treeId, treeLabel, false);
088    }
089
090    public NavTreeDescriptor(String treeId, String treeLabel, boolean directoryTreeBased) {
091        this.treeId = treeId;
092        this.treeLabel = treeLabel;
093        this.directoryTreeBased = directoryTreeBased;
094    }
095
096    public String getTreeId() {
097        return treeId;
098    }
099
100    public void setTreeId(String treeId) {
101        this.treeId = treeId;
102    }
103
104    public String getTreeLabel() {
105        return treeLabel;
106    }
107
108    public void setTreeLabel(String treeLabel) {
109        this.treeLabel = treeLabel;
110    }
111
112    public Integer getOrder() {
113        return order;
114    }
115
116    public void setEnabled(boolean enabled) {
117        this.enabled = enabled;
118    }
119
120    public boolean isEnabled() {
121        return enabled;
122    }
123
124    /**
125     * @since 5.6
126     */
127    @Override
128    public int compareTo(NavTreeDescriptor o) {
129        return getOrder().compareTo(o.getOrder());
130    }
131
132    /**
133     * Helper to register a simple action based on the given descriptor
134     *
135     * @since 6.0
136     */
137    protected Action getAction() {
138        Action a = new Action(ACTION_ID_PREFIX + getTreeId(),
139                new String[] { DirectoryTreeDescriptor.NAV_ACTION_CATEGORY });
140        a.setType("rest_document_link");
141        a.setLabel(getTreeLabel());
142        Map<String, String> props = new HashMap<String, String>();
143        props.put("ajaxSupport", "true");
144        if (isDirectoryTreeBased()) {
145            props.put("link", "/incl/single_directory_tree_explorer.xhtml");
146        } else {
147            props.put("link", getXhtmlview());
148        }
149        ActionPropertiesDescriptor pdesc = new ActionPropertiesDescriptor();
150        pdesc.setProperties(props);
151        a.setPropertiesDescriptor(pdesc);
152        Integer order = getOrder();
153        if (order != null) {
154            a.setOrder(order.intValue());
155        }
156        a.setEnabled(isEnabled());
157        a.setIcon(String.format("/img/%s.png", getTreeId()));
158        // need to set a non-empty list
159        a.setFilterIds(new ArrayList<String>());
160        return a;
161    }
162
163}