brcomponent.java
来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 1,072 行 · 第 1/3 页
JAVA
1,072 行
/*
* 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;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
import java.awt.event.MouseEvent;
import java.io.*;
import java.net.URL;
import org.jdic.web.peer.BrComponentPeer;
import org.jdic.web.event.BrComponentListener;
import org.jdic.web.event.BrComponentEvent;
import javax.swing.*;
/**
* <p>A <code>BrComponent</code> component represents a wrapper for embedded browser
* in which the application can display webpages or from which the application
* can trap browser events. In order to show <code>BrComponent</code> component
* in GUI, users need to add <code>BrComponent</code> to a top-level container,
* such as Frame.
* </p><p>
* The class that is interested in processing a <code>BrComponent</code> event
* should implement interface <code>BrComponentListener</code>, and the object
* created with that class should use <code>BrComponent</code>'s
* <code>addBrComponentListener</code> method to register as a listener.
* </p><p>
* As an <code>JComponent</code> component, a <code>BrComponent</code> component
* must be hosted by a native container somewhere higher up in the component tree
* (for example, by a <code>JPanel</code> object) and it can have the child
* <code>JComponent</code> with correct Z-order mixing procedure.
* </p>
* Attention! You use to set {@link #DESIGN_MODE} global variable
* the <code>false</code> value while top-level container initialization procedure.
*
* @author uta
*/
public class BrComponent
extends JComponent
implements FocusListener, BrComponentConsts
{
/**
* <p>
* While the value is <code>true</code> the
* <code>BrComponent</code> class have a stub painting procedure that makes
* it lightweight completely. It solves smart NetBeans design mode problem.
* </p><p>
* Warning! Don't forget to set it <code>false</code> at run-time.
* </p><p>Example:<code><pre style="background-color:#B8BFD8;width:80ex">
* public class BrowserFrame extends javax.swing.JFrame {
* // Creates new form BrowserFrame
* public BrowserFrame() {
* org.jdic.web.BrComponent.DESIGN_MODE = false;
* initComponents();
* }
* ....
* }</pre></code></p>
*/
public static boolean DESIGN_MODE = true;
/**
* String for java-stream URL navigation.
* Not implemented yet.
*/
public static final String stJavaStream = "js:";
/**
* Initial navigation URL
*/
public static final String stAboutBlank = "about:blank";
/**
* Buffered browser context paint as background for {@link #paintContent}
* paint callback.
*/
public static final int PAINT_JAVA = 0x0001;
/**
* Direct browser context paint just before {@link #paintContent}
* paint callback.
*/
public static final int PAINT_JAVA_NATIVE = 0x0002;
/**
* Only direct browser implementation of painting algorithm.
*/
public static final int PAINT_NATIVE = 0x0004;
/**
* The global hint constants for native browser and {@link #paintContent}
* concurent rendering. Write-only.
* One of <code>PAINT_XXXX</code> constans.
* <code>PAINT_JAVA</code> by default.
*/
protected static int defaultPaintAlgorithm = PAINT_JAVA;
/**
* Setter for {@link #defaultPaintAlgorithm} property.
* @param _defaultPaintAlgorithm PAINT_XXXX const
*/
public static void setDefaultPaintAlgorithm(int _defaultPaintAlgorithm)
{
defaultPaintAlgorithm = _defaultPaintAlgorithm;
}
/**
* The instance hint constants for native browser and {@link #paintContent}
* concurent rendering. It's equeal to {@link #defaultPaintAlgorithm}
* by default.
* One of <code>PAINT_XXXX</code> constans.
* <code>PAINT_JAVA</code> by default.
*/
public int paintAlgorithm = defaultPaintAlgorithm;
/**
* The sprites list to be rendered while default {@link #paintContent}
* procedure.
*/
private java.util.List<BrISprite> sprites;
/**
* The listener for notifications callbacks.
*/
transient BrComponentListener ieListener; //TODO: make it stackable.
/**
* Peer wrapper object for native browser.
*/
private BrComponentPeer brPeer;
/**
* The string that holds a current document URL.
*/
public String stURL;
/**
* Streamed provider for HTML data loader.
* Is used forom native async reader.
*/
public InputStream isHTMLSrc;
public boolean isPeerReady()
{
return null!=brPeer && brPeer.isNativeReady();
}
/**
* Sets the current document URL. If the peer browser object is ready an
* asyncronous navigation starts.
* @param _stURL the string with the current document URL, if the value is
* <code>null</code> the <code>stURL</code> propery contains an empty string
* <code>""</code>
*/
public void setURL(String _stURL) {
isHTMLSrc = null;
stURL = (null==_stURL || 0==_stURL.length()) ? stAboutBlank : _stURL;
if(isPeerReady()) {
setDocumentReady(false);
brPeer.acceptTargetURL();
}
}
/**
* Loads the document from the input stream.
* @param _isHTMLSrc the input stream to load document from.
* @param _stURL the string incarnation of the document URL.
* <cote>null</cote> value means <code>about:blank</code> URL
*/
public void setHTML(InputStream _isHTMLSrc, String _stURL) {
if(null==_isHTMLSrc){
setURL(stAboutBlank);
return;
}
isHTMLSrc = _isHTMLSrc;
stURL = _stURL;
if(isPeerReady()) {
setDocumentReady(false);
brPeer.acceptTargetURL();
}
}
/**
* Loads the document from the string. The document URL doesn't change while this
* call. It simplifies the document post-processing procedure.
* @param stHTML the string with document content inside.
*/
public void setHTML(String stHTML)
{
setHTML(new StringBufferInputStream(stHTML), stURL);
}
/**
* Loads the document from the input stream opened by java connection to URL.
* @param url the URL paramenter for <code>FileInputStream</code>
* constructor. <cote>null</cote> value means <code>about:blank</code>.
* @see #setHTML(InputStream _isHTMLSrc, String _stURL)
* @throws java.io.FileNotFoundException
*/
public void setURL(URL url) throws FileNotFoundException {
if(null==url){
setURL(stAboutBlank);
return;
}
setHTML(new FileInputStream(url.toExternalForm()), url.toString());
}
/**
* Getter for the current document URL.
* @return the string of the current document URL
*/
public String getURL() {
if( isPeerReady() ){
stURL = brPeer.getURL();
}
return stURL;
}
/**
* Getter for the current document HTML content with native browser
* formatting.
* @return the string of the current document HTML content.
* <code>null</code> if peer object is not ready.
*/
public String getHTML() {
if( isPeerReady() ){
return brPeer.getNativeHTML();
}
return null;
}
/**
* Getter for the current document HTML content with XHTML formatting.
* @return the string of the current document HTML content
* <code>null</code> if peer object is not ready.*
*/
public String getXHTML() {
return getXHTML(false);
}
/**
* Getter for the current document HTML content with XHTML formatting.
* @param bWithUniqueID if <code>false</code> HTML content returns as it is,
* <code>true</code> extends tags by
* <a href="http://msdn2.microsoft.com/en-us/library/ms534704(VS.85).aspx">
* unique id</a>.
* @return the string of the current document HTML content
* <code>null</code> if peer object is not ready.
*/
public String getXHTML(boolean bWithUniqueID) {
if( isPeerReady() ){
return brPeer.getNativeXHTML(bWithUniqueID);
}
return null;
}
/**
* Getter for the sprites list.
* @return the list of rendered sprite objects. Those objects are the java
* sprites related with the document, but are not a part of the document.
* For example the map zoom selection area.
*/
public java.util.List getSprites()
{
if(null==sprites){
sprites = new java.util.LinkedList();
}
return sprites;
}
/**
* Makes the component visible or invisible.
* @param aFlag <code>true</code> to make the component visible;
* <code>false</code> to make it invisible
*/
@Override
public void setVisible(boolean aFlag){
super.setVisible(aFlag);
if( isPeerReady() ) {
brPeer.setVisible(isVisible());
}
}
/**
* Sets whether or not this component is enabled. A component that is
* enabled may respond to user input, while a component that is not enabled
* cannot respond to user input. Some components may alter their visual
* representation when they are disabled in order to provide feedback to
* the user that they cannot take input. <br/>
* Note: Disabling a component does not disable its children.<br/>
* @param enabled <code>true</code> if this component should be enabled,
* <code>false</code> otherwise
*/
@Override
public void setEnabled(boolean enabled){
super.setVisible(enabled);
if( isPeerReady() ) {
brPeer.setEnabled(isEnabled());
}
}
/**
* Resets the base JComponent to default state
*/
private void init(){
enableEvents(AWTEvent.KEY_EVENT_MASK |
AWTEvent.INPUT_METHOD_EVENT_MASK |
AWTEvent.MOUSE_EVENT_MASK |
AWTEvent.MOUSE_MOTION_EVENT_MASK |
AWTEvent.MOUSE_WHEEL_EVENT_MASK);
setOpaque(true);
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
setFocusable(true);
addFocusListener(this);
setRequestFocusEnabled(true);
brPeer = PeerClassFactory.createBrComponentPeer(this);
}
/**
* Creates the semilightweight browser component with URL-defined
* document.
* @param stURL the string with URL to be load. <code>null</code> means
* <code>about:blank</code> URL.
*/
public BrComponent(String stURL) {
super();
init();
setURL(stURL);
}
/**
* Creates the semilightweight browser component with empty
* document.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?