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.actions;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.I_CmsDialogContext;
038import org.opencms.ui.Messages;
039import org.opencms.ui.components.CmsBasicDialog;
040import org.opencms.ui.components.CmsLockedResourcesList;
041import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
042import org.opencms.util.CmsUUID;
043
044import java.util.Collections;
045import java.util.List;
046import java.util.Locale;
047
048import org.apache.commons.logging.Log;
049
050import com.google.common.collect.Lists;
051
052/**
053 * Abstract workplace actions class providing helper methods.<p>
054 */
055public abstract class A_CmsWorkplaceAction implements I_CmsWorkplaceAction {
056
057    /** Log instance for this class. */
058    private static final Log LOG = CmsLog.getLog(A_CmsWorkplaceAction.class);
059
060    /**
061     * Gets the title to use for the dialog.<p>
062     *
063     * @return the title to use for the dialog
064     */
065    public String getDialogTitle() {
066
067        return OpenCms.getWorkplaceManager().getMessages(A_CmsUI.get().getLocale()).key(getDialogTitleKey());
068    }
069
070    /**
071     * Returns the workplace message bundle key of the action dialog title.<p>
072     *
073     * @return the dialog title message bundle key
074     */
075    protected String getDialogTitleKey() {
076
077        return getTitleKey();
078    }
079
080    /**
081     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getTitle(java.util.Locale)
082     */
083    public String getTitle(Locale locale) {
084
085        return OpenCms.getWorkplaceManager().getMessages(locale).key(getTitleKey());
086    }
087
088    /**
089     * Returns the workplace message bundle key of the action title.<p>
090     *
091     * @return the title message bundle key
092     */
093    protected abstract String getTitleKey();
094
095    /**
096     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.ui.I_CmsDialogContext)
097     */
098    public CmsMenuItemVisibilityMode getVisibility(I_CmsDialogContext context) {
099
100        return getVisibility(context.getCms(), context.getResources());
101    }
102
103    /**
104     * Returns if there are any blocking locks within the context resources.<p>
105     * Will open the blocking locks dialog if required.<p>
106     *
107     * @param context the dialog context
108     *
109     * @return <code>true</code> in case of blocking locks
110     */
111    protected boolean hasBlockingLocks(final I_CmsDialogContext context) {
112
113        CmsObject cms = context.getCms();
114        List<CmsResource> resources = context.getResources();
115        List<CmsResource> blocked = Lists.newArrayList();
116        for (CmsResource resource : resources) {
117            try {
118                blocked.addAll(cms.getBlockingLockedResources(resource));
119            } catch (CmsException e) {
120                LOG.error(e.getLocalizedMessage(), e);
121            }
122        }
123        if (blocked.isEmpty()) {
124            return false;
125        } else {
126
127            CmsLockedResourcesList dialog = new CmsLockedResourcesList(
128                cms,
129                blocked,
130                CmsVaadinUtils.getMessageText(Messages.GUI_CANT_PERFORM_OPERATION_BECAUSE_OF_LOCKED_RESOURCES_0),
131                new Runnable() {
132
133                    public void run() {
134
135                        List<CmsUUID> noStructureIds = Collections.emptyList();
136                        context.finish(noStructureIds);
137                    }
138
139                },
140                null);
141            context.start(
142                CmsVaadinUtils.getMessageText(org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_LOCKS_0),
143                dialog);
144            return true;
145        }
146    }
147
148    /**
149     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#isActive(org.opencms.ui.I_CmsDialogContext)
150     */
151    public boolean isActive(I_CmsDialogContext context) {
152
153        return getVisibility(context).isActive();
154    }
155
156    /**
157     * Opens the given dialog in a new overlay window.<p>
158     *
159     * @param dialog the dialog
160     * @param context the dialog context
161     */
162    protected void openDialog(CmsBasicDialog dialog, I_CmsDialogContext context) {
163
164        context.start(getDialogTitle(), dialog);
165    }
166
167    /**
168     * Opens the given dialog in a new overlay window.<p>
169     *
170     * @param dialog the dialog
171     * @param context the dialog context
172     * @param dialogWidth The dialog width
173     */
174    protected void openDialog(
175        CmsBasicDialog dialog,
176        I_CmsDialogContext context,
177        CmsBasicDialog.DialogWidth dialogWidth) {
178
179        context.start(getDialogTitle(), dialog, dialogWidth);
180    }
181}