/*
* This library is part of OpenCms -
* the Open Source Content Management System
*
* Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.opencms.ade.containerpage.client;
import org.opencms.ade.containerpage.client.CmsContainerpageEvent.EventType;
import org.opencms.ade.containerpage.client.ui.CmsConfirmRemoveDialog;
import org.opencms.ade.containerpage.client.ui.CmsContainerPageContainer;
import org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel;
import org.opencms.ade.containerpage.client.ui.CmsElementOptionBar;
import org.opencms.ade.containerpage.client.ui.CmsGroupContainerElementPanel;
import org.opencms.ade.containerpage.client.ui.CmsRemovedElementDeletionDialog;
import org.opencms.ade.containerpage.client.ui.CmsSmallElementsHandler;
import org.opencms.ade.containerpage.client.ui.I_CmsDropContainer;
import org.opencms.ade.containerpage.client.ui.css.I_CmsLayoutBundle;
import org.opencms.ade.containerpage.client.ui.groupeditor.A_CmsGroupEditor;
import org.opencms.ade.containerpage.client.ui.groupeditor.CmsGroupContainerEditor;
import org.opencms.ade.containerpage.client.ui.groupeditor.CmsInheritanceContainerEditor;
import org.opencms.ade.containerpage.shared.CmsCntPageData;
import org.opencms.ade.containerpage.shared.CmsContainer;
import org.opencms.ade.containerpage.shared.CmsContainerElement;
import org.opencms.ade.containerpage.shared.CmsContainerElementData;
import org.opencms.ade.containerpage.shared.CmsContainerPageGalleryData;
import org.opencms.ade.containerpage.shared.CmsContainerPageRpcContext;
import org.opencms.ade.containerpage.shared.CmsCreateElementData;
import org.opencms.ade.containerpage.shared.CmsElementViewInfo;
import org.opencms.ade.containerpage.shared.CmsGroupContainer;
import org.opencms.ade.containerpage.shared.CmsGroupContainerSaveResult;
import org.opencms.ade.containerpage.shared.CmsInheritanceContainer;
import org.opencms.ade.containerpage.shared.CmsRemovedElementStatus;
import org.opencms.ade.containerpage.shared.rpc.I_CmsContainerpageService;
import org.opencms.ade.containerpage.shared.rpc.I_CmsContainerpageServiceAsync;
import org.opencms.ade.contenteditor.client.CmsContentEditor;
import org.opencms.gwt.client.CmsCoreProvider;
import org.opencms.gwt.client.dnd.CmsCompositeDNDController;
import org.opencms.gwt.client.dnd.CmsDNDHandler;
import org.opencms.gwt.client.dnd.I_CmsDNDController;
import org.opencms.gwt.client.rpc.CmsRpcAction;
import org.opencms.gwt.client.rpc.CmsRpcPrefetcher;
import org.opencms.gwt.client.ui.CmsErrorDialog;
import org.opencms.gwt.client.ui.CmsNotification;
import org.opencms.gwt.client.ui.CmsNotification.Type;
import org.opencms.gwt.client.util.CmsDebugLog;
import org.opencms.gwt.client.util.CmsDomUtil;
import org.opencms.gwt.client.util.I_CmsSimpleCallback;
import org.opencms.gwt.shared.CmsContextMenuEntryBean;
import org.opencms.gwt.shared.CmsCoreData.AdeContext;
import org.opencms.gwt.shared.CmsGwtConstants;
import org.opencms.gwt.shared.CmsListInfoBean;
import org.opencms.gwt.shared.CmsTemplateContextInfo;
import org.opencms.gwt.shared.rpc.I_CmsCoreServiceAsync;
import org.opencms.util.CmsDefaultSet;
import org.opencms.util.CmsStringUtil;
import org.opencms.util.CmsUUID;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
/**
* Data provider for the container-page editor. All data concerning the container-page is requested and maintained by this provider.
*
* @since 8.0.0
*/
public final class CmsContainerpageController {
/**
* Enum which is used to control how elements are removed from the page.
*/
public enum ElementRemoveMode {
/** Reference checks are performed and the user is asked for confirmation whether they really want to remove the element before the page is saved. */
confirmRemove,
/** Reference checks are only performed after the page or group has been saved. */
saveAndCheckReferences,
/** Element is just removed, no checks are performed. */
silent;
}
/**
* Visitor interface used to process the current container content on the page.
*/
public static interface I_PageContentVisitor {
/**
* This method is called before a container is processed.
*
* If the method returns false, the container will be skipped.
*
* @param name the container name
* @param container the container data object
*
* @return true if the container should be processed, true if it should be skipped
*/
boolean beginContainer(String name, CmsContainer container);
/**
* This method is called after all elements of a container have been processed.
*/
void endContainer();
/**
* This method is called for each element of a container.
*
* @param element the container element
*/
void handleElement(CmsContainerPageElementPanel element);
}
/**
* This visitor implementation checks whether there are other elements in the current page
* which correspond to the same VFS resource as a given container element.
*/
public static class ReferenceCheckVisitor implements I_PageContentVisitor {
/** The element for which we want to check whether there are other references to the same resource. */
private CmsContainerPageElementPanel m_elementPanel;
/** True if other references have been found. */
private boolean m_hasReferences;
/** The structure id of the element. */
private String m_structureId;
/**
* Creates a new instance.
*
* @param elementPanel the element for which we want to check if there are other references
*/
public ReferenceCheckVisitor(CmsContainerPageElementPanel elementPanel) {
m_elementPanel = elementPanel;
m_structureId = getServerId(elementPanel.getId());
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#beginContainer(java.lang.String, org.opencms.ade.containerpage.shared.CmsContainer)
*/
public boolean beginContainer(String name, CmsContainer container) {
return !container.isDetailView();
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#endContainer()
*/
public void endContainer() {
// do nothing
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#handleElement(org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel)
*/
public void handleElement(CmsContainerPageElementPanel element) {
if (element != m_elementPanel) {
String id = getServerId(element.getId());
if (m_structureId.equals(id)) {
m_hasReferences = true;
}
}
}
/**
* Checks if other references have been found.
*
* @return true if other references have been found
*/
public boolean hasReferences() {
return m_hasReferences;
}
}
/**
* Visitor implementation which is used to gather the container contents for saving.
*/
protected class PageStateVisitor implements I_PageContentVisitor {
/** The current container name. */
protected String m_containerName;
/** The contaienr which is currently being processed. */
protected CmsContainer m_currentContainer;
/** The list of collected containers. */
protected List m_resultContainers = new ArrayList();
/** The list of elements of the currently processed container which have already been processed. */
List m_currentElements;
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#beginContainer(java.lang.String, org.opencms.ade.containerpage.shared.CmsContainer)
*/
public boolean beginContainer(String name, CmsContainer container) {
m_currentContainer = container;
m_containerName = name;
m_currentElements = new ArrayList();
return true;
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#endContainer()
*/
public void endContainer() {
CmsContainer container = new CmsContainer(
m_containerName,
m_currentContainer.getType(),
null,
m_currentContainer.getWidth(),
m_currentContainer.getMaxElements(),
m_currentContainer.isDetailView(),
true,
m_currentElements,
m_currentContainer.getParentContainerName(),
m_currentContainer.getParentInstanceId());
container.setDeatilOnly(m_currentContainer.isDetailOnly());
m_resultContainers.add(container);
}
/**
* Gets the list of collected containers.
*
* @return the list of containers
*/
public List getContainers() {
return m_resultContainers;
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#handleElement(org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel)
*/
public void handleElement(CmsContainerPageElementPanel elementWidget) {
CmsContainerElement element = new CmsContainerElement();
element.setClientId(elementWidget.getId());
element.setResourceType(elementWidget.getNewType());
element.setCreateNew(elementWidget.isCreateNew());
element.setModelGroup(elementWidget.isModelGroup());
element.setSitePath(elementWidget.getSitePath());
element.setNewEditorDisabled(elementWidget.isNewEditorDisabled());
m_currentElements.add(element);
}
}
/**
* Visitor implementation which is used to gather the container contents for saving.
*/
protected class SaveDataVisitor implements I_PageContentVisitor {
/** The current container name. */
protected String m_containerName;
/** The contaienr which is currently being processed. */
protected CmsContainer m_currentContainer;
/** The list of collected containers. */
protected List m_resultContainers = new ArrayList();
/** The list of elements of the currently processed container which have already been processed. */
List m_currentElements;
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#beginContainer(java.lang.String, org.opencms.ade.containerpage.shared.CmsContainer)
*/
public boolean beginContainer(String name, CmsContainer container) {
if (container.isDetailView() || ((getData().getDetailId() != null) && !container.isDetailOnly())) {
m_currentContainer = null;
return false;
} else {
m_currentContainer = container;
m_containerName = name;
m_currentElements = new ArrayList();
return true;
}
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#endContainer()
*/
public void endContainer() {
m_resultContainers.add(
new CmsContainer(
m_containerName,
m_currentContainer.getType(),
null,
m_currentContainer.getWidth(),
m_currentContainer.getMaxElements(),
m_currentContainer.isDetailView(),
true,
m_currentElements,
m_currentContainer.getParentContainerName(),
m_currentContainer.getParentInstanceId()));
}
/**
* Gets the list of collected containers.
*
* @return the list of containers
*/
public List getContainers() {
return m_resultContainers;
}
/**
* @see org.opencms.ade.containerpage.client.CmsContainerpageController.I_PageContentVisitor#handleElement(org.opencms.ade.containerpage.client.ui.CmsContainerPageElementPanel)
*/
public void handleElement(CmsContainerPageElementPanel elementWidget) {
CmsContainerElement element = new CmsContainerElement();
element.setClientId(elementWidget.getId());
element.setResourceType(elementWidget.getNewType());
element.setCreateNew(elementWidget.isCreateNew());
element.setModelGroup(elementWidget.isModelGroup());
element.setSitePath(elementWidget.getSitePath());
element.setNewEditorDisabled(elementWidget.isNewEditorDisabled());
m_currentElements.add(element);
}
}
/**
* A type which indicates the locking status of the currently edited container page.
*/
enum LockStatus {
/** Locking the resource failed. */
failed,
/** The resource could be successfully locked. */
locked,
/** Locking the resource has not been tried. */
unknown
}
/**
* A RPC action implementation used to request the data for container-page elements.
*/
private class MultiElementAction extends CmsRpcAction