-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocsPage.java
More file actions
115 lines (104 loc) · 4.36 KB
/
Copy pathDocsPage.java
File metadata and controls
115 lines (104 loc) · 4.36 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
package ensemble;
import ensemble.generated.Samples;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Region;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
/**
* Ensmeble page for showing a page of documentation from the web
*/
public class DocsPage extends Region implements ChangeListener<String>, Page{
private final WebView webView = new WebView();
private final ScrollPane scrollPane = new ScrollPane();
private final VBox sideBar = new VBox(10);
private final Label sideBarTitle = new Label("Related Samples:");
private final PageBrowser pageBrowser;
private boolean isLocalChange = false;
private boolean showSideBar = false;
public DocsPage(PageBrowser pageBrowser) {
this.pageBrowser = pageBrowser;
getChildren().add(webView);
scrollPane.setContent(sideBar);
sideBar.setAlignment(Pos.TOP_CENTER);
sideBar.getChildren().add(sideBarTitle);
sideBarTitle.getStyleClass().add("sidebar-title");
scrollPane.setFitToWidth(true);
sideBar.setPadding(new Insets(10));
webView.getEngine().locationProperty().addListener(this);
}
@Override public ReadOnlyStringProperty titleProperty() {
return webView.getEngine().titleProperty();
}
@Override public String getTitle() {
return webView.getEngine().getTitle();
}
@Override public String getUrl() {
return webView.getEngine().getLocation();
}
@Override public Node getNode() {
return this;
}
@Override public void changed(ObservableValue<? extends String> ov, String oldLocation, String newLocation) {
if (!isLocalChange) pageBrowser.externalPageChange(newLocation);
updateSidebar(newLocation);
}
public void goToUrl(String url) {
isLocalChange = true;
webView.getEngine().load(url);
isLocalChange = false;
}
@Override protected void layoutChildren() {
final double w = getWidth();
final double h = getHeight();
if (showSideBar) {
final double sideBarWidth = sideBar.prefWidth(-1)+14;
webView.resize(w - sideBarWidth, h);
scrollPane.setLayoutX(w - sideBarWidth);
scrollPane.resize(sideBarWidth, h);
} else {
webView.resize(w,h);
}
}
private void updateSidebar(String url) {
String key = url;
if (key.startsWith("https://docs.oracle.com/javase/8/javafx/api/")) {
key = key.substring("https://docs.oracle.com/javase/8/javafx/api/".length(), key.lastIndexOf('.'));
key = key.replaceAll("/", ".");
} else if (key.startsWith("https://docs.oracle.com/javase/8/docs/api/")) {
key = key.substring("https://docs.oracle.com/javase/8/docs/api/".length(), key.lastIndexOf('.'));
key = key.replaceAll("/", ".");
}
SampleInfo[] samples = Samples.getSamplesForDoc(key);
if (samples == null || samples.length == 0) {
sideBar.getChildren().clear();
getChildren().remove(scrollPane);
showSideBar = false;
} else {
sideBar.getChildren().setAll(sideBarTitle);
for (final SampleInfo sample: samples) {
Button sampleButton = new Button(sample.name);
sampleButton.setCache(true);
sampleButton.getStyleClass().setAll("sample-button");
sampleButton.setGraphic(sample.getMediumPreview());
sampleButton.setContentDisplay(ContentDisplay.TOP);
sampleButton.setOnAction((ActionEvent actionEvent) -> {
pageBrowser.goToSample(sample);
});
sideBar.getChildren().add(sampleButton);
}
if (!showSideBar) getChildren().add(scrollPane);
showSideBar = true;
}
}
}