KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > windows > view > ui > slides > ResizeGestureRecognizer


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.slides;
21
22 import java.awt.AWTEvent JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Cursor JavaDoc;
25 import java.awt.Point JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.awt.event.AWTEventListener JavaDoc;
28 import java.awt.event.MouseAdapter JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.awt.event.MouseMotionListener JavaDoc;
32 import javax.swing.JComponent JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JRootPane JavaDoc;
35 import javax.swing.SwingUtilities JavaDoc;
36 import org.netbeans.core.windows.Constants;
37
38 /**
39  *
40  * @author mkleint
41  */

42 public class ResizeGestureRecognizer implements AWTEventListener JavaDoc {
43     
44
45
46      void attachResizeRecognizer(String JavaDoc side, Component JavaDoc component) {
47          update(side, component);
48          Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
49      }
50      void detachResizeRecognizer(String JavaDoc side, Component JavaDoc component) {
51          Toolkit.getDefaultToolkit().removeAWTEventListener(this);
52          update(null, null);
53      }
54      
55     
56     static final int RESIZE_BUFFER = 8;
57     private boolean isResizing = false;
58     private Component JavaDoc comp;
59     private String JavaDoc side;
60     private CommandManager mgr;
61     
62     private GlassPane glass;
63     private Component JavaDoc oldGlass;
64     
65     private int state;
66     private Point JavaDoc startPoint;
67     private static final int STATE_NOOP = 0;
68     private static final int STATE_START = 1;
69     private static final int STATE_DRAGGING = 2;
70     
71     /** Creates a new instance of ResizeGestureRecognizer */
72     public ResizeGestureRecognizer(CommandManager mgr) {
73         this.mgr = mgr;
74         glass = new GlassPane();
75     }
76     
77     public void update(String JavaDoc side, Component JavaDoc component) {
78         this.side = side;
79         comp = component;
80         state = STATE_NOOP;
81         resetState();
82     }
83
84     private boolean isInResizeArea(MouseEvent JavaDoc event) {
85         if (comp == null || side == null) {
86             return false;
87         }
88         Point JavaDoc leftTop = new Point JavaDoc(0, 0);
89         leftTop = SwingUtilities.convertPoint(comp, leftTop, SwingUtilities.getRoot(comp));
90         Component JavaDoc comp2 = event.getComponent();
91         if (!comp2.isDisplayable()) {
92             //#54329 under specific conditions the component in the event can be gone.
93
return false;
94         }
95         Point JavaDoc evtPoint = SwingUtilities.convertPoint(comp2,
96                               event.getPoint(), SwingUtilities.getRoot(comp2));
97         if (Constants.BOTTOM.equals(side)) {
98             if (evtPoint.x > leftTop.x && evtPoint.x < (leftTop.x + comp.getBounds().width)) {
99                 if ( Math.abs(evtPoint.y - leftTop.y) < RESIZE_BUFFER) {
100                     return true;
101                 }
102             }
103         }
104         if (Constants.LEFT.equals(side)) {
105             if (evtPoint.y > leftTop.y && evtPoint.y < (leftTop.y + comp.getBounds().height)) {
106                 int right = comp.getBounds().width + leftTop.x;
107                 if (Math.abs(evtPoint.x - right) < RESIZE_BUFFER) {
108                     return true;
109                 }
110             }
111         }
112         if (Constants.RIGHT.equals(side)) {
113             if (evtPoint.y > leftTop.y && evtPoint.y < (leftTop.y + comp.getBounds().height)) {
114                 if ( Math.abs(evtPoint.x - leftTop.x) < RESIZE_BUFFER) {
115                     return true;
116                 }
117             }
118         }
119         return false;
120     }
121
122     private int resize(MouseEvent JavaDoc event, Point JavaDoc dragPoint) {
123         if (comp == null || side == null) {
124             return 0;
125         }
126         Point JavaDoc leftTop = SwingUtilities.convertPoint(comp,
127                               new Point JavaDoc(0,0), SwingUtilities.getRoot(comp));
128         Point JavaDoc evtPoint = SwingUtilities.convertPoint(event.getComponent(),
129                               event.getPoint(), SwingUtilities.getRoot(event.getComponent()));
130         if (Constants.BOTTOM.equals(side)) {
131             if (evtPoint.x > leftTop.x && evtPoint.x < (leftTop.x + comp.getBounds().width)) {
132                 return evtPoint.y - dragPoint.y;
133             }
134         }
135         if (Constants.LEFT.equals(side) || Constants.RIGHT.equals(side)) {
136             if (evtPoint.y > leftTop.y && evtPoint.y < (leftTop.y + comp.getBounds().height)) {
137                 return evtPoint.x - dragPoint.x;
138             }
139         }
140         return 0;
141     }
142     
143     public void eventDispatched(java.awt.AWTEvent JavaDoc aWTEvent) {
144         if (comp == null || side == null) {
145             state = STATE_NOOP;
146             resetState();
147             return;
148         }
149         MouseEvent JavaDoc evt = (MouseEvent JavaDoc)aWTEvent;
150         if (evt.getID() == MouseEvent.MOUSE_MOVED) {
151             boolean noModif = evt.getModifiersEx() == 0;
152             if (noModif && isInResizeArea(evt)) {
153                 // make glasspane visible
154
if (state == STATE_NOOP) {
155                     state = STATE_START;
156                     JRootPane JavaDoc pane = SwingUtilities.getRootPane(comp);
157                     oldGlass = pane.getGlassPane();
158                     glass.setCursor(side);
159                     pane.setGlassPane(glass);
160                     glass.setVisible(true);
161                 }
162                 return;
163             } else if (state != STATE_NOOP) {
164                 resetState();
165             }
166             return;
167         }
168         if (evt.getID() == MouseEvent.MOUSE_PRESSED && state == STATE_START) {
169             boolean button1 = (evt.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK;
170             if (button1) {
171                 if (isInResizeArea(evt)) {
172                     state = STATE_DRAGGING;
173                     startPoint = SwingUtilities.convertPoint(evt.getComponent(),
174                                          evt.getPoint(), SwingUtilities.getRoot(evt.getComponent()));
175                     evt.consume();
176                     return;
177                 }
178             }
179             resetState();
180             return;
181         }
182         if (evt.getID() == MouseEvent.MOUSE_DRAGGED && state == STATE_DRAGGING) {
183             boolean button1 = (evt.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK;
184             if (button1 && startPoint != null) {
185                 int delta = resize(evt, startPoint);
186                 if (Math.abs(delta) > 3) {
187                     startPoint = SwingUtilities.convertPoint(evt.getComponent(),
188                                          evt.getPoint(), SwingUtilities.getRoot(evt.getComponent()));
189                     mgr.slideResize(delta);
190                 }
191             }
192             return;
193         }
194     }
195     
196     public boolean isDragging() {
197         return state == STATE_DRAGGING;
198     }
199     
200     private void resetState() {
201         state = STATE_NOOP;
202         JRootPane JavaDoc pane = SwingUtilities.getRootPane(comp);
203         glass.setVisible(false);
204         if (pane != null && oldGlass != null) {
205             // when clicking results in hidden slide window, pne can be null?
206
// how to avoid?
207
JComponent JavaDoc current = (JComponent JavaDoc) pane.getGlassPane();
208             if (current instanceof GlassPane) {
209                 pane.setGlassPane(oldGlass);
210             }
211         }
212         oldGlass = null;
213         startPoint = null;
214     }
215     
216     private class GlassPane extends JPanel JavaDoc {
217
218         private MouseListener JavaDoc list = new MouseAdapter JavaDoc() {};
219         
220         public GlassPane() {
221             setOpaque(false);
222             putClientProperty("dontActivate", Boolean.TRUE);
223             // have a listener to make the galsspane consume mouse events.
224
addMouseListener(list);
225         }
226         
227         public void setCursor(String JavaDoc side) {
228             setCursor(Constants.BOTTOM.equals(side) ?
229                       Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR) :
230                       Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
231         }
232         
233     }
234 }
235
236
Popular Tags