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.gwt.client.ui.externallink;
029
030import org.opencms.file.CmsResource;
031import org.opencms.gwt.client.CmsCoreProvider;
032import org.opencms.gwt.client.Messages;
033import org.opencms.gwt.client.rpc.CmsRpcAction;
034import org.opencms.gwt.client.ui.CmsPopup;
035import org.opencms.gwt.client.ui.CmsPushButton;
036import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
037import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
038import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler;
039import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
040import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
041import org.opencms.gwt.client.ui.input.CmsLabel;
042import org.opencms.gwt.client.ui.input.CmsTextBox;
043import org.opencms.gwt.client.ui.input.form.CmsFieldsetFormFieldPanel;
044import org.opencms.gwt.client.ui.input.form.CmsFormDialog;
045import org.opencms.gwt.shared.CmsExternalLinkInfoBean;
046import org.opencms.gwt.shared.CmsListInfoBean;
047import org.opencms.util.CmsStringUtil;
048import org.opencms.util.CmsUUID;
049
050import com.google.gwt.dom.client.Style;
051import com.google.gwt.event.dom.client.ClickEvent;
052import com.google.gwt.event.dom.client.ClickHandler;
053import com.google.gwt.event.logical.shared.ValueChangeEvent;
054import com.google.gwt.event.logical.shared.ValueChangeHandler;
055import com.google.gwt.user.client.ui.FlowPanel;
056import com.google.gwt.user.client.ui.Widget;
057
058/**
059 * Dialog to create and edit external link resources.<p>
060 */
061public final class CmsEditExternalLinkDialog extends CmsPopup implements ValueChangeHandler<String> {
062
063    /** The link gallery resource type name. */
064    public static final String LINK_GALLERY_RESOURCE_TYPE_NAME = "linkgallery";
065
066    /** The pointer resource type name. */
067    public static final String POINTER_RESOURCE_TYPE_NAME = "pointer";
068
069    /** The text metrics key. */
070    private static final String METRICS_KEY = "CREATE_NEW_GALLERY_DIALOG";
071
072    /** The context menu handler. */
073    I_CmsContextMenuHandler m_contextMenuHandler;
074
075    /** The link info bean. */
076    CmsExternalLinkInfoBean m_linkInfo;
077
078    /** The parent folder path. */
079    String m_parentFolderPath;
080
081    /** The pointer resource structure id. */
082    CmsUUID m_structureId;
083
084    /** The dialog content panel. */
085    private CmsFieldsetFormFieldPanel m_dialogContent;
086
087    /** The file name input. */
088    private CmsTextBox m_fileName;
089
090    /** The create new flag. */
091    private boolean m_isCreateNew;
092
093    /** The folder name input. */
094    private CmsTextBox m_linkContent;
095
096    /** The title input. */
097    private CmsTextBox m_linkTitle;
098
099    /** The OK button. */
100    private CmsPushButton m_okButton;
101
102    /** The previous link. */
103    private String m_previousLink;
104
105    /** The previous link title. */
106    private String m_previousTitle;
107
108    /**
109     * Constructor. Use to create new link resources.<p>
110     *
111     * @param typeInfo the 'pointer' type info
112     * @param parentFolderPath the parent folder path
113     */
114    private CmsEditExternalLinkDialog(CmsListInfoBean typeInfo, String parentFolderPath) {
115
116        this(Messages.get().key(Messages.GUI_CREATE_NEW_LINK_DIALOG_TITLE_0));
117        m_isCreateNew = true;
118        m_parentFolderPath = parentFolderPath;
119        CmsExternalLinkInfoBean linkInfo = new CmsExternalLinkInfoBean();
120        linkInfo.setTitle(typeInfo.getTitle());
121        linkInfo.setSubTitle(typeInfo.getSubTitle());
122        linkInfo.setResourceType(POINTER_RESOURCE_TYPE_NAME);
123        linkInfo.setBigIconClasses(typeInfo.getBigIconClasses());
124        initContent(linkInfo);
125    }
126
127    /**
128     * Constructor.<p>
129     *
130     * @param structureId the structure id of the resource to edit
131     */
132    private CmsEditExternalLinkDialog(CmsUUID structureId) {
133
134        this(Messages.get().key(Messages.GUI_EDIT_LINK_DIALOG_TITLE_0));
135        m_structureId = structureId;
136    }
137
138    /**
139     * Constructor.<p>
140     *
141     * @param title the dialog title
142     */
143    private CmsEditExternalLinkDialog(String title) {
144
145        super(title, CmsFormDialog.STANDARD_DIALOG_WIDTH);
146    }
147
148    /**
149     * Loads the link info and shows the edit dialog.<p>
150     *
151     * @param structureId the structure id
152     *
153     * @return the dialog object
154     */
155    public static CmsEditExternalLinkDialog loadAndShowDialog(final CmsUUID structureId) {
156
157        final CmsEditExternalLinkDialog dialog = new CmsEditExternalLinkDialog(structureId);
158        CmsRpcAction<CmsExternalLinkInfoBean> action = new CmsRpcAction<CmsExternalLinkInfoBean>() {
159
160            @Override
161            public void execute() {
162
163                CmsCoreProvider.getVfsService().loadLinkInfo(structureId, this);
164            }
165
166            @Override
167            protected void onResponse(CmsExternalLinkInfoBean result) {
168
169                dialog.initContent(result);
170            }
171        };
172        action.execute();
173        dialog.center();
174        return dialog;
175    }
176
177    /**
178     * Shows the create new link dialog.<p>
179     *
180     * @param typeInfo the 'pointer' type info
181     * @param parentFolderPath the parent folder site path
182     *
183     * @return the dialog object
184     */
185    public static CmsEditExternalLinkDialog showNewLinkDialog(CmsListInfoBean typeInfo, String parentFolderPath) {
186
187        CmsEditExternalLinkDialog dialog = new CmsEditExternalLinkDialog(typeInfo, parentFolderPath);
188        dialog.center();
189        return dialog;
190    }
191
192    /**
193     * Validates the form input.<p>
194     *
195     * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
196     */
197    public void onValueChange(ValueChangeEvent<String> event) {
198
199        String message = null;
200        boolean enableOk = true;
201        if (m_isCreateNew) {
202            if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_fileName.getFormValueAsString())) {
203                enableOk = false;
204                message = Messages.get().key(Messages.GUI_EDIT_LINK_NO_FILE_NAME_0);
205            } else if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_linkContent.getFormValueAsString())) {
206                enableOk = false;
207                message = Messages.get().key(Messages.GUI_EDIT_LINK_NO_LINK_0);
208            }
209        } else {
210            if ((m_linkContent.getFormValueAsString().equals(m_previousLink)
211                && (m_linkTitle.getFormValueAsString().equals(m_previousTitle)))) {
212                enableOk = false;
213                message = Messages.get().key(Messages.GUI_EDIT_LINK_NO_CHANGES_0);
214            } else if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_linkContent.getFormValueAsString())) {
215                enableOk = false;
216                message = Messages.get().key(Messages.GUI_EDIT_LINK_NO_LINK_0);
217            }
218        }
219        setOkEnabled(enableOk, message);
220    }
221
222    /**
223     * Sets the context menu handler.<p>
224     *
225     * @param contextMenuHandler the context menu handler to set
226     */
227    public void setContextMenuHandler(I_CmsContextMenuHandler contextMenuHandler) {
228
229        m_contextMenuHandler = contextMenuHandler;
230    }
231
232    /**
233     * Initializes the dialog content.<p>
234     *
235     * @param linkInfo the link info bean
236     */
237    protected void initContent(CmsExternalLinkInfoBean linkInfo) {
238
239        CmsPushButton closeButton = new CmsPushButton();
240        closeButton.setText(Messages.get().key(Messages.GUI_CANCEL_0));
241        closeButton.setUseMinWidth(true);
242        closeButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
243        closeButton.addClickHandler(new ClickHandler() {
244
245            /**
246             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
247             */
248            public void onClick(ClickEvent event) {
249
250                hide();
251            }
252        });
253        addButton(closeButton);
254
255        m_okButton = new CmsPushButton();
256        m_okButton.setText(Messages.get().key(Messages.GUI_OK_0));
257        m_okButton.setUseMinWidth(true);
258        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
259        m_okButton.addClickHandler(new ClickHandler() {
260
261            /**
262             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
263             */
264            public void onClick(ClickEvent event) {
265
266                onOk();
267            }
268        });
269        addButton(m_okButton);
270
271        m_linkInfo = linkInfo;
272        m_previousLink = m_linkInfo.getLink() != null ? m_linkInfo.getLink() : "";
273        m_previousTitle = m_linkInfo.getTitle() != null ? m_linkInfo.getTitle() : "";
274        m_dialogContent = new CmsFieldsetFormFieldPanel(m_linkInfo, null);
275        m_dialogContent.addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().highTextBoxes());
276        m_dialogContent.getFieldSet().setOpenerVisible(false);
277        m_dialogContent.getFieldSet().getElement().getStyle().setMarginTop(4, Style.Unit.PX);
278        setMainContent(m_dialogContent);
279        if (m_isCreateNew) {
280            m_fileName = new CmsTextBox();
281            m_fileName.setTriggerChangeOnKeyPress(true);
282            m_fileName.addValueChangeHandler(this);
283            addInputRow(Messages.get().key(Messages.GUI_EDIT_LINK_LABEL_FILE_NAME_0), m_fileName);
284        }
285
286        m_linkTitle = new CmsTextBox();
287        m_linkTitle.setFormValueAsString(m_previousTitle);
288        m_linkTitle.setTriggerChangeOnKeyPress(true);
289        m_linkTitle.addValueChangeHandler(this);
290        addInputRow(Messages.get().key(Messages.GUI_EDIT_LINK_LABEL_TITLE_0), m_linkTitle);
291        m_linkContent = new CmsTextBox();
292        m_linkContent.setFormValueAsString(m_previousLink);
293        m_linkContent.setTriggerChangeOnKeyPress(true);
294        m_linkContent.addValueChangeHandler(this);
295        addInputRow(Messages.get().key(Messages.GUI_EDIT_LINK_LABEL_LINK_0), m_linkContent);
296        addDialogClose(null);
297        setOkEnabled(
298            false,
299            m_isCreateNew
300            ? Messages.get().key(Messages.GUI_EDIT_LINK_NO_FILE_NAME_0)
301            : Messages.get().key(Messages.GUI_EDIT_LINK_NO_CHANGES_0));
302
303    }
304
305    /**
306     * Called on dialog OK.<p>
307     */
308    protected void onOk() {
309
310        final String title = m_linkTitle.getFormValueAsString();
311        final String link = m_linkContent.getFormValueAsString();
312        m_linkTitle.setEnabled(false);
313        m_linkContent.setEnabled(false);
314        m_okButton.setEnabled(false);
315        if (m_isCreateNew) {
316            final String fileName = m_fileName.getFormValueAsString();
317            CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
318
319                @Override
320                public void execute() {
321
322                    CmsCoreProvider.getVfsService().createNewExternalLink(
323                        title,
324                        link,
325                        fileName,
326                        m_parentFolderPath,
327                        this);
328                }
329
330                @Override
331                protected void onResponse(Void result) {
332
333                    hide();
334                }
335            };
336            action.execute();
337        } else {
338            CmsRpcAction<Void> action = new CmsRpcAction<Void>() {
339
340                @Override
341                public void execute() {
342
343                    CmsCoreProvider.getVfsService().saveExternalLink(
344                        m_structureId,
345                        title,
346                        link,
347                        CmsResource.getName(m_linkInfo.getSitePath()),
348                        this);
349                }
350
351                @Override
352                protected void onResponse(Void result) {
353
354                    if (m_contextMenuHandler != null) {
355                        m_contextMenuHandler.refreshResource(m_structureId);
356                    }
357                    hide();
358                }
359            };
360            action.execute();
361        }
362    }
363
364    /**
365     * Enables or disables the OK button.<p>
366     *
367     * @param enabled <code>true</code> to enable the button
368     * @param message the disabled reason
369     */
370    protected void setOkEnabled(boolean enabled, String message) {
371
372        if (enabled) {
373            m_okButton.enable();
374        } else {
375            m_okButton.disable(message);
376        }
377    }
378
379    /**
380     * Adds a row to the form.<p>
381     *
382     * @param label the label
383     * @param inputWidget the input widget
384     */
385    private void addInputRow(String label, Widget inputWidget) {
386
387        FlowPanel row = new FlowPanel();
388        row.setStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().simpleFormRow());
389        CmsLabel labelWidget = new CmsLabel(label);
390        labelWidget.setStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().simpleFormLabel());
391        row.add(labelWidget);
392        inputWidget.addStyleName(I_CmsLayoutBundle.INSTANCE.generalCss().simpleFormInputBox());
393        row.add(inputWidget);
394        m_dialogContent.getFieldSet().add(row);
395    }
396
397}