All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencms.ade.galleries.client.ui.CmsResultsTab Maven / Gradle / Ivy

Go to download

OpenCms is an enterprise-ready, easy to use website content management system based on Java and XML technology. Offering a complete set of features, OpenCms helps content managers worldwide to create and maintain beautiful websites fast and efficiently.

There is a newer version: 18.0
Show newest version
/*
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) Alkacon Software GmbH (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.galleries.client.ui;

import org.opencms.ade.galleries.client.CmsResultContextMenuHandler;
import org.opencms.ade.galleries.client.CmsResultsTabHandler;
import org.opencms.ade.galleries.client.I_CmsGalleryHandler;
import org.opencms.ade.galleries.client.Messages;
import org.opencms.ade.galleries.client.ui.css.I_CmsLayoutBundle;
import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
import org.opencms.ade.galleries.shared.CmsResultItemBean;
import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.SortParams;
import org.opencms.ade.upload.client.ui.CmsDialogUploadButtonHandler;
import org.opencms.gwt.client.dnd.CmsDNDHandler;
import org.opencms.gwt.client.ui.CmsList;
import org.opencms.gwt.client.ui.I_CmsListItem;
import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuButton;
import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuHandler;
import org.opencms.gwt.client.ui.input.upload.CmsUploadButton;
import org.opencms.gwt.client.ui.input.upload.I_CmsUploadButtonHandler;
import org.opencms.gwt.client.util.CmsDebugLog;
import org.opencms.gwt.client.util.CmsDomUtil;
import org.opencms.util.CmsUUID;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Lists;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.ScrollEvent;
import com.google.gwt.event.dom.client.ScrollHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Provides the widget for the results tab.

* * It displays the selected search parameter, the sort order and * the search results for the current search. * * @since 8.0. */ public class CmsResultsTab extends A_CmsListTab { /** * Click-handler for the delete button.

*/ public class DeleteHandler implements ClickHandler { /** The resource path of the selected item. */ protected String m_resourcePath; /** * Constructor.

* * @param resourcePath the item resource path */ protected DeleteHandler(String resourcePath) { m_resourcePath = resourcePath; } /** * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) */ public void onClick(ClickEvent event) { getTabHandler().deleteResource(m_resourcePath); } } /** * Scroll handler which executes an action when the user has scrolled to the bottom.

* * @since 8.0.0 */ protected class CmsAsynchronousScrollToBottomHandler implements ScrollHandler { /** * If the lower edge of the content being scrolled is at most this many pixels below the lower * edge of the scrolling viewport, the action is triggered. */ public static final int DEFAULT_SCROLL_THRESHOLD = 200; /** * Constructs a new scroll handler with a custom scroll threshold. * * The scroll threshold is the distance from the bottom edge of the scrolled content * such that when the distance from the bottom edge of the scroll viewport to the bottom * edge of the scrolled content becomes lower than the distance, the scroll action is triggered. * */ public CmsAsynchronousScrollToBottomHandler() { // noop } /** * @see com.google.gwt.event.dom.client.ScrollHandler#onScroll(com.google.gwt.event.dom.client.ScrollEvent) */ public void onScroll(ScrollEvent event) { if (!m_hasMoreResults || getTabHandler().isLoading()) { return; } final ScrollPanel scrollPanel = (ScrollPanel)event.getSource(); final int scrollPos = scrollPanel.getVerticalScrollPosition(); Widget child = scrollPanel.getWidget(); int childHeight = child.getOffsetHeight(); int ownHeight = scrollPanel.getOffsetHeight(); boolean isBottom = (scrollPos + ownHeight) >= (childHeight - DEFAULT_SCROLL_THRESHOLD); if (isBottom) { getTabHandler().onScrollToBottom(); setScrollPosition(scrollPos); } } } /** * Special click handler to use with preview button.

*/ protected class PreviewHandler implements ClickHandler { /** The resource path of the selected item. */ private String m_resourcePath; /** The resource type of the selected item. */ private String m_resourceType; /** * Constructor.

* * @param resourcePath the item resource path * @param resourceType the item resource type */ public PreviewHandler(String resourcePath, String resourceType) { m_resourcePath = resourcePath; m_resourceType = resourceType; } /** * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) */ public void onClick(ClickEvent event) { getTabHandler().openPreview(m_resourcePath, m_resourceType); } } /** * Special click handler to use with select button.

*/ protected class SelectHandler implements ClickHandler, DoubleClickHandler { /** The id of the selected item. */ private String m_resourcePath; /** The resource type of the selected item. */ private String m_resourceType; /** The structure id. */ private CmsUUID m_structureId; /** The resource title. */ private String m_title; /** * Constructor.

* * @param resourcePath the item resource path * @param structureId the structure id * @param title the resource title * @param resourceType the item resource type */ public SelectHandler(String resourcePath, CmsUUID structureId, String title, String resourceType) { m_resourcePath = resourcePath; m_structureId = structureId; m_resourceType = resourceType; m_title = title; } /** * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent) */ public void onClick(ClickEvent event) { getTabHandler().selectResource(m_resourcePath, m_structureId, m_title, m_resourceType); } /** * @see com.google.gwt.event.dom.client.DoubleClickHandler#onDoubleClick(com.google.gwt.event.dom.client.DoubleClickEvent) */ public void onDoubleClick(DoubleClickEvent event) { getTabHandler().selectResource(m_resourcePath, m_structureId, m_title, m_resourceType); } } /** Text metrics key. */ private static final String TM_RESULT_TAB = "ResultTab"; /** The handler for scrolling to the top of the scroll panel. */ protected CmsResultsBackwardsScrollHandler m_backwardScrollHandler = new CmsResultsBackwardsScrollHandler(this); /** Stores the information if more results in the search object are available. */ protected boolean m_hasMoreResults; /** The result list item which corresponds to a preset value in the editor. */ protected CmsResultListItem m_preset; /** The gallery handler. */ I_CmsGalleryHandler m_galleryHandler; /** The context menu handler. */ private CmsContextMenuHandler m_contextMenuHandler; /** The optional dnd manager. */ private CmsDNDHandler m_dndHandler; /** A HTML widget for the message if nor search params were selected. */ private HTML m_noParamsMessage; /** The panel showing the search parameters. */ private FlowPanel m_params; /** The reference to the handler of this tab. */ private CmsResultsTabHandler m_tabHandler; /** Set of resource types currently displayed in the result list. */ private Set m_types; /** The upload button. */ private CmsUploadButton m_uploadButton; /** * The constructor.

* * @param tabHandler the tab handler * @param dndHandler the dnd manager * @param galleryHandler the gallery handler **/ public CmsResultsTab(CmsResultsTabHandler tabHandler, CmsDNDHandler dndHandler, I_CmsGalleryHandler galleryHandler) { super(GalleryTabId.cms_tab_results); m_galleryHandler = galleryHandler; m_contextMenuHandler = new CmsResultContextMenuHandler(tabHandler); m_types = new HashSet(); m_hasMoreResults = false; m_dndHandler = dndHandler; m_tabHandler = tabHandler; m_scrollList.truncate(TM_RESULT_TAB, CmsGalleryDialog.DIALOG_WIDTH); m_params = new FlowPanel(); m_params.setStyleName(I_CmsLayoutBundle.INSTANCE.galleryDialogCss().tabParamsPanel()); m_tab.insert(m_params, 0); getList().addScrollHandler(new CmsAsynchronousScrollToBottomHandler()); getList().addScrollHandler(m_backwardScrollHandler); init(); } /** * Clears all search parameters.

*/ @Override public void clearParams() { CmsDebugLog.getInstance().printLine("Unallowed call to clear params in result tab."); } /** * Fill the content of the results tab.

* * @param searchObj the current search object containing search results * @param paramPanels list of search parameter panels to show */ public void fillContent(final CmsGallerySearchBean searchObj, List paramPanels) { removeNoParamMessage(); // in case there is a single type selected and the current sort order is not by type, // hide the type ascending and type descending sort order options SortParams currentSorting = SortParams.valueOf(searchObj.getSortOrder()); if ((searchObj.getTypes().size() == 1) && !((currentSorting == SortParams.type_asc) || (currentSorting == SortParams.type_desc))) { m_sortSelectBox.setItems(getSortList(false)); } else { m_sortSelectBox.setItems(getSortList(true)); } m_sortSelectBox.selectValue(searchObj.getSortOrder()); displayResultCount(getResultsDisplayed(searchObj), searchObj.getResultCount()); m_hasMoreResults = searchObj.hasMore(); if (searchObj.getPage() == 1) { m_preset = null; getList().scrollToTop(); // getList().getElement().getStyle().setDisplay(Display.NONE); clearList(); showParams(paramPanels); m_backwardScrollHandler.updateSearchBean(searchObj); getList().getElement().getStyle().clearDisplay(); scrollToPreset(); } else { showParams(paramPanels); addContent(searchObj); } showUpload(searchObj); } /** * Returns the drag and drop handler.

* * @return the drag and drop handler */ public CmsDNDHandler getDNDHandler() { return m_dndHandler; } /** * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getParamPanels(org.opencms.ade.galleries.shared.CmsGallerySearchBean) */ @Override public List getParamPanels(CmsGallerySearchBean searchObj) { // not available for this tab return Collections.emptyList(); } /** * Returns the delete handler.

* * @param resourcePath the resource path of the resource * * @return the delete handler */ public DeleteHandler makeDeleteHandler(String resourcePath) { return new DeleteHandler(resourcePath); } /** * Removes the no params message.

*/ public void removeNoParamMessage() { if (m_noParamsMessage != null) { m_tab.remove(m_noParamsMessage); } } /** * Shows the message if no search params were selected.

*/ public void showNoParamsMessage() { m_scrollList.clear(); if (m_noParamsMessage == null) { StringBuffer buf = new StringBuffer(); buf.append("

"); buf.append(""); buf.append(""); buf.append("
"); buf.append(Messages.get().key(Messages.GUI_RESULT_NO_PARAMS_0)); buf.append("
"); buf.append("
"); m_noParamsMessage = new HTML(buf.toString()); } m_tab.add(m_noParamsMessage); } /** * Updates the height (with border) of the result list panel according to the search parameter panels shown.

*/ public void updateListSize() { int tabHeight = m_tab.getElement().getClientHeight() - 13; // sanity check on tab height tabHeight = tabHeight > 0 ? tabHeight : 421; int paramsHeight = m_params.isVisible() ? m_params.getOffsetHeight() + CmsDomUtil.getCurrentStyleInt(m_params.getElement(), CmsDomUtil.Style.marginBottom) : 21; int optionsHeight = m_options.getOffsetHeight() + CmsDomUtil.getCurrentStyleInt(m_options.getElement(), CmsDomUtil.Style.marginBottom); // 3 is some offset, because of the list border int newListSize = tabHeight - paramsHeight - optionsHeight - 1; // another sanity check, don't set any negative height if (newListSize > 0) { m_list.getElement().getStyle().setHeight(newListSize, Unit.PX); } } /** * Appends the list items for the search results from a search bean.

* * @param searchBean a search bean containing results */ protected void addContent(CmsGallerySearchBean searchBean) { if (searchBean.getResults() != null) { addContentItems(searchBean.getResults(), false); } } /** * Adds list items for a list of search results.

* * @param list the list of search results * * @param front if true, list items will be added to the front of the list, else at the back */ protected void addContentItems(List list, boolean front) { if (front) { list = Lists.reverse(list); } for (CmsResultItemBean resultItem : list) { addSingleResult(resultItem, front); } if (m_types.size() == 1) { getList().addStyleName(I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().tilingList()); } else { getList().removeStyleName(I_CmsLayoutBundle.INSTANCE.galleryResultItemCss().tilingList()); } } /** * Adds a list item for a single search result.

* * @param resultItem the search result * @param front if true, adds the list item to the front of the list, else at the back */ protected void addSingleResult(CmsResultItemBean resultItem, boolean front) { m_types.add(resultItem.getType()); boolean hasPreview = m_tabHandler.hasPreview(resultItem.getType()); CmsDNDHandler dndHandler = m_dndHandler; if (!m_galleryHandler.filterDnd(resultItem)) { dndHandler = null; } CmsResultListItem listItem = new CmsResultListItem(resultItem, hasPreview, dndHandler); if (resultItem.isPreset()) { m_preset = listItem; } if (hasPreview) { listItem.addPreviewClickHandler(new PreviewHandler(resultItem.getPath(), resultItem.getType())); } CmsUUID structureId = new CmsUUID(resultItem.getClientId()); listItem.getListItemWidget().addButton(new CmsContextMenuButton(structureId, m_contextMenuHandler)); if (m_tabHandler.hasSelectResource()) { SelectHandler selectHandler = new SelectHandler( resultItem.getPath(), structureId, resultItem.getTitle(), resultItem.getType()); listItem.addSelectClickHandler(selectHandler); // this affects both tiled and non-tiled result lists. listItem.addDoubleClickHandler(selectHandler); } m_galleryHandler.processResultItem(listItem); if (front) { addWidgetToFrontOfList(listItem); } else { addWidgetToList(listItem); } } /** * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#clearList() */ @Override protected void clearList() { super.clearList(); m_types.clear(); } /** * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#getSortList() */ @Override protected LinkedHashMap getSortList() { return getSortList(true); } /** * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#getTabHandler() */ @Override protected CmsResultsTabHandler getTabHandler() { return m_tabHandler; } /** * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#hasQuickFilter() */ @Override protected boolean hasQuickFilter() { // quick filter not available for this tab return false; } /** * Scrolls to the result which corresponds to a preset value in the editor.

*/ protected void scrollToPreset() { final ScrollPanel scrollPanel = getList(); if (m_preset != null) { Widget child = scrollPanel.getWidget(); if (child instanceof CmsList) { @SuppressWarnings("unchecked") CmsList list = (CmsList)child; if (list.getWidgetCount() > 0) { final Widget first = (Widget)list.getItem(0); Timer timer = new Timer() { @Override public void run() { int firstTop = first.getElement().getAbsoluteTop(); int presetTop = m_preset.getElement().getAbsoluteTop(); final int offset = presetTop - firstTop; if (offset >= 0) { scrollPanel.setVerticalScrollPosition(offset); } else { // something is seriously wrong with the positioning if this case occurs scrollPanel.scrollToBottom(); } } }; timer.schedule(10); } } } } /** * Helper for setting the scroll position of the scroll panel.

* * @param pos the scroll position */ protected void setScrollPosition(final int pos) { getList().setVerticalScrollPosition(pos); Scheduler.get().scheduleDeferred(new ScheduledCommand() { /** * @see com.google.gwt.core.client.Scheduler.ScheduledCommand#execute() */ public void execute() { if (getList().getVerticalScrollPosition() != pos) { getList().setVerticalScrollPosition(pos); } } }); } /** * Displays the result count.

* * @param displayed the displayed result items * @param total the total of result items */ private void displayResultCount(int displayed, int total) { String message = Messages.get().key( Messages.GUI_LABEL_NUM_RESULTS_2, new Integer(displayed), new Integer(total)); m_infoLabel.setText(message); } /** * Returns the count of the currently displayed results.

* * @param searchObj the search bean * * @return the count of the currently displayed results */ private int getResultsDisplayed(CmsGallerySearchBean searchObj) { int resultsDisplayed = searchObj.getMatchesPerPage() * searchObj.getLastPage(); return (resultsDisplayed > searchObj.getResultCount()) ? searchObj.getResultCount() : resultsDisplayed; } /** * Returns the list of properties to sort the results according to.

* * @param includeType true to include sort according to type * * @return the sort list */ private LinkedHashMap getSortList(boolean includeType) { LinkedHashMap list = new LinkedHashMap(); list.put(SortParams.title_asc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_TITLE_ASC_0)); list.put(SortParams.title_desc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_TITLE_DECS_0)); list.put( SortParams.dateLastModified_asc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_DATELASTMODIFIED_ASC_0)); list.put( SortParams.dateLastModified_desc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_DATELASTMODIFIED_DESC_0)); list.put(SortParams.path_asc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_PATH_ASC_0)); list.put(SortParams.path_desc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_PATH_DESC_0)); if (includeType) { list.put(SortParams.type_asc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_TYPE_ASC_0)); list.put(SortParams.type_desc.name(), Messages.get().key(Messages.GUI_SORT_LABEL_TYPE_DESC_0)); } return list; } /** * Displays the selected search parameters in the result tab.

* * @param paramPanels the list of search parameter panels to show * */ private void showParams(List paramPanels) { m_params.clear(); if ((paramPanels == null) || (paramPanels.size() == 0)) { m_params.setVisible(false); updateListSize(); return; } m_params.setVisible(true); for (CmsSearchParamPanel panel : paramPanels) { m_params.add(panel); } updateListSize(); } /** * Shows the upload button if appropriate.

* * @param searchObj the current search object */ private void showUpload(CmsGallerySearchBean searchObj) { Set targets = new HashSet(); if (searchObj.getGalleries() != null) { targets.addAll(searchObj.getGalleries()); } if (searchObj.getFolders() != null) { targets.addAll(searchObj.getFolders()); } if (m_uploadButton == null) { m_uploadButton = createUploadButtonForTarget("", false); m_uploadButton.addStyleName(I_CmsLayoutBundle.INSTANCE.galleryDialogCss().resultTabUpload()); m_tab.insert(m_uploadButton, 0); } String uploadTarget = null; if (targets.size() == 1) { uploadTarget = targets.iterator().next(); I_CmsUploadButtonHandler handler = m_uploadButton.getButtonHandler(); if (handler instanceof CmsDialogUploadButtonHandler) { ((CmsDialogUploadButtonHandler)handler).setTargetFolder(uploadTarget); } m_uploadButton.enable(); m_uploadButton.setTitle(Messages.get().key(Messages.GUI_GALLERY_UPLOAD_TITLE_1, uploadTarget)); } else { m_uploadButton.disable(Messages.get().key(Messages.GUI_GALLERY_UPLOAD_TARGET_UNSPECIFIC_0)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy