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     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getTitle(java.util.Locale)
072     */
073    public String getTitle(Locale locale) {
074
075        return OpenCms.getWorkplaceManager().getMessages(locale).key(getTitleKey());
076    }
077
078    /**
079     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.ui.I_CmsDialogContext)
080     */
081    public CmsMenuItemVisibilityMode getVisibility(I_CmsDialogContext context) {
082
083        return getVisibility(context.getCms(), context.getResources());
084    }
085
086    /**
087     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#isActive(org.opencms.ui.I_CmsDialogContext)
088     */
089    public boolean isActive(I_CmsDialogContext context) {
090
091        return getVisibility(context).isActive();
092    }
093
094    /**
095     * Returns the workplace message bundle key of the action dialog title.<p>
096     *
097     * @return the dialog title message bundle key
098     */
099    protected String getDialogTitleKey() {
100
101        return getTitleKey();
102    }
103
104    /**
105     * Returns the workplace message bundle key of the action title.<p>
106     *
107     * @return the title message bundle key
108     */
109    protected abstract String getTitleKey();
110
111    /**
112     * Returns if there are any blocking locks within the context resources.<p>
113     * Will open the blocking locks dialog if required.<p>
114     *
115     * @param context the dialog context
116     *
117     * @return <code>true</code> in case of blocking locks
118     */
119    protected boolean hasBlockingLocks(final I_CmsDialogContext context) {
120
121        return hasBlockingLocks(context, true);
122    }
123
124    /**
125     * Returns if there are any blocking locks within the context resources.<p>
126     * Will open the blocking locks dialog if required.<p>
127     *
128     * @param context the dialog context
129     * @param showDialog flag, indicating if the dialog should be displayed, hinting to the locked resources.
130     *
131     * @return <code>true</code> in case of blocking locks
132     */
133    protected boolean hasBlockingLocks(final I_CmsDialogContext context, boolean showDialog) {
134
135        CmsObject cms = context.getCms();
136        List<CmsResource> resources = context.getResources();
137        List<CmsResource> blocked = Lists.newArrayList();
138        for (CmsResource resource : resources) {
139            try {
140                blocked.addAll(cms.getBlockingLockedResources(resource));
141            } catch (CmsException e) {
142                LOG.error(e.getLocalizedMessage(), e);
143            }
144        }
145        if (blocked.isEmpty()) {
146            return false;
147        } else if (showDialog) {
148
149            CmsLockedResourcesList dialog = new CmsLockedResourcesList(
150                cms,
151                blocked,
152                CmsVaadinUtils.getMessageText(Messages.GUI_CANT_PERFORM_OPERATION_BECAUSE_OF_LOCKED_RESOURCES_0),
153                new Runnable() {
154
155                    public void run() {
156
157                        List<CmsUUID> noStructureIds = Collections.emptyList();
158                        context.finish(noStructureIds);
159                    }
160
161                },
162                null);
163            context.start(
164                CmsVaadinUtils.getMessageText(org.opencms.workplace.explorer.Messages.GUI_EXPLORER_CONTEXT_LOCKS_0),
165                dialog);
166        }
167            return true;
168        }
169
170    /**
171     * Opens the given dialog in a new overlay window.<p>
172     *
173     * @param dialog the dialog
174     * @param context the dialog context
175     */
176    protected void openDialog(CmsBasicDialog dialog, I_CmsDialogContext context) {
177
178        context.start(getDialogTitle(), dialog);
179    }
180
181    /**
182     * Opens the given dialog in a new overlay window.<p>
183     *
184     * @param dialog the dialog
185     * @param context the dialog context
186     * @param dialogWidth The dialog width
187     */
188    protected void openDialog(
189        CmsBasicDialog dialog,
190        I_CmsDialogContext context,
191        CmsBasicDialog.DialogWidth dialogWidth) {
192
193        context.start(getDialogTitle(), dialog, dialogWidth);
194    }
195}