Skip to content

Commit f9345a5

Browse files
committed
tut3 ok
1 parent 0dd8a4b commit f9345a5

File tree

5 files changed

+106
-76
lines changed

5 files changed

+106
-76
lines changed

gef5.mvc.tutorial1/src/gef5/mvc/tutorial/Gef5MvcTutorial1.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
public class Gef5MvcTutorial1 extends Application {
2222

2323
public static void main(String[] args) {
24+
System.setProperty("javafx.sg.warn", "true");
2425
Application.launch(args);
2526
}
2627

gef5.mvc.tutorial2/src/gef5/mvc/tutorial/Gef5MvcTutorial.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ protected Module createGuiceModule() {
122122
protected void configure() {
123123
super.configure();
124124

125-
binder().bind(new TypeLiteral<IContentPartFactory>() {
126-
}).toInstance(new ModelPartFactory());
125+
bind(IContentPartFactory.class).to(ModelPartFactory.class);
127126

128127
}
129128
};

gef5.mvc.tutorial3/src/gef5/mvc/tutorial/Gef5MvcTutorial.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@
1414
import javafx.application.*;
1515
import javafx.scene.*;
1616
import javafx.scene.control.*;
17+
import javafx.scene.effect.*;
18+
import javafx.scene.effect.Light.*;
1719
import javafx.scene.layout.*;
20+
import javafx.scene.paint.*;
1821
import javafx.stage.Stage;
1922

2023
public class Gef5MvcTutorial extends Application {
2124

2225
private Model model;
26+
private final AnchorPane paneCtrl = new AnchorPane();
27+
private final AnchorPane paneDraw = new AnchorPane();
28+
private final VBox vbox = new VBox(paneCtrl, paneDraw);
2329

2430
public static void main(String[] args) {
31+
System.setProperty("javafx.sg.warn", "true");
2532
Application.launch(args);
2633
}
2734

@@ -34,9 +41,15 @@ public void start(final Stage primaryStage) throws Exception {
3441

3542
IViewer viewer = domain.getAdapter(IViewer.class);
3643

37-
AnchorPane paneCtrl = new AnchorPane();
38-
AnchorPane paneDraw = new AnchorPane();
39-
VBox vbox = new VBox(paneCtrl, paneDraw);
44+
configureStage(primaryStage, viewer, "GEF4 MVC Tutorial 3 - update model");
45+
46+
domain.activate();
47+
48+
viewer.getContents().setAll(createContents());
49+
configureGlobalVisualEffect(viewer);
50+
}
51+
52+
private void configureStage(final Stage stage, IViewer viewer, String title) {
4053
vbox.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
4154

4255
Button btnUpdateModel = new Button("update model");
@@ -55,17 +68,45 @@ public void start(final Stage primaryStage) throws Exception {
5568
AnchorPane.setRightAnchor(drawingPane, 10d);
5669
AnchorPane.setBottomAnchor(drawingPane, 10d);
5770

58-
primaryStage.setScene(new Scene(vbox));
71+
stage.setScene(new Scene(vbox));
5972

60-
primaryStage.setResizable(true);
61-
primaryStage.setWidth(640);
62-
primaryStage.setHeight(480);
63-
primaryStage.setTitle("GEF4 MVC Tutorial 3 - 2 level model");
64-
primaryStage.show();
73+
stage.setResizable(true);
74+
stage.setWidth(640);
75+
stage.setHeight(480);
76+
stage.setTitle(title);
77+
stage.show();
78+
}
6579

66-
domain.activate();
80+
private void configureGlobalVisualEffect(IViewer viewer) {
81+
DropShadow outerShadow = new DropShadow();
82+
outerShadow.setRadius(3);
83+
outerShadow.setSpread(0.2);
84+
outerShadow.setOffsetX(3);
85+
outerShadow.setOffsetY(3);
86+
outerShadow.setColor(new Color(0.3, 0.3, 0.3, 0.5));
87+
88+
Distant light = new Distant();
89+
light.setAzimuth(-135.0f);
90+
light.setElevation(70);
91+
92+
Lighting l = new Lighting();
93+
l.setLight(light);
94+
l.setSurfaceScale(1.0f);
95+
l.setSpecularConstant(2.0f);
96+
l.setSpecularExponent(40.0f);
97+
98+
Blend effects = new Blend(BlendMode.MULTIPLY);
99+
effects.setTopInput(l);
100+
effects.setBottomInput(outerShadow);
101+
102+
applyEffectOnContentLayer(viewer, effects);
103+
}
67104

68-
viewer.getContents().setAll(createContents());
105+
private void applyEffectOnContentLayer(IViewer viewer, Blend effects) {
106+
// LayeredRootPart is the default impl for the root part, configured in
107+
// MvcFxModule
108+
// see https://www.eclipse.org/forums/index.php/m/1820560/#msg_1820560
109+
((LayeredRootPart) viewer.getRootPart()).getContentLayer().setEffect(effects);
69110
}
70111

71112
protected List<? extends Object> createContents() {
@@ -79,8 +120,7 @@ protected Module createGuiceModule() {
79120
protected void configure() {
80121
super.configure();
81122

82-
binder().bind(new TypeLiteral<IContentPartFactory>() {
83-
}).toInstance(new ModelPartFactory());
123+
bind(IContentPartFactory.class).to(ModelPartFactory.class);
84124

85125
}
86126
};

gef5.mvc.tutorial3/src/gef5/mvc/tutorial/model/TextNode.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package gef5.mvc.tutorial.model;
22

3-
import org.eclipse.gef.geometry.planar.Point;
3+
import org.eclipse.gef.geometry.planar.*;
44

5-
import javafx.beans.property.ObjectProperty;
6-
import javafx.beans.property.SimpleObjectProperty;
7-
import javafx.beans.value.ChangeListener;
8-
import javafx.scene.paint.Color;
5+
import javafx.beans.property.*;
6+
import javafx.beans.value.*;
7+
import javafx.scene.paint.*;
98

109
public class TextNode {
1110

@@ -20,10 +19,10 @@ public class TextNode {
2019
public TextNode(double x, double y, String text) {
2120
Point point = new Point(x, y);
2221

23-
this.position = new SimpleObjectProperty<Point>(this, POSITION_PROPERTY);
24-
this.text = new SimpleObjectProperty<String>(this, TEXT_PROPERTY);
22+
position = new SimpleObjectProperty<>(this, POSITION_PROPERTY);
23+
this.text = new SimpleObjectProperty<>(this, TEXT_PROPERTY);
2524

26-
this.position.setValue(point);
25+
position.setValue(point);
2726
this.text.setValue(text);
2827
}
2928

@@ -49,14 +48,21 @@ public void setPosition(Point position) {
4948

5049
public void doChange() {
5150
setPosition(new Point(getPosition().x + Math.random() * 10 - 5, getPosition().y + Math.random() * 10 - 5));
52-
setText(String.format("%s %s", getText().split(" ")[0], Math.round(Math.random() * 100)));
51+
52+
if (Math.random() > 0.5) {
53+
setText(String.format("%s %s", getText().split(" ")[0], Math.round(Math.random() * 100)));
54+
} else {
55+
setText(getText().split(" ")[0]);
56+
}
5357
}
5458

5559
public void addPropertyChangeListener(ChangeListener<Object> pointObserver) {
5660
position.addListener(pointObserver);
61+
text.addListener(pointObserver);
5762
}
5863

5964
public void removePropertyChangeListener(ChangeListener<Object> pointObserver) {
6065
position.removeListener(pointObserver);
66+
text.removeListener(pointObserver);
6167
}
6268
}

gef5.mvc.tutorial3/src/gef5/mvc/tutorial/parts/TextNodePart.java

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package gef5.mvc.tutorial.parts;
22

3-
import java.beans.*;
43
import java.util.*;
54

65
import org.eclipse.gef.fx.nodes.*;
@@ -16,10 +15,10 @@
1615
import javafx.scene.paint.*;
1716
import javafx.scene.text.*;
1817

19-
public class TextNodePart extends AbstractContentPart<Group> implements PropertyChangeListener {
18+
public class TextNodePart extends AbstractContentPart<Group> {
2019

21-
private Text text;
22-
private GeometryNode<RoundedRectangle> fxRoundedRectNode;
20+
private final Text text = new Text();
21+
private final GeometryNode<RoundedRectangle> rectangleNode = new GeometryNode<>();
2322

2423
private final ChangeListener<Object> objectObserver = new ChangeListener<Object>() {
2524
@Override
@@ -47,67 +46,52 @@ public TextNode getContent() {
4746

4847
@Override
4948
protected Group doCreateVisual() {
49+
50+
text.setFont(Font.font("Monospace", FontWeight.BOLD, 50));
51+
text.setFill(Color.BLACK);
52+
text.setStrokeWidth(2);
53+
text.setTextOrigin(VPos.CENTER);
54+
55+
rectangleNode.setGeometry(new RoundedRectangle(new Rectangle(), 10, 10));
56+
rectangleNode.setStroke(Color.BLACK);
57+
5058
Group group = new Group();
51-
text = new Text();
52-
fxRoundedRectNode = new GeometryNode<>();
59+
group.getChildren().addAll(rectangleNode, text);
5360

54-
group.getChildren().add(fxRoundedRectNode);
55-
group.getChildren().add(text);
5661
return group;
5762
}
5863

5964
@Override
6065
protected void doRefreshVisual(Group visual) {
61-
TextNode model = getContent();
6266

63-
Font font = Font.font("Monospace", FontWeight.BOLD, 50);
64-
Color textColor = Color.BLACK;
65-
int textStrokeWidth = 2;
66-
67-
text.setText(model.getText());
68-
text.setFont(font);
69-
text.setFill(textColor);
70-
text.setStrokeWidth(textStrokeWidth);
71-
72-
// measure size
73-
Bounds textBounds = msrText(model.getText(), font, textStrokeWidth);
74-
75-
Rectangle bounds = new Rectangle(model.getPosition(),
76-
new Dimension(textBounds.getWidth() + textBounds.getHeight(), textBounds.getHeight() * 1.5));
77-
78-
// the rounded rectangle
79-
{
80-
RoundedRectangle roundRect = new RoundedRectangle(bounds, 10, 10);
81-
fxRoundedRectNode.setGeometry(roundRect);
82-
fxRoundedRectNode.setFill(model.getColor());
83-
fxRoundedRectNode.setStroke(Color.BLACK);
84-
fxRoundedRectNode.setStrokeWidth(2);
85-
fxRoundedRectNode.toBack();
86-
}
87-
// the text
88-
{
89-
text.setTextOrigin(VPos.CENTER);
90-
text.setY(bounds.getY() + bounds.getHeight() / 2);
91-
text.setX(bounds.getX() + bounds.getWidth() / 2 - textBounds.getWidth() / 2);
92-
text.toFront();
93-
}
67+
text.setText(getContent().getText());
68+
69+
Bounds textBounds = text.getLayoutBounds();
70+
71+
refreshRectangleBounds(textBounds);
72+
73+
refreshText(visual, textBounds);
74+
75+
visual.setTranslateX(getContent().getPosition().x);
76+
visual.setTranslateY(getContent().getPosition().y);
9477
}
9578

96-
private Bounds msrText(String string, Font font, int textStrokeWidth) {
97-
Text msrText = new Text(string);
98-
msrText.setFont(font);
99-
msrText.setStrokeWidth(textStrokeWidth);
79+
private void refreshRectangleBounds(Bounds textBounds) {
80+
TextNode model = getContent();
81+
double textWidth = textBounds.getWidth();
82+
double textHeight = textBounds.getHeight();
83+
84+
Rectangle bounds = new Rectangle(new Point(), new Dimension(textWidth + textHeight, textHeight * 1.5));
10085

101-
new Scene(new Group(msrText));
102-
Bounds textBounds = msrText.getLayoutBounds();
103-
return textBounds;
86+
rectangleNode.setGeometry(new RoundedRectangle(bounds, 10, 10));
87+
rectangleNode.setFill(model.getColor());
10488
}
10589

106-
@Override
107-
public void propertyChange(PropertyChangeEvent evt) {
108-
if (evt.getSource() == getContent()) {
109-
refreshVisual();
110-
}
90+
private void refreshText(Group visual, Bounds textBounds) {
91+
text.setText(getContent().getText());
92+
Rectangle bounds = rectangleNode.getGeometry().getBounds();
93+
text.setY(bounds.getHeight() / 2);
94+
text.setX(bounds.getWidth() / 2 - textBounds.getWidth() / 2);
11195
}
11296

11397
@Override

0 commit comments

Comments
 (0)