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.apps.dbmanager;
029
030import org.opencms.file.CmsPropertyDefinition;
031import org.opencms.main.CmsException;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.CmsCssIcon;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.CmsAppWorkplaceUi;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.apps.search.CmsSearchReplaceSettings;
038import org.opencms.ui.apps.search.CmsSourceSearchApp;
039import org.opencms.ui.apps.search.CmsSourceSearchAppConfiguration;
040import org.opencms.ui.apps.search.CmsSourceSearchForm.SearchType;
041import org.opencms.ui.components.CmsBasicDialog;
042import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
043import org.opencms.ui.components.OpenCmsTheme;
044import org.opencms.ui.contextmenu.CmsContextMenu;
045import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
046import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
047import org.opencms.util.CmsStringUtil;
048
049import java.util.ArrayList;
050import java.util.Collections;
051import java.util.List;
052import java.util.Locale;
053import java.util.Set;
054
055import com.vaadin.server.Resource;
056import com.vaadin.shared.MouseEventDetails.MouseButton;
057import com.vaadin.ui.Window;
058import com.vaadin.ui.themes.ValoTheme;
059import com.vaadin.v7.data.Item;
060import com.vaadin.v7.data.util.IndexedContainer;
061import com.vaadin.v7.data.util.filter.Or;
062import com.vaadin.v7.data.util.filter.SimpleStringFilter;
063import com.vaadin.v7.event.ItemClickEvent;
064import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
065import com.vaadin.v7.ui.Table;
066
067/**
068 * Class for the table containing all property definitions in the system.<p>
069 */
070public class CmsPropertyTable extends Table {
071
072    /**Table columns.*/
073    protected enum TableColumn {
074
075        /**The icon column.*/
076        Icon(null, Resource.class, new CmsCssIcon(OpenCmsTheme.ICON_DATABASE)),
077        /**The Name column.*/
078        Name(Messages.GUI_MESSAGES_BROADCAST_COLS_USER_0, String.class, "");
079
080        /**Default value for column.*/
081        private Object m_defaultValue;
082
083        /**Header Message key.*/
084        private String m_headerMessage;
085
086        /**Type of column property.*/
087        private Class<?> m_type;
088
089        /**
090         * constructor.
091         *
092         * @param headerMessage key
093         * @param type to property
094         * @param defaultValue of column
095         */
096        TableColumn(String headerMessage, Class<?> type, Object defaultValue) {
097
098            m_headerMessage = headerMessage;
099            m_type = type;
100            m_defaultValue = defaultValue;
101        }
102
103        /**
104         * Returns list of all properties with non-empty header.<p>
105         *
106         * @return list of properties
107         */
108        static List<TableColumn> withHeader() {
109
110            List<TableColumn> props = new ArrayList<TableColumn>();
111
112            for (TableColumn prop : TableColumn.values()) {
113                if (prop.m_headerMessage != null) {
114                    props.add(prop);
115                }
116            }
117            return props;
118        }
119
120        /**
121         * Returns the default value of property.<p>
122         *
123         * @return object
124         */
125        Object getDefaultValue() {
126
127            return m_defaultValue;
128        }
129
130        /**
131         * Returns localized header.<p>
132         *
133         * @return string for header
134         */
135        String getLocalizedMessage() {
136
137            if (m_headerMessage == null) {
138                return "";
139            }
140            return CmsVaadinUtils.getMessageText(m_headerMessage);
141        }
142
143        /**
144         * Returns tye of value for given property.<p>
145         *
146         * @return type
147         */
148        Class<?> getType() {
149
150            return m_type;
151        }
152
153    }
154
155    /**
156     * Menu entry for showing resources.<p>
157     */
158    class EntryDelete implements I_CmsSimpleContextMenuEntry<Set<String>> {
159
160        /**
161         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
162         */
163        public void executeAction(Set<String> data) {
164
165            Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
166            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_DELETE_0));
167            window.setContent(new CmsPropertyDeleteDialog(data.iterator().next(), window, new Runnable() {
168
169                public void run() {
170
171                    init();
172                }
173            }));
174
175            A_CmsUI.get().addWindow(window);
176        }
177
178        /**
179         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
180         */
181        public String getTitle(Locale locale) {
182
183            return CmsVaadinUtils.getMessageText(Messages.GUI_DATABASEAPP_PROPERTY_DELETE_0);
184        }
185
186        /**
187         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
188         */
189        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
190
191            return (data != null) && (data.size() == 1)
192            ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE
193            : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
194        }
195    }
196
197    /**
198     * Menu entry for showing resources.<p>
199     */
200    class EntryResources
201    implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles {
202
203        /**
204         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
205         */
206        public void executeAction(Set<String> data) {
207
208            showResources(data.iterator().next());
209        }
210
211        /**
212         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles()
213         */
214        public String getStyles() {
215
216            return ValoTheme.LABEL_BOLD;
217        }
218
219        /**
220         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
221         */
222        public String getTitle(Locale locale) {
223
224            return CmsVaadinUtils.getMessageText(Messages.GUI_PQUEUE_RESOURCES_0);
225        }
226
227        /**
228         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
229         */
230        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
231
232            return (data != null) && (data.size() == 1)
233            ? CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE
234            : CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
235        }
236    }
237
238    /**vaadin serial id.*/
239    private static final long serialVersionUID = 8004865379309420561L;
240
241    /**The container.*/
242    private IndexedContainer m_container;
243
244    /** The context menu. */
245    CmsContextMenu m_menu;
246
247    /** The available menu entries. */
248    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
249
250    /**
251     * public constructor.<p>
252     */
253    public CmsPropertyTable() {
254
255        m_menu = new CmsContextMenu();
256        m_menu.setAsTableContextMenu(this);
257
258        init();
259
260        setColumnWidth(null, 40);
261        setSelectable(true);
262        setMultiSelect(false);
263
264    }
265
266    /**
267     * Filters the table according to given search string.<p>
268     *
269     * @param search string to be looked for.
270     */
271    public void filterTable(String search) {
272
273        m_container.removeAllContainerFilters();
274        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) {
275            m_container.addContainerFilter(new Or(new SimpleStringFilter(TableColumn.Name, search, true, false)));
276        }
277    }
278
279    /**
280     * Fills table with items.<p>
281     */
282    public void init() {
283
284        try {
285            List<CmsPropertyDefinition> properties = A_CmsUI.getCmsObject().readAllPropertyDefinitions();
286
287            m_container = new IndexedContainer();
288            for (TableColumn col : TableColumn.values()) {
289                m_container.addContainerProperty(col, col.getType(), col.getDefaultValue());
290            }
291            setContainerDataSource(m_container);
292            setItemIconPropertyId(TableColumn.Icon);
293            setRowHeaderMode(RowHeaderMode.ICON_ONLY);
294
295            setVisibleColumns(TableColumn.Name);
296
297            for (CmsPropertyDefinition prop : properties) {
298                Item item = m_container.addItem(prop);
299                item.getItemProperty(TableColumn.Name).setValue(prop.getName());
300            }
301            addItemClickListener(new ItemClickListener() {
302
303                private static final long serialVersionUID = 4807195510202231174L;
304
305                public void itemClick(ItemClickEvent event) {
306
307                    setValue(null);
308                    select(event.getItemId());
309                    if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
310                        m_menu.setEntries(
311                            getMenuEntries(),
312                            Collections.singleton(((CmsPropertyDefinition)getValue()).getName()));
313                        m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsPropertyTable.this);
314                    } else if (TableColumn.Name.equals(event.getPropertyId())) {
315                        showResources(((CmsPropertyDefinition)getValue()).getName());
316                    }
317
318                }
319
320            });
321
322            setCellStyleGenerator(new CellStyleGenerator() {
323
324                private static final long serialVersionUID = 1L;
325
326                public String getStyle(Table source, Object itemId, Object propertyId) {
327
328                    if (TableColumn.Name.equals(propertyId)) {
329                        return " " + OpenCmsTheme.HOVER_COLUMN;
330                    }
331                    return null;
332                }
333            });
334
335        } catch (CmsException e) {
336            //
337        }
338    }
339
340    /**
341     * Change to source search for showing resources for property definition.<p>
342     *
343     * @param propertyName to search resources for
344     */
345    protected void showResources(String propertyName) {
346
347        CmsSearchReplaceSettings settings = new CmsSearchReplaceSettings();
348        settings.setPaths(Collections.singletonList("/"));
349        settings.setSiteRoot("");
350        settings.setSearchpattern(".*");
351        settings.setType(SearchType.properties);
352        try {
353            settings.setProperty(A_CmsUI.getCmsObject().readPropertyDefinition(propertyName));
354        } catch (CmsException e) {
355            //
356        }
357        CmsAppWorkplaceUi.get().showApp(
358            CmsSourceSearchAppConfiguration.APP_ID,
359            CmsSourceSearchApp.generateState(settings));
360    }
361
362    /**
363     * Returns the available menu entries.<p>
364     *
365     * @return the menu entries
366     */
367    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
368
369        if (m_menuEntries == null) {
370            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
371            m_menuEntries.add(new EntryResources());
372            m_menuEntries.add(new EntryDelete());
373        }
374        return m_menuEntries;
375    }
376}