package jsynoptic.ui;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import simtools.shapes.AbstractShape;
public class HTMLSheetInformationViewer extends JDialog implements HyperlinkListener, ActionListener{
/** Resources */
/** Stacks for forward, backward actions */
protected Stack backward, forward;
/**
* Help pages are displayed into this pane
*/
protected AntiAliasedTextPane viewer;
/**
* tools bar buttons
*/
protected JButton bSave, bBack, bNext;
protected HTMLSheetInformation sid;
public HTMLSheetInformationViewer(Frame parent, HTMLSheetInformation sid) {
super(parent, sid.getTitle(), false);
this.sid = sid;
backward = new Stack();
forward = new Stack();
// Create html viewer
viewer = new AntiAliasedTextPane();
JScrollPane scrollPane = new JScrollPane(viewer);
viewer.setEditable(false);
viewer.addHyperlinkListener(this);
// Create tools bar
JToolBar toolsBar = new JToolBar();
toolsBar.add(bSave = JSynopticPanels.resources.getBox("save", this));
toolsBar.addSeparator();
toolsBar.add(bBack= JSynopticPanels.resources.getBox("prev", this));
toolsBar.add(bNext = JSynopticPanels.resources.getBox("next", this));
// Create global frame
Container content = getContentPane();
content.setLayout(new BorderLayout());
content.add(toolsBar, BorderLayout.NORTH);
content.add(scrollPane, BorderLayout.CENTER);
// diplay html page
try{
displayPage(sid.getHTMLFile().toURL());
}catch (MalformedURLException e){}
}
public void actionPerformed(ActionEvent e){
if ((e.getSource() == bBack)) {
back();
}else if ((e.getSource() == bNext)) {
next();
}else if ((e.getSource() == bSave)) {
sid.save(this);
}
}
/* (non-Javadoc)
* @see javax.swing.event.HyperlinkListener#hyperlinkUpdate(javax.swing.event.HyperlinkEvent)
*/
public void hyperlinkUpdate (HyperlinkEvent event){
if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
if (event instanceof HTMLFrameHyperlinkEvent) {
HTMLDocument doc = (HTMLDocument)viewer.getDocument ();
doc.processHTMLFrameHyperlinkEvent((HTMLFrameHyperlinkEvent)event);
}else
displayPage(event.getURL());
}
}
protected void displayPage(URL page){
if (page!=null){
try {
viewer.setPage(page);
}catch (IOException ex){
System.err.println("Erreur dans la lecture de " + page);
}
// Update buttons
bBack.setEnabled(!backward.isEmpty());
bNext.setEnabled(!forward.isEmpty());
// Remind this page in backward stack
backward.push(page);
}else{
System.err.println("L'URL est nulle !");
}
}
protected void back(){
if (!backward.isEmpty()){
URL urlBack = (URL)backward.pop();
forward.push(urlBack);
displayPage((URL)backward.pop());
}
}
protected void next(){
if (!forward.isEmpty()){
displayPage((URL)forward.pop());
}
}
public class AntiAliasedTextPane extends JTextPane {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
AbstractShape.ANTI_ALIASING? RenderingHints.VALUE_TEXT_ANTIALIAS_ON: RenderingHints.VALUE_TEXT_ANTIALIAS_OFF
);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
super.paintComponent(g2);
}
}
}