📄 xcustomrenderer.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 javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.applet.*;
import java.util.*;
import java.awt.print.*;
import xbrowser.util.*;
import xbrowser.widgets.*;
import xbrowser.renderer.*;
import xbrowser.renderer.event.*;
public class XCustomRenderer extends JEditorPane implements XRenderer, Printable
{
public void setContext(XRendererContext context)
{
XRendererSupport.setContext(context);
}
public Component getComponent()
{
return this;
}
public XCustomRenderer()
{
setEditable(false);
addHyperlinkListener( new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent e)
{
if( e.getEventType()==HyperlinkEvent.EventType.ACTIVATED )
{
if( e instanceof HTMLFrameHyperlinkEvent )
{
HTMLFrameHyperlinkEvent frame_hyperlink_event = (HTMLFrameHyperlinkEvent)e;
processXHyperLink(frame_hyperlink_event.getURL().toString(), frame_hyperlink_event.getTarget(), frame_hyperlink_event.getSourceElement(), true);
}
else
processXHyperLink(e.getURL().toString(), true);
}
else if( e.getEventType()==HyperlinkEvent.EventType.ENTERED )
{
currentFocusedLink = e.getURL().toString();
rendererListeners.hyperlinkEntered(currentFocusedLink);
}
else if( e.getEventType()==HyperlinkEvent.EventType.EXITED )
{
String last_entered_link = currentFocusedLink;
currentFocusedLink = null;
rendererListeners.hyperlinkExited(last_entered_link);
}
}
});
addPropertyChangeListener( new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e)
{
if( e.getPropertyName().equals("page") )
rendererListeners.pageLoadingFinished(null);
}
});
}
public void addRendererListener(XRendererListener renderer_listener)
{
rendererListeners.addListener( renderer_listener);
}
public void removeRendererListener(XRendererListener renderer_listener)
{
rendererListeners.removeListener( renderer_listener);
}
public void showURL(String url)
{
processXHyperLink(url, true);
}
private void processXHyperLink(String url, boolean add_to_document_history)
{
processXHyperLink(url, null, null, add_to_document_history);
}
private void processXHyperLink(final String url, final String target_frame, final Element source_elem, final boolean add_to_document_history)
{
destroying();
if( url.equals(getCurrentURL()) && getDocument().getProperty(Document.StreamDescriptionProperty)!=null )
return;
loaderThread = new Thread() {
public void run()
{
URL prev_stream = null;
try
{
boolean dont_add = !pageHistory.isEmpty() && pageHistory.getLast().equals(url);
prev_stream = (URL)getDocument().getProperty(Document.StreamDescriptionProperty);
currentURL = url;
rendererListeners.pageLoadingStarted();
if( target_frame==null )
setPage(url);
else
{
HTMLFrameHyperlinkEvent frame_hyperlink_event = new HTMLFrameHyperlinkEvent(XCustomRenderer.this, HyperlinkEvent.EventType.ACTIVATED, new URL(url), target_frame);
((HTMLDocument)getDocument()).processHTMLFrameHyperlinkEvent(frame_hyperlink_event);
}
urlList.add(url);
if( add_to_document_history && !dont_add )
{
int size = pageHistory.size();
if( currentPageIndex!=size-1 )
{
for( int i=currentPageIndex+1; i<size; i++ )
pageHistory.remove( currentPageIndex+1 );
}
addToPageHistory(url);
currentPageIndex++;
}
}
catch( Exception e )
{
XRendererSupport.getContext().getLogger().error(this, e);
if( !isInterrupted() )
{
rendererListeners.pageLoadingFinished(e);
getDocument().putProperty(Document.StreamDescriptionProperty, prev_stream);
if( prev_stream != null)
currentURL = prev_stream.toString();
}
}
loaderThread = null;
}
};
loaderThread.start();
}
public String getPageTitle()
{
if( getCurrentURL()==null )
return XRenderer.EMPTY_DOCUMENT;
else if( getCurrentURL().trim().equals("") )
return XRenderer.UNTITLED_DOCUMENT;
else
{
String title = (String)getDocument().getProperty(Document.TitleProperty);
if( (title==null) || (title.equals("")) )
{
try
{
String file_name = (new URL(getCurrentURL())).getFile();
if( file_name.equals("/") || file_name.equals("\\") || file_name.equals("") )
title = XRenderer.UNTITLED_DOCUMENT;
else
{
int slash_index = file_name.lastIndexOf("/");
int backslash_index = file_name.lastIndexOf("\\");
if( slash_index==-1 && backslash_index==-1 )
title = file_name;
else if( slash_index!=-1 )
title = file_name.substring(slash_index+1);
else
title = file_name.substring(backslash_index+1);
}
}
catch( Exception e )
{
title = XRenderer.UNTITLED_DOCUMENT;
}
}
return title;
}
}
public boolean hasSource()
{
return getContentType().equals("text/html");
}
public String getSource()
{
return pageSource.toString();
}
public void reload()
{
if( getCurrentURL()!=null )
{
// Force to reload !!
getDocument().putProperty(Document.StreamDescriptionProperty, null);
XRendererSupport.getContext().getCacheManager().removeFromCache(getCurrentURL());
processXHyperLink(getCurrentURL(), false);
}
}
public void refresh()
{
repaint();
}
public void stopRendering()
{
if( loaderThread!=null )
{
loaderThread.interrupt();
loaderThread = null;
}
if( myStream!=null )
{
try
{
myStream.skip( myStream.available() );
}
catch( Exception e )
{
}
//myStream = null;
}
rendererListeners.pageLoadingStopped();
}
public void destroying()
{
EditorKit kit = getEditorKit();
stopRendering();
stopPrinting();
if( kit instanceof XHTMLEditorKit )
((XHTMLEditorKit)kit).destroyAllApplets();
}
public void saveContent(File dest_file, int saving_mode)
{
Document doc = getDocument();
try
{
if( saving_mode==XRenderer.SAVING_WEB_PAGE_COMPLETELY ) // Maybe asynchronously?
{
// new Thread() { public void run() { final Document doc = xdoc;
saveCorrected( doc, dest_file);
// } }.start();
}
else if( saving_mode==XRenderer.SAVING_WEB_PAGE_HTML_ONLY ) // save HTML only
{
OutputStream fos = null;
try
{
fos = new FileOutputStream(dest_file);
XRendererSupport.copyStreams( saved.getSavedStream(), fos, -1);
}
finally
{
fos.close();
}
}
}
catch( Exception e )
{
XRendererSupport.getContext().getLogger().error(this, "An error occured while saving the requested page!");
XRendererSupport.getContext().getLogger().error(this, e);
}
}
private void saveCorrected( Document doc, File dest_file) throws IOException
{
String charset = (String) getClientProperty("charset");
OutputStreamWriter file = null;
String enc;
if( encoding != null)
enc = encoding;
else if( charset != null)
enc = charset;
else
enc = System.getProperty("file.encoding");
if( doc instanceof HTMLDocument )
{
File dir = dest_file.getParentFile();
if( doc instanceof XHTMLDocument)
{
XHTMLDocument xdoc = (XHTMLDocument)doc;
while( !xdoc.getSaveQueue().isEmpty() )
{
XRendererSupport.getContext().getLogger().warning(this, "Not loaded some pictures! Wait");
try
{
Thread.sleep(60);
}
catch(InterruptedException ie)
{
return;
}
}
}
XRendererSupport.makeDocLocal((HTMLDocument)doc, dir.toString(), XRendererSupport.MODE_IMAGES_ALONG);
}
try
{
OutputStream fos = new FileOutputStream(dest_file);
file = new OutputStreamWriter(fos, enc);
write( file );
}
finally
{
file.close();
}
}
public void showNextPage()
{
if( hasForwardHistory() )
processXHyperLink((String)pageHistory.get(++currentPageIndex), false);
}
public void showPreviousPage()
{
if( hasBackwardHistory() )
processXHyperLink((String)pageHistory.get(--currentPageIndex), false);
}
public boolean hasForwardHistory()
{
return( currentPageIndex<pageHistory.size()-1 );
}
public boolean hasBackwardHistory()
{
return( currentPageIndex>0 );
}
public String getNextPagePath()
{
if( hasForwardHistory() )
return( (String)pageHistory.get(currentPageIndex+1) );
else
return null;
}
public String getPreviousPagePath()
{
if( hasBackwardHistory() )
return( (String)pageHistory.get(currentPageIndex-1) );
else
return null;
}
public Iterator getForwardHistory()
{
if( hasForwardHistory() )
return pageHistory.listIterator(currentPageIndex+1);
else
return null;
}
public Iterator getBackwardHistory()
{
if( hasBackwardHistory() )
return pageHistory.subList(0, currentPageIndex).iterator();
else
return null;
}
public void showPageFromHistory(int index, int history_type)
{
currentPageIndex = (history_type==XRenderer.BACKWARD_HISTORY) ? index : currentPageIndex+index+1;
processXHyperLink((String)pageHistory.get(currentPageIndex), false);
}
public String getPageCompletePath()
{
return( (getCurrentURL()==null) ? "" : getCurrentURL() );
}
public String getCurrentFocusedLink()
{
return currentFocusedLink;
}
public String getCurrentURL()
{
return currentURL;
//return( (getPage()==null) ? null : getPage().toString() );
}
public Iterator getURLList()
{
return urlList.iterator();
}
private void setSelection(int start_index, int end_index, boolean move_up)
{
if( move_up )
{
setCaretPosition(end_index);
moveCaretPosition(start_index);
}
else
select(start_index, end_index);
}
private boolean isSeparator(char ch)
{
for( int k=0; k<WORD_SEPARATORS.length; k++ )
{
if( ch==WORD_SEPARATORS[k] )
return true;
}
return false;
}
private String getRenderedText()
{
Document doc = getDocument();
String content = "";
try
{
content = doc.getText(0, doc.getLength());
}
catch( Exception e )
{
}
return content;
}
public void findNext(String find_phrase, boolean case_sensitive, boolean whole_word, boolean search_up) throws Exception
{
int pos = getCaretPosition();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -