001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.rename;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.rpc.CmsRpcAction;
032import org.opencms.gwt.client.ui.CmsListItemWidget;
033import org.opencms.gwt.client.ui.CmsPopup;
034import org.opencms.gwt.client.ui.CmsPushButton;
035import org.opencms.gwt.client.ui.input.CmsTextBox;
036import org.opencms.gwt.shared.CmsRenameInfoBean;
037import org.opencms.util.CmsUUID;
038
039import java.util.ArrayList;
040import java.util.List;
041
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.event.dom.client.ClickEvent;
044import com.google.gwt.event.dom.client.KeyCodes;
045import com.google.gwt.event.dom.client.KeyPressEvent;
046import com.google.gwt.event.dom.client.KeyPressHandler;
047import com.google.gwt.uibinder.client.UiBinder;
048import com.google.gwt.uibinder.client.UiField;
049import com.google.gwt.uibinder.client.UiHandler;
050import com.google.gwt.user.client.rpc.AsyncCallback;
051import com.google.gwt.user.client.ui.Composite;
052import com.google.gwt.user.client.ui.HasText;
053import com.google.gwt.user.client.ui.Panel;
054import com.google.gwt.user.client.ui.Widget;
055
056/**
057 * The dialog contents of the 'Rename' dialog.<p>
058 */
059public class CmsRenameView extends Composite {
060
061    /** The UiBinder interface for this widget. */
062    interface I_CmsRenameViewUiBinder extends UiBinder<Widget, CmsRenameView> {
063        // empy UiBinder interface
064    }
065
066    /** An enum representing the widget state. */
067    enum State {
068        /** The validation has just failed. */
069        validationFailed,
070
071        /** The validation was triggered but hasn't finished yet. */
072        validationRunning,
073
074        /** The validation status is unknown. */
075        validationUnknown
076    }
077
078    /** The UiBinder instance for this widget. */
079    private static I_CmsRenameViewUiBinder uiBinder = GWT.create(I_CmsRenameViewUiBinder.class);
080
081    /** The Cancel button. */
082    @UiField
083    protected CmsPushButton m_cancelButton;
084
085    /** The container for the resource info box. */
086    @UiField
087    protected Panel m_infoBoxContainer;
088
089    /** The input field for the new name. */
090    @UiField
091    protected CmsTextBox m_newNameInput;
092
093    /** The label for the input box. */
094    @UiField
095    protected HasText m_newNameLabel;
096
097    /** The OK button. */
098    @UiField
099    protected CmsPushButton m_okButton;
100
101    /** The label for the old name. */
102    @UiField
103    protected HasText m_oldNameLabel;
104
105    /** The label containing the old name. */
106    @UiField
107    protected HasText m_oldNameValue;
108
109    /** The callback which is invoked when the resource has been renamed successfully. */
110    AsyncCallback<String> m_renameCallback;
111
112    /** The widget state. */
113    State m_state;
114
115    /** The structure id of the resource which is being renamed. **/
116    CmsUUID m_structureId;
117
118    /** The dialog which contains this view. */
119    private CmsPopup m_dialog;
120
121    /**
122     * Creates a new instance.<p>
123     *
124     * @param renameInfo the information for the resource which is being renamed
125     * @param renameCallback the callback which should be called when the resource has been successfully renamed
126     */
127    public CmsRenameView(CmsRenameInfoBean renameInfo, AsyncCallback<String> renameCallback) {
128
129        m_structureId = renameInfo.getStructureId();
130        m_renameCallback = renameCallback;
131        String sitePath = renameInfo.getSitePath().replaceFirst("/$", "");
132        String oldName = sitePath.substring(1 + sitePath.lastIndexOf('/'));
133
134        initWidget(uiBinder.createAndBindUi(this));
135        m_oldNameLabel.setText(CmsRenameMessages.messageOldNameLabel());
136        m_newNameLabel.setText(CmsRenameMessages.messageNewNameLabel());
137        m_okButton.setText(CmsRenameMessages.messageOk());
138        m_okButton.setUseMinWidth(true);
139        m_cancelButton.setText(CmsRenameMessages.messageCancel());
140        m_cancelButton.setUseMinWidth(true);
141        m_oldNameValue.setText(oldName);
142
143        CmsListItemWidget listItemWidget = new CmsListItemWidget(renameInfo.getListInfo());
144        m_infoBoxContainer.add(listItemWidget);
145        m_newNameInput.addKeyPressHandler(new KeyPressHandler() {
146
147            public void onKeyPress(KeyPressEvent event) {
148
149                if (event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
150                    onClickOk(null);
151                } else if (m_state == State.validationFailed) {
152                    changeState(State.validationUnknown);
153                }
154            }
155        });
156    }
157
158    /**
159     * Gets the buttons which should be inserted into the parent dialog.<p>
160     *
161     * @return the list of buttons for the parent dialog
162     */
163    public List<CmsPushButton> getDialogButtons() {
164
165        List<CmsPushButton> result = new ArrayList<CmsPushButton>();
166        result.add(m_cancelButton);
167        result.add(m_okButton);
168        return result;
169    }
170
171    /**
172     * Sets the dialog in which this view is contained.<p>
173     *
174     * @param popup the dialog containing this view
175     */
176    public void setDialog(CmsPopup popup) {
177
178        m_dialog = popup;
179    }
180
181    /**
182     * Event handler for the Cancel button.<p>
183     *
184     * @param e the click event
185     */
186    @UiHandler("m_cancelButton")
187    protected void onClickCancel(ClickEvent e) {
188
189        close();
190    }
191
192    /**
193     * Event handler for the OK button.<p>
194     *
195     * @param e the click event
196     */
197    @UiHandler("m_okButton")
198    protected void onClickOk(ClickEvent e) {
199
200        changeState(State.validationRunning);
201        final String newName = getValue();
202        CmsRpcAction<String> validate = new CmsRpcAction<String>() {
203
204            @Override
205            public void execute() {
206
207                start(200, false);
208                CmsCoreProvider.getVfsService().renameResource(m_structureId, newName, this);
209            }
210
211            @Override
212            public void onFailure(Throwable t) {
213
214                super.onFailure(t);
215                changeState(State.validationUnknown);
216            }
217
218            @Override
219            protected void onResponse(String result) {
220
221                stop(false);
222                if (result == null) {
223                    close();
224                    if (m_renameCallback != null) {
225                        m_renameCallback.onSuccess(newName);
226                    }
227                } else {
228                    changeState(State.validationFailed);
229                    setValidationError(result);
230                }
231            }
232
233        };
234        validate.execute();
235
236    }
237
238    /**
239     * Changes the state of the widget.<p>
240     *
241     * @param state the state to change to
242     */
243    void changeState(State state) {
244
245        m_state = state;
246        switch (state) {
247            case validationRunning:
248                m_cancelButton.setEnabled(false);
249                m_okButton.setEnabled(false);
250                clearValidationError();
251                break;
252            case validationFailed:
253                m_okButton.setEnabled(false);
254                m_cancelButton.setEnabled(true);
255                break;
256            case validationUnknown:
257            default:
258                m_cancelButton.setEnabled(true);
259                m_okButton.setEnabled(true);
260                clearValidationError();
261                break;
262
263        }
264    }
265
266    /**
267     * Closes the dialog containing this widget.<p>
268     */
269    void close() {
270
271        m_dialog.hide();
272    }
273
274    /**
275     * Gets the new name entered by the user.<p>
276     *
277     * @return the new name
278     */
279    String getValue() {
280
281        return m_newNameInput.getText();
282    }
283
284    /**
285     * Sets the validation error in the widget.<p>
286     *
287     * @param error the validation error message
288     */
289    void setValidationError(String error) {
290
291        m_newNameInput.setErrorMessage(error);
292    }
293
294    /**
295     * Clears the validation error message.<p>
296     */
297    private void clearValidationError() {
298
299        m_newNameInput.setErrorMessage(null);
300    }
301
302}