Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5a5977b16a68bc5e683e28069ebd226373cef60c (plain) (blame)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
 * Copyright (c) 2014 BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.fx.ui.controls.tabpane;

import java.util.function.Consumer;
import java.util.function.Function;

import org.eclipse.fx.ui.controls.dnd.EFXDragEvent;
import org.eclipse.fx.ui.controls.markers.PositionMarker;
import org.eclipse.fx.ui.controls.markers.TabOutlineMarker;
import org.eclipse.fx.ui.controls.tabpane.skin.DnDTabPaneSkin;
import org.eclipse.fx.ui.controls.tabpane.skin.DnDTabPaneSkinFullDrag;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import javafx.event.Event;
import javafx.geometry.BoundingBox;
import javafx.geometry.Bounds;
import javafx.scene.Node;
import javafx.scene.control.TabPane;
import javafx.scene.input.DragEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;

/**
 * Factory to create a tab pane who support DnD
 */
public final class DndTabPaneFactory {
	private static MarkerFeedback CURRENT_FEEDBACK;

	private DndTabPaneFactory() {

	}

	/**
	 * Setup a drag and drop for the given instance
	 *
	 * @param feedbackType
	 *            the feedback type
	 * @param dragSetup
	 *            the drag setup
	 * @param detachHandler
	 *            the detach handler
	 * @return a node to add to the scene graph
	 */
	public static <D extends Node & DragSetup> Pane setup(FeedbackType feedbackType, D dragSetup, Consumer<GenericTab> detachHandler) {
		StackPane pane = new StackPane();
		setup(feedbackType, pane, dragSetup, detachHandler);
		pane.getChildren().add(dragSetup);
		return pane;
	}

	/**
	 * Create a tab pane and set the drag strategy
	 *
	 * @param setup
	 *            the setup instance for the pane
	 * @return the tab pane
	 */
	public static TabPane createDndTabPane(Consumer<DragSetup> setup) {
		return createDndTabPane(setup, false);
	}

	/**
	 * Create a tab pane with a default setup for drag feedback
	 *
	 * @param feedbackType
	 *            the feedback type
	 * @param setup
	 *            consumer to set up the tab pane
	 * @return a pane containing the TabPane
	 */
	public static Pane createDefaultDnDPane(FeedbackType feedbackType, Consumer<TabPane> setup) {
		return createDefaultDnDPane(feedbackType, false, setup);
	}

	/**
	 * Create a tab pane and set the drag strategy
	 *
	 * @param setup
	 *            the setup instance for the pane
	 * @param allowDetach
	 * 			  allow detaching
	 * @return the tab pane
	 */
	public static TabPane createDndTabPane(Consumer<DragSetup> setup, boolean allowDetach) {
		return new TabPane() {
			@Override
			protected javafx.scene.control.Skin<?> createDefaultSkin() {
				if( allowDetach ) {
					DnDTabPaneSkinFullDrag skin = new DnDTabPaneSkinFullDrag(this);
					setup.accept(skin);
					return skin;
				} else {
					DnDTabPaneSkin skin = new DnDTabPaneSkin(this);
					setup.accept(skin);
					return skin;
				}
			}
		};
	}

	/**
	 * Create a tab pane with a default setup for drag feedback
	 *
	 * @param feedbackType
	 *            the feedback type
	 * @param allowDetach
	 *            allow detaching
	 * @param setup
	 *            consumer to set up the tab pane
	 * @return a pane containing the TabPane
	 */
	public static Pane createDefaultDnDPane(FeedbackType feedbackType, boolean allowDetach, Consumer<TabPane> setup) {
		StackPane pane = new StackPane();
		TabPane tabPane = new TabPane() {
			@Override
			protected javafx.scene.control.Skin<?> createDefaultSkin() {
				if (allowDetach) {
					DnDTabPaneSkinFullDrag skin = new DnDTabPaneSkinFullDrag(this);
					setup(feedbackType, pane, skin, null);
					return skin;
				} else {
					DnDTabPaneSkin skin = new DnDTabPaneSkin(this);
					setup(feedbackType, pane, skin, null);
					return skin;
				}
			}
		};
		setup.accept(tabPane);
		pane.getChildren().add(tabPane);
		return pane;
	}

	/**
	 * Extract the tab content
	 *
	 * @param e
	 *            the event
	 * @return the content
	 */
	public static boolean hasDnDContent(Event e) {
		if (e instanceof DragEvent) {
			return ((DragEvent) e).getDragboard().hasContent(DnDTabPaneSkin.TAB_MOVE);
		} else if (e instanceof EFXDragEvent) {
			return ((EFXDragEvent) e).getDraggedContent() != null;
		}
		return false;
	}

	/**
	 * Extract the content
	 *
	 * @param e
	 *            the event
	 * @return the return value
	 */
	public static String getDnDContent(Event e) {
		if (e instanceof DragEvent) {
			return (String) ((DragEvent) e).getDragboard().getContent(DnDTabPaneSkin.TAB_MOVE);
		} else if (e instanceof EFXDragEvent) {
			return (String) ((EFXDragEvent) e).getDraggedContent();
		}
		return null;
	}

	/**
	 * Setup insert marker
	 *
	 * @param layoutNode
	 *            the layout node used to position
	 * @param setup
	 *            the setup
	 */
	@SuppressWarnings("null")
	static void setup(FeedbackType type, Pane layoutNode, DragSetup setup, Consumer<GenericTab> detachHandler) {
		setup.setStartFunction((t) -> Boolean.TRUE);
		setup.setFeedbackConsumer((d) -> handleFeedback(type, layoutNode, d));
		setup.setDropConsumer(d -> handleDropped(d, detachHandler));
		setup.setDragFinishedConsumer(DndTabPaneFactory::handleFinished);
	}

	private static void handleDropped(DroppedData data, Consumer<GenericTab> detachHandler) {
		if (data.dropType == DropType.DETACH) {
			if (detachHandler != null) {
				detachHandler.accept(data.draggedTab);
			}
		} else if (data.targetTab != null) {
			GenericTabPane targetPane = data.targetTab.getOwner();
			data.draggedTab.getOwner().remove(data.draggedTab);
			int idx = targetPane.indexOf(data.targetTab);
			if (data.dropType == DropType.AFTER) {
				if (idx + 1 <= targetPane.getTabNumber()) {
					targetPane.add(idx + 1, data.draggedTab);
				} else {
					targetPane.add(data.draggedTab);
				}
			} else {
				targetPane.add(idx, data.draggedTab);
			}
			data.draggedTab.getOwner().select(data.draggedTab);
		}
	}

	private static void handleFeedback(FeedbackType type, Pane layoutNode, FeedbackData data) {
		if (data.dropType == DropType.NONE) {
			cleanup();
			return;
		}

		MarkerFeedback f = CURRENT_FEEDBACK;
		if (f == null || !f.data.equals(data)) {
			cleanup();
			if (type == FeedbackType.MARKER) {
				CURRENT_FEEDBACK = handleMarker(layoutNode, data);
			} else {
				CURRENT_FEEDBACK = handleOutline(layoutNode, data);
			}
		}
	}

	private static void handleFinished(GenericTab tab) {
		cleanup();
	}

	static void cleanup() {
		if (CURRENT_FEEDBACK != null) {
			CURRENT_FEEDBACK.hide();
			CURRENT_FEEDBACK = null;
		}
	}

	private static MarkerFeedback handleMarker(Pane layoutNode, FeedbackData data) {
		PositionMarker marker = null;
		for (Node n : layoutNode.getChildren()) {
			if (n instanceof PositionMarker) {
				marker = (PositionMarker) n;
			}
		}

		if (marker == null) {
			marker = new PositionMarker();
			marker.setManaged(false);
			layoutNode.getChildren().add(marker);
		} else {
			marker.setVisible(true);
		}

		double w = marker.getBoundsInLocal().getWidth();
		double h = marker.getBoundsInLocal().getHeight();

		double ratio = data.bounds.getHeight() / h;
		ratio += 0.1;
		marker.setScaleX(ratio);
		marker.setScaleY(ratio);

		double wDiff = w / 2;
		double hDiff = (h - h * ratio) / 2;

		if (data.dropType == DropType.AFTER) {
			marker.relocate(data.bounds.getMinX() + data.bounds.getWidth() - wDiff, data.bounds.getMinY() - hDiff);
		} else {
			marker.relocate(data.bounds.getMinX() - wDiff, data.bounds.getMinY() - hDiff);
		}

		final PositionMarker fmarker = marker;

		return new MarkerFeedback(data) {

			@Override
			public void hide() {
				fmarker.setVisible(false);
			}
		};
	}

	@SuppressWarnings("null")
	private static MarkerFeedback handleOutline(Pane layoutNode, FeedbackData data) {
		TabOutlineMarker marker = null;

		for (Node n : layoutNode.getChildren()) {
			if (n instanceof TabOutlineMarker) {
				marker = (TabOutlineMarker) n;
			}
		}

		if (marker == null) {
			marker = new TabOutlineMarker(layoutNode.getBoundsInLocal(), new BoundingBox(data.bounds.getMinX(), data.bounds.getMinY(), data.bounds.getWidth(), data.bounds.getHeight()), data.dropType == DropType.BEFORE);
			marker.setManaged(false);
			marker.setMouseTransparent(true);
			layoutNode.getChildren().add(marker);
		} else {
			marker.updateBounds(layoutNode.getBoundsInLocal(), new BoundingBox(data.bounds.getMinX(), data.bounds.getMinY(), data.bounds.getWidth(), data.bounds.getHeight()), data.dropType == DropType.BEFORE);
			marker.setVisible(true);
		}

		final TabOutlineMarker fmarker = marker;

		return new MarkerFeedback(data) {

			@Override
			public void hide() {
				fmarker.setVisible(false);
			}
		};
	}

	private abstract static class MarkerFeedback {
		public final FeedbackData data;

		public MarkerFeedback(FeedbackData data) {
			this.data = data;
		}

		public abstract void hide();
	}

	/**
	 * The drop type
	 */
	public enum DropType {
		/**
		 * No dropping
		 */
		NONE,
		/**
		 * Dropped before a reference tab
		 */
		BEFORE,
		/**
		 * Dropped after a reference tab
		 */
		AFTER,
		/**
		 * Dropped in an area to detach
		 */
		DETACH
	}

	/**
	 * The feedback type to use
	 */
	public enum FeedbackType {
		/**
		 * Show a marker
		 */
		MARKER,
		/**
		 * Show an outline
		 */
		OUTLINE
	}

	/**
	 * Data to create a feedback
	 */
	public static class FeedbackData {
		/**
		 * The tab dragged
		 */
		public final @NonNull GenericTab draggedTab;
		/**
		 * The reference tab
		 */
		public final GenericTab targetTab;
		/**
		 * The bounds of the reference tab
		 */
		public final Bounds bounds;
		/**
		 * The drop type
		 */
		public final @NonNull DropType dropType;

		/**
		 * Create a feedback data
		 *
		 * @param draggedTab
		 *            the dragged tab
		 * @param targetTab
		 *            the reference tab
		 * @param bounds
		 *            the bounds of the reference tab
		 * @param dropType
		 *            the drop type
		 */
		public FeedbackData(@NonNull GenericTab draggedTab, GenericTab targetTab, Bounds bounds, @NonNull DropType dropType) {
			this.draggedTab = draggedTab;
			this.targetTab = targetTab;
			this.bounds = bounds;
			this.dropType = dropType;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((this.bounds == null) ? 0 : this.bounds.hashCode());
			result = prime * result + this.draggedTab.hashCode();
			result = prime * result + this.dropType.hashCode();
			result = prime * result + ((this.targetTab == null) ? 0 : this.targetTab.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			FeedbackData other = (FeedbackData) obj;
			if (this.bounds == null) {
				if (other.bounds != null)
					return false;
			} else if (!this.bounds.equals(other.bounds))
				return false;
			if (!this.draggedTab.equals(other.draggedTab))
				return false;
			if (this.dropType != other.dropType)
				return false;
			if (this.targetTab == null) {
				if (other.targetTab != null)
					return false;
			} else if (!this.targetTab.equals(other.targetTab))
				return false;
			return true;
		}

	}

	/**
	 * The drop data
	 */
	public static class DroppedData {
		/**
		 * The dragged tab
		 */
		public final @NonNull GenericTab draggedTab;
		/**
		 * The reference tab
		 */
		public final @Nullable GenericTab targetTab;
		/**
		 * The drop type
		 */
		public final @NonNull DropType dropType;

		/**
		 * The x coordinate relative to the screen
		 */
		public final double x;

		/**
		 * The y coordinate relative to the screen
		 */
		public final double y;

		/**
		 * Create drop data
		 *
		 * @param x
		 *            the x coordinate
		 *
		 * @param y
		 *            the y coordinate
		 *
		 * @param draggedTab
		 *            the dragged tab
		 * @param targetTab
		 *            the target tab
		 * @param dropType
		 *            the drop type
		 */
		public DroppedData(double x, double y, @NonNull GenericTab draggedTab, @Nullable GenericTab targetTab, @NonNull DropType dropType) {
			this.x = x;
			this.y = y;
			this.draggedTab = draggedTab;
			this.targetTab = targetTab;
			this.dropType = dropType;
		}
	}

	/**
	 * Setup of the drag and drop
	 */
	public interface DragSetup {
		/**
		 * Function to handle the starting of the the drag
		 *
		 * @param startFunction
		 *            the function
		 */
		public void setStartFunction(@Nullable Function<@NonNull GenericTab, @NonNull Boolean> startFunction);

		/**
		 * Consumer called to handle the finishing of the drag process
		 *
		 * @param dragFinishedConsumer
		 *            the consumer
		 */
		public void setDragFinishedConsumer(@Nullable Consumer<@NonNull GenericTab> dragFinishedConsumer);

		/**
		 * Consumer called to present drag feedback
		 *
		 * @param feedbackConsumer
		 *            the consumer to call
		 */
		public void setFeedbackConsumer(@Nullable Consumer<@NonNull FeedbackData> feedbackConsumer);

		/**
		 * Consumer called when the drop has to be handled
		 *
		 * @param dropConsumer
		 *            the consumer
		 */
		public void setDropConsumer(@Nullable Consumer<@NonNull DroppedData> dropConsumer);

		/**
		 * Function to translate the tab content into clipboard content
		 *
		 * @param clipboardDataFunction
		 *            the function
		 */
		public void setClipboardDataFunction(@Nullable Function<@NonNull GenericTab, @NonNull String> clipboardDataFunction);
	}
}

Back to the top