/**
* This file is part of Jahia, next-generation open source CMS:
* Jahia's next-generation, open source CMS stems from a widely acknowledged vision
* of enterprise application convergence - web, search, document, social and portal -
* unified by the simplicity of web content management.
*
* For more information, please visit http://www.jahia.com.
*
* Copyright (C) 2002-2011 Jahia Solutions Group SA. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* As a special exception to the terms and conditions of version 2.0 of
* the GPL (or any later version), you may redistribute this Program in connection
* with Free/Libre and Open Source Software ("FLOSS") applications as described
* in Jahia's FLOSS exception. You should have received a copy of the text
* describing the FLOSS exception, and it is also available here:
* http://www.jahia.com/license
*
* Commercial and Supported Versions of the program (dual licensing):
* alternatively, commercial and supported versions of the program may be used
* in accordance with the terms and conditions contained in a separate
* written agreement between you and Jahia Solutions Group SA.
*
* If you are unsure which license is appropriate for your use,
* please contact the sales department at sales@jahia.com.
*/
package org.jahia.ajax.gwt.client.widget.edit.sidepanel;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.dnd.DND;
import com.extjs.gxt.ui.client.dnd.TreeGridDropTarget;
import com.extjs.gxt.ui.client.event.DNDEvent;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGrid;
import com.extjs.gxt.ui.client.widget.treegrid.TreeGridCellRenderer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import org.jahia.ajax.gwt.client.core.BaseAsyncCallback;
import org.jahia.ajax.gwt.client.data.node.GWTJahiaNode;
import org.jahia.ajax.gwt.client.data.toolbar.GWTSidePanelTab;
import org.jahia.ajax.gwt.client.util.icons.ContentModelIconProvider;
import org.jahia.ajax.gwt.client.widget.Linker;
import org.jahia.ajax.gwt.client.widget.NodeColumnConfigList;
import org.jahia.ajax.gwt.client.widget.edit.EditLinker;
import org.jahia.ajax.gwt.client.widget.edit.EditModeDNDListener;
import org.jahia.ajax.gwt.client.widget.node.GWTJahiaNodeTreeFactory;
import java.util.ArrayList;
import java.util.List;
/**
* Repository browser tab.
* User: toto
* Date: Dec 21, 2009
* Time: 2:22:30 PM
*/
abstract class BrowseTabItem extends SidePanelTabItem {
protected List<String> folderTypes = new ArrayList<String>();
private List<String> paths = new ArrayList<String>();
protected transient LayoutContainer treeContainer;
protected transient TreeGrid<GWTJahiaNode> tree;
protected transient TreeGridDropTarget treeDropTarget;
protected transient String repositoryType;
private transient GWTJahiaNodeTreeFactory factory;
public TabItem create(GWTSidePanelTab config) {
super.create(config);
VBoxLayout l = new VBoxLayout();
l.setVBoxLayoutAlign(VBoxLayout.VBoxLayoutAlign.STRETCH);
tab.setLayout(l);
treeContainer = new LayoutContainer();
treeContainer.setBorders(false);
treeContainer.setScrollMode(Style.Scroll.AUTO);
treeContainer.setLayout(new FitLayout());
factory = new GWTJahiaNodeTreeFactory(paths);
factory.setNodeTypes(this.folderTypes);
NodeColumnConfigList columns = new NodeColumnConfigList(config.getTreeColumns());
columns.init();
columns.get(0).setRenderer(new TreeGridCellRenderer());
tree = factory.getTreeGrid(new ColumnModel(columns));
tree.setAutoExpandColumn(columns.getAutoExpand());
tree.getTreeView().setRowHeight(25);
tree.getTreeView().setForceFit(true);
tree.setHeight("100%");
tree.setIconProvider(ContentModelIconProvider.getInstance());
treeContainer.add(tree);
VBoxLayoutData treeVBoxData = new VBoxLayoutData();
treeVBoxData.setFlex(1);
tab.add(treeContainer, treeVBoxData);
treeDropTarget = new BrowseTreeGridDropTarget();
return tab;
}
@Override
public void initWithLinker(EditLinker linker) {
super.initWithLinker(linker);
treeDropTarget.addDNDListener(linker.getDndListener());
treeDropTarget.setAllowDropOnLeaf(true);
treeDropTarget.setAllowSelfAsSource(false);
treeDropTarget.setAutoExpand(true);
treeDropTarget.setFeedback(DND.Feedback.APPEND);
}
class BrowseTreeGridDropTarget extends TreeGridDropTarget {
public BrowseTreeGridDropTarget() {
super(BrowseTabItem.this.tree);
}
@Override
protected void onDragEnter(DNDEvent e) {
super.onDragEnter(e);
setStatus(e);
}
private void setStatus(DNDEvent e) {
if (EditModeDNDListener.SIMPLEMODULE_TYPE.equals(e.getStatus().getData(EditModeDNDListener.SOURCE_TYPE))) {
List<GWTJahiaNode> nodes = e.getStatus().getData(EditModeDNDListener.SOURCE_NODES);
if (acceptNode(nodes.get(0))) {
e.getStatus().setData(EditModeDNDListener.TARGET_TYPE, EditModeDNDListener.BROWSETREE_TYPE);
e.getStatus().setStatus(true);
}
else {
e.getStatus().setStatus(false);
}
} else {
e.getStatus().setStatus(false);
}
e.setCancelled(false);
}
@Override
protected void showFeedback(DNDEvent e) {
super.showFeedback(e);
setStatus(e);
if (activeItem != null) {
GWTJahiaNode activeNode = (GWTJahiaNode) activeItem.getModel();
e.getStatus().setData(EditModeDNDListener.TARGET_PATH, activeNode.get("path"));
} else {
e.getStatus().setData(EditModeDNDListener.TARGET_PATH, null);
}
}
public AsyncCallback<Object> getCallback() {
AsyncCallback<Object> callback = new BaseAsyncCallback<Object>() {
public void onSuccess(Object o) {
refresh(0);
}
};
return callback;
}
}
@Override public void refresh(int flag) {
if ((flag & Linker.REFRESH_FOLDERS) != 0) {
factory.getStore().removeAll();
factory.getLoader().load();
}
}
protected abstract boolean acceptNode(GWTJahiaNode node);
public List<String> getFolderTypes() {
return folderTypes;
}
public void setFolderTypes(List<String> folderTypes) {
this.folderTypes = folderTypes;
}
public List<String> getPaths() {
return paths;
}
public void setPaths(List<String> paths) {
this.paths = paths;
}
}