/*
 * KommentarEditor.java
 *
 * Created on 14. März 2001, 16:21
 */

import java.awt.*;
import java.io.*;
import java.lang.*;
import java.applet.*;

import com.oroinc.text.regex.*;
import com.oroinc.text.perl.*;

/**
 *
 * @author  Dr. Jan-Torsten Milde
 * @version
 */
public class KommentarEditor extends java.applet.Applet {

    private KommentarListe KL = new KommentarListe();
    private static String DEFAULT_FNAME = "zugabe.wko";
    private static String DEFAULT_XML_FNAME = "c:/temp/kommentare.xml";
    private static String DEFAULT_HTML_FNAME = "../eintrag.htm";
    private static String DEFAULT_HTML_FNAME1 = "../eintraga.htm";
    private Perl5Util perl = new Perl5Util();


    public void setToLetter (String letter) {
        if (letter != null) {
            if (KL.containsKey(letter.toLowerCase())) {
                KL.setCurrentLetter(letter.toLowerCase());
                KommentarTextArea.setText(KL.getCurrentComment());
                setBrieflabel(KL.getCurrentLetter());
            }
        }
    }

    public void readFile (String fname) throws IOException {
        String myFile = fname;
        String firstLine;

        SecurityException se = null;
        File f = new File(myFile);

        if (f.exists())
        try {
            FileInputStream fileIn = new FileInputStream(myFile);
            ObjectInputStream in = new ObjectInputStream(fileIn);
            KL = new KommentarListe();
            KL = (KommentarListe)(in.readObject());
            in.close();
        } catch (ClassNotFoundException cnf) {
            System.out.println("cannot find class KommentarListe");
        } catch (IOException ioe) {
            System.out.println("readFile: caught IO exception");
            throw ioe;
        } catch (SecurityException e) {
            se = e;
            throw e;
        } else {
            System.out.println ("but " + myFile + " doesn't exist");
        }

        if (KL != null) {
            KommentarTextArea.setText(KL.getCurrentComment());
            setBrieflabel(KL.getCurrentLetter());
            Letterchoice1.select(KL.getCurrentLetter());
        }

    }

    public void writeFile (String fname) {
        String myFile = fname;
        File f = new File(myFile);

        try {
            FileOutputStream fileOut = new FileOutputStream(myFile);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(this.KL);
            out.close();
            System.out.println("Successful attempt to write to " + myFile);
        }
        catch (SecurityException e) {
            System.out.println("writeFile: caught security exception");
        }
        catch (IOException ioe) {
            System.out.println("writeFile: caught i/o exception");
        }
    }

    public String normalizeString (String s) {
        try {
            String line = perl.substitute("s/\\n/<N><\\/N>/g", s);
            return (line);
        } catch(MalformedPerl5PatternException e) {
            System.out.println ("Fehler bei Ersetzung");
            return (s);
        }
    }


    public void exportFile (String fname, int type, boolean isSyn) {
        String myFile = fname;
        try {
            FileOutputStream fileOut = new FileOutputStream(fname);
            OutputStreamWriter outr = new OutputStreamWriter(fileOut, "ISO-8859-1");
            PrintWriter pout = new PrintWriter(outr);
            String content = normalizeString(KL.toString());
            if (type == 1) {
                content = normalizeString(KL.toHTMLString(isSyn));
            }
            pout.println (content);
            pout.close();
            System.out.println("Successful attempt to write to " + myFile);
        }
        catch (SecurityException e) {
            System.out.println("writeFile: caught security exception");
        }
        catch (IOException ioe) {
            System.out.println("writeFile: caught i/o exception" + ioe);
        }
    }




    public void setBrieflabel (String s) {
        BriefLabel.setText(s);
        this.repaint();
    }

    /** Initializes the applet KommentarEditor */
    public void init () {
        initComponents ();

        KommentarTextArea.setText(KL.getCurrentComment());
        setBrieflabel(KL.getCurrentLetter());
        FileNameTextField.setText(DEFAULT_FNAME);
        loadDefaultFile();
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    private void initComponents () {//GEN-BEGIN:initComponents
        KommentarTextArea = new java.awt.TextArea ();
        panel3 = new java.awt.Panel ();
        button7 = new java.awt.Button ();
        button1 = new java.awt.Button ();
        panel4 = new java.awt.Panel ();
        BriefLabel = new java.awt.Label ();
        FileNameTextField = new java.awt.TextField ();
        panel5 = new java.awt.Panel ();
        Letterchoice1 = new java.awt.Choice ();
        initChoice();
        button8 = new java.awt.Button ();
        button9 = new java.awt.Button ();
        button2 = new java.awt.Button ();
        setLayout (new java.awt.BorderLayout ());

        KommentarTextArea.setBackground (java.awt.Color.white);
        KommentarTextArea.setFont (new java.awt.Font ("Dialog", 0, 14));
        KommentarTextArea.setForeground (java.awt.Color.black);


        add (KommentarTextArea, java.awt.BorderLayout.CENTER);

        panel3.setFont (new java.awt.Font ("Dialog", 0, 11));
        panel3.setBackground (new java.awt.Color (15, 5, 150));
        panel3.setForeground (java.awt.Color.black);

          button7.setFont (new java.awt.Font ("Dialog", 0, 11));
          button7.setLabel ("Speichern");
          button7.setName ("button7");
          button7.setBackground (java.awt.Color.lightGray);
          button7.setForeground (java.awt.Color.black);
          button7.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  button7ActionPerformed (evt);
              }
          }
          );
  
          panel3.add (button7);
  
          button1.setFont (new java.awt.Font ("Dialog", 0, 11));
          button1.setLabel ("Laden");
          button1.setBackground (java.awt.Color.lightGray);
          button1.setForeground (java.awt.Color.black);
          button1.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  button1ActionPerformed (evt);
              }
          }
          );
  
          panel3.add (button1);
  

        add (panel3, java.awt.BorderLayout.SOUTH);

        panel4.setLayout (new java.awt.BorderLayout ());
        panel4.setFont (new java.awt.Font ("Dialog", 0, 11));
        panel4.setBackground (new java.awt.Color (15, 5, 150));
        panel4.setForeground (java.awt.Color.black);

          BriefLabel.setFont (new java.awt.Font ("Dialog", 1, 14));
          BriefLabel.setName ("BriefLabel");
          BriefLabel.setBackground (new java.awt.Color (15, 5, 150));
          BriefLabel.setForeground (java.awt.Color.white);
          BriefLabel.setText ("12-12-72");
          BriefLabel.setAlignment (java.awt.Label.CENTER);
  
          panel4.add (BriefLabel, java.awt.BorderLayout.CENTER);
  
          FileNameTextField.setBackground (java.awt.Color.white);
          FileNameTextField.setFont (new java.awt.Font ("Dialog", 0, 11));
          FileNameTextField.setForeground (java.awt.Color.black);
          FileNameTextField.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  FileNameTextFieldActionPerformed (evt);
              }
          }
          );
  
          panel4.add (FileNameTextField, java.awt.BorderLayout.SOUTH);
  

        add (panel4, java.awt.BorderLayout.NORTH);

        panel5.setLayout (new java.awt.GridLayout (5, 1, 5, 0));
        panel5.setFont (new java.awt.Font ("Dialog", 0, 11));
        panel5.setForeground (java.awt.Color.black);

          Letterchoice1.setFont (new java.awt.Font ("Dialog", 0, 11));
          Letterchoice1.setBackground (java.awt.Color.white);
          Letterchoice1.setForeground (java.awt.Color.black);
          Letterchoice1.addItemListener (new java.awt.event.ItemListener () {
              public void itemStateChanged (java.awt.event.ItemEvent evt) {
                  Letterchoice1ItemStateChanged (evt);
              }
          }
          );
  
          panel5.add (Letterchoice1);
  
          button8.setFont (new java.awt.Font ("Dialog", 0, 11));
          button8.setLabel ("Zur\u00fcck");
          button8.setName ("button8");
          button8.setBackground (java.awt.Color.lightGray);
          button8.setForeground (java.awt.Color.black);
          button8.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  button8ActionPerformed (evt);
              }
          }
          );
  
          panel5.add (button8);
  
          button9.setFont (new java.awt.Font ("Dialog", 0, 11));
          button9.setLabel ("Vorw\u00e4rts");
          button9.setName ("button9");
          button9.setBackground (java.awt.Color.lightGray);
          button9.setForeground (java.awt.Color.black);
          button9.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  button9ActionPerformed (evt);
              }
          }
          );
  
          panel5.add (button9);
  
          button2.setFont (new java.awt.Font ("Dialog", 0, 11));
          button2.setLabel ("Alles l\u00f6schen");
          button2.setBackground (java.awt.Color.lightGray);
          button2.setForeground (java.awt.Color.black);
          button2.addActionListener (new java.awt.event.ActionListener () {
              public void actionPerformed (java.awt.event.ActionEvent evt) {
                  button2ActionPerformed (evt);
              }
          }
          );
  
          panel5.add (button2);
  

        add (panel5, java.awt.BorderLayout.EAST);

    }//GEN-END:initComponents

  private void Letterchoice1ItemStateChanged (java.awt.event.ItemEvent evt) {//GEN-FIRST:event_Letterchoice1ItemStateChanged
    // Add your handling code here:
    this.setToLetter(Letterchoice1.getSelectedItem());
  }//GEN-LAST:event_Letterchoice1ItemStateChanged

  private void FileNameTextFieldActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FileNameTextFieldActionPerformed
    // Add your handling code here:
    DEFAULT_FNAME=FileNameTextField.getText();
    FileNameTextField.setText(DEFAULT_FNAME);
  }//GEN-LAST:event_FileNameTextFieldActionPerformed

  private void button2ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
    // Add your handling code here:
    if (KL != null) {
        int n = javax.swing.JOptionPane.showConfirmDialog(this,
        "Sollen alle Kommentare gelöscht werden?",
        "Kommentare löschen",
        javax.swing.JOptionPane.YES_NO_OPTION);
        if (n == javax.swing.JOptionPane.YES_OPTION) {
            KL.initWerther();
            KommentarTextArea.setText(KL.getCurrentComment());
            setBrieflabel(KL.getCurrentLetter());
            Letterchoice1.select(KL.getCurrentLetter());
        }
    }
  }//GEN-LAST:event_button2ActionPerformed

  private void button1ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
    // Add your handling code here:
        // Add your handling code here:
    DEFAULT_FNAME=FileNameTextField.getText();
    FileNameTextField.setText(DEFAULT_FNAME);

    loadDefaultFile();
  }//GEN-LAST:event_button1ActionPerformed

  private void button9ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button9ActionPerformed
    // Add your handling code here:
    //System.out.println ("runter");
    String content = KommentarTextArea.getText();
    KL.setCurrentComment(content);
    KL.nextLetter();
    this.setBrieflabel(KL.getCurrentLetter());
    KommentarTextArea.setText(KL.getCurrentComment());
  }//GEN-LAST:event_button9ActionPerformed

  private void button8ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button8ActionPerformed
    // Add your handling code here:
    //System.out.println ("hoch");
    String content = KommentarTextArea.getText();
    KL.setCurrentComment(content);
    KL.previousLetter();
    this.setBrieflabel(KL.getCurrentLetter());
    KommentarTextArea.setText(KL.getCurrentComment());
  }//GEN-LAST:event_button8ActionPerformed

  private void button7ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button7ActionPerformed
    // Add your handling code here:
    DEFAULT_FNAME=FileNameTextField.getText();
    FileNameTextField.setText(DEFAULT_FNAME);
    //System.out.println ("sichern");
    String content = KommentarTextArea.getText();
    KL.setCurrentComment(content);
    this.writeFile(DEFAULT_FNAME);
    //this.exportFile(DEFAULT_XML_FNAME, 0);
    this.exportFile(DEFAULT_HTML_FNAME, 1, true);
    this.exportFile(DEFAULT_HTML_FNAME1, 1, false);
  }//GEN-LAST:event_button7ActionPerformed

  private void menuItem5ActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItem5ActionPerformed
    // Add your handling code here:
    System.out.println ("now saving file");
  }//GEN-LAST:event_menuItem5ActionPerformed


// Variables declaration - do not modify//GEN-BEGIN:variables
private java.awt.TextArea KommentarTextArea;
private java.awt.Panel panel3;
private java.awt.Button button7;
private java.awt.Button button1;
private java.awt.Panel panel4;
private java.awt.Label BriefLabel;
private java.awt.TextField FileNameTextField;
private java.awt.Panel panel5;
private java.awt.Choice Letterchoice1;
private java.awt.Button button8;
private java.awt.Button button9;
private java.awt.Button button2;
// End of variables declaration//GEN-END:variables


public void initChoice () {
    Letterchoice1.add("komplett");

    // 1-tes Buch
    Letterchoice1.add("vorrede");

    Letterchoice1.add("04-05-71");
    Letterchoice1.add("10-05-71");
    Letterchoice1.add("12-05-71");
    Letterchoice1.add("13-05-71");
    Letterchoice1.add("15-05-71");
    Letterchoice1.add("22-05-71");
    Letterchoice1.add("26-05-71");
    Letterchoice1.add("30-05-71");

    Letterchoice1.add("16-06-71");
    Letterchoice1.add("19-06-71");
    Letterchoice1.add("29-06-71");

    Letterchoice1.add("01-07-71");
    Letterchoice1.add("06-07-71");
    Letterchoice1.add("08-07-71");
    Letterchoice1.add("10-07-71");
    Letterchoice1.add("11-07-71");
    Letterchoice1.add("13-07-71");
    Letterchoice1.add("16-07-71");
    Letterchoice1.add("18-07-71");
    Letterchoice1.add("19-07-71");
    Letterchoice1.add("20-07-71");
    Letterchoice1.add("24-07-71");
    Letterchoice1.add("26-07-71");
    Letterchoice1.add("30-07-71");

    Letterchoice1.add("08-08-71");
    Letterchoice1.add("10-08-71");
    Letterchoice1.add("12-08-71");
    Letterchoice1.add("15-08-71");
    Letterchoice1.add("18-08-71");
    Letterchoice1.add("21-08-71");
    Letterchoice1.add("22-08-71");
    Letterchoice1.add("28-08-71");
    Letterchoice1.add("30-08-71");

    Letterchoice1.add("03-09-71");
    Letterchoice1.add("10-09-71");

    // 2-tes Buch
    Letterchoice1.add("20-10-71");
    Letterchoice1.add("26-11-71");
    Letterchoice1.add("24-12-71");

    Letterchoice1.add("08-01-72");
    Letterchoice1.add("20-01-72");

    Letterchoice1.add("08-02-72");
    Letterchoice1.add("17-02-72");
    Letterchoice1.add("20-02-72");

    Letterchoice1.add("15-03-72");
    Letterchoice1.add("16-03-72");
    Letterchoice1.add("24-03-72");

    Letterchoice1.add("19-04-72");

    Letterchoice1.add("05-05-72");
    Letterchoice1.add("09-05-72");
    Letterchoice1.add("25-05-72");

    Letterchoice1.add("11-06-72");
    Letterchoice1.add("16-06-72");
    Letterchoice1.add("18-06-72");

    Letterchoice1.add("29-07-72");

    Letterchoice1.add("04-08-72");
    Letterchoice1.add("21-08-72");

    Letterchoice1.add("03-09-72");
    Letterchoice1.add("04-09-72");
    Letterchoice1.add("05-09-72");
    Letterchoice1.add("06-09-72");
    Letterchoice1.add("12-09-72");
    Letterchoice1.add("15-09-72");

    Letterchoice1.add("10-10-72");
    Letterchoice1.add("12-10-72");
    Letterchoice1.add("26-10-72");
    Letterchoice1.add("27-10-72");
    Letterchoice1.add("30-10-72");

    Letterchoice1.add("03-11-72");
    Letterchoice1.add("08-11-72");
    Letterchoice1.add("15-11-72");
    Letterchoice1.add("21-11-72");
    Letterchoice1.add("22-11-72");
    Letterchoice1.add("24-11-72");
    Letterchoice1.add("26-11-72");
    Letterchoice1.add("30-11-72");

    Letterchoice1.add("01-12-72");
    Letterchoice1.add("04-12-72");
    Letterchoice1.add("06-12-72");

    Letterchoice1.add("bericht 1");

    Letterchoice1.add("12-12-72");
    Letterchoice1.add("14-12-72");
    Letterchoice1.add("20-12-72");

    Letterchoice1.add("bericht 2");
    Letterchoice1.add("ossian");
    Letterchoice1.add("abschied");
    Letterchoice1.add("ende");

}


private void loadDefaultFile () {
    try {
        this.readFile(DEFAULT_FNAME);
    } catch (Exception e) {
        System.out.println ("fehler beim laden");
    }
}

}