-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraggablePanelsExample.java
More file actions
250 lines (212 loc) · 8.42 KB
/
Copy pathDraggablePanelsExample.java
File metadata and controls
250 lines (212 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright (c) 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
* This file is available and licensed under the following license:
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Handling mouse events with event filters
*/
package application;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Tooltip;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public final class DraggablePanelsExample extends Application
{
private final BooleanProperty dragModeActiveProperty = new SimpleBooleanProperty(this, "dragModeActive", true);
@Override
public void start(final Stage stage)
{
final Node loginPanel = makeDraggable(createLoginPanel());
final Node confirmationPanel = makeDraggable(createConfirmationPanel());
final Node progressPanel = makeDraggable(createProgressPanel());
loginPanel.relocate(0, 0);
confirmationPanel.relocate(0, 67);
progressPanel.relocate(0, 106);
final Pane panelsPane = new Pane();
panelsPane.getChildren().addAll(loginPanel, confirmationPanel, progressPanel);
final BorderPane sceneRoot = new BorderPane();
BorderPane.setAlignment(panelsPane, Pos.TOP_LEFT);
sceneRoot.setCenter(panelsPane);
final CheckBox dragModeCheckbox = new CheckBox("Drag mode");
BorderPane.setMargin(dragModeCheckbox, new Insets(6));
sceneRoot.setBottom(dragModeCheckbox);
dragModeActiveProperty.bind(dragModeCheckbox.selectedProperty());
final Scene scene = new Scene(sceneRoot, 400, 300);
stage.setScene(scene);
stage.setTitle("Draggable Panels Example");
stage.show();
}
public static void main(final String[] args)
{
launch(args);
}
private Node makeDraggable(final Node node)
{
final DragContext dragContext = new DragContext();
final Group wrapGroup = new Group(node);
wrapGroup.addEventFilter(MouseEvent.ANY, new EventHandler<MouseEvent>(){
public void handle(final MouseEvent mouseEvent)
{
if(dragModeActiveProperty.get())
{
// disable mouse events for all children
mouseEvent.consume();
}
}
});
wrapGroup.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>(){
public void handle(final MouseEvent mouseEvent)
{
if(dragModeActiveProperty.get())
{
// remember initial mouse cursor coordinates
// and node position
dragContext.mouseAnchorX = mouseEvent.getX();
dragContext.mouseAnchorY = mouseEvent.getY();
dragContext.initialTranslateX = node.getTranslateX();
dragContext.initialTranslateY = node.getTranslateY();
}
}
});
wrapGroup.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>(){
public void handle(final MouseEvent mouseEvent)
{
if(dragModeActiveProperty.get())
{
// shift node from its initial position by delta
// calculated from mouse cursor movement
node.setTranslateX(dragContext.initialTranslateX + mouseEvent.getX() - dragContext.mouseAnchorX);
node.setTranslateY(dragContext.initialTranslateY + mouseEvent.getY() - dragContext.mouseAnchorY);
}
}
});
return wrapGroup;
}
private static Node createLoginPanel()
{
final ToggleGroup toggleGroup = new ToggleGroup();
final TextField textField = new TextField();
textField.setPrefColumnCount(10);
textField.setPromptText("Your name");
final PasswordField passwordField = new PasswordField();
passwordField.setPrefColumnCount(10);
passwordField.setPromptText("Your password");
final ChoiceBox<String> choiceBox = new ChoiceBox<String>(FXCollections.observableArrayList("English", "\u0420\u0443\u0441\u0441\u043a\u0438\u0439", "Fran\u00E7ais"));
choiceBox.setTooltip(new Tooltip("Your language"));
choiceBox.getSelectionModel().select(0);
final HBox panel = createHBox(6, createVBox(2, createRadioButton("High", toggleGroup, true), createRadioButton("Medium", toggleGroup, false), createRadioButton("Low", toggleGroup, false)), createVBox(2, textField, passwordField), choiceBox);
panel.setAlignment(Pos.BOTTOM_LEFT);
configureBorder(panel);
return panel;
}
private static Node createConfirmationPanel()
{
final Label acceptanceLabel = new Label("Not Available");
final Button acceptButton = new Button("Accept");
acceptButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(final ActionEvent event)
{
acceptanceLabel.setText("Accepted");
}
});
final Button declineButton = new Button("Decline");
declineButton.setOnAction(new EventHandler<ActionEvent>(){
public void handle(final ActionEvent event)
{
acceptanceLabel.setText("Declined");
}
});
final HBox panel = createHBox(6, acceptButton, declineButton, acceptanceLabel);
panel.setAlignment(Pos.CENTER_LEFT);
configureBorder(panel);
return panel;
}
private static Node createProgressPanel()
{
final Slider slider = new Slider();
final ProgressIndicator progressIndicator = new ProgressIndicator(0);
progressIndicator.progressProperty().bind(Bindings.divide(slider.valueProperty(), slider.maxProperty()));
final HBox panel = createHBox(6, new Label("Progress:"), slider, progressIndicator);
configureBorder(panel);
return panel;
}
private static void configureBorder(final Region region)
{
region.setStyle("-fx-background-color: white;" + "-fx-border-color: black;" + "-fx-border-width: 1;" + "-fx-border-radius: 6;" + "-fx-padding: 6;");
}
private static RadioButton createRadioButton(final String text, final ToggleGroup toggleGroup, final boolean selected)
{
final RadioButton radioButton = new RadioButton(text);
radioButton.setToggleGroup(toggleGroup);
radioButton.setSelected(selected);
return radioButton;
}
private static HBox createHBox(final double spacing, final Node... children)
{
final HBox hbox = new HBox(spacing);
hbox.getChildren().addAll(children);
return hbox;
}
private static VBox createVBox(final double spacing, final Node... children)
{
final VBox vbox = new VBox(spacing);
vbox.getChildren().addAll(children);
return vbox;
}
private static final class DragContext
{
public double mouseAnchorX;
public double mouseAnchorY;
public double initialTranslateX;
public double initialTranslateY;
}
}