/**
* 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.allen_sauer.gwt.log.client.Log;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.data.*;
import com.extjs.gxt.ui.client.dnd.DND;
import com.extjs.gxt.ui.client.dnd.ListViewDragSource;
import com.extjs.gxt.ui.client.dnd.StatusProxy;
import com.extjs.gxt.ui.client.event.*;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.VBoxLayoutData;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Image;
import org.jahia.ajax.gwt.client.data.node.GWTJahiaNode;
import org.jahia.ajax.gwt.client.messages.Messages;
import org.jahia.ajax.gwt.client.service.content.JahiaContentManagementService;
import org.jahia.ajax.gwt.client.util.content.JCRClientUtils;
import org.jahia.ajax.gwt.client.widget.content.ThumbsListView;
import org.jahia.ajax.gwt.client.widget.edit.EditLinker;
import org.jahia.ajax.gwt.client.widget.edit.EditModeDNDListener;
import org.jahia.ajax.gwt.client.data.toolbar.GWTSidePanelTab;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Side panel tab item for browsing image resources in the repository.
* User: toto
* Date: Dec 21, 2009
* Time: 2:22:24 PM
*/
class FileImagesBrowseTabItem extends BrowseTabItem {
protected transient LayoutContainer contentContainer;
protected transient ListLoader<ListLoadResult<GWTJahiaNode>> listLoader;
protected transient ListStore<GWTJahiaNode> contentStore;
protected transient ImageDragSource dragSource;
public TabItem create(GWTSidePanelTab config) {
super.create(config);
contentContainer = new LayoutContainer();
contentContainer.setId("images-view");
contentContainer.setBorders(true);
contentContainer.setScrollMode(Style.Scroll.AUTOY);
// data proxy
RpcProxy<PagingLoadResult<GWTJahiaNode>> listProxy = new RpcProxy<PagingLoadResult<GWTJahiaNode>>() {
@Override
protected void load(Object gwtJahiaFolder, AsyncCallback<PagingLoadResult<GWTJahiaNode>> listAsyncCallback) {
if (gwtJahiaFolder != null) {
Log.debug("retrieving children of " + ((GWTJahiaNode) gwtJahiaFolder).getName());
try {
JahiaContentManagementService.App.getInstance()
.lsLoad((GWTJahiaNode) gwtJahiaFolder, JCRClientUtils.FILE_NODETYPES, null, null, Arrays.asList(GWTJahiaNode.ICON, GWTJahiaNode.PUBLICATION_INFO, GWTJahiaNode.THUMBNAILS, GWTJahiaNode.TAGS, "j:width", "j:height"), false, -1, -1, false, null, null, listAsyncCallback);
} catch (org.jahia.ajax.gwt.client.service.GWTJahiaServiceException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} else {
contentContainer.unmask();
}
}
};
listLoader = new BaseListLoader<ListLoadResult<GWTJahiaNode>>(listProxy);
listLoader.addLoadListener(new LoadListener() {
@Override
public void loaderLoad(LoadEvent le) {
if (!le.isCancelled()) {
contentContainer.unmask();
}
}
});
contentStore = new ListStore<GWTJahiaNode>(listLoader);
tree.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<GWTJahiaNode>() {
@Override
public void selectionChanged(SelectionChangedEvent<GWTJahiaNode> event) {
contentContainer.mask(Messages.get("label.loading","Loading..."), "x-mask-loading");
listLoader.load(event.getSelectedItem());
}
});
tree.setContextMenu(createContextMenu(config.getTreeContextMenu(), tree.getSelectionModel()));
final ThumbsListView listView = new ThumbsListView(true);
listView.setStyleAttribute("overflow-x", "hidden");
listView.setStore(contentStore);
contentStore.setSortField("display");
contentContainer.add(listView);
VBoxLayoutData contentVBoxData = new VBoxLayoutData();
contentVBoxData.setFlex(2);
tab.add(contentContainer, contentVBoxData);
listView.addListener(Events.DoubleClick, new Listener<ListViewEvent<GWTJahiaNode>>() {
public void handleEvent(ListViewEvent<GWTJahiaNode> be) {
Window w = new Window();
GWTJahiaNode node = listView.getSelectionModel().getSelectedItem();
final String text = "Preview of " + node.getDisplayName();
w.setHeading(text);
w.setScrollMode(Style.Scroll.AUTO);
w.setModal(true);
w.setClosable(true);
w.setMaximizable(true);
w.setSize(Math.max(Integer.parseInt((String)node.get("j:width")) + 60, 400), Math.max(Integer.parseInt((String)node.get("j:height")) + 80, 50));
w.setBlinkModal(true);
w.setPlain(true);
w.setToolTip(text);
w.setLayout(new CenterLayout());
w.add(new Image(listView.getSelectionModel().getSelectedItem().getUrl()));
w.show();
}
});
listView.setContextMenu(createContextMenu(config.getTableContextMenu(), listView.getSelectionModel()));
dragSource = new ImageDragSource(listView);
return tab;
}
@Override
public void initWithLinker(EditLinker linker) {
super.initWithLinker(linker);
dragSource.addDNDListener(linker.getDndListener());
}
public class ImageDragSource extends ListViewDragSource {
public ImageDragSource(ListView<GWTJahiaNode> listView) {
super(listView);
DragListener listener = new DragListener() {
public void dragEnd(DragEvent de) {
DNDEvent e = new DNDEvent(ImageDragSource.this, de.getEvent());
e.setData(data);
e.setDragEvent(de);
e.setComponent(component);
e.setStatus(statusProxy);
onDragEnd(e);
}
};
draggable.addDragListener(listener);
}
@Override
protected void onDragStart(DNDEvent e) {
e.getStatus().setData(EditModeDNDListener.SOURCE_TYPE, EditModeDNDListener.CONTENT_SOURCE_TYPE);
List<GWTJahiaNode> nodes = new ArrayList<GWTJahiaNode>(1);
nodes.add((GWTJahiaNode) listView.getSelectionModel().getSelectedItem());
e.setData(nodes);
List<GWTJahiaNode> list = new ArrayList<GWTJahiaNode>(1);
GWTJahiaNode selectedItem = (GWTJahiaNode) listView.getSelectionModel().getSelectedItem();
list.add(selectedItem.getReferencedNode()!=null?selectedItem.getReferencedNode():selectedItem);
e.getStatus().setData("size", list.size());
e.getStatus().setData(EditModeDNDListener.SOURCE_NODES, list);
e.setOperation(DND.Operation.COPY);
super.onDragStart(e);
}
@Override
protected void onDragCancelled(DNDEvent dndEvent) {
super.onDragCancelled(dndEvent);
onDragEnd(dndEvent);
}
protected void onDragEnd(DNDEvent e) {
StatusProxy sp = e.getStatus();
sp.setData(EditModeDNDListener.SOURCE_TYPE, null);
sp.setData(EditModeDNDListener.CONTENT_SOURCE_TYPE, null);
sp.setData(EditModeDNDListener.TARGET_TYPE, null);
sp.setData(EditModeDNDListener.TARGET_NODE, null);
sp.setData(EditModeDNDListener.TARGET_PATH, null);
sp.setData(EditModeDNDListener.SOURCE_NODES, null);
sp.setData(EditModeDNDListener.SOURCE_QUERY, null);
sp.setData(EditModeDNDListener.SOURCE_TEMPLATE, null);
sp.setData(EditModeDNDListener.OPERATION_CALLED, null);
e.setData(null);
}
}
@Override
protected boolean acceptNode(GWTJahiaNode node) {
return node.getInheritedNodeTypes().contains("nt:file");
}
}