You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.8 KiB
106 lines
3.8 KiB
package com.fr.design.extra; |
|
|
|
import com.fr.base.TemplateUtils; |
|
import com.fr.general.GeneralContext; |
|
import com.fr.general.IOUtils; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.StableUtils; |
|
import com.fr.stable.StringUtils; |
|
import javafx.application.Platform; |
|
import javafx.embed.swing.JFXPanel; |
|
import javafx.event.EventHandler; |
|
import javafx.scene.Scene; |
|
import javafx.scene.layout.BorderPane; |
|
import javafx.scene.web.WebEngine; |
|
import javafx.scene.web.WebEvent; |
|
import javafx.scene.web.WebView; |
|
import netscape.javascript.JSObject; |
|
|
|
import javax.swing.JOptionPane; |
|
import javax.swing.SwingUtilities; |
|
import java.io.BufferedReader; |
|
import java.io.IOException; |
|
import java.io.InputStream; |
|
import java.io.InputStreamReader; |
|
import java.net.URLEncoder; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
|
|
/** |
|
* Created by richie on 16/3/19. |
|
*/ |
|
public class PluginWebPane extends JFXPanel { |
|
private static final String RESOURCE_URL = "resourceURL"; |
|
private static final String LANGUAGE = "language"; |
|
private WebEngine webEngine; |
|
|
|
public PluginWebPane(final String installHome, final String mainJs) { |
|
Platform.setImplicitExit(false); |
|
Platform.runLater(new Runnable() { |
|
@Override |
|
public void run() { |
|
BorderPane root = new BorderPane(); |
|
Scene scene = new Scene(root); |
|
PluginWebPane.this.setScene(scene); |
|
WebView webView = new WebView(); |
|
webEngine = webView.getEngine(); |
|
try{ |
|
String htmlString = getRenderedHtml(installHome, mainJs); |
|
webEngine.loadContent(htmlString); |
|
webEngine.setOnAlert(new EventHandler<WebEvent<String>>() { |
|
@Override |
|
public void handle(WebEvent<String> event) { |
|
showAlert(event.getData()); |
|
} |
|
}); |
|
JSObject obj = (JSObject) webEngine.executeScript("window"); |
|
obj.setMember("PluginHelper", PluginWebBridge.getHelper(webEngine)); |
|
webView.setContextMenuEnabled(false);//屏蔽右键 |
|
root.setCenter(webView); |
|
}catch (Exception e){ |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
|
|
} |
|
}); |
|
} |
|
|
|
private String getRenderedHtml(String installHome, String mainJs) throws IOException { |
|
InputStream inp = IOUtils.readResource(StableUtils.pathJoin(installHome, mainJs)); |
|
if (inp == null) { |
|
throw new IOException("Not found template: " + mainJs); |
|
} |
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inp, StableUtils.RESOURCE_ENCODER)); |
|
BufferedReader read = new BufferedReader(reader); |
|
StringBuffer sb = new StringBuffer(); |
|
String line; |
|
Map<String, Object> map4Tpl = new HashMap<String, Object>(); |
|
|
|
map4Tpl.put(RESOURCE_URL, "file:///" + URLEncoder.encode(installHome, "UTF-8")); |
|
map4Tpl.put(LANGUAGE, GeneralContext.getLocale().toString()); |
|
while ((line = read.readLine()) != null) { |
|
if (sb.length() > 0) { |
|
sb.append('\n'); |
|
} |
|
sb.append(line); |
|
} |
|
String htmlString = StringUtils.EMPTY; |
|
try{ |
|
htmlString = TemplateUtils.renderParameter4Tpl(sb.toString(), map4Tpl); |
|
}catch (Exception e){ |
|
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
|
} |
|
reader.close(); |
|
inp.close(); |
|
return htmlString; |
|
} |
|
|
|
private void showAlert(final String message) { |
|
SwingUtilities.invokeLater(new Runnable() { |
|
@Override |
|
public void run() { |
|
JOptionPane.showMessageDialog(PluginWebPane.this, message); |
|
} |
|
}); |
|
} |
|
}
|
|
|