/*
* Copyright (C) 2013 Vinu K.N
*
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
package org.domainmath.gui.help;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JEditorPane;
import javax.swing.SwingWorker;
import javax.swing.event.HyperlinkEvent;
import org.domainmath.gui.MainFrame;
/**
*
* @author Vinu K.N
*/
public class QuickHelpPanel extends javax.swing.JPanel {
private String dir;
private LoadPageTask loadPageTask;
private String parent_root;
int n;
/**
* Creates new form QuickHelpPanel
*/
public QuickHelpPanel() {
String path = System.getProperty("user.dir")+File.separator+"doc_cache";
n=0;
new File(path).mkdir();
parent_root=path+File.separator;
initComponents();
this.htmlView.addHyperlinkListener((HyperlinkEvent e) -> {
try
{
if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED){
String c = e.getDescription();
String s = c.replace("#", "").toLowerCase();
generate(s);
}
}catch(Exception ex){
}
});
}
public JEditorPane getHtmlView() {
return htmlView;
}
public void setHtmlView(JEditorPane htmlView) {
this.htmlView = htmlView;
}
public void load(String dir){
this.dir=dir;
(loadPageTask = new LoadPageTask()).execute();
}
private void displayPage(String address) {
try {
this.htmlView.setPage(address);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void setHelpText(String s) {
this.jTextField1.setText(s);
}
/**
* 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() {
jToolBar2 = new javax.swing.JToolBar();
jButton2 = new javax.swing.JButton();
jSeparator1 = new javax.swing.JToolBar.Separator();
jLabel1 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
htmlView = new javax.swing.JEditorPane();
setLayout(new java.awt.BorderLayout());
jToolBar2.setFloatable(false);
jToolBar2.setRollover(true);
jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/icons/view-refresh.png"))); // NOI18N
jButton2.setToolTipText("Refresh");
jButton2.setFocusable(false);
jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jToolBar2.add(jButton2);
jToolBar2.add(jSeparator1);
jLabel1.setText("Search ");
jToolBar2.add(jLabel1);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jToolBar2.add(jTextField1);
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/domainmath/gui/icons/edit-find-5.png"))); // NOI18N
jButton1.setToolTipText("Search");
jButton1.setFocusable(false);
jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jToolBar2.add(jButton1);
add(jToolBar2, java.awt.BorderLayout.PAGE_START);
htmlView.setEditable(false);
htmlView.setContentType("text/html"); // NOI18N
jScrollPane1.setViewportView(htmlView);
add(jScrollPane1, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
public void search(String s) {
if(s != null) {
generate(s);
}
}
public void generate(String s){
String file="doc_"+n;
MainFrame.octavePanel.evaluate("DomainMath_QuickHelp('"+parent_root+file+".html','"+s+"');");
load(parent_root+file+".html");
n++;
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
String s= jTextField1.getText().toLowerCase();
if(s != null) {
generate(s);
}
}//GEN-LAST:event_jTextField1ActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
String s= jTextField1.getText().toLowerCase();
MainFrame.octavePanel.evaluate("pkg load all;");
if(s != null) {
generate(s);
}
}//GEN-LAST:event_jButton2ActionPerformed
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
String s= jTextField1.getText().toLowerCase();
MainFrame.octavePanel.evaluate("pkg load all;");
if(s != null) {
generate(s);
}
}//GEN-LAST:event_jButton1ActionPerformed
private class LoadPageTask extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() {
int delay = 1000; //milliseconds
ActionListener taskPerformer = (ActionEvent evt) -> {
displayPage("file:///"+dir);
};
javax.swing.Timer t= new javax.swing.Timer(delay, taskPerformer);
t.setRepeats(false);
t.start();
return null;
}
@Override
protected void done() {
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JEditorPane htmlView;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JToolBar.Separator jSeparator1;
private javax.swing.JTextField jTextField1;
private javax.swing.JToolBar jToolBar2;
// End of variables declaration//GEN-END:variables
}