Skip to content
This repository was archived by the owner on Aug 15, 2021. It is now read-only.

Commit 486fa60

Browse files
committed
screenshots added for wiki
1 parent ca458a5 commit 486fa60

File tree

9 files changed

+128
-50
lines changed

9 files changed

+128
-50
lines changed
Loading
Loading
2.11 KB
Loading
Loading

src/main/java/com/dansoftware/pdfdisplayer/PDFDisplayer.java

+75-37
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.dansoftware.pdfdisplayer;
22

33
import javafx.application.Platform;
4-
import javafx.collections.ObservableList;
4+
import javafx.beans.value.ChangeListener;
5+
import javafx.beans.value.ObservableValue;
56
import javafx.concurrent.Worker;
67
import javafx.scene.Parent;
78
import javafx.scene.web.WebEngine;
@@ -10,7 +11,6 @@
1011

1112
import java.io.*;
1213
import java.net.URL;
13-
import java.nio.file.Files;
1414
import java.util.Base64;
1515

1616
public class PDFDisplayer {
@@ -20,9 +20,11 @@ public class PDFDisplayer {
2020
private ProcessListener processListener;
2121

2222
private WebView nodeValue;
23+
private String loadScript;
2324
private String toExecuteWhenPDFJSLoaded = "";
2425

2526

27+
2628
public PDFDisplayer() {
2729
}
2830

@@ -79,7 +81,7 @@ public void displayPdf(InputStream inputStream) throws IOException {
7981
try {
8082
nodeValue.getEngine().executeScript(js);
8183
} catch (Exception ex) {
82-
if (!pdfJsLoaded) toExecuteWhenPDFJSLoaded += js;
84+
if (!pdfJsLoaded) loadScript = js;
8385
}
8486
});
8587

@@ -89,27 +91,52 @@ public void displayPdf(InputStream inputStream) throws IOException {
8991

9092
}
9193

92-
@SuppressWarnings("all")
94+
9395
public void setSecondaryToolbarToggleVisibility(boolean value) {
94-
String css;
96+
setVisibilityOf("secondaryToolbarToggle", value);
97+
98+
String js;
9599
if (value){
100+
js = new StringBuilder()
101+
.append("var element = document.getElementsByClassName('verticalToolbarSeparator')[0];")
102+
.append("element.style.display = 'inherit';")
103+
.append("element.style.visibility = 'inherit';")
104+
.toString();
105+
} else {
106+
js = new StringBuilder()
107+
.append("var element = document.getElementsByClassName('verticalToolbarSeparator')[0];")
108+
.append("element.style.display = 'none';")
109+
.append("element.style.visibility = 'hidden';")
110+
.toString();
111+
}
112+
113+
try {
114+
nodeValue.getEngine().executeScript(js);
115+
} catch (Exception ex){
116+
if (!pdfJsLoaded) toExecuteWhenPDFJSLoaded += js;
117+
}
118+
}
119+
120+
@SuppressWarnings("all")
121+
public void setVisibilityOf(String id, boolean value){
122+
String css;
123+
if (value) {
96124
css = new StringBuilder()
97-
.append("document.getElementById('secondaryToolbarToggle').style.display = 'inherit';")
98-
.append("document.getElementById('secondaryToolbarToggle').style.visibility = 'inherit';")
125+
.append("document.getElementById('" + id + "').style.display = 'inherit';")
126+
.append("document.getElementById('" + id + "').style.visibility = 'inherit';")
99127
.toString();
100128
} else {
101129
css = new StringBuilder()
102-
.append("document.getElementById('secondaryToolbarToggle').style.display = 'none';")
103-
.append("document.getElementById('secondaryToolbarToggle').style.visibility = 'hidden';")
130+
.append("document.getElementById('" + id + "').style.display = 'none';")
131+
.append("document.getElementById('" + id + "').style.visibility = 'hidden';")
104132
.toString();
105133
}
106134

107135
try {
108136
nodeValue.getEngine().executeScript(css);
109-
} catch (Exception ex){
137+
} catch (Exception ex) {
110138
if (!pdfJsLoaded) this.toExecuteWhenPDFJSLoaded += css;
111139
}
112-
113140
}
114141

115142
public void navigateByPage(int pageNum) {
@@ -125,53 +152,64 @@ public void setProcessListener(ProcessListener listener) {
125152
this.processListener = listener;
126153
}
127154

155+
public void executeScript(String js) {
156+
try {
157+
this.nodeValue.getEngine().executeScript(js);
158+
} catch (Exception ex) {
159+
if (!pdfJsLoaded) toExecuteWhenPDFJSLoaded += String.format("%s;", js);
160+
}
161+
}
162+
128163
private void updateProcessListener(boolean val) {
129164
if (processListener != null && pdfJsLoaded) processListener.listen(val);
130165
}
131166

132167
private WebView createWebView() {
133168
WebView webView = new WebView();
169+
webView.setContextMenuEnabled(false);
170+
webView.getStylesheets().add("/com/dansoftware/pdfdisplayer/base.css");
171+
134172
WebEngine engine = webView.getEngine();
135173
String url = getClass().getResource("/pdfjs/web/viewer.html").toExternalForm();
136174

137175
engine.setJavaScriptEnabled(true);
138176
engine.load(url);
139177

140178
if (processListener != null) processListener.listen(false);
179+
180+
141181
engine.getLoadWorker()
142182
.stateProperty()
143-
.addListener((observable, oldValue, newValue) -> {
144-
JSObject window = (JSObject) engine.executeScript("window");
145-
window.setMember("java", new JSLogListener());
146-
engine.executeScript("console.log = function(message){ try {java.log(message);} catch(e) {} };");
147-
148-
if (newValue == Worker.State.SUCCEEDED) {
149-
try {
150-
if (processListener != null) processListener.listen(pdfJsLoaded = true);
151-
152-
engine.executeScript(toExecuteWhenPDFJSLoaded);
153-
toExecuteWhenPDFJSLoaded = null;
154-
} catch (Exception e) {
155-
throw new RuntimeException(e);
156-
}
157-
}
158-
});
183+
.addListener(
184+
new ChangeListener<Worker.State>() {
185+
@Override
186+
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
187+
JSObject window = (JSObject) engine.executeScript("window");
188+
window.setMember("java", new JSLogListener());
189+
engine.executeScript("console.log = function(message){ try {java.log(message);} catch(e) {} };");
190+
191+
if (newValue == Worker.State.SUCCEEDED) {
192+
try {
193+
if (processListener != null) processListener.listen(pdfJsLoaded = true);
194+
195+
if (loadScript != null)
196+
engine.executeScript(loadScript);
197+
198+
engine.executeScript(toExecuteWhenPDFJSLoaded);
199+
toExecuteWhenPDFJSLoaded = null;
200+
observable.removeListener(this);
201+
} catch (Exception e) {
202+
throw new RuntimeException(e);
203+
}
204+
}
205+
}
206+
});
159207

160208
return webView;
161209

162210
}
163211

164-
public ObservableList<String> getStylesSheets(){
165-
return nodeValue.getStylesheets();
166-
}
167212

168-
public void executeScript(String js) {
169-
try {
170-
this.nodeValue.getEngine().executeScript(js);
171-
} catch (Exception ex) {
172-
if (!pdfJsLoaded) toExecuteWhenPDFJSLoaded += String.format("%s;", js);
173-
}
174-
}
175213

176214
public Parent toNode() {
177215
if (nodeValue == null)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*Styles of the scroll-bar*/
2+
.scroll-bar:horizontal,
3+
.scroll-bar:vertical {
4+
5+
-fx-background-color: #474a48;
6+
}
7+
8+
.scroll-bar:horizontal .thumb,
9+
.scroll-bar:vertical .thumb {
10+
-fx-background-color: #9d9b9b;
11+
-fx-background-radius: 0em;
12+
}
13+
14+
.scroll-bar .corner {
15+
-fx-background-color: #dbdbdb;
16+
-fx-disabled: true;
17+
}

src/test/java/Demo.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ else if (!alertStage.isShowing())
6060
}).start();
6161

6262
displayer.setSecondaryToolbarToggleVisibility(true);
63-
displayer.getStylesSheets().add("style.css");
63+
displayer.toNode().getStylesheets().add("style.css");
6464

6565
displayer.executeScript("document.getElementById('secondaryToolbarToggle').style.backgroundColor = 'blue';");
6666

src/test/java/SimpleDemo.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import com.dansoftware.pdfdisplayer.JSLogListener;
2+
import com.dansoftware.pdfdisplayer.PDFDisplayer;
3+
import javafx.application.Application;
4+
import javafx.scene.Scene;
5+
import javafx.scene.control.Button;
6+
import javafx.scene.layout.StackPane;
7+
import javafx.scene.layout.VBox;
8+
import javafx.stage.Stage;
9+
10+
import java.net.URL;
11+
12+
public class SimpleDemo extends Application {
13+
private boolean visible;
14+
@Override
15+
public void start(Stage primaryStage) throws Exception {
16+
17+
PDFDisplayer displayer = new PDFDisplayer(new URL("https://www.tutorialspoint.com/javafx/javafx_tutorial.pdf"));
18+
displayer.setSecondaryToolbarToggleVisibility(visible);
19+
displayer.setVisibilityOf("sidebarToggle", false);
20+
21+
Button btn = new Button("Hide/Show");
22+
btn.setOnAction(event -> {
23+
displayer.setSecondaryToolbarToggleVisibility(visible = !visible);
24+
});
25+
26+
JSLogListener.setOutputStream(System.err);
27+
28+
primaryStage.setScene(new Scene(new VBox(displayer.toNode(), btn)));
29+
primaryStage.show();
30+
}
31+
32+
public static void main(String[] args) {
33+
launch(args);
34+
}
35+
}

src/test/resources/style.css

-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
11

2-
/*Styles of the scroll-bar*/
3-
.scroll-bar:horizontal,
4-
.scroll-bar:vertical {
5-
6-
-fx-background-color: #474a48;
7-
}
8-
9-
.scroll-bar:horizontal .thumb,
10-
.scroll-bar:vertical .thumb {
11-
-fx-background-color: #9d9b9b;
12-
-fx-background-radius: 0em;
13-
}

0 commit comments

Comments
 (0)