notnull
[idea/community.git] / platform / util / src / com / intellij / ui / awt / RelativePoint.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 package com.intellij.ui.awt;
17
18 import org.jetbrains.annotations.NotNull;
19 import org.jetbrains.annotations.Nullable;
20
21 import javax.swing.*;
22 import java.awt.*;
23 import java.awt.event.MouseEvent;
24
25 public class RelativePoint {
26
27   private Component myComponent;
28   private Point myPointOnComponent;
29
30   private Component myOriginalComponent;
31   private Point myOriginalPoint;
32
33   public RelativePoint(@NotNull MouseEvent event) {
34     init(event.getComponent(), event.getPoint());
35
36     myOriginalComponent = event.getComponent();
37     myOriginalPoint = event.getPoint();
38   }
39
40   public RelativePoint(@NotNull Component aComponent, Point aPointOnComponent) {
41     init(aComponent, aPointOnComponent);
42   }
43
44   public RelativePoint(@NotNull Point screenPoint) {
45     Point p = new Point(screenPoint.x, screenPoint.y);
46     Window[] windows = Window.getWindows();
47     Window targetWindow = null;
48     for (Window each : windows) {
49       if (each.isActive()) {
50         targetWindow = each;
51         break;
52       }
53     }
54
55     if (targetWindow == null) {
56       targetWindow = JOptionPane.getRootFrame();
57     }
58
59     SwingUtilities.convertPointFromScreen(p, targetWindow);
60     init(targetWindow, p);
61   }
62
63   private void init(@NotNull Component aComponent, Point aPointOnComponent) {
64     if (aComponent.isShowing()) {
65       myComponent = SwingUtilities.getRootPane(aComponent);
66       myPointOnComponent = SwingUtilities.convertPoint(aComponent, aPointOnComponent, myComponent);
67     }
68     else {
69       myComponent = aComponent;
70       myPointOnComponent = aPointOnComponent;
71     }
72
73     myOriginalComponent = myComponent;
74     myOriginalPoint = myPointOnComponent;
75   }
76
77   public Component getComponent() {
78     return myComponent;
79   }
80
81   public Point getPoint() {
82     return myPointOnComponent;
83   }
84
85   public Point getPoint(@Nullable Component aTargetComponent) {
86 //todo: remove that after implementation of DND to html design time controls
87     if (aTargetComponent == null || aTargetComponent.getParent() == null || SwingUtilities.getWindowAncestor(aTargetComponent) == null) return new Point();
88
89     return SwingUtilities.convertPoint(getComponent(), getPoint(), aTargetComponent);
90   }
91
92   @NotNull
93   public RelativePoint getPointOn(@NotNull Component aTargetComponent) {
94     final Point point = getPoint(aTargetComponent);
95     return new RelativePoint(aTargetComponent, point);
96   }
97
98   @NotNull
99   public Point getScreenPoint() {
100     final Point point = (Point) getPoint().clone();
101     SwingUtilities.convertPointToScreen(point, getComponent());
102     return point;
103   }
104
105   @NotNull
106   public MouseEvent toMouseEvent() {
107     return new MouseEvent(myComponent, 0, 0, 0, myPointOnComponent.x, myPointOnComponent.y, 1, false); 
108   }
109
110   @NotNull
111   public String toString() {
112     //noinspection HardCodedStringLiteral
113     return getPoint() + " on " + getComponent().toString();
114   }
115
116   @NotNull
117   public static RelativePoint getCenterOf(@NotNull JComponent component) {
118     final Rectangle visibleRect = component.getVisibleRect();
119     final Point point = new Point(visibleRect.x + visibleRect.width/2, visibleRect.y + visibleRect.height/2);
120     return new RelativePoint(component, point);
121   }
122
123   @NotNull
124   public static RelativePoint getSouthEastOf(@NotNull JComponent component) {
125     final Rectangle visibleRect = component.getVisibleRect();
126     final Point point = new Point(visibleRect.x + visibleRect.width, visibleRect.y + visibleRect.height);
127     return new RelativePoint(component, point);
128   }
129
130   @NotNull
131   public static RelativePoint getSouthWestOf(@NotNull JComponent component) {
132     final Rectangle visibleRect = component.getVisibleRect();
133     final Point point = new Point(visibleRect.x, visibleRect.y + visibleRect.height);
134     return new RelativePoint(component, point);
135   }
136
137   @NotNull
138   public static RelativePoint getNorthWestOf(@NotNull JComponent component) {
139     final Rectangle visibleRect = component.getVisibleRect();
140     final Point point = new Point(visibleRect.x, visibleRect.y);
141     return new RelativePoint(component, point);
142   }
143
144   @NotNull
145   public static RelativePoint getNorthEastOf(@NotNull JComponent component) {
146     final Rectangle visibleRect = component.getVisibleRect();
147     final Point point = new Point(visibleRect.x + visibleRect.width, visibleRect.y);
148     return new RelativePoint(component, point);
149   }
150
151   @NotNull
152   public static RelativePoint fromScreen(Point screenPoint) {
153     Frame root = JOptionPane.getRootFrame();
154     SwingUtilities.convertPointFromScreen(screenPoint, root);
155     return new RelativePoint(root, screenPoint);
156   }
157
158   public Component getOriginalComponent() {
159     return myOriginalComponent;
160   }
161
162   public Point getOriginalPoint() {
163     return myOriginalPoint;
164   }
165 }