001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (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.gwt.client.ui.input;
029
030import org.opencms.gwt.client.util.CmsDomUtil;
031
032import com.google.gwt.dom.client.Element;
033import com.google.gwt.dom.client.Style.Position;
034import com.google.gwt.event.dom.client.DomEvent;
035import com.google.gwt.event.shared.EventHandler;
036import com.google.gwt.event.shared.HandlerRegistration;
037import com.google.gwt.user.client.ui.Composite;
038import com.google.gwt.user.client.ui.RootPanel;
039
040/**
041 * An abstract class for widgets which represent options for select boxes.<p>
042 *
043 * @since 8.0.0
044 *
045 */
046public abstract class A_CmsSelectCell extends Composite {
047
048    /**
049     * Measures the required width for this cell.<p>
050     *
051     * @return the required width
052     */
053    public int getRequiredWidth() {
054
055        Element clone = CmsDomUtil.clone(getElement());
056        clone.getStyle().setPosition(Position.ABSOLUTE);
057        RootPanel.getBodyElement().appendChild(clone);
058        int result = clone.getOffsetWidth();
059        clone.removeFromParent();
060        return result;
061    }
062
063    /**
064     * Returns the value of the select option as a string.<p>
065     *
066     * @return the value of the select option
067     */
068    public abstract String getValue();
069
070    /**
071     * Adds a new event handler to the widget.<p>
072     *
073     * This method is used because we want the select box to register some event handlers on this widget,
074     * but we can't use {@link com.google.gwt.user.client.ui.Widget#addDomHandler} directly, since it's both protected
075     * and final.
076     *
077     * @param <H> the event type
078     * @param handler the new event handler
079     * @param type the event type object
080     *
081     * @return the HandlerRegistration for removing the event handler
082     */
083    public <H extends EventHandler> HandlerRegistration registerDomHandler(final H handler, DomEvent.Type<H> type) {
084
085        return addDomHandler(handler, type);
086    }
087
088}