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.ade.galleries.client.preview.ui;
029
030import org.opencms.ade.galleries.client.preview.CmsFocalPointController;
031
032import com.google.gwt.core.client.GWT;
033import com.google.gwt.dom.client.Style;
034import com.google.gwt.dom.client.Style.Unit;
035import com.google.gwt.event.dom.client.MouseDownEvent;
036import com.google.gwt.event.dom.client.MouseDownHandler;
037import com.google.gwt.uibinder.client.UiBinder;
038import com.google.gwt.user.client.ui.Composite;
039import com.google.gwt.user.client.ui.Widget;
040
041/**
042 * Widget which displays the focal point for an image.<p>
043 */
044public class CmsFocalPoint extends Composite {
045
046    /** The ui-binder for this widget. */
047    interface I_UiBinder extends UiBinder<Widget, CmsFocalPoint> {
048        // GWT interface, nothing to do
049    }
050
051    /** The ui binder instance. */
052    private static I_UiBinder m_uiBinder = GWT.create(I_UiBinder.class);
053
054    /** The controller. */
055    private CmsFocalPointController m_controller;
056
057    /**
058     * Creates a new instance.<p>
059     *
060     * @param controller the controller instance
061     */
062    public CmsFocalPoint(CmsFocalPointController controller) {
063
064        m_controller = controller;
065        initWidget(m_uiBinder.createAndBindUi(this));
066        addDomHandler(new MouseDownHandler() {
067
068            @SuppressWarnings("synthetic-access")
069            public void onMouseDown(MouseDownEvent event) {
070
071                event.preventDefault();
072                m_controller.onStartDrag();
073            }
074        }, MouseDownEvent.getType());
075    }
076
077    /**
078     * Positions the center of this widget over the given coordinates.<p>
079     *
080     * @param x the x coordinate
081     * @param y the y coordinate
082     */
083    public void setCenterCoordsRelativeToParent(int x, int y) {
084
085        Style style = getElement().getStyle();
086        style.setLeft(x - 10, Unit.PX);
087        style.setTop(y - 10, Unit.PX);
088    }
089
090    /**
091     * Sets or clears the 'is default' style on the focal point.<p>
092     *
093     * @param isDefault true if the 'is default' style should be set, false if it should be cleared
094     */
095    public void setIsDefault(boolean isDefault) {
096
097        String style = "imagepointdefault";
098        if (isDefault) {
099            addStyleName(style);
100        } else {
101            removeStyleName(style);
102        }
103    }
104
105}