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.ade.contenteditor.shared.CmsContentAugmentationDetails;
031import org.opencms.ade.contenteditor.shared.rpc.I_CmsContentServiceAsync;
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.util.CmsUUID;
037
038import java.util.ArrayList;
039import java.util.List;
040import java.util.Objects;
041import java.util.function.Consumer;
042
043import com.google.gwt.core.client.GWT;
044import com.google.gwt.uibinder.client.UiBinder;
045import com.google.gwt.uibinder.client.UiField;
046import com.google.gwt.user.client.Timer;
047import com.google.gwt.user.client.rpc.AsyncCallback;
048import com.google.gwt.user.client.ui.Composite;
049import com.google.gwt.user.client.ui.HTML;
050import com.google.gwt.user.client.ui.Panel;
051
052/**
053 * Displays the progress about a translation job while said job is running on the server.
054 *
055 * <p>Also allows a user to try to abort the job.
056 */
057public class CmsContentTranslationProgressView extends Composite {
058
059    /**
060     * UiBinder interface for this dialog.<p>
061     */
062    interface I_UiBinder extends UiBinder<Panel, CmsContentTranslationProgressView> {
063        // empty uibinder interface
064    }
065
066    /** Length of the progress check interval in milliseconds. */
067    public static final int PROGRESS_CHECK_INTERVAL = 1000;
068
069    /** UiBinder instance for this dialog. */
070    private static I_UiBinder uibinder = GWT.create(I_UiBinder.class);
071
072    /** The label used to display the progress. */
073    @UiField
074    protected HTML m_message;
075
076    /** The action to execute after the translation has finished. */
077    private Consumer<CmsContentAugmentationDetails> m_action;
078
079    /** The buttons. */
080    private List<CmsPushButton> m_buttons = new ArrayList<>();
081
082    /** The ID of the job whose progress should be checked. */
083    private CmsUUID m_jobId;
084
085    /** The popup in which the widget is displayed. */
086    private CmsPopup m_popup;
087
088    private String m_lastProgress;
089
090    /** The content service. */
091    private I_CmsContentServiceAsync m_service;
092
093    /** The timer used to periodically check the progress. */
094    private Timer m_timer;
095
096    /**
097     * Creates a new instance.
098     *
099     * @param jobId the id of the job whose progress to check
100     * @param service the content service
101     * @param action the action to execute after the job has finished
102     */
103    public CmsContentTranslationProgressView(
104        CmsUUID jobId,
105        I_CmsContentServiceAsync service,
106        Consumer<CmsContentAugmentationDetails> action) {
107
108        m_jobId = jobId;
109        m_action = action;
110        m_service = service;
111        initWidget(uibinder.createAndBindUi(this));
112        m_message.setText(Messages.get().key(Messages.GUI_TRANSLATION_DIALOG_INITIAL_PROGRESS_0));
113        CmsPushButton abortButton = new CmsPushButton();
114        abortButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
115        abortButton.setText(Messages.get().key(Messages.GUI_TRANSLATION_DIALOG_ABORT_BUTTON_0));
116        abortButton.setUseMinWidth(true);
117        abortButton.addClickHandler(event -> {
118            if (m_timer != null) {
119                m_timer.cancel();
120                m_timer = null;
121            }
122            service.abortAugmentationJob(m_jobId, new AsyncCallback<Void>() {
123
124                @Override
125                public void onFailure(Throwable caught) {
126
127                    m_popup.hide();
128                }
129
130                @Override
131                public void onSuccess(Void result) {
132
133                    m_popup.hide();
134                }
135            });
136
137        });
138
139        m_buttons.add(abortButton);
140
141    }
142
143    /**
144     * Displays a popup containing a widget of this class.
145     *
146     * @param jobId the ID of the job whose progress should be checked
147     * @param service the content service
148     * @param action the action to execute after the translation has finished
149     */
150    public static void showDialog(
151        CmsUUID jobId,
152        I_CmsContentServiceAsync service,
153        Consumer<CmsContentAugmentationDetails> action) {
154
155        CmsPopup popup = new CmsPopup(
156            Messages.get().key(Messages.GUI_TRANSLATION_DIALOG_PROGRESS_TITLE_0),
157            CmsPopup.DEFAULT_WIDTH);
158        CmsContentTranslationProgressView view = new CmsContentTranslationProgressView(jobId, service, action);
159        popup.setMainContent(view);
160        popup.setModal(true);
161        popup.setGlassEnabled(true);
162        view.setPopup(popup);
163        for (CmsPushButton button : view.getButtons()) {
164            popup.addButton(button);
165        }
166
167        popup.center();
168    }
169
170    /**
171     * Gets the dialog buttons.
172     *
173     * @return the dialog buttons
174     */
175    public List<CmsPushButton> getButtons() {
176
177        return m_buttons;
178    }
179
180    /**
181     * Sets the action to execute after the job has finished.
182     * @param action
183     */
184    public void setAction(Consumer<CmsContentAugmentationDetails> action) {
185
186        m_action = action;
187
188    }
189
190    /**
191     * Sets the popup in which this widget is displayed.
192     *
193     * @param popup the popup
194     */
195    public void setPopup(CmsPopup popup) {
196
197        m_popup = popup;
198    }
199
200    /**
201     * Gets information about the job progress from the server, and if the job isn't finished, schedules another progress check.
202     */
203    protected void checkProgress() {
204
205        m_service.getAugmentationProgress(m_jobId, new AsyncCallback<CmsContentAugmentationDetails>() {
206
207            @Override
208            public void onFailure(Throwable caught) {
209
210                // TODO Auto-generated method stub
211
212            }
213
214            @Override
215            public void onSuccess(CmsContentAugmentationDetails result) {
216
217                if (result.isDone() || result.isAborted()) {
218                    m_popup.hide();
219                    m_action.accept(result);
220                } else {
221                    if (result.getProgress() != null) {
222                        if (!Objects.equals(result.getProgress(), m_lastProgress)) {
223                            m_lastProgress = result.getProgress();
224
225                            m_message.setHTML(result.getProgress());
226                        }
227                    }
228                    m_timer = new Timer() {
229
230                        @Override
231                        public void run() {
232
233                            checkProgress();
234                        }
235
236                    };
237                    m_timer.schedule(PROGRESS_CHECK_INTERVAL);
238
239                }
240            }
241        });
242
243    }
244
245    /**
246     * @see com.google.gwt.user.client.ui.Widget#onLoad()
247     */
248    @Override
249    protected void onLoad() {
250
251        checkProgress();
252
253    }
254
255}