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