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.ade.contenteditor.client;
029
030import org.opencms.gwt.client.ui.CmsFieldSet;
031import org.opencms.gwt.client.ui.CmsMessageWidget;
032import org.opencms.gwt.client.ui.CmsPopup;
033import org.opencms.gwt.client.ui.CmsPushButton;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
035import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
036import org.opencms.gwt.client.ui.input.CmsCheckBox;
037
038import java.util.HashSet;
039import java.util.Map;
040import java.util.Map.Entry;
041import java.util.Set;
042
043import com.google.gwt.event.dom.client.ClickEvent;
044import com.google.gwt.event.dom.client.ClickHandler;
045import com.google.gwt.event.logical.shared.ValueChangeEvent;
046import com.google.gwt.event.logical.shared.ValueChangeHandler;
047import com.google.gwt.user.client.ui.FlowPanel;
048
049/**
050 * The copy locale dialog.<p>
051 */
052public class CmsCopyLocaleDialog extends CmsPopup {
053
054    /** Locale checkbox value change handler. */
055    private class CopyLocaleChangeHandler implements ValueChangeHandler<Boolean> {
056
057        /** The locale. */
058        private String m_copyLocale;
059
060        /**
061         * The copy locale checkbox change handler.<p>
062         *
063         * @param locale the locale to copy
064         */
065        CopyLocaleChangeHandler(String locale) {
066
067            m_copyLocale = locale;
068        }
069
070        /**
071         * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
072         */
073        public void onValueChange(ValueChangeEvent<Boolean> event) {
074
075            if (event.getValue().booleanValue()) {
076                m_targetLocales.add(m_copyLocale);
077                m_okButton.enable();
078            } else {
079                m_targetLocales.remove(m_copyLocale);
080                if (m_targetLocales.isEmpty()) {
081                    m_okButton.disable(Messages.get().key(Messages.GUI_LOCALE_DIALOG_NO_LANGUAGE_SELECTED_0));
082                }
083            }
084        }
085    }
086
087    /** The ok button. */
088    CmsPushButton m_okButton;
089
090    /** The synchronize locale independent fields button. */
091    CmsPushButton m_synchronizeLocaleButton;
092
093    /** The locales to copy the current locale values to. */
094    Set<String> m_targetLocales;
095
096    /**
097     * Constructor.<p>
098     *
099     * @param availableLocales the available locales
100     * @param contentLocales the present content locales
101     * @param currentLocale the current content locale
102     * @param hasSync indicates the dialog requires the synchronize locale independent fields button
103     * @param editor the editor instance
104     */
105    public CmsCopyLocaleDialog(
106        Map<String, String> availableLocales,
107        Set<String> contentLocales,
108        String currentLocale,
109        boolean hasSync,
110        final CmsContentEditor editor) {
111
112        super(Messages.get().key(Messages.GUI_LOCALE_DIALOG_TITLE_0));
113        FlowPanel main = new FlowPanel();
114        CmsMessageWidget message = new CmsMessageWidget();
115        message.setMessageText(
116            Messages.get().key(Messages.GUI_LOCALE_DIALOG_DESCRIPTION_1, availableLocales.get(currentLocale)));
117        main.add(message);
118        CmsFieldSet fieldset = new CmsFieldSet();
119        m_targetLocales = new HashSet<String>();
120        for (Entry<String, String> availableLocaleEntry : availableLocales.entrySet()) {
121            if (!availableLocaleEntry.getKey().equals(currentLocale)) {
122                String label = availableLocaleEntry.getValue();
123                if (!contentLocales.contains(availableLocaleEntry.getKey())) {
124                    label += " [-]";
125                }
126                CmsCheckBox checkBox = new CmsCheckBox(label);
127                checkBox.addValueChangeHandler(new CopyLocaleChangeHandler(availableLocaleEntry.getKey()));
128                fieldset.addContent(checkBox);
129            }
130        }
131        m_okButton = new CmsPushButton();
132        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
133        m_okButton.setUseMinWidth(true);
134        m_okButton.setText(Messages.get().key(Messages.GUI_LOCALE_DIALOG_OK_0));
135        m_okButton.disable(Messages.get().key(Messages.GUI_LOCALE_DIALOG_NO_LANGUAGE_SELECTED_0));
136        m_okButton.addClickHandler(new ClickHandler() {
137
138            public void onClick(ClickEvent event) {
139
140                hide();
141                editor.copyLocales(m_targetLocales);
142            }
143        });
144        CmsPushButton cancelButton = new CmsPushButton();
145        cancelButton.setText(Messages.get().key(Messages.GUI_LOCALE_DIALOG_CANCEL_0));
146        cancelButton.addClickHandler(new ClickHandler() {
147
148            public void onClick(ClickEvent event) {
149
150                hide();
151            }
152        });
153        cancelButton.setUseMinWidth(true);
154        main.add(fieldset);
155        setMainContent(main);
156        addButton(cancelButton);
157        addButton(m_okButton);
158        if (hasSync) {
159            m_synchronizeLocaleButton = new CmsPushButton();
160            m_synchronizeLocaleButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.GREEN);
161            m_synchronizeLocaleButton.setUseMinWidth(true);
162            m_synchronizeLocaleButton.setText(Messages.get().key(Messages.GUI_LOCALE_DIALOG_SYNCHRONIZE_0));
163            m_synchronizeLocaleButton.setTitle(Messages.get().key(Messages.GUI_LOCALE_DIALOG_SYNCHRONIZE_TITLE_0));
164            m_synchronizeLocaleButton.addClickHandler(new ClickHandler() {
165
166                public void onClick(ClickEvent event) {
167
168                    hide();
169                    editor.synchronizeCurrentLocale();
170                }
171            });
172            addButton(m_synchronizeLocaleButton);
173        }
174        setGlassEnabled(true);
175    }
176}