//$Header: /cvsroot-fuse/mec-as2/39/mendelson/comm/as2/client/HTMLPanel.java,v 1.1 2012/04/18 14:10:23 heller Exp $
package de.mendelson.comm.as2.client;
import java.awt.Cursor;
import java.awt.Desktop;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JPanel;
/*
* Copyright (C) mendelson-e-commerce GmbH Berlin Germany
*
* This software is subject to the license agreement set forth in the license.
* Please read and agree to all terms before using this software.
* Other product and brand names are trademarks of their respective owners.
*/
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
/**
* Panel that allows to display simple HTML pages
* @author S.Heller
* @version $Revision: 1.1 $
*/
public class HTMLPanel extends JPanel implements HyperlinkListener {
/** Creates new form HTMLPanel */
public HTMLPanel() {
initComponents();
if (Desktop.isDesktopSupported()) {
this.jEditorPane.addHyperlinkListener(this);
}
}
/**Sets a new page to the viewer
*/
public void setPage(File htmlFile) {
try {
this.jEditorPane.setPage(htmlFile.toURI().toURL());
} catch (Exception e) {
//nop
}
}
/**Sets a new page to the viewer and handles the Stack update
*@param url URL to move to
*/
public Header[] setURL(String urlStr, String userAgent, File fallbackOnError) {
Header[] header = new Header[0];
try {
//post for header data
HttpPost filePost = new HttpPost(new URL(urlStr).toExternalForm());
HttpParams params = filePost.getParams();
HttpProtocolParams.setUserAgent(params, userAgent);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(filePost);
header = httpResponse.getAllHeaders();
int status = httpResponse.getStatusLine().getStatusCode();
if (status != HttpServletResponse.SC_OK) {
throw new Exception("HTTP " + status);
}
httpClient.getConnectionManager().shutdown();
//now get for body data
this.jEditorPane.setPage(new URL(urlStr));
} catch (Exception e) {
e.printStackTrace();
try {
this.setPage(fallbackOnError);
} catch (Exception ex) {
//nop
}
}
return (header);
}
/**Listen to be a HyperlinkListener*/
@Override
public void hyperlinkUpdate(HyperlinkEvent e) {
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
URI uri = new URI(e.getURL().toExternalForm());
Desktop.getDesktop().browse(uri);
} catch (Exception ex) {
//nop
}
}
if (e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
jEditorPane.setCursor(new Cursor(Cursor.HAND_CURSOR));
}
if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
jEditorPane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;
jScrollPane = new javax.swing.JScrollPane();
jEditorPane = new javax.swing.JEditorPane();
setLayout(new java.awt.GridBagLayout());
jEditorPane.setContentType("text/html"); // NOI18N
jEditorPane.setEditable(false);
jScrollPane.setViewportView(jEditorPane);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
add(jScrollPane, gridBagConstraints);
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JEditorPane jEditorPane;
private javax.swing.JScrollPane jScrollPane;
// End of variables declaration//GEN-END:variables
}