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 net.sf.json.JSONArray;
025import net.sf.json.JSONObject;
026
027import org.nuxeo.ecm.webengine.forms.FormData;
028import org.nuxeo.ecm.webengine.model.WebContext;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public abstract class JSonTree {
034
035    protected TreeModelImpl tree;
036
037    public TreeModel getTree() {
038        return tree;
039    }
040
041    protected abstract Object getInput(WebContext ctx);
042
043    protected abstract ContentProvider getProvider(WebContext ctx);
044
045    protected abstract JSonTreeSerializer getSerializer(WebContext ctx);
046
047    public String updateSelection(WebContext ctx) {
048        return updateSelection(ctx, getProvider(ctx), getSerializer(ctx));
049    }
050
051    public String enter(WebContext ctx, String path) {
052        return enter(ctx, path, getSerializer(ctx));
053    }
054
055    /**
056     * root=ID - enter node ID toggle=ID - toggle expanded state for node ID
057     */
058    public synchronized String updateSelection(WebContext ctx, ContentProvider provider, JSonTreeSerializer serializer) {
059        try {
060            tree.setContentProvider(provider);
061            if (!tree.hasInput()) {
062                tree.setInput(getInput(ctx));
063            }
064            FormData form = ctx.getForm();
065            String selection = form.getString("root");
066            if (selection == null) {
067                selection = form.getString("toggle");
068                if (selection != null) {
069                    toggle(selection);
070                }
071            } else {
072                String result = null;
073                if ("source".equals(selection)) {
074                    result = enter(ctx, tree.root.getPath().toString(), serializer);
075                } else {
076                    result = enter(ctx, selection, serializer);
077                }
078                if (result != null) {
079                    return result;
080                } else {
081                    ctx.getLog().warn("TreeItem: " + selection + " not found");
082                }
083            }
084        } finally {
085            tree.setContentProvider(null);
086        }
087        return null;
088    }
089
090    public String getTreeAsJSONArray(WebContext ctx) {
091        JSonTreeSerializer serializer = getSerializer(ctx);
092        JSONObject o = serializer.toJSON(tree.root);
093        JSONArray array = new JSONArray();
094        array.add(o);
095        return array.toString();
096    }
097
098    protected String enter(WebContext ctx, String path, JSonTreeSerializer serializer) {
099        TreeItem item = tree.findAndReveal(path);
100        if (item != null) {
101            item.expand();
102            JSONArray result = new JSONArray();
103            if (item.isContainer()) {
104                result = serializer.toJSON(item.getChildren());
105            }
106            return result.toString();
107        } else {
108            return null;
109        }
110    }
111
112    protected void toggle(String path) {
113        TreeItem item = tree.findAndReveal(path);
114        if (item.isExpanded()) {
115            item.collapse();
116        } else {
117            item.expand();
118        }
119    }
120
121}