- import java.awt.*;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.StringTokenizer;
- import java.util.Vector;
-
- import javax.swing.*;
- import javax.swing.event.HyperlinkEvent;
- import javax.swing.event.HyperlinkListener;
- import javax.swing.text.html.HTMLDocument;
- import javax.swing.text.html.HTMLFrameHyperlinkEvent;
- /**
- * @author GodConan
- *
- */
- public class HtmlBrowser extends JFrame
- {
- JEditorPane html;
- String path = "index.html";
- Vector oldPages = new Vector(); // sauve les path des page deja passer
- int currentPageIdx = 0;
-
- public HtmlBrowser() throws HeadlessException
- {
- super();
- setSize( 500, 500 );
- JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
- JPanel2.add(JToolBar1);
- JToolBar1.add(btnPrev);
- JToolBar1.add(btnNext);
- btnPrev.setText("Page precédente");
- btnNext.setText("Page suivante");
-
- Actions aIcareAction = new Actions();
- btnPrev.addActionListener( aIcareAction );
- btnNext.addActionListener( aIcareAction );
- btnNext.setFocusPainted( false );
- btnPrev.setFocusPainted( false );
- btnNext.setEnabled( false );
- btnPrev.setEnabled( false );
-
- try
- {
- URL url = getURL( path );
- html = new JEditorPane( url );
- html.setEditable(false);
- html.addHyperlinkListener( createHyperLinkListener() );
- html.setBounds( 0, 0, 500, 500 );
- JScrollPane scroller = new JScrollPane();
- JViewport vp = scroller.getViewport();
- vp.add( html );
- getContentPane().add( JPanel2, BorderLayout.NORTH );
- getContentPane().add( scroller, BorderLayout.CENTER);
- html.setBackground( this.getBackground() );
- }
- catch (Exception e)
- {
- System.out.println("eror : " + e );
- }
- }
- JPanel JPanel2 = new JPanel();
- JToolBar JToolBar1 = new JToolBar();
- JButton btnPrev = new JButton();
- JButton btnNext = new JButton();
-
- class Actions implements java.awt.event.ActionListener
- {
- public void actionPerformed(java.awt.event.ActionEvent event)
- {
- Object object = event.getSource();
- if (object == btnNext )
- btnNext_actionPerformed(event);
- else if (object == btnPrev)
- btnPrev_actionPerformed(event);
- }
- }
-
- private void btnNext_actionPerformed(java.awt.event.ActionEvent event)
- {
- try {
- if( currentPageIdx++ < oldPages.size() )
- {
- String p = (String)oldPages.elementAt( currentPageIdx );
- URL url = getURL( p );
- html.setPage( url );
- }
- checkBtn();
- } catch (Exception e)
- {
- System.out.println( " btnNext_actionPerformed : " + e );
- }
- }
-
- private void btnPrev_actionPerformed(java.awt.event.ActionEvent event)
- {
- try {
- if( currentPageIdx > 0 && !oldPages.isEmpty() )
- {
- String p = (String)oldPages.elementAt( --currentPageIdx );
- URL url = getURL( p );
- html.setPage( url );
- }
- checkBtn();
- } catch (Exception e)
- {
- System.out.println( " btnNext_actionPerformed : " + e );
- }
- }
-
- private void initTitre( String p )
- {
- String sep = "?";
- if ( p.indexOf( "/" ) > 0 ) sep = "/";
- else if ( p.indexOf( "\\" ) > 0 ) sep = "\\";
- StringTokenizer st = new StringTokenizer( p, sep );
- int n = st.countTokens();
- String s = "";
- while( st.hasMoreTokens() ) { s = st.nextToken(); }
- if ( s.indexOf(".") > 0 ) s = s.substring( 0, s.indexOf( "." ) );
- this.setTitle( "Page : " + s );
- }
-
- public HtmlBrowser( String titre ) throws HeadlessException
- {
- this();
- this.setTitle( titre );
- }
-
- private void checkBtn()
- {
- if ( oldPages == null || oldPages.size() == 0 )
- {
- btnNext.setEnabled( false );
- btnPrev.setEnabled( false );
- }
- else
- {
- if ( currentPageIdx > 0 )
- btnPrev.setEnabled( true );
- else
- btnPrev.setEnabled( false );
- if ( (currentPageIdx+1) < oldPages.size() )
- btnNext.setEnabled( true );
- else
- btnNext.setEnabled( false );
- }
- }
-
- private HyperlinkListener createHyperLinkListener()
- {
- return new HyperlinkListener()
- {
- public void hyperlinkUpdate(HyperlinkEvent e)
- {
- if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
- {
- if (e instanceof HTMLFrameHyperlinkEvent)
- {
- ((HTMLDocument) html
- .getDocument())
- .processHTMLFrameHyperlinkEvent(
- (HTMLFrameHyperlinkEvent) e);
- }
- else
- {
- try
- {
- path = e.getURL().getPath();
- html.setPage( e.getURL() );
- addHisto( path );
- initTitre( path );
- }
- catch (IOException ioe)
- {
- System.out.println("IOE: " + ioe);
- }
- }
- }
- }
- };
- }
-
- private void addHisto( String path )
- {
- oldPages.add( path );
- currentPageIdx = oldPages.size() - 1;
- if ( oldPages.size() > 25 )
- {
- oldPages = new Vector();
- oldPages.add( path );
- currentPageIdx = oldPages.size() - 1;
- }
- checkBtn();
- }
-
- /**
- * Returns le path de la page en cour de lecture.
- * @return String
- */
- public String getPath()
- {
- return path;
- }
-
- /**
- * Sets le path de la page html a lire.
- * @param path de la page html
- */
- public void setPath(String path)
- {
- try
- {
- this.path = path;
- URL url = getURL( path );
- html.setPage( url );
- addHisto( path );
- }
- catch (Exception e)
- {
- System.out.println("setPath : " + e );
- }
-
- }
-
- public void setDocument( String doc )
- {
- html.setText( doc );
- }
-
- public URL getURL( String file ) throws MalformedURLException
- {
- URL documentBase = new URL("file:///" + System.getProperty("user.dir") + "/");
- return new URL( documentBase, file );
- }
-
- public static void main(String[] args)
- {
- HtmlBrowser b = new HtmlBrowser();
- if ( args.length > 0 ) b.setPath( args[ 0 ] );
- b.setLocation( new Point( 0, 0 ) );
- b.setVisible( true );
- }
-
- }
import java.awt.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
/**
* @author GodConan
*
*/
public class HtmlBrowser extends JFrame
{
JEditorPane html;
String path = "index.html";
Vector oldPages = new Vector(); // sauve les path des page deja passer
int currentPageIdx = 0;
public HtmlBrowser() throws HeadlessException
{
super();
setSize( 500, 500 );
JPanel2.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel2.add(JToolBar1);
JToolBar1.add(btnPrev);
JToolBar1.add(btnNext);
btnPrev.setText("Page precédente");
btnNext.setText("Page suivante");
Actions aIcareAction = new Actions();
btnPrev.addActionListener( aIcareAction );
btnNext.addActionListener( aIcareAction );
btnNext.setFocusPainted( false );
btnPrev.setFocusPainted( false );
btnNext.setEnabled( false );
btnPrev.setEnabled( false );
try
{
URL url = getURL( path );
html = new JEditorPane( url );
html.setEditable(false);
html.addHyperlinkListener( createHyperLinkListener() );
html.setBounds( 0, 0, 500, 500 );
JScrollPane scroller = new JScrollPane();
JViewport vp = scroller.getViewport();
vp.add( html );
getContentPane().add( JPanel2, BorderLayout.NORTH );
getContentPane().add( scroller, BorderLayout.CENTER);
html.setBackground( this.getBackground() );
}
catch (Exception e)
{
System.out.println("eror : " + e );
}
}
JPanel JPanel2 = new JPanel();
JToolBar JToolBar1 = new JToolBar();
JButton btnPrev = new JButton();
JButton btnNext = new JButton();
class Actions implements java.awt.event.ActionListener
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Object object = event.getSource();
if (object == btnNext )
btnNext_actionPerformed(event);
else if (object == btnPrev)
btnPrev_actionPerformed(event);
}
}
private void btnNext_actionPerformed(java.awt.event.ActionEvent event)
{
try {
if( currentPageIdx++ < oldPages.size() )
{
String p = (String)oldPages.elementAt( currentPageIdx );
URL url = getURL( p );
html.setPage( url );
}
checkBtn();
} catch (Exception e)
{
System.out.println( " btnNext_actionPerformed : " + e );
}
}
private void btnPrev_actionPerformed(java.awt.event.ActionEvent event)
{
try {
if( currentPageIdx > 0 && !oldPages.isEmpty() )
{
String p = (String)oldPages.elementAt( --currentPageIdx );
URL url = getURL( p );
html.setPage( url );
}
checkBtn();
} catch (Exception e)
{
System.out.println( " btnNext_actionPerformed : " + e );
}
}
private void initTitre( String p )
{
String sep = "?";
if ( p.indexOf( "/" ) > 0 ) sep = "/";
else if ( p.indexOf( "\\" ) > 0 ) sep = "\\";
StringTokenizer st = new StringTokenizer( p, sep );
int n = st.countTokens();
String s = "";
while( st.hasMoreTokens() ) { s = st.nextToken(); }
if ( s.indexOf(".") > 0 ) s = s.substring( 0, s.indexOf( "." ) );
this.setTitle( "Page : " + s );
}
public HtmlBrowser( String titre ) throws HeadlessException
{
this();
this.setTitle( titre );
}
private void checkBtn()
{
if ( oldPages == null || oldPages.size() == 0 )
{
btnNext.setEnabled( false );
btnPrev.setEnabled( false );
}
else
{
if ( currentPageIdx > 0 )
btnPrev.setEnabled( true );
else
btnPrev.setEnabled( false );
if ( (currentPageIdx+1) < oldPages.size() )
btnNext.setEnabled( true );
else
btnNext.setEnabled( false );
}
}
private HyperlinkListener createHyperLinkListener()
{
return new HyperlinkListener()
{
public void hyperlinkUpdate(HyperlinkEvent e)
{
if ( e.getEventType() == HyperlinkEvent.EventType.ACTIVATED )
{
if (e instanceof HTMLFrameHyperlinkEvent)
{
((HTMLDocument) html
.getDocument())
.processHTMLFrameHyperlinkEvent(
(HTMLFrameHyperlinkEvent) e);
}
else
{
try
{
path = e.getURL().getPath();
html.setPage( e.getURL() );
addHisto( path );
initTitre( path );
}
catch (IOException ioe)
{
System.out.println("IOE: " + ioe);
}
}
}
}
};
}
private void addHisto( String path )
{
oldPages.add( path );
currentPageIdx = oldPages.size() - 1;
if ( oldPages.size() > 25 )
{
oldPages = new Vector();
oldPages.add( path );
currentPageIdx = oldPages.size() - 1;
}
checkBtn();
}
/**
* Returns le path de la page en cour de lecture.
* @return String
*/
public String getPath()
{
return path;
}
/**
* Sets le path de la page html a lire.
* @param path de la page html
*/
public void setPath(String path)
{
try
{
this.path = path;
URL url = getURL( path );
html.setPage( url );
addHisto( path );
}
catch (Exception e)
{
System.out.println("setPath : " + e );
}
}
public void setDocument( String doc )
{
html.setText( doc );
}
public URL getURL( String file ) throws MalformedURLException
{
URL documentBase = new URL("file:///" + System.getProperty("user.dir") + "/");
return new URL( documentBase, file );
}
public static void main(String[] args)
{
HtmlBrowser b = new HtmlBrowser();
if ( args.length > 0 ) b.setPath( args[ 0 ] );
b.setLocation( new Point( 0, 0 ) );
b.setVisible( true );
}
}