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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.ui.tree.document;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentModelList;
029import org.nuxeo.ecm.core.api.repository.Repository;
030import org.nuxeo.ecm.webengine.ui.tree.ContentProvider;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035// FIXME: properly handle exceptions
036public class DocumentContentProvider implements ContentProvider {
037
038    private static final Log log = LogFactory.getLog(DocumentContentProvider.class);
039
040    private static final long serialVersionUID = 1L;
041
042    protected CoreSession session;
043
044    public DocumentContentProvider(CoreSession session) {
045        this.session = session;
046    }
047
048    public void setSession(CoreSession session) {
049        this.session = session;
050    }
051
052    public CoreSession getSession() {
053        return session;
054    }
055
056    public Object[] getElements(Object input) {
057        if (input instanceof Repository) {
058            return new DocumentModel[] { session.getRootDocument() };
059        } else { // may be a document
060            return getChildren(input);
061        }
062    }
063
064    public Object[] getChildren(Object obj) {
065        if (obj instanceof DocumentModel) {
066            DocumentModelList list = session.getChildren(((DocumentModel) obj).getRef());
067            return list.toArray(new DocumentModel[list.size()]);
068        }
069        return null;
070    }
071
072    public boolean isContainer(Object obj) {
073        if (obj instanceof DocumentModel) {
074            return ((DocumentModel) obj).isFolder();
075        }
076        return false;
077    }
078
079    public String getLabel(Object obj) {
080        if (obj instanceof DocumentModel) {
081            return ((DocumentModel) obj).getTitle();
082        }
083        return null;
084    }
085
086    public String[] getFacets(Object object) {
087        return null;
088    }
089
090    public String getName(Object obj) {
091        if (obj instanceof DocumentModel) {
092            return ((DocumentModel) obj).getName();
093        }
094        return null;
095    }
096
097}