5093 initial change of Html viewer to use JavaFx

This commit is contained in:
William Schaefer 2019-05-16 14:08:17 -04:00
parent b096a58c61
commit d9ae4dc8cb
2 changed files with 46 additions and 59 deletions

View File

@ -16,41 +16,24 @@
<Layout> <Layout>
<DimensionLayout dim="0"> <DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Component id="htmlScrollPane" pref="300" max="32767" attributes="0"/>
<Group type="102" attributes="0"> <Group type="102" attributes="0">
<Component id="showImagesToggleButton" min="-2" max="-2" attributes="0"/> <Component id="showImagesToggleButton" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/> <EmptySpace min="0" pref="203" max="32767" attributes="0"/>
</Group> </Group>
<Component id="htmlScrollPane" max="32767" attributes="0"/>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
<DimensionLayout dim="1"> <DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0"> <Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0"> <Group type="102" alignment="0" attributes="0">
<Component id="showImagesToggleButton" min="-2" max="-2" attributes="0"/> <Component id="showImagesToggleButton" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/> <EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="htmlScrollPane" pref="71" max="32767" attributes="0"/> <Component id="htmlScrollPane" pref="74" max="32767" attributes="0"/>
</Group> </Group>
</Group> </Group>
</DimensionLayout> </DimensionLayout>
</Layout> </Layout>
<SubComponents> <SubComponents>
<Container class="javax.swing.JScrollPane" name="htmlScrollPane">
<Properties>
<Property name="verticalScrollBarPolicy" type="int" value="22"/>
</Properties>
<AuxValues>
<AuxValue name="autoScrollPane" type="java.lang.Boolean" value="true"/>
</AuxValues>
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JTextPane" name="htmlbodyTextPane">
<Properties>
<Property name="editable" type="boolean" value="false"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JToggleButton" name="showImagesToggleButton"> <Component class="javax.swing.JToggleButton" name="showImagesToggleButton">
<Properties> <Properties>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor"> <Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
@ -61,5 +44,9 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="showImagesToggleButtonActionPerformed"/> <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="showImagesToggleButtonActionPerformed"/>
</Events> </Events>
</Component> </Component>
<Container class="javax.swing.JScrollPane" name="htmlScrollPane">
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
</Container>
</SubComponents> </SubComponents>
</Form> </Form>

View File

@ -18,6 +18,10 @@
*/ */
package org.sleuthkit.autopsy.contentviewers; package org.sleuthkit.autopsy.contentviewers;
import javafx.application.Platform;
import javafx.scene.web.WebView;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.openide.util.NbBundle.Messages; import org.openide.util.NbBundle.Messages;
@ -29,16 +33,23 @@ import org.openide.util.NbBundle.Messages;
final class HtmlPanel extends javax.swing.JPanel { final class HtmlPanel extends javax.swing.JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private WebView webView;
private String htmlText; private String htmlText;
private JFXPanel jfxPanel = new JFXPanel();
/** /**
* Creates new form HtmlViewerPanel * Creates new form HtmlViewerPanel
*/ */
HtmlPanel() { HtmlPanel() {
initComponents(); initComponents();
//wjs
Utilities.configureTextPaneAsHtml(htmlbodyTextPane); Platform.runLater(() -> {
webView = new WebView();
webView.getEngine().setJavaScriptEnabled(false);
Scene scene = new Scene(webView);
jfxPanel.setScene(scene);
htmlScrollPane.setViewportView(jfxPanel);
});
} }
/** /**
@ -55,21 +66,13 @@ final class HtmlPanel extends javax.swing.JPanel {
* Clear the HTML in the text pane and disable the show/hide button. * Clear the HTML in the text pane and disable the show/hide button.
*/ */
void reset() { void reset() {
htmlbodyTextPane.setText(""); //wjs
Platform.runLater(() -> {
webView.getEngine().loadContent("");
});
showImagesToggleButton.setEnabled(false); showImagesToggleButton.setEnabled(false);
} }
/**
* Guarantee the HTML text has 'html' and 'body' tags.
*
* @param htmlText The HTML text
*
* @return The HTML text with the 'html' and 'body' tags applied.
*/
private String wrapInHtmlBody(String htmlText) {
return "<html><body>" + htmlText + "</body></html>";
}
/** /**
* Cleans out input HTML string * Cleans out input HTML string
* *
@ -93,22 +96,26 @@ final class HtmlPanel extends javax.swing.JPanel {
@Messages({ @Messages({
"HtmlPanel_showImagesToggleButton_show=Show Images", "HtmlPanel_showImagesToggleButton_show=Show Images",
"HtmlPanel_showImagesToggleButton_hide=Hide Images", "HtmlPanel_showImagesToggleButton_hide=Hide Images",
"Html_text_display_error=The HTML text cannot be displayed, it may not be correctly formed HTML.", "Html_text_display_error=The HTML text cannot be displayed, it may not be correctly formed HTML.",})
})
private void refresh() { private void refresh() {
if (false == htmlText.isEmpty()) { if (false == htmlText.isEmpty()) {
try { try {
if (showImagesToggleButton.isSelected()) { if (showImagesToggleButton.isSelected()) {
showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_hide()); showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_hide());
this.htmlbodyTextPane.setText(wrapInHtmlBody(htmlText)); Platform.runLater(() -> {
webView.getEngine().loadContent("JUST A STRING");
});
} else { } else {
showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_show()); showImagesToggleButton.setText(Bundle.HtmlPanel_showImagesToggleButton_show());
this.htmlbodyTextPane.setText(wrapInHtmlBody(cleanseHTML(htmlText))); Platform.runLater(() -> {
webView.getEngine().loadContent(cleanseHTML("JUST A STRING"));
});
} }
showImagesToggleButton.setEnabled(true); showImagesToggleButton.setEnabled(true);
htmlbodyTextPane.setCaretPosition(0); } catch (Exception ignored) {
} catch(Exception ex) { Platform.runLater(() -> {
this.htmlbodyTextPane.setText(wrapInHtmlBody(Bundle.Html_text_display_error())); webView.getEngine().loadContent(Bundle.Html_text_display_error());
});
} }
} }
} }
@ -122,14 +129,8 @@ final class HtmlPanel extends javax.swing.JPanel {
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() { private void initComponents() {
htmlScrollPane = new javax.swing.JScrollPane();
htmlbodyTextPane = new javax.swing.JTextPane();
showImagesToggleButton = new javax.swing.JToggleButton(); showImagesToggleButton = new javax.swing.JToggleButton();
htmlScrollPane = new javax.swing.JScrollPane();
htmlScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
htmlbodyTextPane.setEditable(false);
htmlScrollPane.setViewportView(htmlbodyTextPane);
org.openide.awt.Mnemonics.setLocalizedText(showImagesToggleButton, org.openide.util.NbBundle.getMessage(HtmlPanel.class, "HtmlPanel.showImagesToggleButton.text")); // NOI18N org.openide.awt.Mnemonics.setLocalizedText(showImagesToggleButton, org.openide.util.NbBundle.getMessage(HtmlPanel.class, "HtmlPanel.showImagesToggleButton.text")); // NOI18N
showImagesToggleButton.addActionListener(new java.awt.event.ActionListener() { showImagesToggleButton.addActionListener(new java.awt.event.ActionListener() {
@ -142,17 +143,17 @@ final class HtmlPanel extends javax.swing.JPanel {
this.setLayout(layout); this.setLayout(layout);
layout.setHorizontalGroup( layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(htmlScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(showImagesToggleButton) .addComponent(showImagesToggleButton)
.addGap(0, 0, Short.MAX_VALUE)) .addGap(0, 203, Short.MAX_VALUE))
.addComponent(htmlScrollPane)
); );
layout.setVerticalGroup( layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup() .addGroup(layout.createSequentialGroup()
.addComponent(showImagesToggleButton) .addComponent(showImagesToggleButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(htmlScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)) .addComponent(htmlScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 74, Short.MAX_VALUE))
); );
}// </editor-fold>//GEN-END:initComponents }// </editor-fold>//GEN-END:initComponents
@ -163,7 +164,6 @@ final class HtmlPanel extends javax.swing.JPanel {
// Variables declaration - do not modify//GEN-BEGIN:variables // Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane htmlScrollPane; private javax.swing.JScrollPane htmlScrollPane;
private javax.swing.JTextPane htmlbodyTextPane;
private javax.swing.JToggleButton showImagesToggleButton; private javax.swing.JToggleButton showImagesToggleButton;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
} }