KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > DecorationUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.windows.view.ui;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Insets JavaDoc;
28 import java.awt.Point JavaDoc;
29 import java.awt.Rectangle JavaDoc;
30 import java.awt.Window JavaDoc;
31 import java.awt.event.MouseAdapter JavaDoc;
32 import java.awt.event.MouseEvent JavaDoc;
33 import java.awt.event.MouseMotionListener JavaDoc;
34 import javax.swing.SwingUtilities JavaDoc;
35 import javax.swing.border.AbstractBorder JavaDoc;
36 import javax.swing.border.Border JavaDoc;
37
38 /**
39  * Utility class that provides window decorations, custom border and resize
40  * handler for borders useful for window resizing.
41  *
42  * @author Dafe Simonek
43  */

44 final class DecorationUtils {
45     
46     /** No instances, utils class. */
47     private DecorationUtils () {
48     }
49
50     /** Creates and returns border suitable for decorating separate windows
51      * in window system.
52      *
53      * @return Border for separate windows
54      */

55     public static Border JavaDoc createSeparateBorder () {
56         return new SeparateBorder();
57     }
58
59     /** Creates and returns handler of window resizing, which works in given
60      * insets.
61      * @return The handler for resizing.
62      */

63     public static ResizeHandler createResizeHandler (Insets JavaDoc insets) {
64         return new ResizeHandler(insets);
65     }
66
67     /** Simple border with line and the space */
68     private static class SeparateBorder extends AbstractBorder JavaDoc {
69
70         public Insets JavaDoc getBorderInsets (Component JavaDoc c) {
71             return new Insets JavaDoc(3, 3, 3, 3);
72         }
73
74         public void paintBorder (Component JavaDoc c, Graphics JavaDoc g, int x, int y, int width, int height) {
75             g.setColor(Color.DARK_GRAY);
76             g.drawRect(x, y, width - 1, height - 1);
77         }
78
79     } // end of SeparateBorder
80

81     /** Takes care about resizing of the window on mouse drag,
82      * with proper resize cursors.
83      *
84      * Usage: Attach handler as mouse and mouse motion listener to the content pane of
85      * the window:<br>
86      * <code>rootPaneContainer.getContentPane().addMouseListener(resizeHandler);</code>
87      * <code>rootPaneContainer.getContentPane().addMouseMotionListener(resizeHandler);</code>
88      *
89     */

90     static class ResizeHandler extends MouseAdapter JavaDoc implements MouseMotionListener JavaDoc {
91
92         private Insets JavaDoc insets;
93
94         private int cursorType;
95
96         private boolean isPressed = false;
97
98         /** Window resize bounds, class fields to prevent from allocating new
99          * objects */

100         private Rectangle JavaDoc resizedBounds = new Rectangle JavaDoc();
101         private Rectangle JavaDoc movedBounds = new Rectangle JavaDoc();
102
103         private Point JavaDoc startDragLoc;
104         private Rectangle JavaDoc startWinBounds;
105
106         /** holds minimum size of the window being resized */
107         private Dimension JavaDoc minSize;
108
109         public ResizeHandler (Insets JavaDoc insets) {
110             this.insets = insets;
111         }
112
113         public void mouseDragged(MouseEvent JavaDoc e) {
114             check(e);
115             Window JavaDoc w = SwingUtilities.getWindowAncestor((Component JavaDoc)e.getSource());
116
117             if (Cursor.DEFAULT_CURSOR == cursorType) {
118                 // resize only when mouse pointer in resize areas
119
return;
120             }
121
122             Rectangle JavaDoc newBounds = computeNewBounds(w, getScreenLoc(e));
123             if (!w.getBounds().equals(newBounds)) {
124                 w.setBounds(newBounds);
125             }
126         }
127
128         public void mouseMoved(MouseEvent JavaDoc e) {
129             check(e);
130             Component JavaDoc comp = (Component JavaDoc)e.getSource();
131             movedBounds = comp.getBounds(movedBounds);
132
133             cursorType = getCursorType(movedBounds, e.getPoint());
134             comp.setCursor(Cursor.getPredefinedCursor(cursorType));
135         }
136
137         public void mousePressed(MouseEvent JavaDoc e) {
138             isPressed = true;
139             startDragLoc = getScreenLoc(e);
140
141             Window JavaDoc w = SwingUtilities.getWindowAncestor((Component JavaDoc)e.getSource());
142             startWinBounds = w.getBounds();
143             resizedBounds.setBounds(startWinBounds);
144             minSize = w.getMinimumSize();
145         }
146
147         public void mouseReleased(MouseEvent JavaDoc e) {
148             isPressed = false;
149             startDragLoc = null;
150             startWinBounds = null;
151             minSize = null;
152         }
153
154         public void mouseExited(MouseEvent JavaDoc e) {
155             Component JavaDoc comp = (Component JavaDoc)e.getSource();
156             comp.setCursor(Cursor.getDefaultCursor());
157         }
158
159         private int getCursorType (Rectangle JavaDoc b, Point JavaDoc p) {
160             int leftDist = p.x - b.x;
161             int rightDist = (b.x + b.width) - p.x;
162             int topDist = p.y - b.y;
163             int bottomDist = (b.y + b.height) - p.y;
164
165             boolean isNearTop = topDist >= 0 && topDist <= insets.top;
166             boolean isNearBottom = bottomDist >= 0 && bottomDist <= insets.bottom;
167             boolean isNearLeft = leftDist >= 0 && leftDist <= insets.left;
168             boolean isNearRight = rightDist >= 0 && rightDist <= insets.right;
169
170             boolean isInTopPart = topDist >= 0 && topDist <= insets.top + 10;
171             boolean isInBottomPart = bottomDist >= 0 && bottomDist <= insets.bottom + 10;
172             boolean isInLeftPart = leftDist >= 0 && leftDist <= insets.left + 10;
173             boolean isInRightPart = rightDist >= 0 && rightDist <= insets.right + 10;
174
175             if (isNearTop && isInLeftPart || isInTopPart && isNearLeft) {
176                 return Cursor.NW_RESIZE_CURSOR;
177             }
178             if (isNearTop && isInRightPart || isInTopPart && isNearRight) {
179                 return Cursor.NE_RESIZE_CURSOR;
180             }
181             if (isNearBottom && isInLeftPart || isInBottomPart && isNearLeft) {
182                 return Cursor.SW_RESIZE_CURSOR;
183             }
184             if (isNearBottom && isInRightPart || isInBottomPart && isNearRight) {
185                 return Cursor.SE_RESIZE_CURSOR;
186             }
187             if (isNearTop) {
188                 return Cursor.N_RESIZE_CURSOR;
189             }
190             if (isNearLeft) {
191                 return Cursor.W_RESIZE_CURSOR;
192             }
193             if (isNearRight) {
194                 return Cursor.E_RESIZE_CURSOR;
195             }
196             if (isNearBottom) {
197                 return Cursor.S_RESIZE_CURSOR;
198             }
199             return Cursor.DEFAULT_CURSOR;
200         }
201
202         private Rectangle JavaDoc computeNewBounds (Window JavaDoc w, Point JavaDoc dragLoc) {
203             if (startDragLoc == null) {
204                 throw new IllegalArgumentException JavaDoc("Can't compute bounds when startDragLoc is null"); //NOI18N
205
}
206             int xDiff = dragLoc.x - startDragLoc.x;
207             int yDiff = dragLoc.y - startDragLoc.y;
208             resizedBounds.setBounds(startWinBounds);
209
210             switch (cursorType) {
211                 case Cursor.E_RESIZE_CURSOR:
212                     resizedBounds.width = startWinBounds.width + (dragLoc.x - startDragLoc.x);
213                     break;
214
215                 case Cursor.W_RESIZE_CURSOR:
216                     resizedBounds.width = startWinBounds.width - xDiff;
217                     resizedBounds.x = startWinBounds.x + xDiff;
218                     break;
219
220                 case Cursor.N_RESIZE_CURSOR:
221                     resizedBounds.height = startWinBounds.height - yDiff;
222                     resizedBounds.y = startWinBounds.y + yDiff;
223                     break;
224
225                 case Cursor.S_RESIZE_CURSOR:
226                     resizedBounds.height = startWinBounds.height + (dragLoc.y - startDragLoc.y);
227                     break;
228
229                 case Cursor.NE_RESIZE_CURSOR:
230                     resize(resizedBounds, 0, yDiff, xDiff, -yDiff, minSize);
231                     break;
232
233                 case Cursor.NW_RESIZE_CURSOR:
234                     resize(resizedBounds, xDiff, yDiff, -xDiff, -yDiff, minSize);
235                     break;
236
237                 case Cursor.SE_RESIZE_CURSOR:
238                     resize(resizedBounds, 0, 0, xDiff, yDiff, minSize);
239                     break;
240
241                 case Cursor.SW_RESIZE_CURSOR:
242                     resize(resizedBounds, xDiff, 0, -xDiff, yDiff, minSize);
243                     break;
244
245                 default:
246                     System.out.println("unknown cursor type : " + cursorType);
247                     //throw new IllegalArgumentException("Unknown/illegal cursor type: " + cursorType); //NOI18N
248
break;
249             }
250             return resizedBounds;
251         }
252
253         private static void resize (Rectangle JavaDoc rect, int xDiff, int yDiff, int widthDiff, int heightDiff, Dimension JavaDoc minSize) {
254             rect.x += xDiff;
255             rect.y += yDiff;
256             rect.height += heightDiff;
257             rect.width += widthDiff;
258             // keep size at least at minSize
259
rect.height = Math.max(rect.height, minSize.height);
260             rect.width = Math.max(rect.width, minSize.width);
261         }
262
263         private Point JavaDoc getScreenLoc (MouseEvent JavaDoc e) {
264             Point JavaDoc screenP = new Point JavaDoc(e.getPoint());
265             SwingUtilities.convertPointToScreen(screenP, (Component JavaDoc) e.getSource());
266             return screenP;
267         }
268
269         /* Checks that handler is correctly attached to the window */
270         private void check(MouseEvent JavaDoc e) {
271             Object JavaDoc o = e.getSource();
272             if (!(o instanceof Component JavaDoc)) {
273                 throw new IllegalArgumentException JavaDoc("ResizeHandler works only with Component, not with " + o); //NOI18N
274
}
275             Window JavaDoc w = SwingUtilities.getWindowAncestor((Component JavaDoc)o);
276             if (w == null) {
277                 throw new IllegalStateException JavaDoc("Can't find and resize the window, not attached."); //NOI18N
278
}
279         }
280
281     } // end of ResizeHandler
282

283 }
284
Popular Tags