//Titel:            Dialog-Fenster
//Version:
//Copyright:		Copyright (c) 2010
//Autor:
//Organisation:
//Beschreibung:


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

import java.io.*;  // Exception

// Opendialog
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.*;

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;

// loadxml2
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

// tree
import javax.swing.tree.DefaultMutableTreeNode;

public class Xml3 extends JFrame {

  JButton BnOpen1 = new JButton("Öffnen Editor");
  JButton BnOpen2 = new JButton("Öffnen Tree");
  JButton BnEsc = new JButton("Abbruch");

  private JTabbedPane _register;
  private JTextArea _editor;
  private JTree _tree;


  public Xml3() {
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  private void setGUI() {
    setSize(800,600);
    setLocation(50,50);
    setTitle("JFrame als Dialog");

    BnOpen1.setFont( new Font("Arial", Font.BOLD,18)  );
    BnOpen2.setFont( new Font("Arial", Font.BOLD,18)  );

    BnEsc.setFont( new Font("Arial", Font.BOLD,18)  );

    this.getContentPane().setLayout( new BorderLayout() );
    _register = new JTabbedPane();
    _register.setFont( new Font("Arial", Font.BOLD,24));
    this.getContentPane().add( _register, BorderLayout.CENTER);

    _editor = new JTextArea("");
    _editor.setFont( new Font("Arial", Font.BOLD,18)  );
    _register.add("editor",new JScrollPane(_editor));

    _tree = new JTree();;
    _tree.setFont( new Font("Arial", Font.BOLD,24)  );
    _register.add("tree",new JScrollPane(_tree));

	JPanel pnButton = new JPanel();
	//pnButton.setBackground(Color.red);
	pnButton.setLayout( new FlowLayout() );
	pnButton.add(BnOpen1);
	pnButton.add(BnOpen2);
	pnButton.add(BnEsc);

    getContentPane().add( pnButton, BorderLayout.SOUTH );

    BnOpen1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnOpen1_Click();
            }
     });
    BnOpen2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
              BnOpen2_Click();
            }
     });


    BnEsc.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
          BnEsc_Click();
       }
    });


  } // setGUI

    private void BnOpen1_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();
   	     loadXML1(sFileName);
      }
    }


    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 loadXML1(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");
      }
    }
  
  

    private void BnOpen2_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();
   	     loadXML2(sFileName);
      }
     }

    private void insertNodes(DefaultMutableTreeNode root, Node xmlNode) {
      Node xmlNode1;
      DefaultMutableTreeNode node;
      node = new DefaultMutableTreeNode( xmlNode.getNodeName() );
        root.add( node );
       /*switch (xmlNode1.getNodeType() ) {
          case Node.TEXT_NODE:  
                    break;
          case Node.ELEMENT_NODE: break;
          case Node.ENTITY_NODE: break;
          case Node.ATTRIBUTE_NODE: break;
          case Node.CDATA_SECTION_NODE: break;
          case Node.COMMENT_NODE: break;
          case Node.DOCUMENT_FRAGMENT_NODE: break;
          case Node.DOCUMENT_NODE: break;
          case Node.DOCUMENT_TYPE_NODE: break;
          case Node.ENTITY_REFERENCE_NODE: break;
          case Node.NOTATION_NODE: break;
          case Node.PROCESSING_INSTRUCTION_NODE: break;
       }*/
       // Childs
       xmlNode1 = xmlNode.getFirstChild();
       if (xmlNode1!=null) {
         insertNodes(node, xmlNode1);
       }
       // Sibling
       xmlNode1 = xmlNode.getNextSibling();
       if (xmlNode1!=null) {
         insertNodes(root, xmlNode1);
       }

    }
    private void loadXML2(String sFileName) {
       DefaultMutableTreeNode root, node;
      try {
           // ---- Parse XML file ----
           DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
           // factory.setNamespaceAware( true );
           // factory.setValidating( true );
           DocumentBuilder builder  = factory.newDocumentBuilder();
           Document        document = builder.parse( new File( sFileName ) );
           Node            xmlrootNode = document.getDocumentElement();
     
           // root = new DefaultMutableTreeNode("root");
           root = new DefaultMutableTreeNode( xmlrootNode.getNodeName() );
           insertNodes(root, xmlrootNode);

//           node = xmlrootNode.getNodeValue();
           _register.removeTabAt(1);
           _tree = new javax.swing.JTree(root);
           _register.add("tree",new JScrollPane(_tree));
 
           // ---- Error handling ----
         } catch( SAXParseException spe ) {
             System.out.println( "\n** Parsing error, line " + spe.getLineNumber()
                                                 + ", uri "  + spe.getSystemId() );
             System.out.println( "   " + spe.getMessage() );
             Exception e = ( spe.getException() != null ) ? spe.getException() : spe;
             e.printStackTrace();
         } catch( SAXException sxe ) {
             Exception e = ( sxe.getException() != null ) ? sxe.getException() : sxe;
             e.printStackTrace();
         } catch( ParserConfigurationException pce ) {
             pce.printStackTrace();
         } catch( IOException ioe ) {
             ioe.printStackTrace();
         }
    }

    private void BnEsc_Click() {
        setVisible(false);
        System.exit(0);
    }







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

}  // xml3


