/* For License see bottom */
/*
* InfoPanel.java
*
* Created on 1. Juli 2007, 19:06
*/
package jrackattack.gui;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Stack;
import javax.swing.JEditorPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLFrameHyperlinkEvent;
import jonkoshare.util.VersionInformation;
import jrackattack.event.NavigationCallback;
import jrackattack.event.NavigationListener;
import org.apache.log4j.Logger;
/**
*
* @author methke01
*/
@VersionInformation (
lastChanged="$LastChangedDate: 2009-07-25 04:59:33 -0500 (Sat, 25 Jul 2009) $",
authors={"Alexander Methke"},
revision="$LastChangedRevision: 11 $",
lastEditor="$LastChangedBy: onkobu $",
id="$Id"
)
public class InfoPanel extends javax.swing.JPanel
implements HyperlinkListener,
NavigationListener {
private final Logger log=Logger.getLogger(InfoPanel.class);
public InfoPanel() {
this(null);
}
/**
* Creates new form InfoPanel.
*
* @param docRes Key of the resource to load initially.
*/
public InfoPanel(String docRes) {
initComponents();
helpEditor.setContentType("text/html");
helpEditor.setEditable(false);
helpEditor.addHyperlinkListener(this);
setResourceKey(docRes);
navigationPanel.setNavigationListener(this);
initValues();
}
protected void initValues() {
navigationCallback.navigateBackwardStateChanged(false);
navigationCallback.navigateForwardStateChanged(false);
navigationCallback.navigateTopStateChanged(true);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane1 = new javax.swing.JScrollPane();
helpEditor = new javax.swing.JEditorPane();
navigationPanel = new jrackattack.gui.NavigationPanel();
setLayout(new java.awt.GridBagLayout());
jScrollPane1.setEnabled(false);
helpEditor.setEditable(false);
jScrollPane1.setViewportView(helpEditor);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
add(jScrollPane1, gridBagConstraints);
navigationPanel.setMaximumSize(new java.awt.Dimension(144, 32767));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
add(navigationPanel, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
// HyperlinkListener
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
JEditorPane pane = (JEditorPane) e.getSource();
if (e instanceof HTMLFrameHyperlinkEvent) {
HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e;
HTMLDocument doc = (HTMLDocument)pane.getDocument();
doc.processHTMLFrameHyperlinkEvent(evt);
} else {
backwardStack.push(pane.getPage());
navigationCallback.navigateBackwardStateChanged(true);
forwardStack.clear();
navigationCallback.navigateForwardStateChanged(false);
try {
pane.setPage(e.getURL());
} catch (IOException e1) {
log.error("IO-Error", e1);
}
}
}
}
public String getResourceKey() {
return resourceKey;
}
public void setResourceKey(String rk) {
resourceKey=rk;
if (resourceKey==null || resourceKey.length()==0) {
return;
}
ResourceBundle bdl=ResourceBundle.getBundle("jrackattack.gui");
if (helpEditor.getPage()!=null) {
backwardStack.push(helpEditor.getPage());
navigationCallback.navigateBackwardStateChanged(true);
forwardStack.clear();
navigationCallback.navigateForwardStateChanged(false);
}
URL u=ClassLoader.getSystemClassLoader().getResource(bdl.getString(resourceKey));
try {
helpEditor.setPage(u);
} catch (IOException e) {
log.error("Faild to load "+resourceKey,e);
helpEditor.setText("<h2>Faild to load "+resourceKey+"</h2>");
}
if (topUrl==null) {
topUrl=u;
}
}
// NavigationListener
public void navigateForward() {
if (forwardStack.size()==0) {
navigationCallback.navigateForwardStateChanged(false);
return;
}
URL u=forwardStack.pop();
try {
helpEditor.setPage(u);
} catch (IOException e) {
log.error("error",e);
}
if (forwardStack.size()==0) {
navigationCallback.navigateForwardStateChanged(false);
}
backwardStack.push(u);
navigationCallback.navigateBackwardStateChanged(true);
}
/**
* Go one step backward. Only possible, if there were
* preceeding navigation events.
*/
public void navigateBackward() {
if (backwardStack.size()==0) {
navigationCallback.navigateBackwardStateChanged(false);
return;
}
URL u=backwardStack.pop();
forwardStack.push(helpEditor.getPage());
navigationCallback.navigateForwardStateChanged(true);
try {
helpEditor.setPage(u);
} catch (IOException e) {
log.error("error",e);
}
if (backwardStack.size()==0) {
navigationCallback.navigateBackwardStateChanged(false);
}
}
/**
* Go back to where the user started from.
*/
public void navigateTop() {
if (backwardStack.size()==0 ||
!backwardStack.peek().equals(helpEditor.getPage())) {
backwardStack.push(helpEditor.getPage());
navigationCallback.navigateBackwardStateChanged(true);
}
try {
helpEditor.setPage(topUrl);
} catch (IOException e) {
log.error("error",e);
}
forwardStack.clear();
navigationCallback.navigateForwardStateChanged(false);
}
/**
* Sets the callback to notify the navigator about
* history state changes.
*/
public void setNavigationCallback(NavigationCallback cb) {
navigationCallback=cb;
}
private String resourceKey;
private NavigationCallback navigationCallback;
private URL topUrl;
private Stack<URL> forwardStack=new Stack<URL>();
private Stack<URL> backwardStack=new Stack<URL>();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JEditorPane helpEditor;
private javax.swing.JScrollPane jScrollPane1;
private jrackattack.gui.NavigationPanel navigationPanel;
// End of variables declaration//GEN-END:variables
}
/*
Copyright (C) 2008 Alexander Methke
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program (gplv3.txt).
*/