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

📄 xhistorymanager.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.history;

import java.net.*;
import java.util.*;

import xbrowser.*;
import xbrowser.history.event.*;

public final class XHistoryManager
{
	public void load()
	{
		try
		{
		String file_name = XProjectConstants.CONFIG_DIR+XRepository.getConfiguration().getHistoryFileName();

			XRepository.getConfiguration().getActiveHistorySerializer().getSerializer().importHistory(file_name, this);
		}
		catch( Exception e )
		{
			XRepository.getLogger().warning(this, "An error occured on loading the history!");
			XRepository.getLogger().warning(this, e);

			clearHistory();
			//save();
		}
	}

	public void save()
	{
		try
		{
		String file_name = XProjectConstants.CONFIG_DIR+XRepository.getConfiguration().getHistoryFileName();

			XRepository.getConfiguration().getActiveHistorySerializer().getSerializer().exportHistory(file_name, this);
		}
		catch( Exception e )
		{
			XRepository.getLogger().warning(this, "An error occured while saving the history!");
			XRepository.getLogger().warning(this, e);
		}
	}

	public void updateHistoryData(XHistoryData history_data)
	{
	int index = history.indexOf(history_data);

		if( index!=-1 )
			((XHistoryData)history.get(index)).setTitle(history_data.getTitle());
	}

	public void addHistoryData(XHistoryData history_data)
	{
	int index = history.indexOf(history_data);

		if( index!=-1 )
			((XHistoryData)history.get(index)).visit(history_data);
		else if( history_data.getExpiration().after(new Date()) )
		{
			history.add(history_data);
			for( int i=0; i<historyListeners.size(); i++ )
				((XHistoryListener)historyListeners.get(i)).historyAdded(history_data);
		}
	}

	public void removeHistoryData(XHistoryData history_data)
	{
		if( !history.contains(history_data) )
			return;

		history.remove(history_data);
		for( int i=0; i<historyListeners.size(); i++ )
			((XHistoryListener)historyListeners.get(i)).historyRemoved(history_data);
	}

	public void clearHistory()
	{
		history.clear();

		for( int i=0; i<historyListeners.size(); i++ )
			((XHistoryListener)historyListeners.get(i)).historyCleaned();
	}

	public Iterator getHistory()
	{
		return history.iterator();
	}

	public void addHistoryListener(XHistoryListener listener)
	{
		if( !historyListeners.contains(listener) )
		{
			historyListeners.add(listener);
			updateListener(listener);
		}
	}

	public void removeHistoryListener(XHistoryListener listener)
	{
		historyListeners.remove(listener);
	}

	public XHistoryData createNewHistoryData(String location, String title)
	{
	Date now = new Date();
	long exp_milsec = MILLISECONDS_PER_DAY * XRepository.getConfiguration().getHistoryExpirationDays();
	Date expiration = new Date(now.getTime()+exp_milsec);
	XHistoryData new_history = new XHistoryData(location);

		new_history.setTitle(title);
		new_history.setFirstVisited(now);
		new_history.setLastVisited(now);
		new_history.setExpiration(expiration);
		new_history.setVisitCount(1);

		return new_history;
	}

	public String getCompleteURL(String str)
	{
	Iterator it = history.iterator();
	String item, path, str_path;
	URL item_url, str_url;
	boolean include_protocol = true;

		try
		{
			str_url = new URL( str );
		}
		catch( Exception e )
		{
			include_protocol = false;
			try
			{
				str_url = new URL( XProjectConstants.DEFAULT_PROTOCOL+str );
			}
			catch( Exception ex)
			{
				return null;
			}
		}

		str_path = str_url.toString().substring( str_url.getProtocol().length() );
		if( str_path.startsWith("://") )
			str_path = str_path.substring(3);
		else if( str_path.startsWith(":/") )
			str_path = str_path.substring(2);
		else if( str_path.startsWith(":file:/") )
			str_path = str_path.substring(7);
		else if( str_path.startsWith(":") )
			str_path = str_path.substring(1);
		str_path = str_path.toLowerCase();

		while( it.hasNext() )
		{
			item = ((XHistoryData)it.next()).getLocation();
			try
			{
				item_url = new URL( item );
			}
			catch( Exception e )
			{
				try
				{
					item_url = new URL( XProjectConstants.DEFAULT_PROTOCOL+item );
				}
				catch( Exception ex)
				{
					return null;
				}
			}

			path = item_url.toString().substring( item_url.getProtocol().length() );
			if( path.startsWith("://") )
				path = path.substring(3);
			else if( path.startsWith(":/") )
				path = path.substring(2);
			else if( path.startsWith(":file:/") )
				path = path.substring(7);
			else if( path.startsWith(":") )
				path = path.substring(1);
			path = path.toLowerCase();

			if( !str_path.trim().equals("") && path.startsWith(str_path) )
			{
				if( include_protocol )
					return item_url.toString();
				else
					return path;
			}
		}

		return null;
	}

	private void updateListener(XHistoryListener listener)
	{
	Iterator it = history.iterator();

		while( it.hasNext() )
			listener.historyAdded( (XHistoryData)it.next() );
	}

// Attributes:
	private final long MILLISECONDS_PER_DAY = 86400000;
    private LinkedList historyListeners = new LinkedList();
    private LinkedList history = new LinkedList();
}

⌨️ 快捷键说明

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