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.rewrite;
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.CmsRewriteAliasTableRow;
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 * Table class used for editing rewrite aliases.<p>
050 */
051public class CmsRewriteAliasTable extends CellTable<CmsRewriteAliasTableRow> {
052
053    /** The controller class for the alias dialog. */
054    private CmsAliasTableController m_controller;
055
056    /** The list data provider for this table. */
057    private ListDataProvider<CmsRewriteAliasTableRow> m_dataProvider;
058
059    /**
060     * Creates a new table instance.<p>
061     *
062     * @param controller the controller instance for the alias table view
063     */
064    public CmsRewriteAliasTable(CmsAliasTableController controller) {
065
066        super(
067            Integer.MAX_VALUE,
068            (CellTable.Resources)GWT.create(I_CmsCellTableResources.class),
069            new ProvidesKey<CmsRewriteAliasTableRow>() {
070
071                public Object getKey(CmsRewriteAliasTableRow item) {
072
073                    return item.getId();
074                }
075
076            });
077        m_controller = controller;
078        @SuppressWarnings("unchecked")
079        A_CmsAliasTableColumn<CmsRewriteAliasTableRow, ?, CmsRewriteAliasTable>[] columns = new A_CmsAliasTableColumn[] {
080            new CmsRewriteAliasSelectColumn(this),
081            new CmsRewriteAliasPatternColumn(this),
082            new CmsRewriteAliasReplacementColumn(this),
083            new CmsRewriteAliasModeColumn(this),
084            new CmsRewriteAliasErrorColumn()};
085        m_dataProvider = new ListDataProvider<CmsRewriteAliasTableRow>();
086        m_dataProvider.addDataDisplay(this);
087        ColumnSortEvent.ListHandler<CmsRewriteAliasTableRow> sortHandler = new ColumnSortEvent.ListHandler<CmsRewriteAliasTableRow>(
088            m_dataProvider.getList());
089        for (A_CmsAliasTableColumn<CmsRewriteAliasTableRow, ?, CmsRewriteAliasTable> column : columns) {
090            column.initSortHandler(sortHandler);
091        }
092        addColumnSortHandler(sortHandler);
093
094        final MultiSelectionModel<CmsRewriteAliasTableRow> selectionModel = new MultiSelectionModel<CmsRewriteAliasTableRow>(
095            getKeyProvider());
096        selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
097
098            public void onSelectionChange(SelectionChangeEvent event) {
099
100                getController().changeRewriteSelection(selectionModel.getSelectedSet());
101            }
102        });
103        setSelectionModel(
104            selectionModel,
105            DefaultSelectionEventManager.<CmsRewriteAliasTableRow> createCheckboxManager());
106        for (A_CmsAliasTableColumn<CmsRewriteAliasTableRow, ?, CmsRewriteAliasTable> column : columns) {
107            column.addToTable(this);
108        }
109        CmsCellTableUtil.ensureCellTableParentResize(this);
110
111    }
112
113    /**
114     * Gets the list of rows internally used by the data provider for this table.<p>
115     *
116     * @return the internal data list
117     */
118    public List<CmsRewriteAliasTableRow> getLiveDataList() {
119
120        return m_dataProvider.getList();
121    }
122
123    /**
124     * Gets the list of selected rows.<p>
125     *
126     * @return the list of selected rows
127     */
128    public List<CmsRewriteAliasTableRow> getSelectedRows() {
129
130        return new ArrayList<CmsRewriteAliasTableRow>(getSelectionModel().getSelectedSet());
131
132    }
133
134    /**
135     * @see com.google.gwt.user.cellview.client.AbstractHasData#getSelectionModel()
136     *
137     * (Overridden to use a less general return type)
138     */
139    @SuppressWarnings("unchecked")
140    @Override
141    public MultiSelectionModel<CmsRewriteAliasTableRow> getSelectionModel() {
142
143        return (MultiSelectionModel<CmsRewriteAliasTableRow>)(super.getSelectionModel());
144    }
145
146    /**
147     * Gets the controller instance for the alias view.<p>
148     *
149     * @return the controller instance for the alias view
150     **/
151    CmsAliasTableController getController() {
152
153        return m_controller;
154    }
155
156}