Home | History | Annotate | Download | only in gle2
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.eclipse.org/org/documents/epl-v10.php
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2;
     18 
     19 import org.eclipse.swt.dnd.DragSourceEvent;
     20 import org.eclipse.swt.dnd.DragSourceListener;
     21 import org.eclipse.swt.dnd.DropTargetEvent;
     22 import org.eclipse.swt.events.MenuDetectEvent;
     23 import org.eclipse.swt.events.MouseEvent;
     24 import org.eclipse.swt.events.MouseListener;
     25 import org.eclipse.swt.graphics.Point;
     26 
     27 /**
     28  * A {@link ControlPoint} is a coordinate in the canvas control which corresponds
     29  * exactly to (0,0) at the top left of the canvas. It is unaffected by canvas
     30  * zooming.
     31  */
     32 public final class ControlPoint {
     33     /** Containing canvas which the point is relative to. */
     34     private final LayoutCanvas mCanvas;
     35 
     36     /** The X coordinate of the mouse coordinate. */
     37     public final int x;
     38 
     39     /** The Y coordinate of the mouse coordinate. */
     40     public final int y;
     41 
     42     /**
     43      * Constructs a new {@link ControlPoint} from the given event. The event
     44      * must be from a {@link MouseListener} associated with the
     45      * {@link LayoutCanvas} such that the {@link MouseEvent#x} and
     46      * {@link MouseEvent#y} fields are relative to the canvas.
     47      *
     48      * @param canvas The {@link LayoutCanvas} this point is within.
     49      * @param event The mouse event to construct the {@link ControlPoint}
     50      *            from.
     51      * @return A {@link ControlPoint} which corresponds to the given
     52      *         {@link MouseEvent}.
     53      */
     54     public static ControlPoint create(LayoutCanvas canvas, MouseEvent event) {
     55         // The mouse event coordinates should already be relative to the canvas
     56         // widget.
     57         assert event.widget == canvas : event.widget;
     58         return new ControlPoint(canvas, event.x, event.y);
     59     }
     60 
     61     /**
     62      * Constructs a new {@link ControlPoint} from the given menu detect event.
     63      *
     64      * @param canvas The {@link LayoutCanvas} this point is within.
     65      * @param event The menu detect event to construct the {@link ControlPoint} from.
     66      * @return A {@link ControlPoint} which corresponds to the given
     67      *         {@link MenuDetectEvent}.
     68      */
     69     public static ControlPoint create(LayoutCanvas canvas, MenuDetectEvent event) {
     70         // The menu detect events are always display-relative.
     71         org.eclipse.swt.graphics.Point p = canvas.toControl(event.x, event.y);
     72         return new ControlPoint(canvas, p.x, p.y);
     73     }
     74 
     75     /**
     76      * Constructs a new {@link ControlPoint} from the given event. The event
     77      * must be from a {@link DragSourceListener} associated with the
     78      * {@link LayoutCanvas} such that the {@link DragSourceEvent#x} and
     79      * {@link DragSourceEvent#y} fields are relative to the canvas.
     80      *
     81      * @param canvas The {@link LayoutCanvas} this point is within.
     82      * @param event The mouse event to construct the {@link ControlPoint}
     83      *            from.
     84      * @return A {@link ControlPoint} which corresponds to the given
     85      *         {@link DragSourceEvent}.
     86      */
     87     public static ControlPoint create(LayoutCanvas canvas, DragSourceEvent event) {
     88         // The drag source event coordinates should already be relative to the
     89         // canvas widget.
     90         return new ControlPoint(canvas, event.x, event.y);
     91     }
     92 
     93     /**
     94      * Constructs a new {@link ControlPoint} from the given event.
     95      *
     96      * @param canvas The {@link LayoutCanvas} this point is within.
     97      * @param event The mouse event to construct the {@link ControlPoint}
     98      *            from.
     99      * @return A {@link ControlPoint} which corresponds to the given
    100      *         {@link DropTargetEvent}.
    101      */
    102     public static ControlPoint create(LayoutCanvas canvas, DropTargetEvent event) {
    103         // The drop target events are always relative to the display, so we must
    104         // first convert them to be canvas relative.
    105         org.eclipse.swt.graphics.Point p = canvas.toControl(event.x, event.y);
    106         return new ControlPoint(canvas, p.x, p.y);
    107     }
    108 
    109     /**
    110      * Constructs a new {@link ControlPoint} from the given x,y coordinates,
    111      * which must be relative to the given {@link LayoutCanvas}.
    112      *
    113      * @param canvas The {@link LayoutCanvas} this point is within.
    114      * @param x The mouse event x coordinate relative to the canvas
    115      * @param y The mouse event x coordinate relative to the canvas
    116      * @return A {@link ControlPoint} which corresponds to the given
    117      *         coordinates.
    118      */
    119     public static ControlPoint create(LayoutCanvas canvas, int x, int y) {
    120         return new ControlPoint(canvas, x, y);
    121     }
    122 
    123     /**
    124      * Constructs a new canvas control coordinate with the given X and Y
    125      * coordinates. This is private; use one of the factory methods
    126      * {@link #create(LayoutCanvas, MouseEvent)},
    127      * {@link #create(LayoutCanvas, DragSourceEvent)} or
    128      * {@link #create(LayoutCanvas, DropTargetEvent)} instead.
    129      *
    130      * @param canvas The canvas which contains this coordinate
    131      * @param x The mouse x coordinate
    132      * @param y The mouse y coordinate
    133      */
    134     private ControlPoint(LayoutCanvas canvas, int x, int y) {
    135         mCanvas = canvas;
    136         this.x = x;
    137         this.y = y;
    138     }
    139 
    140     /**
    141      * Returns the equivalent {@link LayoutPoint} to this
    142      * {@link ControlPoint}.
    143      *
    144      * @return The equivalent {@link LayoutPoint} to this
    145      *         {@link ControlPoint}.
    146      */
    147     public LayoutPoint toLayout() {
    148         int lx = mCanvas.getHorizontalTransform().inverseTranslate(x);
    149         int ly = mCanvas.getVerticalTransform().inverseTranslate(y);
    150 
    151         return LayoutPoint.create(mCanvas, lx, ly);
    152     }
    153 
    154     @Override
    155     public String toString() {
    156         return "ControlPoint [x=" + x + ", y=" + y + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    157     }
    158 
    159     @Override
    160     public int hashCode() {
    161         final int prime = 31;
    162         int result = 1;
    163         result = prime * result + x;
    164         result = prime * result + y;
    165         return result;
    166     }
    167 
    168     @Override
    169     public boolean equals(Object obj) {
    170         if (this == obj)
    171             return true;
    172         if (obj == null)
    173             return false;
    174         if (getClass() != obj.getClass())
    175             return false;
    176         ControlPoint other = (ControlPoint) obj;
    177         if (x != other.x)
    178             return false;
    179         if (y != other.y)
    180             return false;
    181         if (mCanvas != other.mCanvas) {
    182             return false;
    183         }
    184         return true;
    185     }
    186 
    187     /**
    188      * Returns this point as an SWT point in the display coordinate system
    189      *
    190      * @return this point as an SWT point in the display coordinate system
    191      */
    192     public Point toDisplayPoint() {
    193         return mCanvas.toDisplay(x, y);
    194     }
    195 }
    196