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.CmsAliasMessages;
032import org.opencms.gwt.shared.alias.CmsAliasMode;
033import org.opencms.gwt.shared.alias.CmsAliasTableRow;
034
035import java.util.ArrayList;
036import java.util.Comparator;
037import java.util.List;
038
039import com.google.common.collect.BiMap;
040import com.google.common.collect.HashBiMap;
041import com.google.gwt.cell.client.FieldUpdater;
042import com.google.gwt.cell.client.SelectionCell;
043import com.google.gwt.dom.client.Style.Unit;
044import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
045
046/**
047 * The table column for displaying/editing the alias mode.<p>
048 */
049public class CmsAliasModeColumn extends A_CmsAliasTableColumn<CmsAliasTableRow, String, CmsAliasCellTable> {
050
051    /** A map used to translate between the internal names and the user readable names of the selectable values. */
052    private static BiMap<CmsAliasMode, String> nameLookup = HashBiMap.create();
053
054    /** The table for which this column is used. */
055    CmsAliasCellTable m_table;
056
057    /**
058     * Creates a new column instance.<p>
059     *
060     * @param table the table for which this column is used
061     */
062    public CmsAliasModeColumn(CmsAliasCellTable table) {
063
064        super(new SelectionCell(createOptions()));
065        m_table = table;
066        setSortable(true);
067        FieldUpdater<CmsAliasTableRow, String> updater = new FieldUpdater<CmsAliasTableRow, String>() {
068
069            public void update(int index, CmsAliasTableRow object, String value) {
070
071                m_table.getController().editAliasMode(object, getModeFromName(value));
072            }
073        };
074        setFieldUpdater(updater);
075    }
076
077    static {
078        nameLookup.put(CmsAliasMode.permanentRedirect, CmsAliasMessages.messagePermanentRedirect());
079        nameLookup.put(CmsAliasMode.redirect, CmsAliasMessages.messageRedirect());
080        nameLookup.put(CmsAliasMode.page, CmsAliasMessages.messagePage());
081    }
082
083    /**
084     * Creates the options for the select box.<p>
085     *
086     * @return the options for the select box
087     */
088    public static List<String> createOptions() {
089
090        List<String> result = new ArrayList<String>();
091        result.add(nameLookup.get(CmsAliasMode.permanentRedirect));
092        result.add(nameLookup.get(CmsAliasMode.redirect));
093        result.add(nameLookup.get(CmsAliasMode.page));
094        return result;
095    }
096
097    /**
098     * Gets the comparator used for this column.<p>
099     *
100     * @return the comparator used for this column
101     */
102    public static Comparator<CmsAliasTableRow> getComparator() {
103
104        return new Comparator<CmsAliasTableRow>() {
105
106            public int compare(CmsAliasTableRow o1, CmsAliasTableRow o2) {
107
108                return o1.getMode().toString().compareTo(o2.getMode().toString());
109            }
110        };
111    }
112
113    /**
114     * Translates a user-readable mode name to the internal mode value.<p>
115     *
116     * @param name the user-readable mode name
117     *
118     * @return the internal mode value
119     */
120    static CmsAliasMode getModeFromName(String name) {
121
122        return nameLookup.inverse().get(name);
123    }
124
125    /**
126     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#addToTable(com.google.gwt.user.cellview.client.CellTable)
127     */
128    @Override
129    public void addToTable(CmsAliasCellTable table) {
130
131        table.addColumn(this, CmsAliasMessages.messageColumnMode());
132        table.setColumnWidth(this, 220, Unit.PX);
133    }
134
135    /**
136     * @see com.google.gwt.user.cellview.client.Column#getValue(java.lang.Object)
137     */
138    @Override
139    public String getValue(CmsAliasTableRow object) {
140
141        return nameLookup.get(object.getMode());
142    }
143
144    /**
145     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#initSortHandler(com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler)
146     */
147    @Override
148    public void initSortHandler(ListHandler<CmsAliasTableRow> sortHandler) {
149
150        sortHandler.setComparator(this, getComparator());
151    }
152
153}