wbrcomponentpeer.java
来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 588 行 · 第 1/2 页
JAVA
588 行
/*
* Copyright (C) 2008 Sun Microsystems, Inc. All rights reserved. Use is
* subject to license terms.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*/
package org.jdic.web.peer;
import sun.awt.windows.WComponentPeer;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.peer.ComponentPeer;
import java.awt.image.*;
import java.io.InputStream;
import org.jdic.web.event.BrComponentEvent;
import org.jdic.web.BrComponent;
import javax.swing.*;
import org.jdic.NativeLoadMgr;
/**
* Implementation of BrComponentPeer interface for Internet Explorer.
* @author uta
*/
public class WBrComponentPeer implements BrComponentPeer {
//Initialize JNI field and method IDs
private static native void initIDs();
private static final DirectColorModel directColorModel =
new DirectColorModel(
24,
0x00FF0000, /* red mask */
0x0000FF00, /* green mask */
0x000000FF /* blue mask */
);
private static final int[] bandmasks = new int[] {
directColorModel.getRedMask(),
directColorModel.getGreenMask(),
directColorModel.getBlueMask()};
private Component parentHW = null;
// Toolkit & peer internals
public WBrComponentPeer(BrComponent target) {
this.target = target;
}
boolean editable = false;
void enableEditing(boolean e) {
execJS(":document.body.contentEditable=" + e + ";");
}
boolean isEditable() {
String st = execJS("document.body.contentEditable").toLowerCase();
return !(st.startsWith("error:") || "inherit".equals(st) || "false".equals(st));
}
// BrComponentPeer implementation
boolean documentReady = false;
public void setEditable(boolean _editable) {
if(_editable != editable){
editable = _editable;
enableEditing(editable);
}
}
public boolean getEditable() {
return editable;
}
public String getURL() {
return execJS("document.URL");
}
public String getNativeHTML() {
//FF has jast an innerHTML :(
//return execJS("document.documentElement.outerHTML");
return "<HTML>" + execJS("document.documentElement.innerHTML") + "</HTML>";
}
public String getNativeXHTML() {
return getNativeXHTML(false);
}
public String getNativeXHTML(boolean bWithUniqueID) {
String stXHTML = execJS(
"#function copyNode(xmlDoc, xmlParent, htmlNode)\n" +
"{\n" +
" var htmlNode = htmlNode.firstChild;\n" +
" if(null==htmlNode){\n" +
" return false;\n" +
" }\n" +
" for(; null!=htmlNode; htmlNode = htmlNode.nextSibling){\n" +
" var stTagLow = htmlNode.nodeName.toLowerCase();\n" +
//ditry HTML
" if( -1!=stTagLow.indexOf(\'/\') ||\n" +
" -1!=stTagLow.indexOf('<') ||\n" +
" -1!=stTagLow.indexOf('!') ||\n" +
" -1!=stTagLow.indexOf('?') ||\n" +
" -1!=stTagLow.indexOf('>')) {\n" +
" continue;\n" +
" }\n" +
" var xmlNode = null;\n" +
" if( \"#comment\"==stTagLow ){\n" +
" xmlNode = xmlDoc.createComment(htmlNode.data" +
//<, >, -- are forbidden in XML, but available in HTML
//".replace(/<+/g, \"<\")" +
//".replace(/>+/g, \">\")" +
".replace(/(--)+/g, \"--\"));\n" + //works
" } else if( \"#text\"==stTagLow ){\n" +
" xmlNode = xmlDoc.createTextNode(htmlNode.nodeValue);\n" +
" } else {\n" +
" xmlNode = xmlDoc.createElement(stTagLow);\n" +
" var htmlAttrs = htmlNode.attributes;\n" +
" var xmlAttrs = xmlNode.attributes;\n" +
" for(var i=0; i < htmlAttrs.length; ++i){\n" +
" var htmlAttr = htmlAttrs.item(i);\n" +
" if( !htmlAttr.specified || " +
" \"style\"==htmlAttr.nodeName || " +
" \"javaEval\"==htmlAttr.nodeName || " +
" null==htmlAttr.nodeValue || " +
" \"inherit\"==htmlAttr.nodeValue)\n" +
" continue;\n" +
" var xmlAttr = xmlDoc.createAttribute(htmlAttr.nodeName);\n" +
" xmlAttr.nodeValue = htmlAttr.nodeValue.toString();\n" +
" xmlAttrs.setNamedItem(xmlAttr);\n" +
" }\n" +
" var stStyle = htmlNode.style.cssText;\n" +
" if(0!=stStyle.length){\n" +
" xmlNode.setAttribute(\"style\", stStyle);\n" +
" }\n" +
//{uniquie ID in IE for all container nodes. Could be used as index in document.all collection.
(bWithUniqueID
?" stStyle = htmlNode.uniqueID;\n" +
" if(null!=stStyle && 0!=stStyle.length){\n" +
" xmlNode.setAttribute(\"_uniqueID\", stStyle);\n" +
" }\n"
:"") +
//}
" if(\"script\"==stTagLow || \"style\"==stTagLow){\n" +
" xmlNode.appendChild(xmlDoc.createTextNode(htmlNode.innerHTML));\n" +
" } else if( !copyNode(xmlDoc, xmlNode, htmlNode) && \n" +
" ( stTagLow==\"div\" || stTagLow==\"p\" || stTagLow==\"td\" || stTagLow==\"th\" )\n" +
" ){\n" +
" xmlNode.appendChild(xmlDoc.createTextNode(\"\\xa0\"));\n" +
" }\n" +
" }\n" +
" xmlParent.appendChild( xmlNode );\n" +
" }\n" +
" return true;\n" +
"};\n" +
"\n" +
"function getXHTML()\n" +
"{\n" +
" var xmlDoc = new ActiveXObject(\"Msxml2.FreeThreadedDOMDocument.3.0\");\n" +
" xmlDoc.async = false;\n" +
" copyNode(xmlDoc, xmlDoc, document);\n" +
" return xmlDoc.xml;\n" +
"}\n" +
"\n" +
"document.documentElement.setAttribute(\"javaEval\", getXHTML());"
);
//IE mandatory compartibility
//System.out.print(stXHTML);
stXHTML = stXHTML.replace("></br>", "/>");//<br></br> -> <br/>
stXHTML = stXHTML.replace("></img>", "/>");//<img ...></img> -> <img/>
stXHTML = stXHTML.replace("<title/>", "<title></title>");
stXHTML = stXHTML.replace("<body/>", "<body></body>");
return stXHTML;
}
public void acceptTargetURL() {
documentReady = false;
setURL(target.stURL, target.isHTMLSrc);
}
public void validate() {
if(target.isVisible()) {
clearRgn();
Rectangle cr;
Component[] cms = target.getComponents();
for (Component cm : cms) {
if (cm.isLightweight() && cm.isVisible()) {
cr = cm.getBounds();
clipChild(cr.x, cr.y, cr.width, cr.height);
//System.out.println("child" + cm);
}
}
cr = target.getBounds();
updateTransparentMask(cr.x, cr.y, cr.width, cr.height);
}
}
//native
public native void clearRgn();
public native void clipChild(int top, int left, int width, int height);
native long create(long hwnd, int iPaintAlgorithm);
public native void destroy();
public native void setURL(String stURL, InputStream is);
public native String execJS(String code);
public native void updateTransparentMask(int top, int left, int width, int height);
public native void blockNativeInputHandler(boolean dropNativeAction);
public native void nativeDraw(int x, int y, int width, int height);
public native void nativePosOnScreen(int x, int y, int width, int height);
private native int[] ImageData(int x, int y, int width, int height);
public native void laizyUpdate(int msDelay);
public native void nativeSetTransparent(boolean bTransparent);
public native long nativeSendMouseEvent(int wnd, int wm, int wParam, int lParam);
public native void nativeReleaseMouseCapture();
public native int setActionFiler(int flag, boolean busyState);
public void reshape(int x, int y, int width, int height)
{
//System.out.println("{reshape x:" + x + " y:" + y + " w:" + width + " h:" + height);
Point pt = new Point(x, y);
SwingUtilities.convertPointToScreen(pt, target.getParent());
nativePosOnScreen(
pt.x,
pt.y,
width,
height);
//System.out.println("}reshape x:" + x + " y:" + y);
}
private void handlePaint(int x, int y, int w, int h) {
//if ( !sun.awt.ComponentAccessor.getIgnoreRepaint(tg) ) //exists only under JDK 1.7
if( target.isVisible() ){
final Rectangle rc = new Rectangle(x, y, w, h);
javax.swing.SwingUtilities.invokeLater ( new Runnable() {
public void run() {
//System.out.printf("{p x:%d, y:%d, w:%d, h:%d", rc.x, rc.y, rc.width, rc.height);
target.repaint(rc.x, rc.y, rc.width, rc.height);
//System.out.println("}pppppppppp");
}
});
}
}
private void refreshHard()
{
javax.swing.SwingUtilities.invokeLater ( new Runnable() {
public void run() {
//System.out.printf("{refreshHard");
target.reshape(target.getX(), target.getY(), target.getWidth(), target.getHeight() );
//System.out.println("}refreshHard");
}
});
}
private void focusMove(final boolean bNext)
{
javax.swing.SwingUtilities.invokeLater ( new Runnable() {
public void run() {
KeyboardFocusManager fm = FocusManager.getCurrentKeyboardFocusManager();
if(bNext) {
fm.focusNextComponent(target);
} else {
fm.focusPreviousComponent(target);
}
}
});
}
boolean isFocusOwner = false;
public boolean hasFocus() {
return isFocusOwner;
}
public void focusGain(boolean bKeyboardFocus) {
if(!hasFocus()){
//System.out.println("##setFocus");
execJS("##setFocus(" + bKeyboardFocus + ")" );
}
}
public final static int WND_TOP = 0;
public final static int WND_PARENT = 1;
public final static int WND_IE = 2;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?