001/*
002 * (C) Copyright 2010 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 * Contributors:
014 *     Nuxeo - initial API and implementation
015 */
016
017package org.nuxeo.ecm.platform.ui.web.component.tree;
018
019import static org.jboss.seam.ScopeType.CONVERSATION;
020import static org.jboss.seam.annotations.Install.FRAMEWORK;
021
022import java.io.Serializable;
023import java.util.List;
024
025import javax.faces.component.UIComponent;
026import javax.faces.component.UIInput;
027import javax.faces.event.ActionEvent;
028
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Install;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.jboss.seam.annotations.web.RequestParameter;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentRef;
037import org.nuxeo.ecm.core.api.PathRef;
038import org.nuxeo.ecm.platform.ui.web.component.list.UIEditableList;
039import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils;
040
041/**
042 * Action to handle tree widget.
043 *
044 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
045 * @since 5.4
046 */
047@Name("treeWidgetActions")
048@Scope(CONVERSATION)
049@Install(precedence = FRAMEWORK)
050public class TreeWidgetActions implements Serializable {
051
052    private static final long serialVersionUID = 1L;
053
054    @In(create = true, required = false)
055    protected transient CoreSession documentManager;
056
057    @RequestParameter
058    protected String selectionListId;
059
060    @RequestParameter
061    protected String selectionInputId;
062
063    @RequestParameter
064    protected String selectedPath;
065
066    @SuppressWarnings("unchecked")
067    public void addSelectionToList(ActionEvent event) {
068        UIComponent component = event.getComponent();
069        if (component == null) {
070            return;
071        }
072        UIComponent base = ComponentUtils.getBase(component);
073        UIEditableList list = ComponentUtils.getComponent(base, selectionListId, UIEditableList.class);
074
075        if (list != null) {
076            List<String> values = (List<String>) list.getEditableModel().getWrappedData();
077            // add selected value to the list
078            if (!values.contains(selectedPath)) {
079                list.addValue(selectedPath);
080            }
081        }
082    }
083
084    public void setUIInputValue(ActionEvent event) {
085        UIComponent component = event.getComponent();
086        if (component == null) {
087            return;
088        }
089        UIComponent base = ComponentUtils.getBase(component);
090        UIInput uiInput = ComponentUtils.getComponent(base, selectionInputId, UIInput.class);
091
092        if (uiInput != null) {
093            uiInput.setSubmittedValue(selectedPath);
094        }
095    }
096
097    /**
098     * Returns the {@code DocumentModel} referenced by the given path if exists, {@code null} otherwise.
099     */
100    public DocumentModel getDocumentFromPath(String path) {
101        // handle root document differently as user may not have browse rights
102        // on it
103        if ("/".equals(path)) {
104            return documentManager.getRootDocument();
105        }
106        DocumentRef ref = new PathRef(path);
107        return documentManager.exists(ref) ? documentManager.getDocument(new PathRef(path)) : null;
108    }
109
110}