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.modules;
029
030import org.opencms.main.OpenCms;
031import org.opencms.module.CmsModule;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.FontOpenCms;
035import org.opencms.ui.apps.A_CmsAttributeAwareApp;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
039import org.opencms.ui.components.CmsInfoButton;
040import org.opencms.ui.components.CmsToolBar;
041import org.opencms.ui.contextmenu.CmsContextMenu;
042import org.opencms.ui.util.table.CmsBeanTableBuilder;
043
044import java.util.Collections;
045import java.util.LinkedHashSet;
046import java.util.List;
047import java.util.Map;
048import java.util.Set;
049
050import com.google.common.collect.Lists;
051import com.google.common.collect.Maps;
052import com.vaadin.shared.MouseEventDetails.MouseButton;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Button.ClickEvent;
055import com.vaadin.ui.Button.ClickListener;
056import com.vaadin.ui.Component;
057import com.vaadin.ui.UI;
058import com.vaadin.ui.Window;
059import com.vaadin.ui.themes.ValoTheme;
060import com.vaadin.v7.data.Container;
061import com.vaadin.v7.data.util.BeanItemContainer;
062import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
063import com.vaadin.v7.event.FieldEvents.TextChangeListener;
064import com.vaadin.v7.event.ItemClickEvent;
065import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
066import com.vaadin.v7.ui.HorizontalLayout;
067import com.vaadin.v7.ui.Table;
068import com.vaadin.v7.ui.TextField;
069
070/**
071 * Overview list for module information.<p>
072 *
073 * @param <T> the row type
074 */
075public class CmsModuleTable<T> extends Table {
076
077    /** The serial version id. */
078    private static final long serialVersionUID = 1L;
079
080    /** The module manager app instance. */
081    protected CmsModuleApp m_app;
082
083    /** The table builder. */
084    protected CmsBeanTableBuilder<T> m_tableBuilder;
085
086    /** The row counter label. */
087    private CmsInfoButton m_counter;
088
089    /** The context menu. */
090    protected CmsContextMenu m_menu = new CmsContextMenu();
091
092    /** The search box. */
093    private TextField m_searchBox = new TextField();
094
095    /**
096     * Creates a new instance.<p>
097     *
098     * @param app the module manager app instance.<p>
099     * @param rowType the row type
100     * @param rows the module rows
101     */
102    public CmsModuleTable(CmsModuleApp app, Class<T> rowType, List<T> rows) {
103
104        m_menu.setAsTableContextMenu(this);
105        m_app = app;
106        addItemClickListener(new ItemClickListener() {
107
108            private static final long serialVersionUID = 1L;
109
110            @Override
111            public void itemClick(ItemClickEvent event) {
112
113                CmsModuleTable.this.onItemClick(event);
114            }
115        });
116        m_searchBox.addTextChangeListener(new TextChangeListener() {
117
118            private static final long serialVersionUID = 1L;
119
120            @Override
121            public void textChange(TextChangeEvent event) {
122
123                String filterString = event.getText();
124
125                Container.Filterable container = (Container.Filterable)getContainerDataSource();
126                container.removeAllContainerFilters();
127                container.addContainerFilter(m_tableBuilder.getDefaultFilter(filterString));
128                if ((getValue() != null)) {
129                    setCurrentPageFirstItemId(getValue());
130                }
131            }
132        });
133        m_searchBox.setIcon(FontOpenCms.FILTER);
134        m_searchBox.setInputPrompt(
135            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
136        m_searchBox.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
137        m_searchBox.setWidth("200px");
138
139        Map<String, Object> attributes = Maps.newHashMap();
140
141        HorizontalLayout hl = new HorizontalLayout();
142        hl.setSpacing(true);
143
144        hl.addComponent(m_searchBox);
145        // hl.setComponentAlignment(m_counter, Alignment.MIDDLE_LEFT);
146        attributes.put(A_CmsAttributeAwareApp.ATTR_INFO_COMPONENT, hl);
147        attributes.put(A_CmsAttributeAwareApp.ATTR_MAIN_HEIGHT_FULL, Boolean.TRUE);
148        List<Component> buttons = Lists.newArrayList();
149        Button addModule = CmsToolBar.createButton(
150            FontOpenCms.WAND,
151            CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_BUTTON_NEW_MODULE_0));
152        addModule.addClickListener(new ClickListener() {
153
154            private static final long serialVersionUID = 1L;
155
156            @Override
157            public void buttonClick(ClickEvent event) {
158
159                m_app.editNewModule(CmsModuleTable.this::reload);
160            }
161        });
162        buttons.add(addModule);
163
164        Button importButton = CmsToolBar.createButton(
165            CmsModuleApp.Icons.IMPORT,
166            CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_BUTTON_IMPORT_0));
167        importButton.addClickListener(new ClickListener() {
168
169            private static final long serialVersionUID = 1L;
170
171            @Override
172            public void buttonClick(ClickEvent event) {
173
174                importModule();
175            }
176        });
177        buttons.add(importButton);
178        m_counter = new CmsInfoButton();
179        m_counter.setWindowCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_STATISTICS_0));
180        m_counter.setDescription(CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_STATISTICS_0));
181        buttons.add(m_counter);
182        attributes.put(CmsModuleApp.Attributes.BUTTONS, buttons);
183        setData(attributes);
184        CmsBeanTableBuilder<T> builder = CmsBeanTableBuilder.newInstance(rowType);
185        m_tableBuilder = builder;
186        builder.buildTable(this, rows);
187        setCellStyleGenerator(builder.getDefaultCellStyleGenerator());
188        setItemIconPropertyId("icon");
189        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
190        setSelectable(true);
191        setMultiSelect(false);
192        sort(new Object[] {"name"}, new boolean[] {true});
193        updateCounter();
194    }
195
196    /**
197     * Opens the import module dialog.<p>
198     */
199    public void importModule() {
200
201        Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
202        CmsImportTabForm form = new CmsImportTabForm(m_app, this::reload);
203        window.setContent(form);
204        window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_IMPORT_TITLE_0));
205        A_CmsUI.get().addWindow(window);
206        window.center();
207    }
208
209    /**
210     * Reloads the table data.<p>
211     */
212    public void reload() {
213
214        List<CmsModule> modules = OpenCms.getModuleManager().getAllInstalledModules();
215        @SuppressWarnings("unchecked")
216        BeanItemContainer<CmsModuleRow> container = (BeanItemContainer<CmsModuleRow>)getContainerDataSource();
217        container.removeAllItems();
218        List<CmsModuleRow> newRows = Lists.newArrayList();
219        for (CmsModule module : modules) {
220            CmsModuleRow row = new CmsModuleRow(module);
221            newRows.add(row);
222        }
223        container.addAll(newRows);
224        sort();
225        updateCounter();
226    }
227
228    /**
229     * Handles the table item clicks.<p>
230     *
231     * @param event the click event
232     */
233    protected void onItemClick(ItemClickEvent event) {
234
235        if (!event.isCtrlKey() && !event.isShiftKey()) {
236
237            Set<String> nameSet = new LinkedHashSet<String>();
238
239            CmsModuleRow moduleRow = (CmsModuleRow)(event.getItemId());
240            select(moduleRow);
241            nameSet.add(moduleRow.getModule().getName());
242            if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
243                select(moduleRow);
244                m_menu.setEntries(m_app.getMenuEntries(), nameSet);
245                m_menu.openForTable(event, this);
246            } else if (event.getButton().equals(MouseButton.LEFT) && "name".equals(event.getPropertyId())) {
247
248                m_app.openModuleInfo(nameSet);
249            }
250
251        }
252    }
253
254    /**
255     * Updates the row counter.<p>
256     */
257    private void updateCounter() {
258
259        m_counter.replaceData(
260            Collections.singletonMap(
261                CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_STATISTICS_ROW_COUNT_0),
262                String.valueOf(getContainerDataSource().size())));
263    }
264
265}