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.resourceinfo;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.I_CmsDescendantResizeHandler;
032import org.opencms.gwt.client.rpc.CmsRpcAction;
033import org.opencms.gwt.client.ui.CmsPopup;
034import org.opencms.gwt.client.ui.CmsTabContentWrapper;
035import org.opencms.gwt.client.ui.CmsTabbedPanel;
036import org.opencms.gwt.client.ui.contextmenu.CmsDialogContextMenuHandler;
037import org.opencms.gwt.client.ui.resourceinfo.CmsResourceRelationView.Mode;
038import org.opencms.gwt.shared.CmsResourceStatusBean;
039import org.opencms.gwt.shared.CmsResourceStatusTabId;
040import org.opencms.util.CmsStringUtil;
041import org.opencms.util.CmsUUID;
042
043import java.util.ArrayList;
044import java.util.List;
045import java.util.Map;
046
047import com.google.common.collect.Lists;
048import com.google.gwt.core.client.Scheduler;
049import com.google.gwt.core.client.Scheduler.ScheduledCommand;
050import com.google.gwt.event.logical.shared.CloseHandler;
051import com.google.gwt.event.logical.shared.SelectionEvent;
052import com.google.gwt.event.logical.shared.SelectionHandler;
053import com.google.gwt.user.client.ui.PopupPanel;
054import com.google.gwt.user.client.ui.Widget;
055
056/**
057 * Dialog for displaying resource information.<p>
058 */
059public class CmsResourceInfoDialog extends CmsPopup {
060
061    /** The scroll panel height. */
062    protected static final int SCROLLPANEL_HEIGHT = 300;
063
064    /** The detail content id. */
065    CmsUUID m_detailContentId;
066
067    /** If relation targets should be displayed. */
068    boolean m_includeTargets;
069
070    /** The content structure id. */
071    CmsUUID m_structureId;
072
073    /** The tab panel. */
074    CmsTabbedPanel<CmsTabContentWrapper> m_tabPanel;
075
076    /**
077     * Creates the dialog for the given resource information.<p>
078     *
079     * @param statusBean the resource information to bean
080     * @param includeTargets <code>true</code> if relation targets should be displayed
081     * @param detailContentId the detail content id
082     */
083    public CmsResourceInfoDialog(CmsResourceStatusBean statusBean, boolean includeTargets, CmsUUID detailContentId) {
084
085        super();
086        setModal(true);
087        setGlassEnabled(true);
088        addDialogClose(null);
089        setWidth(610);
090        removePadding();
091        m_includeTargets = includeTargets;
092        m_detailContentId = detailContentId;
093        m_structureId = statusBean.getStructureId();
094        m_tabPanel = new CmsTabbedPanel<CmsTabContentWrapper>();
095        m_tabPanel.setAutoResize(true);
096        m_tabPanel.setAutoResizeHeightDelta(45);
097        CmsDialogContextMenuHandler menuHandler = new CmsDialogContextMenuHandler();
098        final List<CmsResourceRelationView> relationViews = new ArrayList<CmsResourceRelationView>();
099        for (Map.Entry<CmsResourceStatusTabId, String> tabEntry : statusBean.getTabs().entrySet()) {
100            switch (tabEntry.getKey()) {
101                case tabRelationsFrom:
102                    CmsResourceRelationView targets = new CmsResourceRelationView(
103                        statusBean,
104                        Mode.targets,
105                        menuHandler);
106                    setTabMinHeight(targets);
107                    targets.setPopup(this);
108                    m_tabPanel.add(new CmsTabContentWrapper(targets), tabEntry.getValue());
109                    relationViews.add(targets);
110                    break;
111                case tabRelationsTo:
112                    CmsResourceRelationView usage = new CmsResourceRelationView(statusBean, Mode.sources, menuHandler);
113                    setTabMinHeight(usage);
114                    usage.setPopup(this);
115                    m_tabPanel.add(new CmsTabContentWrapper(usage), tabEntry.getValue());
116                    relationViews.add(usage);
117                    break;
118                case tabStatus:
119                    CmsResourceInfoView infoView = new CmsResourceInfoView(statusBean, menuHandler);
120                    setTabMinHeight(infoView);
121                    m_tabPanel.add(new CmsTabContentWrapper(infoView), tabEntry.getValue());
122                    relationViews.add(null);
123                    break;
124                case tabSiblings:
125                    if (statusBean.getSiblings().size() > 0) {
126                        CmsResourceRelationView siblings = new CmsResourceRelationView(
127                            statusBean,
128                            Mode.siblings,
129                            menuHandler);
130                        setTabMinHeight(siblings);
131                        m_tabPanel.add(new CmsTabContentWrapper(siblings), tabEntry.getValue());
132                        relationViews.add(siblings);
133                    }
134                    break;
135                default:
136                    break;
137            }
138        }
139        if (relationViews.get(0) != null) {
140            relationViews.get(0).onResizeDescendant();
141        }
142        m_tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
143
144            public void onSelection(SelectionEvent<Integer> event) {
145
146                Widget tabContent = m_tabPanel.getWidget(event.getSelectedItem().intValue()).getWidget();
147                if (tabContent instanceof I_CmsDescendantResizeHandler) {
148                    ((I_CmsDescendantResizeHandler)tabContent).onResizeDescendant();
149                }
150                delayedResize();
151            }
152
153        });
154        setMainContent(m_tabPanel);
155        List<CmsResourceStatusTabId> tabKeyList = Lists.newArrayList(statusBean.getTabs().keySet());
156        int startTab = tabKeyList.indexOf(statusBean.getStartTab());
157        m_tabPanel.selectTab(startTab);
158    }
159
160    /**
161     * Loads the resource information for a resource and displays it in a dialog.<p>
162     *
163     * @param structureId the structure id of the resource for which the resource info should be loaded
164     * @param includeTargets true if relation targets should also be displayed
165     * @param detailContentId the structure id of the detail content if present
166     * @param context additional parameters from the context used for displaying additional infos
167     * @param closeHandler the close handler for the dialog (may be null if no close handler is needed)
168     */
169    public static void load(
170        final CmsUUID structureId,
171        final boolean includeTargets,
172        final CmsUUID detailContentId,
173        Map<String, String> context,
174        final CloseHandler<PopupPanel> closeHandler) {
175
176        load(structureId, includeTargets, detailContentId, null, context, closeHandler);
177    }
178
179    /**
180     * Loads the resource information for a resource and displays it in a dialog.<p>
181     *
182     * @param structureId the structure id of the resource for which the resource info should be loaded
183     * @param includeTargets true if relation targets should also be displayed
184     * @param detailContentId the structure id of the detail content if present
185     * @param startTab the start tab id
186     * @param context additional parameters from the context used for displaying additional infos
187     * @param closeHandler the close handler for the dialog (may be null if no close handler is needed)
188     */
189    public static void load(
190        final CmsUUID structureId,
191        final boolean includeTargets,
192        final CmsUUID detailContentId,
193        final String startTab,
194        final Map<String, String> context,
195        final CloseHandler<PopupPanel> closeHandler) {
196
197        CmsRpcAction<CmsResourceStatusBean> action = new CmsRpcAction<CmsResourceStatusBean>() {
198
199            @Override
200            public void execute() {
201
202                start(0, true);
203                CmsCoreProvider.getVfsService().getResourceStatus(
204                    structureId,
205                    CmsCoreProvider.get().getLocale(),
206                    includeTargets,
207                    detailContentId,
208                    context,
209                    this);
210            }
211
212            @Override
213            protected void onResponse(CmsResourceStatusBean result) {
214
215                stop(false);
216                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(startTab)) {
217                    result.setStartTab(CmsResourceStatusTabId.valueOf(startTab));
218                }
219                CmsResourceInfoDialog dialog = new CmsResourceInfoDialog(result, includeTargets, detailContentId);
220                if (closeHandler != null) {
221                    dialog.addCloseHandler(closeHandler);
222                }
223                dialog.centerHorizontally(150);
224            }
225        };
226        action.execute();
227    }
228
229    /**
230     * @see com.google.gwt.user.client.ui.Widget#onLoad()
231     */
232    @Override
233    public void onLoad() {
234
235        delayedResize();
236    }
237
238    /**
239     * Re-initializes the dialog content.<p>
240     *
241     * @param statusBean the resource status
242     */
243    protected void reinitContent(CmsResourceStatusBean statusBean) {
244
245        int selected = m_tabPanel.getSelectedIndex();
246        for (int i = 0; i < m_tabPanel.getTabCount(); i++) {
247            CmsTabContentWrapper wrapper = m_tabPanel.getWidget(i);
248            if (wrapper.getWidget() instanceof CmsResourceInfoView) {
249                ((CmsResourceInfoView)wrapper.getWidget()).initContent(statusBean);
250            } else if (wrapper.getWidget() instanceof CmsResourceRelationView) {
251                ((CmsResourceRelationView)wrapper.getWidget()).initContent(statusBean);
252                if (i == selected) {
253                    ((CmsResourceRelationView)wrapper.getWidget()).onResizeDescendant();
254                }
255            }
256        }
257    }
258
259    /**
260     * Schedules a resize operation.<p>
261     */
262    void delayedResize() {
263
264        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
265
266            public void execute() {
267
268                resize();
269            }
270        });
271
272    }
273
274    /**
275     * Reloads the dialog data.<p>
276     */
277    void reload() {
278
279        CmsRpcAction<CmsResourceStatusBean> action = new CmsRpcAction<CmsResourceStatusBean>() {
280
281            @Override
282            public void execute() {
283
284                start(0, true);
285                CmsCoreProvider.getVfsService().getResourceStatus(
286                    m_structureId,
287                    CmsCoreProvider.get().getLocale(),
288                    m_includeTargets,
289                    m_detailContentId,
290                    null,
291                    this);
292            }
293
294            @Override
295            protected void onResponse(CmsResourceStatusBean result) {
296
297                stop(false);
298                reinitContent(result);
299            }
300        };
301        action.execute();
302    }
303
304    /**
305     * Resizes the tab panel.<p>
306     */
307    void resize() {
308
309        m_tabPanel.onResizeDescendant();
310    }
311
312    /**
313     * Sets the minimum height for a tab content widget.<p>
314     *
315     * @param w the minimum height for a tab content widget
316     */
317    private void setTabMinHeight(Widget w) {
318
319        w.getElement().getStyle().setProperty("minHeight", "355px");
320    }
321}