//Titel:            SDI-Fenster
//Version:          1.0
//Copyright:
//Autor:
//Firma:
//Beschreibung:


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;  // Exception

// Opendialog
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;
//import java.util.Hashtable;  // Filter
//import java.util.Enumeration; // Filter

// InternalFrame
import javax.swing.event.*;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.*;
import java.io.*;


import org.w3c.dom.Document;
import org.xml.sax.SAXException;

//dient der formatierten Ausgabe von XML-Strings
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
//serialize xml
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;


public class xml1 extends JFrame {
    // Menü Variablen
  private JMenuBar menuBar1;
  private JMenu MainFile;
  private JMenuItem MnNew;
  private JMenuItem MnOpen;
  private JMenuItem MnClose;

  private JMenu MainEdit;
  private JMenuItem MnCut;
  private JMenuItem MnCopy;
  private JMenuItem MnInsert;

  private JTextArea _editor;

   // Konstruktor
  public xml1() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  public void setGUI() {
    this.getContentPane().setLayout( new BorderLayout() );
    this.setSize(new Dimension(600, 400));
    this.setVisible(true);

    _editor = new JTextArea("");
    _editor.setFont(new java.awt.Font("Courier New", 0, 24));
    this.getContentPane().add( new JScrollPane(_editor), BorderLayout.CENTER);


    setMenus();
  }  // setGUI


    // setzt die Menüs
  private void setMenus() {
    menuBar1 = new JMenuBar();

      // DATEI
    MainFile = new JMenu("Datei");
    MnNew = new JMenuItem("Neu");
    MnOpen = new JMenuItem("Öffnen");
    MnClose = new JMenuItem("Schließen");

    MainFile.setFont(new java.awt.Font("Dialog", 0, 30));
    MnNew.setFont(new java.awt.Font("Dialog", 0, 30));
    MnOpen.setFont(new java.awt.Font("Dialog", 0, 30));
    MnClose.setFont(new java.awt.Font("Dialog", 0, 30));

    MnNew.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnNew_click();
      }
    });
    MnNew.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
    MainFile.add(MnNew);

    MnOpen.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnOpen_click();
      }
    });
    MnOpen.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
    MainFile.add(MnOpen);
    MainFile.addSeparator();

    // --------------------------
    MnClose.addActionListener(new ActionListener()  {

      public void actionPerformed(ActionEvent e) {
        MnClose_click();
      }
    });
    MainFile.add(MnClose);

    menuBar1.add(MainFile);

    // EDIT
    MainEdit = new JMenu("Bearbeiten");
    MnCut = new JMenuItem("Ausschneiden");
    MnCopy = new JMenuItem("Kopieren");
    MnInsert = new JMenuItem("Einfügen");

    MainEdit.setFont(new java.awt.Font("Dialog", 0, 30));
    MnCut.setFont(new java.awt.Font("Dialog", 0, 30));
    MnCopy.setFont(new java.awt.Font("Dialog", 0, 30));
    MnInsert.setFont(new java.awt.Font("Dialog", 0, 30));

    MnCut.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnCut_click();
      }
    });
    MainEdit.add(MnCut);

    MnCopy.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnCopy_click();
      }
    });
    MainEdit.add(MnCopy);
    // MainFile.addSeparator();

    // --------------------------
    MnInsert.addActionListener(new ActionListener()  {
      public void actionPerformed(ActionEvent e) {
        MnInsert_click();
      }
    });
    MainEdit.add(MnInsert);
    menuBar1.add(MainEdit);


    this.setJMenuBar(menuBar1);
  } // setMenus








  void MnNew_click() {
	  System.out.println("Neue Datei");
  }  // New


  void MnOpen_click() {
   JFileChooser jFileChooser1 = new JFileChooser();
   String sFileName;

   jFileChooser1.setMultiSelectionEnabled(false);
   jFileChooser1.setCurrentDirectory( new File("C:\\DATEN") );
   jFileChooser1.setCurrentDirectory(  new File(  (String) System.getProperties().get("user.dir")  )   );
   if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
      sFileName = jFileChooser1.getSelectedFile().getPath();
	  loadXML(sFileName);
    }
  }  // MnOpen

  private void add(int tab, String sValue) {
    String sTab="";
    int i;
    for (i=0; i<tab; i++) sTab+="  ";
    _editor.append(sTab+sValue+"\n");
  }

  private void loadXML(String sFileName) {
    int i;
    int tab;
    try {
      FileInputStream xin = new FileInputStream(sFileName);
      XMLInputFactory factory = XMLInputFactory.newInstance();
      XMLStreamReader parser = factory.createXMLStreamReader(xin);
        // Dokument durchparsen
      _editor.setText("");
      tab=0;
      while ( parser.hasNext() ) {
        int typ = parser.next();  
        switch (typ) {
          case XMLStreamConstants.START_ELEMENT:
                 add(tab,"Startelement: "+parser.getLocalName() );
                  // Attribute
                 tab++;
                 for (i=0; i<parser.getAttributeCount(); i++) {
                   add(tab,"Attribut: "+parser.getAttributeLocalName(i)+"  Wert: "+parser.getAttributeValue(i) );
                 }
                 break;

          case XMLStreamConstants.CHARACTERS:
                 if ( !parser.isWhiteSpace() )   // tab cr lf space
                 add(tab,"Charakters: "+parser.getText() );
                 break;

          case XMLStreamConstants.END_ELEMENT:
                 tab--;
                 add(tab,"Endelement");
                 break;

          case XMLStreamConstants.END_DOCUMENT:
                 add(0,"Ende des Dokuments" );
                 parser.close();
                 break;
        }
      }  // while
    }
    catch (IOException e) {
      System.out.println("Fehler1 in XML");
    }
    catch (XMLStreamException e2) {
      System.out.println("Fehler2 in XML");
    }
  }



  void MnClose_click() {
    System.exit(0);
  }

   // -----------------
  void MnCut_click() {
    System.out.println("Menüeintrag Cut");
  }
  void MnCopy_click() {
    System.out.println("Menüeintrag Copy");
  }
  void MnInsert_click() {
    System.out.println("Menüeintrag Insert");
  }



  static public void main(String[] args) {
    xml1 frame = new xml1();
    frame.setVisible(true);
  }

}  // xml1
