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.widgets;
029
030import org.opencms.ade.configuration.CmsElementView;
031import org.opencms.file.CmsObject;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsUUID;
034
035import java.util.ArrayList;
036import java.util.List;
037import java.util.Map;
038
039/**
040 * A widget to select an element view.<p>
041 *
042 * If the widget configuration contains the string 'selectparent', the widget will be used for selecting parent views of a view.
043 * This makes sense *only* when the widget is used in the element view content itself.
044 */
045public class CmsElementViewSelectWidget extends CmsSelectWidget {
046
047    /**
048     * Constructor.<p>
049     */
050    public CmsElementViewSelectWidget() {
051
052        super();
053    }
054
055    /**
056     * @see org.opencms.widgets.I_CmsWidget#newInstance()
057     */
058    @Override
059    public I_CmsWidget newInstance() {
060
061        return new CmsElementViewSelectWidget();
062    }
063
064    /**
065     * @see org.opencms.widgets.A_CmsSelectWidget#parseSelectOptions(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
066     */
067    @Override
068    protected List<CmsSelectWidgetOption> parseSelectOptions(
069        CmsObject cms,
070        I_CmsWidgetDialog widgetDialog,
071        I_CmsWidgetParameter param) {
072
073        List<CmsSelectWidgetOption> options = new ArrayList<CmsSelectWidgetOption>();
074        String myPath = getResourcePath(cms, widgetDialog);
075        Map<CmsUUID, CmsElementView> views = OpenCms.getADEManager().getElementViews(cms);
076
077        for (CmsElementView view : views.values()) {
078
079            if (shouldIgnore(view, views, myPath)) {
080                continue;
081            }
082            String value = "";
083            if (view.getResource() != null) {
084                value = cms.getSitePath(view.getResource());
085            } else if ((view.getId() != null) && !view.getId().isNullUUID()) {
086                // synthetic view
087                value = "view://" + view.getId();
088            }
089            options.add(new CmsSelectWidgetOption(value, false, view.getTitle(cms, widgetDialog.getLocale())));
090        }
091        if (isSelectParent()) {
092            //
093            options.add(new CmsSelectWidgetOption(CmsElementView.PARENT_NONE, true, "--"));
094        }
095        return options;
096    }
097
098    /**
099     * Returns true if the 'selectparent' option is enabled.<p>
100     *
101     * @return true if the 'selectparent' option is enabled
102     */
103    private boolean isSelectParent() {
104
105        return (getConfiguration() != null) && getConfiguration().contains("selectparent");
106    }
107
108    /**
109     * Check if the view should be ignored when constructing select options.<p>
110     *
111     * @param view the view to check
112     * @param views the map of all views
113     * @param myPath the path of the currently edited resource
114     *
115     * @return true if the view should be ignored
116     */
117    private boolean shouldIgnore(CmsElementView view, Map<CmsUUID, CmsElementView> views, String myPath) {
118
119        if (isSelectParent()) {
120            for (CmsElementView otherView : views.values()) {
121                CmsUUID parentViewId = otherView.getParentViewId();
122                if (parentViewId != null) {
123                    CmsElementView parentOfOther = views.get(parentViewId);
124                    if ((parentOfOther != null)
125                        && (parentOfOther.getResource() != null)
126                        && parentOfOther.getResource().getRootPath().equals(myPath)) {
127                        // can't have "grandparents"
128                        return true;
129                    }
130                }
131            }
132            boolean isStandardView = (view.getResource() != null) || view.getId().isNullUUID();
133            if ((view.getParentViewId() != null) || !isStandardView) {
134                // synthetic view, or already a sub-view of something else
135                return true;
136            }
137            if ((view.getResource() != null) && view.getResource().getRootPath().equals(myPath)) {
138                // can't make a view its own parent
139                return true;
140            }
141        }
142        return false;
143    }
144}