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 org.opencms.gwt.client.CmsCoreProvider;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
033import org.opencms.gwt.client.util.CmsEmbeddedDialogHandler;
034import org.opencms.gwt.shared.CmsGwtConstants;
035import org.opencms.util.CmsUUID;
036
037import java.util.Collections;
038import java.util.HashMap;
039import java.util.Map;
040
041import com.google.gwt.event.dom.client.ClickEvent;
042import com.google.gwt.event.dom.client.ClickHandler;
043import com.google.gwt.user.client.Command;
044
045/**
046 * The user info toolbar button.<p>
047 */
048public class CmsUserInfo extends A_CmsToolbarButton<I_CmsToolbarHandler> {
049
050    /** The embedded dialog handler. */
051    private CmsEmbeddedDialogHandler m_dialogHandler;
052
053    /**
054     * Constructor.<p>
055     */
056    public CmsUserInfo() {
057        super(null, null);
058        addStyleName(I_CmsLayoutBundle.INSTANCE.toolbarCss().userInfo());
059        getUpFace().setHTML("<img src=\"" + CmsCoreProvider.get().getUserInfo().getUserIcon() + "\" />");
060        setTitle(Messages.get().key(Messages.GUI_USER_INFO_0));
061        m_dialogHandler = new CmsEmbeddedDialogHandler();
062        addClickHandler(new ClickHandler() {
063
064            /**
065             * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
066             */
067            @Override
068            public void onClick(ClickEvent event) {
069
070                onToolbarClick();
071            }
072        });
073    }
074
075    /**
076     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#isActive()
077     */
078    @Override
079    public boolean isActive() {
080
081        return m_dialogHandler.hasDialogFrame();
082    }
083
084    /**
085     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarActivate()
086     */
087    @Override
088    public void onToolbarActivate() {
089
090        Map<String, String> params = new HashMap<String, String>();
091        params.put("left", String.valueOf(getElement().getAbsoluteLeft()));
092        m_dialogHandler.setOnCloseCommand(new Command() {
093
094            public void execute() {
095
096                onClose();
097            }
098        });
099        m_dialogHandler.openDialog(
100            "org.opencms.ui.actions.CmsUserInfoDialogAction",
101            CmsGwtConstants.CONTEXT_TYPE_APP_TOOLBAR,
102            Collections.<CmsUUID> emptyList(),
103            params);
104    }
105
106    /**
107     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarClick()
108     */
109    @Override
110    public void onToolbarClick() {
111
112        boolean active = isActive();
113
114        setActive(!active);
115
116    }
117
118    /**
119     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#onToolbarDeactivate()
120     */
121    @Override
122    public void onToolbarDeactivate() {
123
124        m_dialogHandler.setOnCloseCommand(null);
125        m_dialogHandler.finish(null);
126    }
127
128    /**
129     * @see org.opencms.gwt.client.ui.I_CmsToolbarButton#setActive(boolean)
130     */
131    @Override
132    public void setActive(boolean active) {
133
134        if (active) {
135            if (m_handler != null) {
136                m_handler.deactivateCurrentButton();
137                m_handler.setActiveButton(this);
138            }
139            onToolbarActivate();
140
141        } else {
142            onToolbarDeactivate();
143
144            if (m_handler != null) {
145                m_handler.setActiveButton(null);
146                m_handler.activateSelection();
147            }
148        }
149    }
150
151    /**
152     * Sets the button handler.<p>
153     *
154     * @param handler the button handler
155     */
156    public void setHandler(I_CmsToolbarHandler handler) {
157
158        m_handler = handler;
159    }
160
161    /**
162     * Returns the container-page handler.<p>
163     *
164     * @return the container-page handler
165     */
166    @Override
167    protected I_CmsToolbarHandler getHandler() {
168
169        return m_handler;
170    }
171
172    /**
173     * Executed on window close.<p>
174     */
175    protected void onClose() {
176
177        onToolbarDeactivate();
178        if (m_handler != null) {
179            m_handler.setActiveButton(null);
180            m_handler.activateSelection();
181        }
182    }
183}