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