1
1
/*
2
2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
3
*
4
- * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
4
+ * Copyright (c) 1997, 2015, 2022, Oracle and/or its affiliates. All rights reserved.
5
5
*
6
6
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
7
* Other names may be trademarks of their respective owners.
50
50
import javax .swing .JComponent ;
51
51
import javax .swing .JScrollPane ;
52
52
import javax .swing .SwingUtilities ;
53
+ import javax .swing .JScrollBar ;
53
54
import org .netbeans .api .visual .action .WidgetAction ;
54
55
import org .netbeans .api .visual .action .WidgetAction .State ;
55
56
import org .netbeans .api .visual .action .WidgetAction .WidgetMouseEvent ;
62
63
*/
63
64
public class CustomizablePanAction extends WidgetAction .LockedAdapter {
64
65
private boolean enabled = true ;
66
+ private boolean active = true ;
65
67
66
68
private Scene scene ;
67
69
private JScrollPane scrollPane ;
68
70
private Point lastLocation ;
69
-
70
- private final int modifiersExMask ;
71
+ private Rectangle rectangle ;
71
72
private final int modifiersEx ;
72
73
73
- public CustomizablePanAction (int modifiersExMask , int modifiersEx ) {
74
- this .modifiersExMask = modifiersExMask ;
74
+ public CustomizablePanAction (int modifiersEx ) {
75
75
this .modifiersEx = modifiersEx ;
76
76
}
77
77
@@ -80,72 +80,89 @@ protected boolean isLocked() {
80
80
return scrollPane != null ;
81
81
}
82
82
83
+ private void lock () {
84
+ scrollPane = findScrollPane (scene .getView ());
85
+ }
86
+
87
+ private void unlock () {
88
+ scrollPane = null ;
89
+ }
90
+
83
91
public void setEnabled (boolean enabled ) {
84
92
if (this .enabled != enabled ) {
85
- if (isLocked ())
93
+ if (this . isLocked ()) {
86
94
throw new IllegalStateException ();
87
-
95
+ }
88
96
this .enabled = enabled ;
89
97
}
90
98
}
91
99
92
100
@ Override
93
- public State mousePressed (Widget widget , WidgetMouseEvent event ) {
101
+ public State mouseEntered (Widget widget , WidgetMouseEvent event ) {
102
+ active = true ;
103
+ return super .mouseEntered (widget , event );
104
+ }
105
+
106
+ @ Override
107
+ public State mouseExited (Widget widget , WidgetMouseEvent event ) {
108
+ active = false ;
109
+ return super .mouseExited (widget , event );
110
+ }
111
+
112
+ @ Override
113
+ public State mousePressed (Widget widget , WidgetMouseEvent event ) {
94
114
EditorTopComponent editor = EditorTopComponent .getActive ();
95
115
if (editor != null ) {
96
116
editor .requestActive ();
97
117
}
98
- if (isLocked ())
99
- return State .createLocked (widget , this );
100
- if (enabled && (event .getModifiersEx () & modifiersExMask ) == modifiersEx ) {
101
- scene = widget .getScene ();
102
- scrollPane = findScrollPane (scene .getView ());
103
- if (scrollPane != null ) {
104
- lastLocation = scene .convertSceneToView (widget .convertLocalToScene (event .getPoint ()));
105
- SwingUtilities .convertPointToScreen (lastLocation , scene .getView ());
106
- return State .createLocked (widget , this );
118
+ if (!this .isLocked () && active && enabled && (event .getModifiersEx () == modifiersEx )) {
119
+ scene = widget .getScene ();
120
+ this .lock ();
121
+ if (this .isLocked ()) {
122
+ lastLocation = scene .convertSceneToView (widget .convertLocalToScene (event .getPoint ()));
123
+ SwingUtilities .convertPointToScreen (lastLocation , scene .getView ());
124
+ rectangle = scene .getView ().getVisibleRect ();
107
125
}
108
126
}
109
- return State . REJECTED ;
127
+ return super . mousePressed ( widget , event ) ;
110
128
}
111
129
112
- private JScrollPane findScrollPane (JComponent component ) {
130
+ private JScrollPane findScrollPane (JComponent component ) {
113
131
for (;;) {
114
- if (component == null )
132
+ if (component == null ) {
115
133
return null ;
116
- if (component instanceof JScrollPane )
134
+ }
135
+ if (component instanceof JScrollPane ) {
117
136
return ((JScrollPane ) component );
118
- Container parent = component .getParent ();
119
- if (! (parent instanceof JComponent ))
137
+ }
138
+ Container parent = component .getParent ();
139
+ if (!(parent instanceof JComponent )) {
120
140
return null ;
141
+ }
121
142
component = (JComponent ) parent ;
122
143
}
123
144
}
124
145
125
146
@ Override
126
- public State mouseReleased (Widget widget , WidgetMouseEvent event ) {
127
- boolean state = pan ( widget , event . getPoint ());
128
- if ( state )
129
- scrollPane = null ;
130
- return state ? State . createLocked (widget , this ) : State . REJECTED ;
147
+ public State mouseReleased (Widget widget , WidgetMouseEvent event ) {
148
+ if ( this . isLocked () && scene == widget . getScene ()) {
149
+ this . unlock ();
150
+ }
151
+ return super . mouseReleased (widget , event ) ;
131
152
}
132
153
133
154
@ Override
134
- public State mouseDragged (Widget widget , WidgetMouseEvent event ) {
135
- return pan (widget , event .getPoint ()) ? State .createLocked (widget , this ) : State .REJECTED ;
136
- }
137
-
138
- private boolean pan (Widget widget , Point newLocation ) {
139
- if (scrollPane == null || scene != widget .getScene ())
140
- return false ;
141
- newLocation = scene .convertSceneToView (widget .convertLocalToScene (newLocation ));
142
- SwingUtilities .convertPointToScreen (newLocation , scene .getView ());
143
- JComponent view = scene .getView ();
144
- Rectangle rectangle = view .getVisibleRect ();
145
- rectangle .x += lastLocation .x - newLocation .x ;
146
- rectangle .y += lastLocation .y - newLocation .y ;
147
- view .scrollRectToVisible (rectangle );
148
- lastLocation = newLocation ;
149
- return true ;
155
+ public State mouseDragged (Widget widget , WidgetMouseEvent event ) {
156
+ if (active && this .isLocked () && scene == widget .getScene ()) {
157
+ Point newLocation = event .getPoint ();
158
+ newLocation = scene .convertSceneToView (widget .convertLocalToScene (newLocation ));
159
+ SwingUtilities .convertPointToScreen (newLocation , scene .getView ());
160
+ rectangle .x += lastLocation .x - newLocation .x ;
161
+ rectangle .y += lastLocation .y - newLocation .y ;
162
+ scene .getView ().scrollRectToVisible (rectangle );
163
+ lastLocation = newLocation ;
164
+ return State .createLocked (widget , this );
165
+ }
166
+ return State .REJECTED ;
150
167
}
151
168
}
0 commit comments