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.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.Comparator;
037
038import com.google.gwt.cell.client.SafeHtmlCell;
039import com.google.gwt.dom.client.Style.Unit;
040import com.google.gwt.safehtml.shared.SafeHtml;
041import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
042
043/**
044 * The class for the column of the alias editor table which is used to display validation errors.<p>
045 */
046public class CmsAliasErrorColumn extends A_CmsAliasTableColumn<CmsAliasTableRow, SafeHtml, CmsAliasCellTable> {
047
048    /**
049     * Creates a new instance.<p>
050     */
051    public CmsAliasErrorColumn() {
052
053        super(new SafeHtmlCell());
054        setSortable(true);
055    }
056
057    /**
058     * Gets the comparator which should be used for this column.<p>
059     *
060     * @return the comparator used for this column
061     */
062    public static Comparator<CmsAliasTableRow> getComparator() {
063
064        return new Comparator<CmsAliasTableRow>() {
065
066            public int compare(CmsAliasTableRow o1, CmsAliasTableRow o2) {
067
068                String err1 = getValueInternal(o1);
069                String err2 = getValueInternal(o2);
070                if ((err1 == null) && (err2 == null)) {
071                    return 0;
072                }
073                if (err1 == null) {
074                    return -1;
075                }
076                if (err2 == null) {
077                    return 1;
078                }
079                return 0;
080            }
081        };
082    }
083
084    /**
085     * Static helper method to get the value to display in the column from a row.<p>
086     *
087     * @param row the row
088     *
089     * @return the value to display
090     */
091    protected static String getValueInternal(CmsAliasTableRow row) {
092
093        if (row.getAliasError() != null) {
094            return row.getAliasError();
095        }
096        if (row.getPathError() != null) {
097            return row.getPathError();
098        }
099        return null;
100    }
101
102    /**
103     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#addToTable(com.google.gwt.user.cellview.client.CellTable)
104     */
105    @Override
106    public void addToTable(CmsAliasCellTable table) {
107
108        table.addColumn(this, CmsAliasMessages.messageColumnError());
109        table.setColumnWidth(this, 50, Unit.PX);
110    }
111
112    /**
113     * @see com.google.gwt.user.cellview.client.Column#getCellStyleNames(com.google.gwt.cell.client.Cell.Context, java.lang.Object)
114     */
115    @Override
116    public String getCellStyleNames(com.google.gwt.cell.client.Cell.Context context, CmsAliasTableRow object) {
117
118        if ((object.getAliasError() != null) || (object.getPathError() != null)) {
119            return super.getCellStyleNames(context, object)
120                + " "
121                + I_CmsCellTableResources.INSTANCE.cellTableStyle().cmsCellError();
122        } else {
123            return super.getCellStyleNames(context, object);
124        }
125    }
126
127    /**
128     * @see com.google.gwt.user.cellview.client.Column#getValue(java.lang.Object)
129     */
130    @Override
131    public SafeHtml getValue(CmsAliasTableRow row) {
132
133        String internalValue = getValueInternal(row);
134        return CmsCellTableUtil.formatErrorHtml(internalValue);
135    }
136
137    /**
138     * @see org.opencms.ade.sitemap.client.alias.A_CmsAliasTableColumn#initSortHandler(com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler)
139     */
140    @Override
141    public void initSortHandler(ListHandler<CmsAliasTableRow> sortHandler) {
142
143        sortHandler.setComparator(this, getComparator());
144    }
145
146}