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.sitemap.client;
029
030import org.opencms.ade.galleries.client.ui.CmsGalleryPopup;
031import org.opencms.ade.galleries.shared.CmsGalleryConfiguration;
032import org.opencms.ade.galleries.shared.CmsGalleryTabConfiguration;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryMode;
034import org.opencms.ade.sitemap.client.control.CmsSitemapController;
035import org.opencms.ade.sitemap.client.ui.css.I_CmsSitemapLayoutBundle;
036import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
037import org.opencms.ade.sitemap.shared.CmsGalleryFolderEntry;
038import org.opencms.ade.sitemap.shared.CmsGalleryType;
039import org.opencms.file.CmsResource;
040import org.opencms.gwt.client.property.CmsReloadMode;
041import org.opencms.gwt.client.ui.CmsAlertDialog;
042import org.opencms.gwt.client.ui.CmsListItemWidget;
043import org.opencms.gwt.client.ui.CmsListItemWidget.I_CmsTitleEditHandler;
044import org.opencms.gwt.client.ui.input.CmsLabel;
045import org.opencms.gwt.client.ui.tree.CmsTreeItem;
046import org.opencms.gwt.shared.CmsListInfoBean;
047import org.opencms.gwt.shared.property.CmsClientProperty;
048import org.opencms.gwt.shared.property.CmsPropertyModification;
049import org.opencms.util.CmsStringUtil;
050import org.opencms.util.CmsUUID;
051
052import java.util.ArrayList;
053import java.util.List;
054
055import com.google.gwt.event.dom.client.ClickEvent;
056import com.google.gwt.event.dom.client.ClickHandler;
057import com.google.gwt.user.client.rpc.AsyncCallback;
058import com.google.gwt.user.client.ui.TextBox;
059import com.google.gwt.user.client.ui.Widget;
060
061/**
062 * Tree item for the gallery view.<p>
063 */
064public class CmsGalleryTreeItem extends CmsTreeItem {
065
066    /**
067     * List item widget that displays additional infos dynamically.<p>
068     */
069    protected class CmsGalleryListItemWidget extends CmsListItemWidget {
070
071        /**
072         * Constructor.<p>
073         *
074         * @param infoBean the data to display
075         */
076        public CmsGalleryListItemWidget(CmsListInfoBean infoBean) {
077
078            super(infoBean);
079            ensureOpenCloseAdditionalInfo();
080        }
081
082        /**
083         * @see org.opencms.gwt.client.ui.CmsListItemWidget#setAdditionalInfoVisible(boolean)
084         */
085        @Override
086        public void setAdditionalInfoVisible(boolean visible) {
087
088            if (visible && !m_openClose.isDown()) {
089                CmsClientSitemapEntry entry = CmsSitemapView.getInstance().getController().getEntryById(getEntryId());
090                if (entry != null) {
091                    initAdditionalInfo(CmsSitemapTreeItem.getInfoBean(entry, false));
092                } else {
093                    return;
094                }
095            }
096            super.setAdditionalInfoVisible(visible);
097        }
098
099    }
100
101    /** The folder entry id. */
102    private CmsUUID m_entryId;
103
104    /**
105     * Constructor.<p>
106     *
107     * @param galleryFolder the gallery folder
108     */
109    public CmsGalleryTreeItem(CmsGalleryFolderEntry galleryFolder) {
110
111        super(true);
112        initContent(createListWidget(galleryFolder));
113        m_entryId = galleryFolder.getStructureId();
114    }
115
116    /**
117     * Constructor.<p>
118     *
119     * @param galleryType the gallery type
120     */
121    public CmsGalleryTreeItem(CmsGalleryType galleryType) {
122
123        super(true);
124        CmsListItemWidget itemWidget = createListWidget(galleryType);
125        initContent(itemWidget);
126        itemWidget.addIconClickHandler(new ClickHandler() {
127
128            public void onClick(ClickEvent event) {
129
130                setOpen(!isOpen());
131            }
132        });
133    }
134
135    /**
136     * Creates the list item widget for the given type.<p>
137     *
138     * @param galleryType the gallery type
139     *
140     * @return the list item widget
141     */
142    public static CmsListItemWidget createListWidget(CmsGalleryType galleryType) {
143
144        CmsListItemWidget result = new CmsListItemWidget(galleryType);
145        return result;
146    }
147
148    /**
149     * Returns the folder entry id.<p>
150     *
151     * @return the folder entry id
152     */
153    public CmsUUID getEntryId() {
154
155        return m_entryId;
156    }
157
158    /**
159     * Returns the site path.<p>
160     *
161     * @return the site path
162     */
163    public String getSitePath() {
164
165        // the site path is displayed as the sub title
166        return getListItemWidget().getSubtitleLabel();
167    }
168
169    /**
170     * Updates the site path info.<p>
171     *
172     * @param sitePath the new site path
173     */
174    public void updateSitePath(String sitePath) {
175
176        String oldPath = getSitePath();
177        getListItemWidget().setSubtitleLabel(sitePath);
178        for (Widget child : getChildren()) {
179            ((CmsGalleryTreeItem)child).updateParentPath(sitePath, oldPath);
180        }
181    }
182
183    /**
184     * Shows the gallery dialog for the given entry.<p>
185     *
186     * @param entry the gallery folder entry
187     */
188    protected void showGallery(CmsClientSitemapEntry entry) {
189
190        CmsGalleryConfiguration configuration = new CmsGalleryConfiguration();
191        List<String> typeNames = CmsSitemapView.getInstance().getController().getGalleryType(
192            Integer.valueOf(entry.getResourceTypeId())).getContentTypeNames();
193        configuration.setSearchTypes(typeNames);
194        configuration.setResourceTypes(typeNames);
195        configuration.setGalleryMode(GalleryMode.adeView);
196        configuration.setGalleryStoragePrefix("" + GalleryMode.adeView);
197        configuration.setTabConfiguration(CmsGalleryTabConfiguration.resolve("selectDoc"));
198        configuration.setReferencePath(getSitePath());
199        configuration.setGalleryPath(getSitePath());
200        CmsGalleryPopup dialog = new CmsGalleryPopup(null, configuration);
201        dialog.center();
202    }
203
204    /**
205     * Handles direct editing of the gallery title.<p>
206     *
207     * @param editEntry the edit entry
208     * @param newTitle the new title
209     */
210    void handleEdit(CmsClientSitemapEntry editEntry, final String newTitle) {
211
212        if (CmsStringUtil.isEmpty(newTitle)) {
213            String dialogTitle = Messages.get().key(Messages.GUI_EDIT_TITLE_ERROR_DIALOG_TITLE_0);
214            String dialogText = Messages.get().key(Messages.GUI_TITLE_CANT_BE_EMPTY_0);
215            CmsAlertDialog alert = new CmsAlertDialog(dialogTitle, dialogText);
216            alert.center();
217            return;
218        }
219        String oldTitle = editEntry.getPropertyValue(CmsClientProperty.PROPERTY_TITLE);
220        if (!oldTitle.equals(newTitle)) {
221            CmsPropertyModification propMod = new CmsPropertyModification(
222                getEntryId(),
223                CmsClientProperty.PROPERTY_TITLE,
224                newTitle,
225                true);
226            final List<CmsPropertyModification> propChanges = new ArrayList<CmsPropertyModification>();
227            propChanges.add(propMod);
228            CmsSitemapController controller = CmsSitemapView.getInstance().getController();
229            controller.edit(editEntry, propChanges, CmsReloadMode.reloadEntry);
230        }
231    }
232
233    /**
234     * Creates the list item widget for the given folder.<p>
235     *
236     * @param galleryFolder the gallery folder
237     *
238     * @return the list item widget
239     */
240    private CmsListItemWidget createListWidget(CmsGalleryFolderEntry galleryFolder) {
241
242        String title;
243        if (galleryFolder.getOwnProperties().containsKey(CmsClientProperty.PROPERTY_TITLE)) {
244            title = galleryFolder.getOwnProperties().get(CmsClientProperty.PROPERTY_TITLE).getStructureValue();
245        } else {
246            title = CmsResource.getName(galleryFolder.getSitePath());
247            if (title.endsWith("/")) {
248                title = title.substring(0, title.length() - 1);
249            }
250        }
251        CmsListInfoBean infoBean = new CmsListInfoBean(title, galleryFolder.getSitePath(), null);
252        CmsListItemWidget result = new CmsGalleryListItemWidget(infoBean);
253        result.setIcon(galleryFolder.getIconClasses());
254        result.addIconClickHandler(new ClickHandler() {
255
256            public void onClick(ClickEvent event) {
257
258                CmsSitemapView.getInstance().getController().loadPath(
259                    getSitePath(),
260                    new AsyncCallback<CmsClientSitemapEntry>() {
261
262                        public void onFailure(Throwable caught) {
263
264                            // nothing to do
265
266                        }
267
268                        public void onSuccess(CmsClientSitemapEntry entry) {
269
270                            showGallery(entry);
271                        }
272                    });
273            }
274        });
275        if ((CmsSitemapView.getInstance().getController().getEntryById(galleryFolder.getStructureId()) == null)
276            || CmsSitemapView.getInstance().getController().getEntryById(galleryFolder.getStructureId()).isEditable()) {
277            result.addTitleStyleName(I_CmsSitemapLayoutBundle.INSTANCE.sitemapItemCss().itemTitle());
278            result.setTitleEditable(true);
279            result.setTitleEditHandler(new I_CmsTitleEditHandler() {
280
281                /**
282                 * @see org.opencms.gwt.client.ui.CmsListItemWidget.I_CmsTitleEditHandler#handleEdit(org.opencms.gwt.client.ui.input.CmsLabel, com.google.gwt.user.client.ui.TextBox)
283                 */
284                public void handleEdit(final CmsLabel titleLabel, final TextBox box) {
285
286                    CmsSitemapView.getInstance().getController().loadPath(
287                        getSitePath(),
288                        new AsyncCallback<CmsClientSitemapEntry>() {
289
290                            public void onFailure(Throwable caught) {
291
292                                // nothing to do
293                            }
294
295                            public void onSuccess(CmsClientSitemapEntry editEntry) {
296
297                                CmsGalleryTreeItem.this.handleEdit(editEntry, box.getText());
298                                box.removeFromParent();
299                                titleLabel.setVisible(true);
300                            }
301                        });
302                }
303            });
304        }
305        return result;
306    }
307
308    /**
309     * Updates the site path info.<p>
310     *
311     * @param newParentPath the new parent path
312     * @param oldParentPath the previous parent path
313     */
314    private void updateParentPath(String newParentPath, String oldParentPath) {
315
316        String oldPath = getSitePath();
317        String newPath = oldPath.replaceFirst(oldParentPath, newParentPath);
318        getListItemWidget().setSubtitleLabel(newPath);
319        for (Widget child : getChildren()) {
320            ((CmsGalleryTreeItem)child).updateParentPath(newPath, oldPath);
321        }
322    }
323}