/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csa.tiletools.graphics;

import csa.config.Configuration;
import csa.gui.components.ModalInternalFrame;
import csa.gui.dialogs.QuickHelpModal;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author salchr
 */
public class OriginJPanel extends javax.swing.JPanel {
    JInternalFrame modelDialog;

    /**
     * Creates new form OriginJPanel
     */
    public OriginJPanel() {
        initComponents();
    }

    /**
     * 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() {

        jPopupMenu1 = new javax.swing.JPopupMenu();
        jScrollPane1 = new javax.swing.JScrollPane();
        jEditorPane1 = new javax.swing.JEditorPane();
        jToggleButton1 = new javax.swing.JToggleButton();
        jButton1 = new javax.swing.JButton();

        jEditorPane1.setContentType("text/html");
        jScrollPane1.setViewportView(jEditorPane1);

        jToggleButton1.setText("HTML");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        jButton1.setText("Done");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton1)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        modelDialog.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
        String text = "";

        if (jToggleButton1.isSelected())
        {
            try
            {
                text = jEditorPane1.getText();
            }
            catch (Throwable e) {}

            jToggleButton1.setText("Plain text");
            jEditorPane1.setContentType("text/plain");
        }
        else
        {
            try
            {
                text = jEditorPane1.getDocument().getText(0, jEditorPane1.getDocument().getLength());
            }
            catch (Throwable e) {}

            jToggleButton1.setText("HTML");
            jEditorPane1.setContentType("text/html");
        }
         jEditorPane1.setText(text);

/*
        try
        {
            jEditorPane1.getDocument().remove(0,jEditorPane1.getDocument().getLength());
            jEditorPane1.getDocument().insertString(0, text, null);
        }
        catch (Throwable e) {}
*/
    }//GEN-LAST:event_jToggleButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JEditorPane jEditorPane1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToggleButton jToggleButton1;
    // End of variables declaration//GEN-END:variables


    private String mText;
    public static String showOrigin(String text)
    {
        JFrame frame = Configuration.getConfiguration().getMainFrame();
        OriginJPanel panel = new OriginJPanel();
        panel.mText = text;
        panel.jEditorPane1.setContentType("text/html");
//        panel.jEditorPane1.setContentType("text/plain");
        panel.jEditorPane1.setText(panel.mText);
        JInternalFrame modal = new ModalInternalFrame("Help", frame.getRootPane(), frame, panel, panel.jButton1);
        panel.modelDialog = modal;
        modal.setVisible(true);
        panel.mText = panel.jEditorPane1.getText();

        return panel.mText;
    }
    public static String showOrigin(String text, boolean enableEdit)
    {
        JFrame frame = Configuration.getConfiguration().getMainFrame();
        OriginJPanel panel = new OriginJPanel();
        if (!enableEdit)
        {
            panel.jToggleButton1.setVisible(false);
            panel.jEditorPane1.setEnabled(false);
            panel.jEditorPane1.setDisabledTextColor(Color.black);
        }

        panel.mText = text;
        panel.jEditorPane1.setContentType("text/html");
//        panel.jEditorPane1.setContentType("text/plain");
        panel.jEditorPane1.setText(panel.mText);
        JInternalFrame modal = new ModalInternalFrame("Help", frame.getRootPane(), frame, panel, panel.jButton1);
        panel.modelDialog = modal;
        modal.setVisible(true);
        panel.mText = panel.jEditorPane1.getText();

        return panel.mText;
    }

}
