1 /***
2 * SnappingDesktopManager.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 05.05.2003 sam creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.gui;
31
32 import java.awt.Point;
33 import javax.swing.*;
34
35 /***
36 * <code>SnappingDesktopManager</code> is a DesktopManager for the
37 * Dependency Tool desktop. It doesn't allow moving InternalWindows
38 * beyond desktop's visibility and it makes snapping them if they are
39 * in a certain range.
40 *
41 * @author Samuel Weibel
42 * @version 1.0-beta
43 */
44 public class SnappingDesktopManager extends DefaultDesktopManager {
45
46 private int snapRange = 10;
47
48 /***
49 * Drag a JComponent.
50 *
51 * @param f a <code>JComponent</code> value
52 * @param x an <code>int</code> value
53 * @param y an <code>int</code> value
54 */
55 public void dragFrame(JComponent f, int x, int y) {
56 Point newLoc = findSnapping((JInternalFrame)f, new Point(x, y));
57 super.dragFrame(f, newLoc.x, newLoc.y);
58 }
59
60 protected Point findSnapping(JInternalFrame f, Point loc) {
61 int x = loc.x;
62 int y = loc.y;
63
64 int desktopWidth = f.getDesktopPane().getWidth();
65 int desktopHeight = f.getDesktopPane().getHeight();
66 int frameWidth = f.getWidth();
67 int frameHeight = f.getHeight();
68
69 // check other frames
70 JInternalFrame[] allFrames = f.getDesktopPane().getAllFrames();
71 boolean changedX = false, changedY = false;
72 for (int i = 0; !(changedX && changedY) && i < allFrames.length; i++) {
73 JInternalFrame frame = allFrames[i];
74 if (frame != f) {
75 int left = frame.getX();
76 int top = frame.getY();
77 int right = left + frame.getWidth();
78 int bottom = top + frame.getHeight();
79
80 if (areAdjacent(y, f.getHeight(), frame.getY(), frame.getHeight())) {
81 // adjust x
82 if (!changedX && areNear(right, x)) {
83 x = right;
84 changedX = true;
85 } else if(!changedX && areNear(left, x + frameWidth)) {
86 x = left - frameWidth;
87 changedX = true;
88 }
89
90 if (changedX) {
91 if (areNear(top, y)) {
92 y = top;
93 changedY = true;
94 } else if (areNear(bottom, y + frameHeight)) {
95 y = bottom - frameHeight;
96 changedY = true;
97 }
98 }
99 }
100
101 if (areAdjacent(x, f.getWidth(), frame.getX(), frame.getWidth())) {
102 // adjust y
103 if (!changedY && areNear(bottom, y)) {
104 y = bottom;
105 changedY = true;
106 } else if(!changedY && areNear(top, y + frameHeight)) {
107 y = top - frameHeight;
108 changedY = true;
109 }
110
111 if (changedY) {
112 if (areNear(left, x)) {
113 x = left;
114 changedX = true;
115 } else if (areNear(right, x + frameWidth)) {
116 x = right - frameWidth;
117 changedX = true;
118 }
119 }
120 }
121 }
122 }
123
124 // check right border
125 if (x + frameWidth + snapRange > desktopWidth) {
126 x = desktopWidth - frameWidth;
127 }
128 // check left border
129 if (x < snapRange) {
130 x = 0;
131 }
132 // check bottom border
133 if (y + frameHeight + snapRange > desktopHeight) {
134 y = desktopHeight - frameHeight;
135 }
136 // check top border
137 if (y < snapRange) {
138 y = 0;
139 }
140
141 loc.x = x;
142 loc.y = y;
143 return loc;
144 }
145
146 private boolean areNear(int value1, int value2) {
147 return value1 > value2 - snapRange && value1 < value2 + snapRange;
148 }
149
150 private boolean areAdjacent(int x1, int width1, int x2, int width2) {
151 if (x1 > x2) {
152 return x1 < x2 + width2;
153 } else {
154 return x2 < x1 + width1;
155 }
156 }
157 }
This page was automatically generated by Maven