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.favorites;
029
030import org.opencms.json.JSONException;
031import org.opencms.main.CmsLog;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.components.CmsBasicDialog;
034import org.opencms.ui.components.CmsErrorDialog;
035import org.opencms.util.CmsStringUtil;
036
037import java.util.Collections;
038import java.util.function.Consumer;
039
040import org.apache.commons.logging.Log;
041
042import com.vaadin.ui.Button;
043import com.vaadin.ui.TextField;
044
045/**
046 * Dialog for editing bookmark title.
047 */
048public class CmsEditFavoriteDialog extends CmsBasicDialog {
049
050    /** Serial version id. */
051    private static final long serialVersionUID = 1L;
052
053    /** Logger instance for this class. */
054    private static final Log LOG = CmsLog.getLog(CmsEditFavoriteDialog.class);
055
056    /** The callback to call after the title is changed. */
057    private Consumer<CmsFavoriteEntry> m_callback;
058
059    /** The cancel button. */
060    private Button m_cancelButton;
061
062    /** Boolean flag indicating whether the text field has changed. */
063    private boolean m_changed;
064
065    /** The favorite entry. */
066    private CmsFavoriteEntry m_entry;
067
068    /** The OK button. */
069    private Button m_okButton;
070
071    /** The title field. */
072    private TextField m_title;
073
074    /**
075     * Creates a new instance.
076     *
077     * @param info the info widget
078     * @param callback the callback
079     */
080    public CmsEditFavoriteDialog(CmsFavInfo info, Consumer<CmsFavoriteEntry> callback) {
081
082        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
083        CmsFavoriteEntry entry = info.getEntry();
084        m_callback = callback;
085        m_okButton.addClickListener(evt -> onClickOk());
086        String customTitle = entry.getCustomTitle();
087        if (CmsStringUtil.isEmptyOrWhitespaceOnly(customTitle)) {
088            customTitle = info.getTopLine().getValue();
089        }
090        m_entry = entry;
091        m_title.setValue(customTitle);
092        if (info.getResource() != null) {
093            displayResourceInfo(Collections.singletonList(info.getResource()));
094        }
095        m_cancelButton.addClickListener(evt -> {
096            CmsVaadinUtils.closeWindow(CmsEditFavoriteDialog.this);
097        });
098        m_title.addValueChangeListener(evt -> {
099            m_changed = true;
100        });
101    }
102
103    /**
104     * Handler for the OK button.
105     */
106    protected void onClickOk() {
107
108        if (!m_changed) {
109            CmsVaadinUtils.closeWindow(CmsEditFavoriteDialog.this);
110            return;
111        }
112        String value = m_title.getValue();
113        CmsFavoriteEntry result;
114        try {
115            result = new CmsFavoriteEntry(m_entry.toJson());
116            result.setCustomTitle(value);
117            m_callback.accept(result);
118            CmsVaadinUtils.closeWindow(CmsEditFavoriteDialog.this);
119
120        } catch (JSONException e) {
121            LOG.error(e.getLocalizedMessage(), e);
122            CmsErrorDialog.showErrorDialog(e);
123        }
124    }
125
126}