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.client;
029
030import org.opencms.ade.galleries.client.CmsGalleryConfigurationJSO;
031import org.opencms.ade.galleries.client.ui.CmsGalleryPopup;
032import org.opencms.ade.publish.client.CmsPublishDialog;
033import org.opencms.ade.publish.shared.CmsPublishData;
034import org.opencms.ade.publish.shared.rpc.I_CmsPublishService;
035import org.opencms.ade.publish.shared.rpc.I_CmsPublishServiceAsync;
036import org.opencms.gwt.client.CmsCoreProvider;
037import org.opencms.gwt.client.I_CmsEditableData;
038import org.opencms.gwt.client.rpc.CmsRpcAction;
039import org.opencms.gwt.client.rpc.CmsRpcPrefetcher;
040import org.opencms.gwt.client.ui.CmsLockReportDialog;
041import org.opencms.gwt.client.ui.CmsPreviewDialog;
042import org.opencms.gwt.client.ui.CmsPreviewDialog.I_PreviewInfoProvider;
043import org.opencms.gwt.client.ui.contenteditor.CmsContentEditorDialog;
044import org.opencms.gwt.client.ui.contenteditor.CmsContentEditorDialog.DialogOptions;
045import org.opencms.gwt.client.ui.contenteditor.I_CmsContentEditorHandler;
046import org.opencms.gwt.client.ui.contextmenu.CmsAbout;
047import org.opencms.gwt.client.ui.contextmenu.CmsEditProperties;
048import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuCommand;
049import org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler;
050import org.opencms.gwt.client.ui.externallink.CmsEditExternalLinkDialog;
051import org.opencms.gwt.client.ui.input.category.CmsCategoryDialog;
052import org.opencms.gwt.client.ui.input.upload.CmsFileInput;
053import org.opencms.gwt.client.ui.preferences.CmsUserSettingsDialog;
054import org.opencms.gwt.client.ui.replace.CmsReplaceHandler;
055import org.opencms.gwt.client.ui.resourceinfo.CmsResourceInfoDialog;
056import org.opencms.gwt.client.util.CmsEditableDataUtil;
057import org.opencms.gwt.client.util.I_CmsSimpleCallback;
058import org.opencms.gwt.shared.CmsGwtConstants;
059import org.opencms.gwt.shared.CmsHistoryVersion;
060import org.opencms.gwt.shared.CmsPrepareEditResponse;
061import org.opencms.gwt.shared.CmsPreviewInfo;
062import org.opencms.gwt.shared.I_CmsEditableDataExtensions;
063import org.opencms.ui.components.extensions.CmsGwtDialogExtension;
064import org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc;
065import org.opencms.ui.shared.components.I_CmsGwtDialogServerRpc;
066import org.opencms.util.CmsUUID;
067
068import java.util.Arrays;
069import java.util.HashMap;
070import java.util.List;
071import java.util.Map;
072
073import com.google.common.collect.Lists;
074import com.google.gwt.core.client.GWT;
075import com.google.gwt.event.logical.shared.CloseEvent;
076import com.google.gwt.event.logical.shared.CloseHandler;
077import com.google.gwt.user.client.Command;
078import com.google.gwt.user.client.Window;
079import com.google.gwt.user.client.rpc.AsyncCallback;
080import com.google.gwt.user.client.rpc.SerializationException;
081import com.google.gwt.user.client.ui.PopupPanel;
082import com.vaadin.client.ServerConnector;
083import com.vaadin.client.extensions.AbstractExtensionConnector;
084import com.vaadin.shared.ui.Connect;
085
086/**
087 * Client side part of CmsGwtDialogExtension.<p>
088 */
089@Connect(CmsGwtDialogExtension.class)
090public class CmsGwtDialogExtensionConnector extends AbstractExtensionConnector implements I_CmsGwtDialogClientRpc {
091
092    /**
093     * Context menu handler.<p>
094     */
095    public class ContextMenuHandler implements I_CmsContextMenuHandler {
096
097        /**
098         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#ensureLockOnResource(org.opencms.util.CmsUUID, org.opencms.gwt.client.util.I_CmsSimpleCallback)
099         */
100        public void ensureLockOnResource(CmsUUID structureId, I_CmsSimpleCallback<Boolean> callback) {
101
102            notImplemented();
103        }
104
105        /**
106         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextMenuCommands()
107         */
108        public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
109
110            notImplemented();
111            return null;
112        }
113
114        /**
115         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getContextType()
116         */
117        public String getContextType() {
118
119            return CmsGwtConstants.CONTEXT_TYPE_FILE_TABLE;
120        }
121
122        /**
123         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#getEditorHandler()
124         */
125        public I_CmsContentEditorHandler getEditorHandler() {
126
127            notImplemented();
128            return null;
129        }
130
131        /**
132         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#leavePage(java.lang.String)
133         */
134        public void leavePage(String targetUri) {
135
136            notImplemented();
137        }
138
139        /**
140         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#onSiteOrProjectChange(java.lang.String, java.lang.String)
141         */
142        public void onSiteOrProjectChange(String sitePath, String serverLink) {
143
144            notImplemented();
145        }
146
147        /**
148         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#refreshResource(org.opencms.util.CmsUUID)
149         */
150        public void refreshResource(CmsUUID structureId) {
151
152            List<String> changed = Lists.newArrayList();
153            changed.add("" + structureId);
154            m_changed = changed;
155            close(0);
156        }
157
158        /**
159         * @see org.opencms.gwt.client.ui.contextmenu.I_CmsContextMenuHandler#unlockResource(org.opencms.util.CmsUUID)
160         */
161        public void unlockResource(CmsUUID structureId) {
162
163            notImplemented();
164        }
165
166        /**
167         * Throws an illegal state exception for not implemented methods.<p>
168         */
169        private void notImplemented() {
170
171            throw new IllegalStateException("Not implemented");
172        }
173    }
174
175    /** Serial version id. */
176    private static final long serialVersionUID = 1L;
177
178    /** List of structure ids of changed resources. */
179    protected List<String> m_changed = Lists.newArrayList();
180
181    /**
182     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#editPointer(java.lang.String)
183     */
184    public void editPointer(String pointerStructureId) {
185
186        CmsEditExternalLinkDialog dialog = CmsEditExternalLinkDialog.loadAndShowDialog(new CmsUUID(pointerStructureId));
187        dialog.setContextMenuHandler(new I_CmsContextMenuHandler() {
188
189            public void ensureLockOnResource(CmsUUID lockStructureId, I_CmsSimpleCallback<Boolean> callback) {
190
191                // not used
192            }
193
194            public Map<String, I_CmsContextMenuCommand> getContextMenuCommands() {
195
196                return null;
197            }
198
199            public String getContextType() {
200
201                return CmsGwtConstants.CONTEXT_TYPE_FILE_TABLE;
202            }
203
204            public I_CmsContentEditorHandler getEditorHandler() {
205
206                return null;
207            }
208
209            public void leavePage(String targetUri) {
210
211                // nothing to do
212            }
213
214            public void onSiteOrProjectChange(String sitePath, String serverLink) {
215
216                // nothing to do
217            }
218
219            public void refreshResource(CmsUUID structureId) {
220
221                m_changed.add("" + structureId);
222            }
223
224            public void unlockResource(CmsUUID structureId) {
225
226                // nothing to do
227            }
228        });
229        dialog.addCloseHandler(new CloseHandler<PopupPanel>() {
230
231            public void onClose(CloseEvent<PopupPanel> event) {
232
233                CmsGwtDialogExtensionConnector.this.close(0);
234            }
235        });
236    }
237
238    /**
239     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#editProperties(java.lang.String, boolean)
240     */
241    public void editProperties(String editStructureId, boolean editName) {
242
243        CmsEditProperties.editProperties(
244            new CmsUUID(editStructureId),
245            new ContextMenuHandler(),
246            editName,
247            new Runnable() {
248
249                public void run() {
250
251                    close(100);
252                }
253            },
254            false,
255            new CmsEditProperties.PropertyEditingContext());
256    }
257
258    /**
259     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openCategoriesDialog(java.lang.String, boolean)
260     */
261    public void openCategoriesDialog(final String structureId, boolean collapsed) {
262
263        CmsCategoryDialog dialog = new CmsCategoryDialog(new CmsUUID(structureId), new Command() {
264
265            public void execute() {
266
267                m_changed.add(structureId);
268                close(0);
269            }
270        }, collapsed);
271        dialog.center();
272    }
273
274    /**
275     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openContentEditor(java.lang.String, java.lang.String)
276     */
277    public void openContentEditor(String structureId, String sitePath) {
278
279        CmsRpcAction<CmsPrepareEditResponse> prepareEdit = new CmsRpcAction<CmsPrepareEditResponse>() {
280
281            @Override
282            public void execute() {
283
284                start(0, true);
285                CmsCoreProvider.getVfsService().prepareEdit(new CmsUUID(structureId), sitePath, this);
286            }
287
288            @Override
289            public void onResponse(CmsPrepareEditResponse response) {
290
291                stop(false);
292                doEdit(response.getSitePath(), response.getStructureId());
293            }
294        };
295        prepareEdit.execute();
296
297    }
298
299    /**
300     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openGalleryDialog(java.lang.String)
301     */
302    public void openGalleryDialog(String galleryConfiguration) {
303
304        CmsGalleryPopup popup = new CmsGalleryPopup(
305            null,
306            CmsGalleryConfigurationJSO.parseConfiguration(galleryConfiguration));
307        popup.addCloseHandler(new CloseHandler<PopupPanel>() {
308
309            public void onClose(CloseEvent<PopupPanel> event) {
310
311                close(0);
312            }
313        });
314        popup.center();
315    }
316
317    /**
318     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openInfoDialog(java.lang.String, java.lang.String)
319     */
320    public void openInfoDialog(String string, String startTab) {
321
322        CmsResourceInfoDialog.load(
323            new CmsUUID(string),
324            true,
325            null,
326            startTab,
327            new HashMap<String, String>(),
328            new CloseHandler<PopupPanel>() {
329
330                public void onClose(CloseEvent<PopupPanel> event) {
331
332                    close(0);
333                }
334            });
335
336    }
337
338    /**
339     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openLockReport(java.lang.String, java.lang.String)
340     */
341    public void openLockReport(String dialogTitle, final String structureId) {
342
343        CmsLockReportDialog.openDialogForResource(dialogTitle, new CmsUUID(structureId), new Command() {
344
345            public void execute() {
346
347                m_changed.add(structureId);
348            }
349        }, new Command() {
350
351            public void execute() {
352
353                close(0);
354            }
355        });
356
357    }
358
359    /**
360     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openPublishDialog(java.lang.String)
361     */
362    public void openPublishDialog(String serializedPublishData) {
363
364        try {
365            I_CmsPublishServiceAsync publishService = GWT.create(I_CmsPublishService.class);
366            CmsPublishData initData = (CmsPublishData)CmsRpcPrefetcher.getSerializedObjectFromString(
367                publishService,
368                serializedPublishData);
369            Runnable menuRefreshAction = new Runnable() {
370
371                public void run() {
372
373                    Window.Location.reload();
374                }
375            };
376
377            CloseHandler<PopupPanel> closeHandler = new CloseHandler<PopupPanel>() {
378
379                public void onClose(CloseEvent<PopupPanel> event) {
380
381                    CmsPublishDialog dialog = (CmsPublishDialog)(event.getTarget());
382                    long delay = 0;
383                    if (dialog.hasFailed() || dialog.hasSucceeded()) {
384                        m_changed = Arrays.asList("" + CmsUUID.getNullUUID());
385                        delay = 700;
386                    } else {
387                        m_changed = Lists.newArrayList();
388                    }
389                    close(delay);
390                }
391            };
392
393            CmsPublishDialog.showPublishDialog(initData, closeHandler, menuRefreshAction, null);
394        } catch (SerializationException e) {
395            throw new RuntimeException(e);
396
397        }
398    }
399
400    /**
401     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#openReplaceDialog(java.lang.String)
402     */
403    public void openReplaceDialog(String structureId) {
404
405        CmsReplaceHandler handler = new CmsReplaceHandler(new CmsUUID(structureId));
406        handler.setCloseHandler(new CloseHandler<PopupPanel>() {
407
408            public void onClose(CloseEvent<PopupPanel> event) {
409
410                close(0);
411            }
412        });
413        handler.onChange(new CmsFileInput());
414
415    }
416
417    /**
418     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#showAbout()
419     */
420    public void showAbout() {
421
422        CmsAbout.showAbout();
423    }
424
425    /**
426     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#showPreview(java.lang.String, java.lang.String)
427     */
428    public void showPreview(String uuid, String versionAsString) {
429
430        final CmsUUID id = new CmsUUID(uuid);
431        final CmsHistoryVersion version = CmsHistoryVersion.fromString(versionAsString);
432
433        CmsRpcAction<CmsPreviewInfo> previewAction = new CmsRpcAction<CmsPreviewInfo>() {
434
435            @Override
436            public void execute() {
437
438                start(0, true);
439                CmsCoreProvider.getVfsService().getHistoryPreviewInfo(
440                    id,
441                    CmsCoreProvider.get().getLocale(),
442                    version,
443                    this);
444            }
445
446            @Override
447            protected void onResponse(CmsPreviewInfo result) {
448
449                stop(false);
450                CmsPreviewDialog dialog = CmsPreviewDialog.createPreviewDialog(result);
451                dialog.setPreviewInfoProvider(new I_PreviewInfoProvider() {
452
453                    public void loadPreviewForLocale(String locale, AsyncCallback<CmsPreviewInfo> resultCallback) {
454
455                        CmsCoreProvider.getVfsService().getHistoryPreviewInfo(id, locale, version, resultCallback);
456                    }
457                });
458
459                dialog.center();
460            }
461        };
462        previewAction.execute();
463    }
464
465    /**
466     * @see org.opencms.ui.shared.components.I_CmsGwtDialogClientRpc#showUserPreferences()
467     */
468    public void showUserPreferences() {
469
470        CmsUserSettingsDialog.loadAndShow(new Runnable() {
471
472            public void run() {
473
474                close(true);
475            }
476        });
477    }
478
479    /**
480     *  Disposes of the extension and tells the server whether to re-init the UI.<p>
481     *
482     * @param reinitUI <code>true</code> to reinit the UI
483     */
484    protected void close(boolean reinitUI) {
485
486        getRpcProxy(I_CmsGwtDialogServerRpc.class).onClose(reinitUI);
487    }
488
489    /**
490     * Disposes of the extension on the server side and notifies the server of which resources have been changed.<p>
491     *
492     * @param delayMillis the time to wait on the server before refreshing the view
493     */
494    protected void close(long delayMillis) {
495
496        getRpcProxy(I_CmsGwtDialogServerRpc.class).onClose(m_changed, delayMillis);
497    }
498
499    /**
500     * Actually starts the XML content editor for a given file.<p>
501     *
502     * @param sitePath the site path of the file
503     * @param structureId the file structure id
504     */
505    protected void doEdit(String sitePath, CmsUUID structureId) {
506
507        I_CmsEditableData editData = new I_CmsEditableData() {
508
509            public String getContextId() {
510
511                return "";
512            }
513
514            public String getEditId() {
515
516                return null;
517            }
518
519            public String getElementId() {
520
521                return null;
522            }
523
524            public String getElementLanguage() {
525
526                return null;
527            }
528
529            public String getElementName() {
530
531                return null;
532            }
533
534            public I_CmsEditableDataExtensions getExtensions() {
535
536                return CmsEditableDataUtil.parseExtensions("{}");
537            }
538
539            public String getNewLink() {
540
541                return null;
542            }
543
544            public String getNewTitle() {
545
546                return null;
547            }
548
549            public String getNoEditReason() {
550
551                return null;
552            }
553
554            public String getPostCreateHandler() {
555
556                return null;
557            }
558
559            public String getSitePath() {
560
561                return sitePath;
562            }
563
564            public CmsUUID getStructureId() {
565
566                return structureId;
567            }
568
569            public boolean hasEditHandler() {
570
571                return false;
572            }
573
574            public boolean hasResource() {
575
576                return true;
577            }
578
579            public boolean isUnreleasedOrExpired() {
580
581                return false;
582            }
583
584            public void setSitePath(String path) {
585
586                // nothing to do
587            }
588        };
589
590        I_CmsContentEditorHandler handler = new I_CmsContentEditorHandler() {
591
592            public void onClose(
593                String editedFilesitePath,
594                CmsUUID editedFilestructureId,
595                boolean isNew,
596                boolean hasChangedSettings,
597                boolean usedPublishDialog) {
598
599                close(0);
600            }
601        };
602        CmsContentEditorDialog.get().openEditDialog(editData, false, null, new DialogOptions(), handler);
603
604    }
605
606    /**
607     * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
608     */
609    @Override
610    protected void extend(ServerConnector target) {
611
612        registerRpc(I_CmsGwtDialogClientRpc.class, this);
613    }
614
615}