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.CmsAliasMessages;
032import org.opencms.gwt.shared.alias.CmsAliasMode;
033import org.opencms.gwt.shared.alias.CmsRewriteAliasTableRow;
034
035import java.util.ArrayList;
036import java.util.Comparator;
037import java.util.List;
038
039import com.google.common.base.Function;
040import com.google.common.collect.BiMap;
041import com.google.common.collect.HashBiMap;
042import com.google.common.collect.Ordering;
043import com.google.gwt.cell.client.FieldUpdater;
044import com.google.gwt.cell.client.SelectionCell;
045import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
046
047/**
048 * The mode column for the rewrite table.<p>
049 */
050public class CmsRewriteAliasModeColumn
051extends A_CmsAliasTableColumn<CmsRewriteAliasTableRow, String, CmsRewriteAliasTable>
052implements FieldUpdater<CmsRewriteAliasTableRow, String> {
053
054    /** Comparator used for sorting this column. */
055    private static Comparator<CmsRewriteAliasTableRow> comparator = Ordering.natural().onResultOf(
056        new Function<CmsRewriteAliasTableRow, String>() {
057
058            @SuppressWarnings("synthetic-access")
059            public String apply(CmsRewriteAliasTableRow row) {
060
061                return optionMapping.get(row.getMode());
062            }
063        });
064
065    /** Mapping from option texts to the real values. */
066    private static BiMap<CmsAliasMode, String> optionMapping = HashBiMap.create();
067
068    /** The rewrite alias table. */
069    private CmsRewriteAliasTable m_table;
070
071    /**
072     * Creates a new column instance.<p>
073     *
074     * @param table the cell table for this column
075     */
076    public CmsRewriteAliasModeColumn(CmsRewriteAliasTable table) {
077
078        super(new SelectionCell(createOptions()));
079        m_table = table;
080        setFieldUpdater(this);
081        setSortable(true);
082
083    }
084
085    static {
086        optionMapping.put(CmsAliasMode.permanentRedirect, CmsAliasMessages.messagePermanentRedirect());
087        optionMapping.put(CmsAliasMode.redirect, CmsAliasMessages.messageRedirect());
088        optionMapping.put(CmsAliasMode.passthrough, CmsAliasMessages.messagePassthrough());
089    }
090
091    /**
092     * Creates the options for the select box.<p>
093     *
094     * @return the list of options
095     */
096    protected static List<String> createOptions() {
097
098        List<String> options = new ArrayList<String>();
099        options.add(optionMapping.get(CmsAliasMode.permanentRedirect));
100        options.add(optionMapping.get(CmsAliasMode.redirect));
101        options.add(optionMapping.get(CmsAliasMode.passthrough));
102        return options;
103    }
104
105    /**
106     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#addToTable(com.google.gwt.user.cellview.client.CellTable)
107     */
108    @Override
109    public void addToTable(CmsRewriteAliasTable table) {
110
111        table.addColumn(this, CmsAliasMessages.messageColumnMode());
112        table.setColumnWidth(this, "220px");
113    }
114
115    /**
116     * @see com.google.gwt.user.cellview.client.Column#getValue(java.lang.Object)
117     */
118    @Override
119    public String getValue(CmsRewriteAliasTableRow row) {
120
121        return optionMapping.get(row.getMode());
122    }
123
124    /**
125     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#initSortHandler(com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler)
126     */
127    @Override
128    public void initSortHandler(ListHandler<CmsRewriteAliasTableRow> sortHandler) {
129
130        sortHandler.setComparator(this, comparator);
131    }
132
133    /**
134     * @see com.google.gwt.cell.client.FieldUpdater#update(int, java.lang.Object, java.lang.Object)
135     */
136    public void update(int index, CmsRewriteAliasTableRow row, String value) {
137
138        CmsAliasMode mode = optionMapping.inverse().get(value);
139        row.setMode(mode);
140        m_table.getController().editRewriteAlias(row);
141    }
142
143}