/
FXResizePolicy.java
116 lines (99 loc) · 3.64 KB
/
FXResizePolicy.java
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
/*******************************************************************************
* Copyright (c) 2015 itemis AG 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:
* Matthias Wienand (itemis AG) - initial API and implementation
*
*******************************************************************************/
package org.eclipse.gef4.mvc.fx.policies;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.operations.IUndoableOperation;
import org.eclipse.gef4.geometry.planar.Dimension;
import org.eclipse.gef4.mvc.fx.operations.FXResizeNodeOperation;
import org.eclipse.gef4.mvc.fx.operations.FXRevealOperation;
import org.eclipse.gef4.mvc.fx.parts.FXCircleSegmentHandlePart;
import org.eclipse.gef4.mvc.operations.ForwardUndoCompositeOperation;
import org.eclipse.gef4.mvc.operations.ITransactional;
import org.eclipse.gef4.mvc.policies.AbstractPolicy;
import javafx.geometry.Bounds;
import javafx.scene.Node;
public class FXResizePolicy extends AbstractPolicy<Node>
implements ITransactional {
protected FXResizeNodeOperation resizeOperation;
protected ForwardUndoCompositeOperation resizeAndRevealOperation;
// can be overridden by subclasses to add an operation for model changes
@Override
public IUndoableOperation commit() {
IUndoableOperation commit = null;
if (resizeOperation.hasEffect()) {
commit = resizeAndRevealOperation;
}
resizeAndRevealOperation = null;
resizeOperation = null;
return commit;
}
protected Dimension getInitialSize(Node visualToResize) {
Bounds layoutBounds = visualToResize.getLayoutBounds();
return new Dimension(layoutBounds.getWidth(), layoutBounds.getHeight());
}
protected double getMinimumHeight() {
return FXCircleSegmentHandlePart.DEFAULT_SIZE;
}
protected double getMinimumWidth() {
return FXCircleSegmentHandlePart.DEFAULT_SIZE;
}
protected Node getVisualToResize() {
return getHost().getVisual();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.gef4.mvc.fx.policies.ITransactionalPolicy#init()
*/
@Override
public void init() {
// create "empty" operation
Node visualToResize = getVisualToResize();
resizeOperation = new FXResizeNodeOperation("Resize", visualToResize,
getInitialSize(visualToResize), 0, 0);
resizeAndRevealOperation = new ForwardUndoCompositeOperation(
resizeOperation.getLabel());
resizeAndRevealOperation.add(resizeOperation);
resizeAndRevealOperation.add(new FXRevealOperation(getHost()));
}
public void performResize(double dw, double dh) {
Node visual = resizeOperation.getVisual();
boolean resizable = visual.isResizable();
// convert resize into relocate in case node is not resizable
double layoutDw = resizable ? dw : 0;
double layoutDh = resizable ? dh : 0;
// ensure visual is not resized below threshold
if (resizable) {
if (resizeOperation.getOldSize().width
+ layoutDw < getMinimumWidth()) {
layoutDw = getMinimumWidth()
- resizeOperation.getOldSize().width;
}
if (resizeOperation.getOldSize().height
+ layoutDh < getMinimumHeight()) {
layoutDh = getMinimumHeight()
- resizeOperation.getOldSize().height;
}
}
updateOperation(layoutDw, layoutDh);
// locally execute operation
try {
resizeAndRevealOperation.execute(null, null);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
protected void updateOperation(double layoutDw, double layoutDh) {
resizeOperation.setDw(layoutDw);
resizeOperation.setDh(layoutDh);
}
}