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.acacia.client;
029
030import com.google.gwt.dom.client.Element;
031import com.google.gwt.user.client.DOM;
032import com.google.gwt.user.client.Event;
033import com.google.gwt.user.client.ui.ComplexPanel;
034import com.google.gwt.user.client.ui.IsWidget;
035import com.google.gwt.user.client.ui.RootPanel;
036import com.google.gwt.user.client.ui.Widget;
037
038/**
039 * Inline form parent widget.<p>
040 * Use to wrap any HTML element of the DOM as the parent of an inline form.<p>
041 */
042public class CmsFormParent extends ComplexPanel implements I_CmsInlineFormParent {
043
044    /** The wrapped widget. This will be a @link com.google.gwt.user.client.RootPanel. */
045    private Widget m_widget;
046
047    /**
048     * Constructor.<p>
049     *
050     * @param rootPanel the root panel to wrap
051     */
052    public CmsFormParent(RootPanel rootPanel) {
053
054        initWidget(rootPanel);
055    }
056
057    /**
058     * @see org.opencms.acacia.client.I_CmsInlineFormParent#adoptWidget(com.google.gwt.user.client.ui.IsWidget)
059     */
060    public void adoptWidget(IsWidget widget) {
061
062        getChildren().add(widget.asWidget());
063        adopt(widget.asWidget());
064    }
065
066    /**
067     * @see com.google.gwt.user.client.ui.Widget#isAttached()
068     */
069    @Override
070    public boolean isAttached() {
071
072        if (m_widget != null) {
073            return m_widget.isAttached();
074        }
075        return false;
076    }
077
078    /**
079     * @see com.google.gwt.user.client.ui.Widget#onBrowserEvent(com.google.gwt.user.client.Event)
080     */
081    @Override
082    public void onBrowserEvent(Event event) {
083
084        // Fire any handler added to the composite itself.
085        super.onBrowserEvent(event);
086
087        // Delegate events to the widget.
088        m_widget.onBrowserEvent(event);
089    }
090
091    /**
092     * @see org.opencms.acacia.client.I_CmsInlineFormParent#replaceHtml(java.lang.String)
093     */
094    public void replaceHtml(String html) {
095
096        // detach all children first
097        while (getChildren().size() > 0) {
098            getChildren().get(getChildren().size() - 1).removeFromParent();
099        }
100        Element tempDiv = DOM.createDiv();
101        tempDiv.setInnerHTML(html);
102        getElement().setInnerHTML(tempDiv.getFirstChildElement().getInnerHTML());
103    }
104
105    /**
106     * Provides subclasses access to the topmost widget that defines this
107     * panel.
108     *
109     * @return the widget
110     */
111    protected Widget getWidget() {
112
113        return m_widget;
114    }
115
116    /**
117     * Sets the widget to be wrapped by the composite. The wrapped widget must be
118     * set before calling any {@link Widget} methods on this object, or adding it
119     * to a panel. This method may only be called once for a given composite.
120     *
121     * @param widget the widget to be wrapped
122     */
123    protected void initWidget(Widget widget) {
124
125        // Validate. Make sure the widget is not being set twice.
126        if (m_widget != null) {
127            throw new IllegalStateException("Composite.initWidget() may only be " + "called once.");
128        }
129
130        // Use the contained widget's element as the composite's element,
131        // effectively merging them within the DOM.
132        setElement((Element)widget.getElement());
133
134        adopt(widget);
135
136        // Logical attach.
137        m_widget = widget;
138    }
139}