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 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.webengine.ui.tree;
021
022import org.nuxeo.common.utils.Path;
023
024/**
025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
026 */
027public class TreeModelImpl implements TreeModel {
028
029    private static final long serialVersionUID = 1L;
030
031    protected ContentProvider provider;
032
033    protected TreeItem root;
034
035    public TreeModelImpl() {
036    }
037
038    public TreeModelImpl(ContentProvider provider) {
039        this.provider = provider;
040    }
041
042    public ContentProvider getContentProvider() {
043        return provider;
044    }
045
046    public void setContentProvider(ContentProvider provider) {
047        this.provider = provider;
048    }
049
050    public void setInput(Object input) {
051        if (input == null) {
052            root = null;
053        } else {
054            root = new TreeItemImpl(provider, input);
055        }
056    }
057
058    public TreeItem getRoot() {
059        return root;
060    }
061
062    public TreeItem findAndReveal(String path) {
063        return findAndReveal(new Path(path));
064    }
065
066    public TreeItem find(String path) {
067        return find(new Path(path));
068    }
069
070    public TreeItem findAndReveal(Path path) {
071        if (root == null) {
072            return null;
073        }
074        return root.findAndReveal(path);
075    }
076
077    public TreeItem find(Path path) {
078        if (root == null) {
079            return null;
080        }
081        Path rootPath = root.getPath();
082        int p = path.matchingFirstSegments(rootPath);
083        if (p == rootPath.segmentCount()) {
084            return root.find(path.removeFirstSegments(p));
085        }
086        return null;
087    }
088
089    public Object getInput() {
090        return root == null ? null : root.getObject();
091    }
092
093    public boolean hasInput() {
094        return root != null;
095    }
096
097}