001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.ui.contextmenu; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsResource; 032 033import java.util.List; 034 035/** 036 * Abstract superclass for menu item visibility checks.<p> 037 * 038 * This class automatically handles the case where multiple resources are passed to the getVisibilityMethod. 039 * You just need to implement the getSingleVisibility method in subclasses. 040 */ 041public abstract class A_CmsSimpleVisibilityCheck implements I_CmsHasMenuItemVisibility { 042 043 /** Flag to indicate that the check should not match multiple resources. */ 044 protected boolean m_singleResourceOnly; 045 046 /** 047 * Computes visibility of the menu item for a single resource.<p> 048 * 049 * @param cms the CMS context to use 050 * @param resource the resource to check 051 * 052 * @return the visibility for the given resource 053 */ 054 public abstract CmsMenuItemVisibilityMode getSingleVisibility(CmsObject cms, CmsResource resource); 055 056 /** 057 * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List) 058 */ 059 public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) { 060 061 if (resources.size() <= 1) { 062 // Single-selection case where we just delegate to getSingleVisibility 063 // this applies also to main menu items 064 return getSingleVisibility(cms, resources.size() == 1 ? resources.get(0) : null); 065 } else { 066 if (m_singleResourceOnly) { 067 return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 068 } 069 CmsMenuItemVisibilityMode currentVisibility = null; 070 for (CmsResource resource : resources) { 071 CmsMenuItemVisibilityMode visibilityForResource = getSingleVisibility(cms, resource); 072 if ((currentVisibility == null) 073 || (getPriority(visibilityForResource) > getPriority(currentVisibility))) { 074 currentVisibility = visibilityForResource; 075 } 076 } 077 if (currentVisibility == null) { 078 currentVisibility = CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE; 079 } 080 if (currentVisibility.isInActive()) { 081 // In the multi-selection case, different resources may cause the menu item to be inactive for different reasons, 082 // which would make it hard to give a user-readable message. So we set the status to invisible. 083 currentVisibility = CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE; 084 } 085 return currentVisibility; 086 } 087 } 088 089 /** 090 * Gets the priority of a menu item visibility mode.<p> 091 * 092 * Higher priority modes override the ones with lower priorities.<p> 093 * 094 * 095 * @param mode the mode 096 * @return the priority for the mode 097 */ 098 private int getPriority(CmsMenuItemVisibilityMode mode) { 099 100 if (mode.isPrioritized()) { 101 return 4; 102 } 103 if (mode.isInActive()) { 104 return 0; 105 } 106 if (mode.isActive()) { 107 return 1; 108 } 109 // invisible 110 return 2; 111 } 112}