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.gwt.client.ui.history;
029
030import org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.ui.I_CmsButton;
032import org.opencms.gwt.client.ui.css.I_CmsCellTableResources;
033import org.opencms.gwt.shared.CmsHistoryResourceBean;
034import org.opencms.gwt.shared.CmsHistoryResourceCollection;
035
036import com.google.common.base.Predicate;
037import com.google.gwt.cell.client.AbstractCell;
038import com.google.gwt.cell.client.ActionCell;
039import com.google.gwt.core.client.GWT;
040import com.google.gwt.dom.client.Style.Unit;
041import com.google.gwt.safehtml.client.SafeHtmlTemplates;
042import com.google.gwt.safehtml.shared.SafeHtml;
043import com.google.gwt.user.cellview.client.CellTable;
044import com.google.gwt.user.cellview.client.Column;
045import com.google.gwt.user.cellview.client.IdentityColumn;
046import com.google.gwt.user.cellview.client.TextColumn;
047import com.google.gwt.view.client.ListDataProvider;
048import com.google.gwt.view.client.ProvidesKey;
049
050/**
051 * Cell table used to display historical versions of a content.<p>
052 *
053 * Has buttons to preview or restore a previous version.<p>
054 */
055public class CmsResourceHistoryTable extends CellTable<CmsHistoryResourceBean> {
056
057    /** The templates used by this cell. */
058    static interface Templates extends SafeHtmlTemplates {
059
060        /**
061         * Template for the button HTML.<p>
062         *
063         * @param title the button title
064         * @param cssClass the button CSS class
065         *
066         * @return the HTML for the button
067         */
068        @Template("<span class=\"{1}\" title=\"{0}\"></span>")
069        SafeHtml button(String title, String cssClass);
070
071        /**
072         * Template for a span with a title.<p>
073         *
074         * @param text the span text
075         * @param title the span title
076         *
077         * @return the HTML for the span
078         */
079        @Template("<span title=\"{1}\">{0}</span>")
080        SafeHtml textSpanWithTitle(String text, String title);
081    }
082
083    /** The template instance. */
084    static Templates templates = GWT.create(Templates.class);
085    /** Handler instance for performing actions on the table entries. */
086    private I_CmsHistoryActionHandler m_handler;
087
088    /**
089     * Creates a new instance.<p>
090     *
091     * @param data the data to display in the table
092     * @param handler the handler instance used for performing actions on the table entries
093     */
094    public CmsResourceHistoryTable(CmsHistoryResourceCollection data, I_CmsHistoryActionHandler handler) {
095
096        super(
097            Integer.MAX_VALUE,
098            (CellTable.Resources)GWT.create(I_CmsCellTableResources.class),
099            new ProvidesKey<CmsHistoryResourceBean>() {
100
101                public Object getKey(CmsHistoryResourceBean item) {
102
103                    return item.getStructureId() + "_" + item.getVersion();
104                }
105
106            });
107        m_handler = handler;
108        setWidth("100%", true);
109        setTableLayoutFixed(true);
110
111        addVersionColumn();
112        addPreviewColumn();
113        addRevertColumn();
114        addPathColumn();
115        addSizeColumn();
116        addModificationDateColumn();
117        addUserLastModifiedColumn();
118        addPublishDateColumn();
119
120        ListDataProvider<CmsHistoryResourceBean> dataProvider = new ListDataProvider<CmsHistoryResourceBean>();
121        dataProvider.addDataDisplay(this);
122        dataProvider.setList(data.getResources());
123    }
124
125    /**
126     * Helper method for adding a table column with a given width and label.<p>
127     *
128     * @param label the column label
129     * @param width the column width in pixels
130     * @param col the column to add
131     */
132    private void addColumn(String label, int width, Column<CmsHistoryResourceBean, ?> col) {
133
134        addColumn(col, label);
135        setColumnWidth(col, width, Unit.PX);
136    }
137
138    /**
139     * Adds a table column.<p>
140     */
141    private void addModificationDateColumn() {
142
143        addColumn(CmsHistoryMessages.columnModificationDate(), 190, new TextColumn<CmsHistoryResourceBean>() {
144
145            @Override
146            public String getValue(CmsHistoryResourceBean historyRes) {
147
148                return historyRes.getModificationDate().getDateText();
149            }
150        });
151    }
152
153    /**
154     * Adds a table column.<p>
155     */
156    private void addPathColumn() {
157
158        Column<CmsHistoryResourceBean, ?> col = new TextColumn<CmsHistoryResourceBean>() {
159
160            @Override
161            public String getValue(CmsHistoryResourceBean historyRes) {
162
163                String path = historyRes.getRootPath();
164                String siteRoot = CmsCoreProvider.get().getSiteRoot();
165                if (path.startsWith(siteRoot)) {
166                    path = path.substring(siteRoot.length());
167                    if (!path.startsWith("/")) {
168                        path = "/" + path;
169                    }
170                }
171                return path;
172            }
173        };
174        addColumn(col, CmsHistoryMessages.columnPath());
175        setColumnWidth(col, 100, Unit.PCT);
176    }
177
178    /**
179     * Adds a table column.<p>
180     */
181    private void addPreviewColumn() {
182
183        CmsButtonCell<CmsHistoryResourceBean> previewCell = new CmsButtonCell<CmsHistoryResourceBean>(
184            CmsHistoryMessages.titlePreview(),
185            I_CmsButton.ICON_FONT + " " + I_CmsButton.PREVIEW_SMALL,
186            new ActionCell.Delegate<CmsHistoryResourceBean>() {
187
188                @SuppressWarnings("synthetic-access")
189                public void execute(CmsHistoryResourceBean historyRes) {
190
191                    m_handler.showPreview(historyRes);
192                }
193            },
194            new Predicate<CmsHistoryResourceBean>() {
195
196                public boolean apply(CmsHistoryResourceBean bean) {
197
198                    return true;
199
200                }
201            });
202
203        addColumn(CmsHistoryMessages.columnPreview(), 30, new IdentityColumn<CmsHistoryResourceBean>(previewCell));
204    }
205
206    /**
207     * Adds a table column.<p>
208     */
209    private void addPublishDateColumn() {
210
211        addColumn(CmsHistoryMessages.columnPublishDate(), 190, new TextColumn<CmsHistoryResourceBean>() {
212
213            @Override
214            public String getValue(CmsHistoryResourceBean historyRes) {
215
216                if (historyRes.getPublishDate() != null) {
217                    return historyRes.getPublishDate().getDateText();
218                }
219                return "-";
220            }
221        });
222    }
223
224    /**
225     * Adds a table column.<p>
226     */
227    private void addRevertColumn() {
228
229        CmsButtonCell<CmsHistoryResourceBean> replaceCell = new CmsButtonCell<CmsHistoryResourceBean>(
230            CmsHistoryMessages.titleRevert(),
231            I_CmsButton.ICON_FONT + " " + I_CmsButton.RESET,
232            new ActionCell.Delegate<CmsHistoryResourceBean>() {
233
234                @SuppressWarnings("synthetic-access")
235                public void execute(CmsHistoryResourceBean historyRes) {
236
237                    m_handler.revert(historyRes);
238                }
239            },
240            new Predicate<CmsHistoryResourceBean>() {
241
242                public boolean apply(CmsHistoryResourceBean bean) {
243
244                    return bean.getVersion().getVersionNumber() != null;
245
246                }
247            });
248        addColumn(CmsHistoryMessages.columnReplace(), 30, new IdentityColumn<CmsHistoryResourceBean>(replaceCell));
249    }
250
251    /**
252     * Adds a table column.<p>
253     */
254    private void addSizeColumn() {
255
256        Column<CmsHistoryResourceBean, ?> col = new TextColumn<CmsHistoryResourceBean>() {
257
258            @Override
259            public String getValue(CmsHistoryResourceBean historyRes) {
260
261                return "" + historyRes.getSize();
262            }
263        };
264        addColumn(col, CmsHistoryMessages.columnSize());
265        setColumnWidth(col, 100, Unit.PX);
266    }
267
268    /**
269     * Adds a table column.<p>
270     */
271    private void addUserLastModifiedColumn() {
272
273        addColumn(CmsHistoryMessages.columnUserLastModified(), 120, new TextColumn<CmsHistoryResourceBean>() {
274
275            @Override
276            public String getValue(CmsHistoryResourceBean historyRes) {
277
278                return historyRes.getUserLastModified();
279            }
280        });
281    }
282
283    /**
284     * Adds a table column.<p>
285     */
286    private void addVersionColumn() {
287
288        AbstractCell<CmsHistoryResourceBean> cell = new CmsVersionCell();
289        addColumn(CmsHistoryMessages.columnVersion(), 40, new IdentityColumn<CmsHistoryResourceBean>(cell));
290    }
291
292}