⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xappletview.java

📁 XBrowser是一个完全免费并且开源的Web浏览器
💻 JAVA
字号:
/****************************************************************
*              XBrowser  -  eXtended web Browser                *
*                                                               *
*           Copyright (c) 2000-2001  Armond Avanes              *
*     Refer to ReadMe & License files for more information      *
*                                                               *
*                                                               *
*                      By: Armond Avanes                        *
*       Armond555@yahoo.com     &    Armond333@yahoo.com        *
*                http://xbrowser.sourceforge.net/               *
*****************************************************************/
package xbrowser.renderer.custom;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

import xbrowser.renderer.event.*;

public class XAppletView extends ComponentView
{
	public XAppletView(Element elem, XHTMLEditorKit.XAppletContext applet_context)
	{
		super(elem);

		retrieveAppletAttributes();
		appletContext = applet_context;

		documentBase = ((HTMLDocument)getDocument()).getBase();
		try
		{
			if( codebaseParam==null )
				codeBase = new URL(documentBase, ".");
			else
				codeBase = new URL(documentBase, codebaseParam.endsWith("/") ? codebaseParam : codebaseParam+"/");
		}
		catch( Exception e )
		{
			XRendererSupport.getContext().getLogger().error(this, "Error on construction of the applet's CodeBase parameter! Using DocumentBase instead!!");
			XRendererSupport.getContext().getLogger().error(this, e);
			codeBase = documentBase;
		}
	}

	public Applet getApplet()
	{
		return myApplet;
	}

	public void destroyApplet()
	{
		appletContainer.destroyApplet();
	}

	private void retrieveAppletAttributes()
	{
	AttributeSet attr = getElement().getAttributes();

		altParam = getAttribute(attr, HTML.Attribute.ALT);
		archiveParam = getAttribute(attr, HTML.Attribute.ARCHIVE);
		codeParam = getAttribute(attr, HTML.Attribute.CODE);
		codebaseParam = getAttribute(attr, HTML.Attribute.CODEBASE);
		heightParam = getAttribute(attr, HTML.Attribute.HEIGHT);
		hspaceParam = getAttribute(attr, HTML.Attribute.HSPACE);
		nameParam = getAttribute(attr, HTML.Attribute.NAME);
		vspaceParam = getAttribute(attr, HTML.Attribute.VSPACE);
		widthParam = getAttribute(attr, HTML.Attribute.WIDTH);

		if( codeParam!=null && codeParam.endsWith(".class") )
			codeParam = codeParam.substring(0, codeParam.length()-6);
	}

	private String getAttribute(AttributeSet attr, HTML.Attribute attr_name)
	{
	String result = (String)attr.getAttribute(attr_name);

		return( result==null ? result : result.trim() );
	}

	protected Component createComponent()
	{
		if( appletContainer!=null )
			return appletContainer;

		appletContainer = new XAppletContainer();

		return appletContainer;
	}

	/*private void printDebugInfo()
	{
	URL[] urls = myClassLoader.getURLs();

		XRendererSupport.getContext().getLogger().message(this, "--- ClassLoader search path ---");
		for( int i=0; i<urls.length; i++ )
			XRendererSupport.getContext().getLogger().message(this, urls[i]);
		XRendererSupport.getContext().getLogger().message(this, "-------------------------------");

		XRendererSupport.getContext().getLogger().message(this, "--- Applet Attributes & Parameters ---");

	AttributeSet attr = getElement().getAttributes();
	Enumeration enum = attr.getAttributeNames();

		while( enum.hasMoreElements() )
		{
		Object key = enum.nextElement();
		Object value = attr.getAttribute(key);

			XRendererSupport.getContext().getLogger().message(this, key+" = "+value);
		}
		XRendererSupport.getContext().getLogger().message(this, "--------------------------------------");
	}*/

	private class XAppletContainer extends JPanel
	{
		public XAppletContainer()
		{
			super( new BorderLayout(), false);

		Dimension size = null;

			setBackground(Color.lightGray);
			try
			{
				size = new Dimension( Integer.parseInt(widthParam), Integer.parseInt(heightParam));
			}
			catch( Exception ex )
			{
				size = new Dimension(50, 50);
			}

			setSize(size);
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);

			if( DEBUG )
				setBorder( new SoftBevelBorder( BevelBorder.RAISED));
		}

		public void addNotify()
		{
			super.addNotify();

			if( !APPLETS_ENABLED )
				return;

			appletContext.showAppletLifeCycle(null, XRendererListener.STARTING_APPLET_LOAD);
			try
			{
				ClassLoader cl = XRendererSupport.getContext().getResourceManager().buildClassLoaderForApplet(codeBase, archiveParam);
				myApplet = (Applet)XRendererSupport.getContext().getResourceManager().loadObject(codeParam, cl);
			}
			catch( Exception e )
			{
				// This line allows debugging of loading problems for applet writers
				XRendererSupport.getContext().getLogger().error(this, "Err: CodeBase = " + codeBase + " codeParam = " + codeParam);
				appletContext.showAppletLifeCycle(null, XRendererListener.APPLET_LOAD_FAILED);
				return;
			}

			appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_LOAD_SUCCESS);

			appletThread = new XAppletThread();
			myApplet.setStub(new XAppletStub());

			add( myApplet); // Damn, it can requires paint() before init()

			startApplet();
		}

		private void startApplet()
		{
			if( appletThread==null || appletThread.isActive() )
				return;

			appletThread.initializeApplet();
			appletThread.startApplet();
			// add( myApplet); // This will call paint() which isn't safe to call earlier.
		}

		public void destroyApplet()
		{
			// remove(myApplet);
			if( appletThread != null)
				appletThread.finalizeApplet();
			appletThread = null;
		}
	}

	private class XAppletStub implements AppletStub
	{
		/** Browser should call this for resize AFAIK. */
		public void appletResize(int width, int height)
		{
			if( myApplet!=null && width > 0 && height > 0 )
				appletContainer.setSize( width, height);
		}

		public AppletContext getAppletContext()
		{
			return appletContext;
		}

		public URL getCodeBase()
		{
			return codeBase;
		}

		public URL getDocumentBase()
		{
			return documentBase;
		}

		public String getParameter(String param_name)
		{
		Object param_value = getElement().getAttributes().getAttribute(param_name);

			return( (param_value==null) ? null : param_value.toString() );
		}

		public boolean isActive()
		{
			return appletThread != null && appletThread.isActive();
		}
	}

	private class XAppletThread extends Thread
	{
		public boolean isActive()
		{
			return active;
		}

		public boolean isInitialized()
		{
			return initialized;
		}

		public void initializeApplet()
		{
			start();
			doAction(INIT);
		}

		public void startApplet()
		{
			doAction(START);
		}

		public void finalizeApplet()
		{
			doAction(STOP);
			doAction(DESTROY);
		}

		public synchronized void doAction(Integer type)
		{
			synchronized( jobList )
			{
				jobList.addLast(type);
			}
		}

		private void doInitAction()
		{
			if( !initialized && !active )
			{
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.STARTING_APPLET_INIT);

				try
				{
					myApplet.init();
					initialized = true;
					appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_INIT_SUCCESS);
				}
				catch( Exception e )
				{
					XRendererSupport.getContext().getLogger().error(this, e);
					appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_INIT_FAILED);
				}
			}
		}

		private void doStartAction()
		{
			if( initialized && !active )
			{
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.STARTING_APPLET_START);

				try
				{
					myApplet.start();
					active = true;

					if( DEBUG ) // When set, Applet does not get events
					{
						myApplet.addMouseListener( new MouseAdapter() {
							public void mouseEntered(MouseEvent e)
							{
								if( myApplet == null)
									return;
								String info = myApplet.getAppletInfo();
								if( info == null)
									info = altParam;
								if( info == null)
									info = nameParam;
								if( info == null)
									info = codeBase.toString();

								appletContext.showStatus( info);
							}

							public void mouseExited(MouseEvent e)
							{
								appletContext.showStatus(null);
							}
						});
					}

					appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_START_SUCCESS);
				}
				catch( Exception e )
				{
					XRendererSupport.getContext().getLogger().error(this, e);
					appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_START_FAILED);
				}
			}
		}

		private void doStopAction()
		{
			appletContext.showAppletLifeCycle(myApplet, XRendererListener.STARTING_APPLET_STOP);
			active = false;

			try
			{
				myApplet.stop();
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_STOP_SUCCESS);
			}
			catch( Exception e )
			{
				XRendererSupport.getContext().getLogger().error(this, e);
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_STOP_FAILED);
			}
		}

		private void doDestroyAction()
		{
			appletContext.showAppletLifeCycle(myApplet, XRendererListener.STARTING_APPLET_DESTROY);

			try
			{
				myApplet.destroy();
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_DESTROY_SUCCESS);
			}
			catch( Exception e )
			{
				XRendererSupport.getContext().getLogger().error(this, e);
				appletContext.showAppletLifeCycle(myApplet, XRendererListener.APPLET_DESTROY_FAILED);
			}
			myApplet = null;
		}

		public void run()
		{
		Integer status = null;

			while( true )
			{
				try
				{
					// Quicker response time...
					sleep(60); // This will be enough
				}
				catch( InterruptedException e )
				{
					XRendererSupport.getContext().getLogger().warning(this, e);
				}

				synchronized( jobList )
				{
					if( !jobList.isEmpty() )
						status = (Integer)jobList.removeFirst();
					else
						continue;
				}

				if( status==INIT )
					doInitAction();
				else if( status==START )
					doStartAction();
				else if( status==STOP )
					doStopAction();
				else if( status==DESTROY )
				{
					doDestroyAction();
					return;
				}
			}
		}

	// Attributes:
		private boolean	active = false;
		private boolean	initialized = false;

		private LinkedList jobList = new LinkedList();

		private final Integer INIT = new Integer(0);
		private final Integer START = new Integer(1);
		private final Integer STOP = new Integer(2);
		private final Integer DESTROY = new Integer(3);
	}

	// Attributes:
	private static boolean APPLETS_ENABLED = true; // Prepare for Options element.
	private static boolean DEBUG = false; // Ease of debugging at later time.

	private String				altParam, archiveParam, codeParam, codebaseParam, heightParam, hspaceParam, nameParam, vspaceParam, widthParam, objectParam;
	private Applet				myApplet = null;
	private XHTMLEditorKit.XAppletContext   appletContext = null;
	private URL				documentBase, codeBase;

	private XAppletContainer				appletContainer = null;
	private XAppletThread					appletThread = null;
}

⌨️ 快捷键说明

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