001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (C) Alkacon Software (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.alias.simple;
029
030import org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn;
031import org.opencms.ade.sitemap.client.alias.CmsAliasTableController;
032import org.opencms.ade.sitemap.client.alias.CmsCellTableUtil;
033import org.opencms.gwt.client.ui.css.I_CmsCellTableResources;
034import org.opencms.gwt.shared.alias.CmsAliasTableRow;
035
036import java.util.ArrayList;
037import java.util.List;
038
039import com.google.gwt.core.client.GWT;
040import com.google.gwt.user.cellview.client.CellTable;
041import com.google.gwt.user.cellview.client.ColumnSortEvent;
042import com.google.gwt.view.client.DefaultSelectionEventManager;
043import com.google.gwt.view.client.ListDataProvider;
044import com.google.gwt.view.client.MultiSelectionModel;
045import com.google.gwt.view.client.ProvidesKey;
046import com.google.gwt.view.client.SelectionChangeEvent;
047
048/**
049 * The cell table which is the main widget used for the bulk alias editor.<p>
050 */
051public class CmsAliasCellTable extends CellTable<CmsAliasTableRow> {
052
053    /** The alias controller. */
054    CmsAliasTableController m_controller;
055
056    /** The data provider. */
057    private ListDataProvider<CmsAliasTableRow> m_dataProvider;
058
059    /** The error column. */
060    private CmsAliasErrorColumn m_errorColumn;
061
062    /**
063     * Creates a new cell table with the given controller.<p>
064     *
065     * @param controller the alias editor controller which should be used by the cell table
066     */
067    public CmsAliasCellTable(CmsAliasTableController controller) {
068
069        super(
070            Integer.MAX_VALUE,
071            (CellTable.Resources)GWT.create(I_CmsCellTableResources.class),
072            new ProvidesKey<CmsAliasTableRow>() {
073
074                public Object getKey(CmsAliasTableRow item) {
075
076                    return item.getKey();
077                }
078
079            });
080        m_controller = controller;
081        m_errorColumn = new CmsAliasErrorColumn();
082        @SuppressWarnings("unchecked")
083        A_CmsAliasTableColumn<CmsAliasTableRow, ?, CmsAliasCellTable>[] columns = new A_CmsAliasTableColumn[] {
084            new CmsAliasSelectionColumn(this),
085            new CmsAliasPathColumn(this),
086            new CmsResourcePathColumn(this),
087            new CmsAliasModeColumn(this),
088            m_errorColumn};
089        m_dataProvider = new ListDataProvider<CmsAliasTableRow>();
090        m_dataProvider.addDataDisplay(this);
091        ColumnSortEvent.ListHandler<CmsAliasTableRow> sortHandler = new ColumnSortEvent.ListHandler<CmsAliasTableRow>(
092            m_dataProvider.getList());
093        for (A_CmsAliasTableColumn<CmsAliasTableRow, ?, CmsAliasCellTable> column : columns) {
094            column.initSortHandler(sortHandler);
095        }
096        addColumnSortHandler(sortHandler);
097
098        final MultiSelectionModel<CmsAliasTableRow> selectionModel = new MultiSelectionModel<CmsAliasTableRow>(
099            getKeyProvider());
100        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
101
102            public void onSelectionChange(SelectionChangeEvent event) {
103
104                m_controller.changeSelection(selectionModel.getSelectedSet());
105            }
106        });
107        setSelectionModel(selectionModel, DefaultSelectionEventManager.<CmsAliasTableRow> createCheckboxManager());
108        for (A_CmsAliasTableColumn<?, ?, CmsAliasCellTable> column : columns) {
109            column.addToTable(this);
110        }
111        CmsCellTableUtil.ensureCellTableParentResize(this);
112    }
113
114    /**
115     * Gets the alias editor controller used by this table.<p>
116     *
117     * @return the alias editor controller
118     */
119    public CmsAliasTableController getController() {
120
121        return m_controller;
122    }
123
124    /**
125     * Gets the error column.<p>
126     *
127     * @return the error column
128     */
129    public CmsAliasErrorColumn getErrorColumn() {
130
131        return m_errorColumn;
132    }
133
134    /**
135     * Gets the list of rows internally used by the data provider for this table.<p>
136     *
137     * @return the internal data list
138     */
139    public List<CmsAliasTableRow> getLiveDataList() {
140
141        return m_dataProvider.getList();
142    }
143
144    /**
145     * Gets the list of selected rows.<p>
146     *
147     * @return the list of selected rows
148     */
149    public List<CmsAliasTableRow> getSelectedRows() {
150
151        return new ArrayList<CmsAliasTableRow>(getSelectionModel().getSelectedSet());
152
153    }
154
155    /**
156     * @see com.google.gwt.user.cellview.client.AbstractHasData#getSelectionModel()
157     *
158     * (Overridden to use a less general return type)
159     */
160    @SuppressWarnings("unchecked")
161    @Override
162    public MultiSelectionModel<CmsAliasTableRow> getSelectionModel() {
163
164        return (MultiSelectionModel<CmsAliasTableRow>)(super.getSelectionModel());
165    }
166}