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    @Override
045    public ContentProvider getContentProvider() {
046        return provider;
047    }
048
049    @Override
050    public void setContentProvider(ContentProvider provider) {
051        this.provider = provider;
052    }
053
054    @Override
055    public void setInput(Object input) {
056        if (input == null) {
057            root = null;
058        } else {
059            root = new TreeItemImpl(provider, input);
060        }
061    }
062
063    @Override
064    public TreeItem getRoot() {
065        return root;
066    }
067
068    @Override
069    public TreeItem findAndReveal(String path) {
070        return findAndReveal(new Path(path));
071    }
072
073    @Override
074    public TreeItem find(String path) {
075        return find(new Path(path));
076    }
077
078    @Override
079    public TreeItem findAndReveal(Path path) {
080        if (root == null) {
081            return null;
082        }
083        return root.findAndReveal(path);
084    }
085
086    @Override
087    public TreeItem find(Path path) {
088        if (root == null) {
089            return null;
090        }
091        Path rootPath = root.getPath();
092        int p = path.matchingFirstSegments(rootPath);
093        if (p == rootPath.segmentCount()) {
094            return root.find(path.removeFirstSegments(p));
095        }
096        return null;
097    }
098
099    @Override
100    public Object getInput() {
101        return root == null ? null : root.getObject();
102    }
103
104    public boolean hasInput() {
105        return root != null;
106    }
107
108}