001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *
018 * $Id$
019 */
020
021package org.nuxeo.ecm.webapp.tree.nav;
022
023import static org.jboss.seam.ScopeType.CONVERSATION;
024import static org.jboss.seam.annotations.Install.FRAMEWORK;
025
026import java.io.Serializable;
027import java.util.Map;
028
029import org.apache.commons.lang.StringUtils;
030import org.jboss.seam.annotations.In;
031import org.jboss.seam.annotations.Install;
032import org.jboss.seam.annotations.Name;
033import org.jboss.seam.annotations.Observer;
034import org.jboss.seam.annotations.Scope;
035import org.jboss.seam.annotations.intercept.BypassInterceptors;
036import org.jboss.seam.core.Events;
037import org.nuxeo.ecm.core.api.impl.DocumentModelImpl;
038import org.nuxeo.ecm.platform.ui.web.api.WebActions;
039import org.nuxeo.ecm.webapp.action.WebActionsBean;
040import org.nuxeo.ecm.webapp.directory.DirectoryTreeDescriptor;
041import org.nuxeo.ecm.webapp.directory.DirectoryTreeManager;
042import org.nuxeo.ecm.webapp.helpers.EventNames;
043
044/**
045 * Seam component to handle MultiTree navigation
046 * <p>
047 * Moved from module nuxeo-platform-virtual-navigation-web, added in 5.6.
048 *
049 * @since 6.0
050 */
051@Name("multiNavTreeManager")
052@Scope(CONVERSATION)
053@Install(precedence = FRAMEWORK)
054public class MultiNavTreeManager implements Serializable {
055
056    private static final long serialVersionUID = 1L;
057
058    public static final String STD_NAV_TREE = "CONTENT_TREE";
059
060    protected String thePath = "";
061
062    @In(required = false, create = true)
063    protected WebActionsBean webActions;
064
065    @In(required = false, create = true)
066    protected DirectoryTreeManager directoryTreeManager;
067
068    @In(create = true)
069    protected Map<String, String> messages;
070
071    public void setSelectedNavigationTree(String selectedNavigationTree) {
072        webActions.setCurrentTabId(DirectoryTreeDescriptor.NAV_ACTION_CATEGORY, selectedNavigationTree);
073    }
074
075    public String getSelectedNavigationTree() {
076        String id = webActions.getCurrentTabId(DirectoryTreeDescriptor.NAV_ACTION_CATEGORY);
077        if (id != null) {
078            if (id.startsWith(DirectoryTreeDescriptor.ACTION_ID_PREFIX)) {
079                return id.substring(DirectoryTreeDescriptor.ACTION_ID_PREFIX.length());
080            }
081            if (id.startsWith(DirectoryTreeDescriptor.ACTION_ID_PREFIX)) {
082                return id.substring(DirectoryTreeDescriptor.ACTION_ID_PREFIX.length());
083            }
084            return id;
085        }
086        return null;
087    }
088
089    @Observer(value = { WebActions.CURRENT_TAB_CHANGED_EVENT + "_" + DirectoryTreeDescriptor.NAV_ACTION_CATEGORY,
090            WebActions.CURRENT_TAB_CHANGED_EVENT + "_" + DirectoryTreeDescriptor.DIR_ACTION_CATEGORY }, create = true)
091    public void onCurrentTreeChange(String category, String tabId) {
092        // raise this event in order to reset the documents lists from
093        // 'conversationDocumentsListsManager'
094        Events.instance().raiseEvent(EventNames.FOLDERISHDOCUMENT_SELECTION_CHANGED, new DocumentModelImpl("Folder"));
095        if (tabId != null) {
096            if (tabId.startsWith(DirectoryTreeDescriptor.ACTION_ID_PREFIX)) {
097                directoryTreeManager.setSelectedTreeName(tabId.substring(DirectoryTreeDescriptor.ACTION_ID_PREFIX.length()));
098            }
099            if (tabId.startsWith(DirectoryTreeDescriptor.DIR_ACTION_CATEGORY)) {
100                directoryTreeManager.setSelectedTreeName(tabId.substring(DirectoryTreeDescriptor.DIR_ACTION_CATEGORY.length()));
101            }
102        }
103    }
104
105    @Observer(value = { "PATH_PROCESSED" }, create = false)
106    @BypassInterceptors
107    public void setThePath(String myPath) {
108        thePath = myPath;
109    }
110
111    public String getVirtualNavPath() {
112        String[] partOfPath = thePath.split("/");
113        String finalPath = "";
114        for (String aPart : partOfPath) {
115            if (!StringUtils.isBlank(aPart)) {
116                finalPath = finalPath + " > " + messages.get(aPart);
117            }
118        }
119        return finalPath;
120    }
121
122}