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