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