001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.ade.contenteditor.client;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.CmsPopup;
032import org.opencms.gwt.client.ui.CmsPushButton;
033import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
035import org.opencms.gwt.client.ui.input.CmsRadioButtonGroupWidget;
036import org.opencms.gwt.shared.CmsGwtLog;
037
038import java.util.ArrayList;
039import java.util.LinkedHashMap;
040import java.util.List;
041import java.util.Map;
042import java.util.function.Consumer;
043
044import com.google.gwt.core.client.GWT;
045import com.google.gwt.uibinder.client.UiBinder;
046import com.google.gwt.uibinder.client.UiField;
047import com.google.gwt.user.client.ui.Composite;
048import com.google.gwt.user.client.ui.FlowPanel;
049import com.google.gwt.user.client.ui.Label;
050import com.google.gwt.user.client.ui.Panel;
051
052public class CmsContentTranslationView extends Composite {
053
054    /**
055     * UiBinder interface for this dialog.<p>
056     */
057    interface I_UiBinder extends UiBinder<Panel, CmsContentTranslationView> {
058        // empty uibinder interface
059    }
060
061    /** UiBinder instance for this dialog. */
062    private static I_UiBinder uibinder = GWT.create(I_UiBinder.class);
063
064    /** The action to execute after selecting the locale. */
065    private Consumer<String> m_action;
066
067    /** The selection widget for the target locale. */
068    private CmsRadioButtonGroupWidget m_input;
069
070    /** The popup in which this widget is displayed. */
071    private CmsPopup m_popup;
072
073    /** The label to display above the radio buttons. */
074    @UiField
075    protected Label m_label;
076
077    /**
078     * The container for the radio buttons.
079     */
080    @UiField
081    protected FlowPanel m_radioButtonContainer;
082
083    /** The buttons. */
084    private List<CmsPushButton> m_buttons = new ArrayList<>();
085
086    /**
087     * Creates a new instance.
088     *
089     * @param locale the current locale
090     * @param availableLocales the available locales
091     * @param action the action to execute after selecting a locale
092     */
093    public CmsContentTranslationView(String locale, Map<String, String> availableLocales, Consumer<String> action) {
094
095        m_action = action;
096        Panel content = uibinder.createAndBindUi(this);
097        initWidget(content);
098
099        LinkedHashMap<String, String> selectableLocales = new LinkedHashMap<>(availableLocales);
100        selectableLocales.remove(locale);
101
102        CmsPushButton okButton = new CmsPushButton();
103        okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
104        okButton.setText(Messages.get().key(Messages.GUI_OK_0));
105        okButton.setUseMinWidth(true);
106        m_label.setText(
107            org.opencms.ade.contenteditor.client.Messages.get().key(
108                org.opencms.ade.contenteditor.client.Messages.GUI_TRANSLATION_DIALOG_LOCALE_CHOICE_0));
109        m_input = new CmsRadioButtonGroupWidget(selectableLocales);
110        m_input.setFormValueAsString(selectableLocales.keySet().iterator().next());
111        m_radioButtonContainer.add(m_input);
112        okButton.addClickHandler(event -> {
113
114            String formValue = m_input.getFormValueAsString();
115            String localeSelected = formValue.trim();
116            CmsGwtLog.log("ok clicked, locale=" + localeSelected); //$NON-NLS-1$
117            m_popup.hide();
118            m_action.accept(localeSelected);
119        });
120
121        CmsPushButton cancelButton = new CmsPushButton();
122        cancelButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
123        cancelButton.setText(Messages.get().key(Messages.GUI_CANCEL_0));
124        cancelButton.setUseMinWidth(true);
125        cancelButton.addClickHandler(event -> m_popup.hide());
126        m_buttons.add(cancelButton);
127        m_buttons.add(okButton);
128
129    }
130
131    /**
132     * Shows the content translation dialog.
133     *
134     * @param locale the current locale
135     * @param availableLocales the available locales
136     * @param action the action to perform after locale selection
137     */
138    public static void showDialog(String locale, Map<String, String> availableLocales, Consumer<String> action) {
139
140        CmsPopup popup = new CmsPopup(
141            org.opencms.ade.contenteditor.client.Messages.get().key(
142                org.opencms.ade.contenteditor.client.Messages.GUI_TRANSLATION_DIALOG_TITLE_0),
143            CmsPopup.DEFAULT_WIDTH);
144        CmsContentTranslationView view = new CmsContentTranslationView(locale, availableLocales, action);
145
146        popup.setMainContent(view);
147        popup.setModal(true);
148        popup.setGlassEnabled(true);
149        view.setPopup(popup);
150        for (CmsPushButton button : view.getButtons()) {
151            popup.addButton(button);
152        }
153
154        popup.center();
155    }
156
157    /**
158     * Gets the buttons.
159     *
160     * @return the buttons
161     */
162    private List<CmsPushButton> getButtons() {
163
164        return m_buttons;
165    }
166
167    /**
168     * Sets the popup the widget is displayed in.
169     *
170     * @param popup the popup
171     */
172    private void setPopup(CmsPopup popup) {
173
174        m_popup = popup;
175
176    }
177
178}