-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageBrowser.java
More file actions
189 lines (174 loc) · 6.86 KB
/
Copy pathPageBrowser.java
File metadata and controls
189 lines (174 loc) · 6.86 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
package ensemble;
import static ensemble.PlatformFeatures.WEB_SUPPORTED;
import ensemble.samplepage.SourcePage;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ReadOnlyBooleanProperty;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.Node;
import javafx.scene.layout.Region;
import java.util.LinkedList;
import ensemble.generated.Samples;
import ensemble.samplepage.SamplePage;
/**
* Sample page navigation with history.
*
* Also knows how to create Ensemble pages.
*/
public class PageBrowser extends Region {
public static final String HOME_URL = "home";
private HomePage homePage;
private Page currentPage;
private SamplePage samplePage;
private SourcePage sourcePage;
private String currentPageUrl;
private DocsPage docsPage;
private LinkedList<String> pastHistory = new LinkedList<>();
private LinkedList<String> futureHistory = new LinkedList<>();
private BooleanProperty forwardPossible = new SimpleBooleanProperty(false);
public ReadOnlyBooleanProperty forwardPossibleProperty() { return forwardPossible; }
public boolean isForwardPossible() { return forwardPossible.get(); }
private BooleanProperty backPossible = new SimpleBooleanProperty(false);
public ReadOnlyBooleanProperty backPossibleProperty() { return backPossible; }
public boolean isBackPossible() { return backPossible.get(); }
private BooleanProperty atHome = new SimpleBooleanProperty(false);
public ReadOnlyBooleanProperty atHomeProperty() { return atHome; }
public boolean isAtHome() { return atHome.get(); }
private StringProperty currentPageTitle = new SimpleStringProperty(null);
public ReadOnlyStringProperty currentPageTitleProperty() { return currentPageTitle; };
public String getCurrentPageTitle() { return currentPageTitle.get(); }
public void forward() {
String newUrl = futureHistory.pop();
if (newUrl != null) {
pastHistory.push(getCurrentPageUrl());
goToPage(newUrl, null, false);
}
}
public void backward() {
String newUrl = pastHistory.pop();
if (newUrl != null) {
futureHistory.push(getCurrentPageUrl());
goToPage(newUrl, null, false);
}
}
public void goToSample(SampleInfo sample) {
goToPage("sample://"+sample.ensemblePath, sample, true);
}
public void goToPage(String url) {
goToPage(url, null, true);
}
public void goHome() {
goToPage(HOME_URL, null, true);
}
/**
* This is called when a inner url has changed inside of a page and we want
* to update the history.
*
* @param newUrl The new url that the currentPage node is displaying
*/
public void externalPageChange(String newUrl) {
if (currentPageUrl != null) {
pastHistory.push(getCurrentPageUrl());
}
futureHistory.clear();
currentPageUrl = newUrl;
}
private void goToPage(String url, SampleInfo sample, boolean updateHistory) {
Page nextPage = null;
// get node for page
if (url.equals(HOME_URL)) {
nextPage = getHomePage();
} else if (url.startsWith("http://") || url.startsWith("https://")) {
if (WEB_SUPPORTED) {
nextPage = updateDocsPage(url);
} else {
System.err.println("Web pages are not supported and links to them should be disabled!");
}
} else if (sample != null) {
nextPage = updateSamplePage(sample, url);
} else if (url.startsWith("sample://")) {
String samplePath = url.substring("sample://".length());
if (samplePath.contains("?")) {
samplePath = samplePath.substring(0, samplePath.indexOf('?') - 1);
}
sample = Samples.ROOT.sampleForPath(samplePath);
if (sample != null) {
nextPage = updateSamplePage(sample, url);
} else {
throw new UnsupportedOperationException("Unknown sample url ["+url+"]");
}
} else if (url.startsWith("sample-src://")) {
String samplePath = url.substring("sample-src://".length());
if (samplePath.contains("?")) {
samplePath = samplePath.substring(0, samplePath.indexOf('?') - 1);
}
sample = Samples.ROOT.sampleForPath(samplePath);
if (sample != null) {
nextPage = updateSourcePage(sample);
} else {
System.err.println("Unknown sample url [" + url + "]");
}
} else {
System.err.println("Unknown ensemble page url [" + url + "]");
}
if (nextPage != null) {
// update history
if (updateHistory) {
if (currentPageUrl != null) {
pastHistory.push(getCurrentPageUrl());
}
futureHistory.clear();
}
currentPageUrl = url;
// remove old page
if (currentPage != null) {
getChildren().remove((Node) currentPage);
}
currentPage = nextPage;
getChildren().add(currentPage.getNode());
// update properties
atHome.set(url.equals(HOME_URL));
forwardPossible.set(!futureHistory.isEmpty());
backPossible.set(!pastHistory.isEmpty());
currentPageTitle.bind(currentPage.titleProperty());
}
}
@Override protected void layoutChildren() {
if (currentPage != null) {
currentPage.getNode().resize(getWidth(), getHeight());
}
}
public String getCurrentPageUrl() {
return currentPageUrl;
}
private SamplePage updateSamplePage(SampleInfo sample, String url) {
if (samplePage == null) {
samplePage = new SamplePage(sample, url, this);
} else {
samplePage.update(sample, url);
}
return samplePage;
}
private SourcePage updateSourcePage(SampleInfo sample) {
if (sourcePage == null) {
sourcePage = new SourcePage();
}
sourcePage.setSampleInfo(sample);
return sourcePage;
}
private Page getHomePage() {
if (homePage == null) {
homePage = new HomePage(this);
}
return homePage;
}
private DocsPage updateDocsPage(String url) {
if (docsPage == null) {
docsPage = new DocsPage(this);
}
docsPage.goToUrl(url);
return docsPage;
}
}