brcomponent.java

来自「java调用ie浏览器demo源码,可以用在windows或者linux」· Java 代码 · 共 1,072 行 · 第 1/3 页

JAVA
1,072
字号
     */
    public BrComponent() {
       this(stAboutBlank);
   }
    
    /**
     * Creates the semilightweight browser component with streamed document.
     * @param _isHTMLSrc the input stream to load document from.
     * @param stURL the string incarnation of the document URL. 
     */
     public BrComponent(InputStream _isHTMLSrc, String stURL) {
       super();
       init();
       setHTML(_isHTMLSrc, stURL);
    }

    //public actions
    public void back()
    {
         execJSLater(":window.history.back()");
    }
    public void forward()
    {
        execJSLater(":window.history.forward()");
    }
    public void refresh()
    {
        execJSLater(":window.history.go(0)");
    }
    
    public void stop()
    {
        execJSLater("##stop");//can be done correctly only by IBrComponent
    }

    public void save()
    {
        execJSLater(":document.execCommand(\"SaveAs\", true)");
    }

    public void save(String stHTMLPath)
    {
        execJSLater(":document.execCommand(\"SaveAs\", false, \"" + stHTMLPath + "\")");
    }

    public void open()
    {
        Frame fr = (Frame)getTopLevelAncestor();
        FileDialog fd = new FileDialog(fr, "Open document", FileDialog.LOAD);
        //Rectangle w = fr.getBounds();
        //Rectangle cprc = fd.getBounds();
        //cprc.x = w.x + 22;
        fd.setVisible(true);
        //fd.setLocation(new Point(fr.getX(), fr.getY()));
        if(null!=fd.getFile()){
            open( fd.getDirectory() + fd.getFile() );            
        }
    }

    public void open(String stHTMLPath)
    {
        setURL(stHTMLPath);
    }


    public void print()
    {
        execJSLater(":document.execCommand(\"print\", true)");
    }

    @Override
    public void validate()
    {
        super.validate(); 
        if( isPeerReady() ) {
            brPeer.validate();
        }
    }
    
    @Override
    public void reshape(int x, int y, int width, int height)
    {
        super.reshape(x, y, width, height);
        if( isPeerReady() ) {
            brPeer.reshape(x, y, width, height);
            brPeer.validate();                    
        }
    }

    public JComponent getCentralPanel()
    {
        return brPeer.getCentralPanel();
    }

    /**
     * Makes this Component displayable by connecting it to a
     * native screen resource.
     * This method is called internally by the toolkit and should
     * not be called directly by programs.
     * @see       #removeNotify
     */
    @Override
    public void addNotify() {
        super.addNotify();
        brPeer.onAddNotify();        
    }
    
    /**
     * Removes the <code>BrComponent</code>'s peer.
     * The peer allows us to modify the appearance of the
     * <code>BrComponent</code> without changing its
     * functionality.
     */
    @Override
    public void removeNotify() {
        brPeer.onRemoveNotify();
        super.removeNotify();
    }

    /**
     * Postponed Java Script execution.
     * @param code the string with Java Script code.
     */
    public void execJSLater(final String code) {
        javax.swing.SwingUtilities.invokeLater ( new Runnable() {
            public void run() {
                execJS(code);
            }
        });
    }
    
    /**
     * Synchronous Java Script execution.
     * @param code the string with Java Script code.
     * @return the string with execution result.
     */
    public String execJS(String code) {
        if( isPeerReady() && null!=code || 0!=code.length() ) {
            return brPeer.execJS(code);
        }
        return null;
    }

    /**
     * Switches browser acccess to user input.
     * @param dropNativeAction <code>true</code> switches off browser from user 
     * input; <code>false</code> switches on native input events treatment. 
     */
    public void blockNativeInputHandler(boolean dropNativeAction)
    {
        if( isPeerReady() ) {
           brPeer.blockNativeInputHandler(dropNativeAction);
        }
    }
    
    public void paintPlaceHolder(
            Graphics g,
            int x, int y, int width, int height,
            String stLabel)
    {
        g.setColor(Color.green);
        GradientPaint gradient = new GradientPaint(
            x, y,
            new Color(0.0F, 1.0F, 1.0F, 0.5F),
            x + width, y + height,
            new Color(1.0F, 1.0F, 0.0F, 0.5F),
            false); // true means to repeat pattern

        Graphics2D g2 = (Graphics2D)g;
        g2.setPaint(gradient);
        g2.fill(new Rectangle(x,y,width,height));
        g2.setPaint(null);
        g2.setColor(Color.BLACK);
        g2.drawRect(
            x, y,
            width-1, height-1
        );

        if( null!=stLabel && 0!=stLabel.length() ) {
            FontMetrics fm   = g2.getFontMetrics(g2.getFont());    
            Rectangle2D rect = fm.getStringBounds(stLabel, g);
            int textHeight = (int)(rect.getHeight());
            int textWidth  = (int)(rect.getWidth());
            if(
                textHeight <= height &&
                textWidth  <= width)
            {
                g2.drawString(
                    stLabel,
                    x + (width - textWidth)/2,
                    y + (height + textHeight)/2
                );
                g2.setColor(Color.white);
                g2.drawLine(
                    x + 1, y + 1,
                    x + width - 2, y + height - 2
                );
            }
        }        
    }

    public void paintDesignMode(Graphics g) {
        paintPlaceHolder(
                g,
                0, 0,
                getWidth(), getHeight(),
                getClass().getName() + " " + getName() );        
    }

    public void paintContent(Graphics g) {
        if(null!=sprites){
            for (Object sprite : sprites) {
                ((BrISprite) sprite).drawOn(this, g);
            }
        }
    }

    static int iPaintCount = 0;
    public void paintClientArea(Graphics g,  boolean withPeer) {
        if( isPeerReady() && withPeer ){
            brPeer.paintClientArea(g, paintAlgorithm);
        } else if(DESIGN_MODE){
            paintDesignMode(g);
        }
        paintContent(g);
        if(isDebugDrawBorder()){
            Rectangle updateRect = g.getClipBounds();
            paintPlaceHolder(
                g, 
                updateRect.x, updateRect.y,
                updateRect.width, updateRect.height, 
                (++iPaintCount) + "");
        }
    }
    
    @Override
    public void paintComponent(Graphics g) {
        //execJS("##showCaret(false)");
        super.paintComponent(g);
        if( !isPeerReady() || !brPeer.isSelfPaint()  ){
            paintClientArea(g, true);
        }    
    }


    /**
     * Adds the specified browser event listener to receive stURL events
     * from this stURL component.
     * If <code>l</code> is <code>null</code>, no exception is
     * thrown and no action is performed.
     *
     * @param l the browser event listener
     * #see           	#removeTextListener
     * #see           	#getTextListeners
     * @see           	java.awt.event.TextListener
     */
    public void addBrComponentListener(BrComponentListener l) {
        ieListener = l;
    }

    public void removeBrComponentListener(BrComponentListener l) {
        if( ieListener == l ) {
            ieListener = null;
        }
    }

            
     /**
      * Internal browser event processor. Converts some events to 
      * <code>BrComponentListener</code> inteface callbacks and property-changed 
      * notifications.
      * @param e the happened browser event
      */
     public String processBrComponentEvent(final BrComponentEvent e) {
        //System.out.println( "IEE:" + e.getID() + " Name:" + e.getName() + " Value:"+ e.getValue() ); 
        String res = null;
        BrComponentListener listener = ieListener;
        if (listener != null) {
            res = listener.sync(e);
        }
        SwingUtilities.invokeLater ( new Runnable() { public void run() {
                //System.out.println(e);
                String stValue = e.getValue();
                stValue = (null==stValue)?"":stValue; 
                switch(e.getID()){
                case BrComponentEvent.DISPID_STATUSTEXTCHANGE:
                    setStatusText(stValue);
                    break;
                case BrComponentEvent.DISPID_BEFORENAVIGATE2:
                    break;
                case BrComponentEvent.DISPID_NAVIGATECOMPLETE2:
                    setNavigatedURL(stValue);
                    break;
                case BrComponentEvent.DISPID_DOWNLOADCOMPLETE:
                    setDocumentReady(true);
                    break;
                case BrComponentEvent.DISPID_PROGRESSCHANGE:
                    setProgressBar(stValue);
                    break;
                case BrComponentEvent.DISPID_SETSECURELOCKICON:
                    setSecurityIcon(stValue);
                    break;
                case BrComponentEvent.DISPID_TITLECHANGE:
                    setWindowTitle(stValue);
                    break;
                case BrComponentEvent.DISPID_COMMANDSTATECHANGE:
                    {
                        String st[] = stValue.split(",");
                        boolean enable = (0!=Integer.valueOf(st[0]));
                        switch(Integer.valueOf(st[1])){
                        case -1:    
                            setToolbarChanged(enable);                            
                            break;
                        case 1:    
                            setGoForwardEnable(enable);                            
                            break;
                        case 2:    
                            setGoBackEnable(enable);                                                        
                            break;                            
                        }
                    }    
                    break;
                }
        }});//end posponed operation   
        return res;
    }

    /**
     * Returns a string representing the state of the browser component.
     * The method is intended to be used only for debugging purposes, and the
     * content and format of the returned string may vary between
     * implementations. The returned string may be empty but may not be
     * <code>null</code>.
     * @return the parameter string of this stURL component
     */
    @Override
    protected String paramString() {
        return super.paramString() + ",URL=" + stURL;
    }

    //FocusListener
    public void focusGained(FocusEvent e) {
        if( isPeerReady() ){
            brPeer.focusGain(true);
        }
    }
    public void focusLost(FocusEvent e) {
    }

    //Input methods capturing
    public void checkMouse(MouseEvent e) {
        if( isPeerReady() ){
            brPeer.sendMouseEvent(e);
        }
    }
    
    @Override
    protected void processMouseEvent(MouseEvent e) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?