This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
256e829
commit a716fed
Showing
33 changed files
with
1,079 additions
and
166 deletions.
There are no files selected for viewing
226 changes: 226 additions & 0 deletions
226
src/main/java/com/github/shyykoserhiy/gfm/browser/BrowserFx.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
package com.github.shyykoserhiy.gfm.browser; | ||
|
||
import javafx.application.Platform; | ||
import javafx.beans.value.ChangeListener; | ||
import javafx.beans.value.ObservableValue; | ||
import javafx.concurrent.Worker.State; | ||
import javafx.embed.swing.JFXPanel; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.AnchorPane; | ||
import javafx.scene.web.WebHistory; | ||
import javafx.scene.web.WebView; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import java.awt.*; | ||
import java.io.File; | ||
|
||
public class BrowserFx implements IsBrowser { | ||
private final JPanel jPanel; | ||
private WebView webView; | ||
private final JFXPanelRetina jfxPanelRetina; | ||
|
||
public BrowserFx() { | ||
jPanel = new JPanel(new BorderLayout(), true); | ||
jfxPanelRetina = new JFXPanelRetina(); // initializing javafx | ||
jPanel.add(jfxPanelRetina, BorderLayout.CENTER); | ||
Platform.setImplicitExit(false); | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView = new WebView(); | ||
AnchorPane anchorPane = new AnchorPane(); | ||
AnchorPane.setTopAnchor(webView, 0.0); | ||
AnchorPane.setBottomAnchor(webView, 0.0); | ||
AnchorPane.setLeftAnchor(webView, 0.0); | ||
AnchorPane.setRightAnchor(webView, 0.0); | ||
anchorPane.getChildren().add(webView); | ||
jfxPanelRetina.setScene(new Scene(anchorPane)); | ||
} | ||
}); | ||
} | ||
|
||
public synchronized WebView getWebView() { | ||
return webView; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public synchronized JComponent getComponent() { | ||
return jPanel; | ||
} | ||
|
||
@Override | ||
public void executeJavaScriptAndReturnValue(final String script, final JsExecutedListener jsExecutedListener) { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
Object result; | ||
try { | ||
result = webView.getEngine().executeScript(script); | ||
} catch (netscape.javascript.JSException e) { | ||
result = e.toString(); | ||
} | ||
if (result == null) { | ||
jsExecutedListener.onJsExecuted("null"); | ||
} | ||
jsExecutedListener.onJsExecuted(result.toString()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void goBack() { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
WebHistory history = webView.getEngine().getHistory(); | ||
if (canGoBack()) { | ||
history.go(-1); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void goForward() { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
WebHistory history = webView.getEngine().getHistory(); | ||
if (canGoForward()) { | ||
history.go(+1); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void reload() { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().reload(); | ||
} | ||
}); | ||
|
||
} | ||
|
||
@Override | ||
public void stop() { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().getLoadWorker().cancel(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void addLoadListener(final LoadListener loadAdapter) { | ||
if (webView != null) { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<State>() { | ||
@Override | ||
public void changed(ObservableValue ov, State oldState, State newState) { | ||
if (newState == State.SCHEDULED) { | ||
loadAdapter.onStartLoadingFrame(); | ||
return; | ||
} | ||
if (newState == State.RUNNING) { | ||
loadAdapter.onProvisionalLoadingFrame(); | ||
return; | ||
} | ||
loadAdapter.onFinishLoadingFrame(); //CANCELED, FAILED, READY? | ||
} | ||
}); | ||
} | ||
}); | ||
} else { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
addLoadListener(loadAdapter); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return webView.getEngine().getLocation(); | ||
} | ||
|
||
@Override | ||
public boolean canGoForward() { | ||
WebHistory history = webView.getEngine().getHistory(); | ||
int previousIndex = history.getCurrentIndex() + 1; | ||
return previousIndex < history.getEntries().size(); | ||
} | ||
|
||
@Override | ||
public boolean canGoBack() { | ||
WebHistory history = webView.getEngine().getHistory(); | ||
int previousIndex = history.getCurrentIndex() - 1; | ||
return previousIndex > 0; | ||
} | ||
|
||
@Override | ||
public synchronized void dispose() { | ||
jfxPanelRetina.removeNotify(); //fixme @see com.github.shyykoserhiy.gfm.editor.GfmPreviewFX.JFXPanelRetina.removeNotify() | ||
} | ||
|
||
@Override | ||
public synchronized void loadUrl(@NotNull final String url) { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().load(url); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized void loadFile(@NotNull final File file) { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().load("file:" + file.getAbsolutePath()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized void loadContent(@NotNull final String content) { | ||
Platform.runLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getEngine().loadContent(content); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Fix for rendering bug on retina | ||
* | ||
* @see <http://cr.openjdk.java.net/~ant/RT-38915/webrev.0/> | ||
*/ | ||
private class JFXPanelRetina extends JFXPanel { | ||
@Override | ||
public void removeNotify() { | ||
/*try { //fixme? significantly increases performance(but probably can lead to errors:)) | ||
Field scaleFactor = JFXPanel.class.getDeclaredField("scaleFactor"); | ||
scaleFactor.setAccessible(true); | ||
scaleFactor.setInt(this, 1); | ||
} catch (NoSuchFieldException e) { | ||
e.printStackTrace(); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} | ||
super.removeNotify();*/ | ||
} | ||
} | ||
} |
147 changes: 147 additions & 0 deletions
147
src/main/java/com/github/shyykoserhiy/gfm/browser/BrowserJx.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package com.github.shyykoserhiy.gfm.browser; | ||
|
||
import com.teamdev.jxbrowser.chromium.Browser; | ||
import com.teamdev.jxbrowser.chromium.JSValue; | ||
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent; | ||
import com.teamdev.jxbrowser.chromium.events.LoadAdapter; | ||
import com.teamdev.jxbrowser.chromium.events.ProvisionalLoadingEvent; | ||
import com.teamdev.jxbrowser.chromium.events.StartLoadingEvent; | ||
import com.teamdev.jxbrowser.chromium.swing.BrowserView; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import java.io.File; | ||
|
||
public class BrowserJx implements IsBrowser { | ||
private final BrowserView webView; | ||
|
||
public BrowserJx() { | ||
webView = new BrowserView(new Browser()); | ||
} | ||
|
||
public synchronized BrowserView getWebView() { | ||
return webView; | ||
} | ||
|
||
@Override | ||
public synchronized void loadUrl(@NotNull final String url) { | ||
SwingUtilities.invokeLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getBrowser().loadURL(url); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized void loadFile(@NotNull final File file) { | ||
SwingUtilities.invokeLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getBrowser().loadURL("file:" + file.getAbsolutePath()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public synchronized void loadContent(@NotNull final String content) { | ||
SwingUtilities.invokeLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getBrowser().loadHTML(content); | ||
} | ||
}); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public synchronized JComponent getComponent() { | ||
return webView; | ||
} | ||
|
||
@Override | ||
public synchronized void dispose() { | ||
SwingUtilities.invokeLater(new Runnable() { | ||
@Override | ||
public void run() { | ||
webView.getBrowser().dispose(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void executeJavaScriptAndReturnValue(String script, JsExecutedListener jsExecutedListener) { | ||
JSValue jsValue = webView.getBrowser().executeJavaScriptAndReturnValue(script); | ||
if (jsValue.isBoolean()) { | ||
jsExecutedListener.onJsExecuted(Boolean.toString(jsValue.getBoolean())); | ||
} | ||
if (jsValue.isFunction()) { | ||
jsExecutedListener.onJsExecuted(jsValue.getString()); | ||
} | ||
if (jsValue.isNumber()) { | ||
jsExecutedListener.onJsExecuted(Double.toString(jsValue.getNumber())); | ||
} | ||
jsExecutedListener.onJsExecuted(jsValue.toString()); | ||
} | ||
|
||
@Override | ||
public void goBack() { | ||
webView.getBrowser().goBack(); | ||
} | ||
|
||
@Override | ||
public void goForward() { | ||
webView.getBrowser().goForward(); | ||
} | ||
|
||
@Override | ||
public void reload() { | ||
webView.getBrowser().reload(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
webView.getBrowser().stop(); | ||
} | ||
|
||
@Override | ||
public void addLoadListener(final LoadListener loadAdapter) { | ||
webView.getBrowser().addLoadListener(new LoadAdapter() { | ||
@Override | ||
public void onStartLoadingFrame(StartLoadingEvent startLoadingEvent) { | ||
if (startLoadingEvent.isMainFrame()) { | ||
loadAdapter.onStartLoadingFrame(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onProvisionalLoadingFrame(ProvisionalLoadingEvent provisionalLoadingEvent) { | ||
if (provisionalLoadingEvent.isMainFrame()) { | ||
loadAdapter.onProvisionalLoadingFrame(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFinishLoadingFrame(FinishLoadingEvent finishLoadingEvent) { | ||
if (finishLoadingEvent.isMainFrame()) { | ||
loadAdapter.onFinishLoadingFrame(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public String getUrl() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean canGoForward() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canGoBack() { | ||
return false; | ||
} | ||
} |
Oops, something went wrong.