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 GmbH & Co. KG, 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.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.i18n.CmsMessages;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect;
035import org.opencms.ui.dialogs.permissions.CmsPrincipalSelectDialog;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.workplace.CmsWorkplace;
038import org.opencms.xml.content.I_CmsXmlContentHandler.DisplayType;
039import org.opencms.xml.types.A_CmsXmlContentValue;
040
041import java.util.List;
042import java.util.Locale;
043
044/**
045 * Provides an OpenCms Principal selection widget, for use on a widget dialog.<p>
046 *
047 * @since 6.5.6
048 */
049public class CmsPrincipalWidget extends A_CmsWidget implements I_CmsADEWidget {
050
051    /** Configuration parameter to set the flags of the principals to display, optional. */
052    public static final String CONFIGURATION_FLAGS = "flags";
053
054    /** The the flags used in the popup window. */
055    private Integer m_flags;
056
057    /**
058     * Creates a new principals selection widget.<p>
059     */
060    public CmsPrincipalWidget() {
061
062        // empty constructor is required for class registration
063        this("");
064    }
065
066    /**
067     * Creates a new principals selection widget with the parameters to configure the popup window behaviour.<p>
068     *
069     * @param flags the group flags to restrict the group selection, can be <code>null</code>
070     */
071    public CmsPrincipalWidget(Integer flags) {
072
073        m_flags = flags;
074    }
075
076    /**
077     * Creates a new principals selection widget with the given configuration.<p>
078     *
079     * @param configuration the configuration to use
080     */
081    public CmsPrincipalWidget(String configuration) {
082
083        super(configuration);
084    }
085
086    /**
087     * Returns the needed java script for the search button.<p>
088     *
089     * @param id the id of the widget to generate the search button for
090     * @param form the id of the form where to which the widget belongs
091     *
092     * @return javascript code
093     */
094    public String getButtonJs(String id, String form) {
095
096        StringBuffer buttonJs = new StringBuffer(8);
097        buttonJs.append("javascript:openPrincipalWin('");
098        buttonJs.append(OpenCms.getSystemInfo().getOpenCmsContext());
099        buttonJs.append("/system/workplace/commons/principal_selection.jsp");
100        buttonJs.append("','" + form + "',  '");
101        buttonJs.append(id);
102        buttonJs.append("', document, '");
103        if (m_flags != null) {
104            buttonJs.append(m_flags);
105        } else {
106            buttonJs.append("null");
107        }
108        buttonJs.append("'");
109        buttonJs.append(");");
110        return buttonJs.toString();
111    }
112
113    /**
114     * @see org.opencms.widgets.A_CmsWidget#getConfiguration()
115     */
116    @Override
117    public String getConfiguration() {
118
119        StringBuffer result = new StringBuffer(8);
120
121        // append flags to configuration
122        if (m_flags != null) {
123            if (result.length() > 0) {
124                result.append("|");
125            }
126            result.append(CONFIGURATION_FLAGS);
127            result.append("=");
128            result.append(m_flags);
129        }
130        return result.toString();
131    }
132
133    /**
134     * @see org.opencms.widgets.I_CmsADEWidget#getConfiguration(org.opencms.file.CmsObject, org.opencms.xml.types.A_CmsXmlContentValue, org.opencms.i18n.CmsMessages, org.opencms.file.CmsResource, java.util.Locale)
135     */
136    public String getConfiguration(
137        CmsObject cms,
138        A_CmsXmlContentValue schemaType,
139        CmsMessages messages,
140        CmsResource resource,
141        Locale contentLocale) {
142
143        return CmsPrincipalSelectDialog.PARAM_TYPE + "=" + CmsPrincipalSelect.WidgetType.principalwidget.name();
144    }
145
146    /**
147     * @see org.opencms.widgets.I_CmsADEWidget#getCssResourceLinks(org.opencms.file.CmsObject)
148     */
149    public List<String> getCssResourceLinks(CmsObject cms) {
150
151        return null;
152    }
153
154    /**
155     * @see org.opencms.widgets.I_CmsADEWidget#getDefaultDisplayType()
156     */
157    public DisplayType getDefaultDisplayType() {
158
159        return DisplayType.singleline;
160    }
161
162    /**
163     * @see org.opencms.widgets.I_CmsWidget#getDialogIncludes(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog)
164     */
165    @Override
166    public String getDialogIncludes(CmsObject cms, I_CmsWidgetDialog widgetDialog) {
167
168        StringBuffer result = new StringBuffer(16);
169        result.append(getJSIncludeFile(CmsWorkplace.getSkinUri() + "components/widgets/principalselector.js"));
170        return result.toString();
171    }
172
173    /**
174     * @see org.opencms.widgets.I_CmsWidget#getDialogWidget(org.opencms.file.CmsObject, org.opencms.widgets.I_CmsWidgetDialog, org.opencms.widgets.I_CmsWidgetParameter)
175     */
176    public String getDialogWidget(CmsObject cms, I_CmsWidgetDialog widgetDialog, I_CmsWidgetParameter param) {
177
178        String id = param.getId();
179        StringBuffer result = new StringBuffer(128);
180
181        result.append("<td class=\"xmlTd\">");
182        result.append(
183            "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" class=\"maxwidth\"><tr><td style=\"width: 100%;\">");
184        result.append("<input style=\"width: 99%;\" class=\"xmlInput");
185        if (param.hasError()) {
186            result.append(" xmlInputError");
187        }
188        result.append("\" value=\"");
189        result.append(param.getStringValue(cms));
190        result.append("\" name=\"");
191        result.append(id);
192        result.append("\" id=\"");
193        result.append(id);
194        result.append("\"></td>");
195        result.append(widgetDialog.dialogHorizontalSpacer(10));
196        result.append(
197            "<td><table class=\"editorbuttonbackground\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>");
198
199        result.append(
200            widgetDialog.button(
201                getButtonJs(id, "EDITOR"),
202                null,
203                "principal",
204                org.opencms.workplace.Messages.GUI_DIALOG_BUTTON_SEARCH_0,
205                widgetDialog.getButtonStyle()));
206        result.append("</tr></table>");
207        result.append("</td></tr></table>");
208
209        result.append("</td>");
210
211        return result.toString();
212    }
213
214    /**
215     * Returns the flags, or <code>null</code> if all.<p>
216     *
217     * @return the flags, or <code>null</code> if all
218     */
219    public Integer getFlags() {
220
221        return m_flags;
222    }
223
224    /**
225     * @see org.opencms.widgets.I_CmsADEWidget#getInitCall()
226     */
227    public String getInitCall() {
228
229        return null;
230    }
231
232    /**
233     * @see org.opencms.widgets.I_CmsADEWidget#getJavaScriptResourceLinks(org.opencms.file.CmsObject)
234     */
235    public List<String> getJavaScriptResourceLinks(CmsObject cms) {
236
237        return null;
238    }
239
240    /**
241     * @see org.opencms.widgets.I_CmsADEWidget#getWidgetName()
242     */
243    public String getWidgetName() {
244
245        return CmsPrincipalWidget.class.getName();
246    }
247
248    /**
249     * @see org.opencms.widgets.I_CmsADEWidget#isInternal()
250     */
251    public boolean isInternal() {
252
253        return true;
254    }
255
256    /**
257     * @see org.opencms.widgets.I_CmsWidget#newInstance()
258     */
259    public I_CmsWidget newInstance() {
260
261        return new CmsPrincipalWidget(getConfiguration());
262    }
263
264    /**
265     * @see org.opencms.widgets.A_CmsWidget#setConfiguration(java.lang.String)
266     */
267    @Override
268    public void setConfiguration(String configuration) {
269
270        m_flags = null;
271        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(configuration)) {
272            int flagsIndex = configuration.indexOf(CONFIGURATION_FLAGS);
273            if (flagsIndex != -1) {
274                // flags are given
275                String flags = configuration.substring(CONFIGURATION_FLAGS.length() + 1);
276                if (flags.indexOf('|') != -1) {
277                    // cut eventual following configuration values
278                    flags = flags.substring(0, flags.indexOf('|'));
279                }
280                try {
281                    m_flags = Integer.valueOf(flags);
282                } catch (Throwable t) {
283                    // invalid flags
284                }
285            }
286        }
287        super.setConfiguration(configuration);
288    }
289}