/*
 * frmExec.java
 *
 */

package OOXMLconvert;

import java.awt.Color;
import java.awt.Cursor;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.swing.text.*;

/**
 *
 * @author  ferou
 */
public class frmExec extends javax.swing.JDialog {

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

	/**
	 * Funkcia pridá štýly k StyledDocument
	 * @param doc StyledDocument ku ktorému chceme pridať štýl
	 */
	private void addStylesToDocument(StyledDocument doc) {
		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = doc.addStyle("regular", def);
		StyleConstants.setFontFamily(def, "SansSerif");

		Style s = doc.addStyle("red", regular);
		StyleConstants.setForeground(s, Color.RED);

		s = doc.addStyle("black", regular);
		StyleConstants.setForeground(s, Color.BLACK);

		s = doc.addStyle("blue", regular);
		StyleConstants.setForeground(s, Color.BLUE);
	}

	/**
	 * Funkcia spustí na základe parametru daný príkaz a výstup vypíše do TextArea
	 * @param convertCmd String obsahujúci príkaz na spustenie
	 */
	public void convert(String convertCmd) {
		try {
			this.setCursor(new Cursor(Cursor.WAIT_CURSOR));
			jButton2.setEnabled(false);
			
			StyledDocument doc = jTextPane1.getStyledDocument();
			addStylesToDocument(doc);
			doc.insertString(doc.getLength(), "Executing: " + convertCmd + "\n*********\n", doc.getStyle("blue"));
			jPanel1.paintImmediately(jTextPane1.getBounds());
			Process p = Runtime.getRuntime().exec(convertCmd);
			BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
			BufferedReader es = new BufferedReader(new InputStreamReader(p.getErrorStream()));
			String line;
			while ((line = es.readLine()) != null) {
				doc.insertString(doc.getLength(), line, doc.getStyle("red"));
				jPanel1.paintImmediately(jTextPane1.getBounds());
			}
			while ((line = is.readLine()) != null) {
				doc.insertString(doc.getLength(), line, doc.getStyle("black"));
				jPanel1.paintImmediately(jTextPane1.getBounds());
			}
			doc.insertString(doc.getLength(), "\n*********\nExecuting finished.", doc.getStyle("blue"));
			jPanel1.paintImmediately(jTextPane1.getBounds());

			jButton2.setEnabled(true);
			this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		}
		catch (Exception e) {
			System.err.println(this.getClass().getName() + " (convert): " + e.toString());
		}
}

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

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jButton2 = new javax.swing.JButton();

        setTitle("konverzia DOCX na XHTML");

        jTextPane1.setEditable(false);
        jTextPane1.setPreferredSize(new java.awt.Dimension(6, 6));
        jScrollPane2.setViewportView(jTextPane1);

        jButton2.setText("Zatvor");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 468, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(173, 173, 173)
                            .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(90, 90, 90))
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE))
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 270, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jButton2)
                    .addContainerGap()))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-476)/2, (screenSize.height-304)/2, 476, 304);
    }// </editor-fold>//GEN-END:initComponents

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// koniec
	setVisible(false);
}//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables

}
