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;
029
030import java.util.List;
031
032import com.google.gwt.core.client.GWT;
033import com.google.gwt.dom.client.Style;
034import com.google.gwt.uibinder.client.UiBinder;
035import com.google.gwt.uibinder.client.UiField;
036import com.google.gwt.user.client.ui.Composite;
037import com.google.gwt.user.client.ui.FlowPanel;
038import com.google.gwt.user.client.ui.Widget;
039
040/**
041 * A panel containing two sub-panels next to each other , one for 'decorations' (check boxes, etc.) and one containing a main widget.<p>
042 *
043 * This widget does not calculate the width of the decoration panel automatically. You have to pass the appropriate width
044 * as a parameter to the constructor.
045 *
046 * @since 8.0.0
047 *
048 */
049public class CmsSimpleDecoratedPanel extends Composite implements I_CmsTruncable {
050
051    /**
052     * @see com.google.gwt.uibinder.client.UiBinder
053     */
054    protected interface I_CmsSimpleDecoratedPanelUiBinder extends UiBinder<Widget, CmsSimpleDecoratedPanel> {
055        // GWT interface, nothing to do here
056    }
057
058    /** The ui-binder instance for this class. */
059    private static I_CmsSimpleDecoratedPanelUiBinder uiBinder = GWT.create(I_CmsSimpleDecoratedPanelUiBinder.class);
060
061    /** The float panel. */
062    @UiField
063    protected FlowPanel m_decorationBox = new FlowPanel();
064
065    /** The panel containing both the main and float panel. */
066    @UiField
067    protected FlowPanel m_panel = new FlowPanel();
068
069    /** The main panel. */
070    @UiField
071    protected FlowPanel m_primary = new FlowPanel();
072
073    /** The width of the decoration box. */
074    private int m_decorationWidth;
075
076    /**
077     * Creates a new instance of this widget.<p>
078     *
079     * @param decorationWidth the width which the decoration box should have
080     * @param mainWidget the main widget
081     * @param decoration the list of decoration widgets (from left to right)
082     */
083    public CmsSimpleDecoratedPanel(int decorationWidth, Widget mainWidget, List<Widget> decoration) {
084
085        initWidget(uiBinder.createAndBindUi(this));
086        for (Widget widget : decoration) {
087            m_decorationBox.add(widget);
088        }
089        if (mainWidget != null) {
090            m_primary.add(mainWidget);
091        }
092        m_decorationWidth = decorationWidth;
093        init();
094    }
095
096    /**
097     * Adds a style name to the decoration box.<p>
098     *
099     * @param cssClass the CSS class to add
100     */
101    public void addDecorationBoxStyle(String cssClass) {
102
103        m_decorationBox.addStyleName(cssClass);
104    }
105
106    /**
107     * Returns the widget at the given position.<p>
108     *
109     * @param index the position
110     *
111     * @return  the widget at the given position
112     */
113    public Widget getWidget(int index) {
114
115        return m_primary.getWidget(index);
116    }
117
118    /**
119     * @see org.opencms.gwt.client.ui.I_CmsTruncable#truncate(java.lang.String, int)
120     */
121    public void truncate(String textMetricsPrefix, int widgetWidth) {
122
123        int width = widgetWidth;
124        width -= getDecorationWidth();
125        for (Widget widget : m_primary) {
126            if (widget instanceof I_CmsTruncable) {
127                ((I_CmsTruncable)widget).truncate(textMetricsPrefix, width);
128            }
129        }
130    }
131
132    /**
133     * Returns the width of the decoration box.<p>
134     *
135     * @return the width of the decoration box
136     */
137    private int getDecorationWidth() {
138
139        return m_decorationWidth;
140    }
141
142    /**
143     * Internal helper method for initializing the layout of this widget.<p>
144     */
145    private void init() {
146
147        int decorationWidth = getDecorationWidth();
148        m_decorationBox.setWidth(decorationWidth + "px");
149        m_primary.getElement().getStyle().setMarginLeft(decorationWidth, Style.Unit.PX);
150    }
151
152}