package traduccion;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.JTabbedPane;
import java.awt.Dimension;
import javax.swing.JSplitPane;
import javax.swing.JTree;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;

//import com.umlet.element.base.Actor;
import myuml.*;
import javax.swing.BorderFactory;
import com.umlet.control.Constants;
import traduccion.TraduccionMarco;

//Entrada/Salida
import javax.swing.text.*;

//import java.io.File;
import java.io.*;
import java.util.Date;
import java.io.Serializable;

//Librerias necesarias para tratar el xml
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.Rectangle;
import javax.swing.tree.*;

import java.net.*;

/**
 * <p>Title: Generación de Coreografías de Servicios Web</p>
 *
 * <p>Description: Clase que gestiona la ventana de diálogo de la aplicación </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: UCLM</p>
 *
 * @version 1.0
 */
public class Dialogo extends JComponent implements Serializable{
    Document doc;
    String fichero = new String();
    Element content, header, raiz, contmodelo, collaboration, interaction;
    Date dat = new Date();
    //Identificador para los objetos, es un número aleatorio
    Random ident = new Random();
    //Declaración de todos los componentes que se utilizan en el diseño de la aplicación
    JPanel contentPane;
    BorderLayout borderLayout1 = new BorderLayout();
    JLabel statusBar = new JLabel();
    JPanel jPanelUML = new JPanel();
    BorderLayout borderLayout2 = new BorderLayout();
    DefaultMutableTreeNode elemen= new DefaultMutableTreeNode("Código XMI");
    public JPanel jPanelDraw = new JPanel();
    JToolBar jToolBar1 = new JToolBar();
    JButton jButtonActor = new JButton();
    JButton jButtonLifeline = new JButton();
    JButton jButtonMessage = new JButton();
    String ruta = "";
    Border border1 = BorderFactory.createEmptyBorder(1, 0, 0, 0);
    JButton jButton4 = new JButton();
    XYLayout xYLayout1 = new XYLayout();


    public Dialogo() {
        try {
            jbInit();
            icons();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(borderLayout1);
        setSize(new Dimension(591, 584));
        setTitle("Generación de Coreografías");
        statusBar.setText(" ");
        jPanelUML.setLayout(borderLayout2);
        jPanelDraw.setLayout(xYLayout1);
        jToolBar1.setBorder(BorderFactory.createEtchedBorder());
        jToolBar1.setFloatable(false);
        jButtonLifeline.addActionListener(new
                                          Dialogo_jButtonLifeline_actionAdapter(this));
        jButtonActor.addActionListener(new Dialogo_jButtonActor_actionAdapter(this));
        jButtonMessage.addActionListener(new
                                         Dialogo_jButtonMessage_actionAdapter(this));
        jButton4.setText("Clear");
        jButton4.addActionListener(new Dialogo_jButton4_actionAdapter(this));
        borderLayout2.setHgap(10);
        borderLayout2.setVgap(5);
        contentPane.add(jPanelUML, java.awt.BorderLayout.CENTER);
        contentPane.add(statusBar, BorderLayout.SOUTH);

        jPanelUML.add(jPanelDraw, java.awt.BorderLayout.CENTER);
        jToolBar1.add(jButtonLifeline);
        jToolBar1.add(jButtonActor);
        jToolBar1.add(jButtonMessage);
        jToolBar1.add(jButton4);
        jPanelDraw.add(jToolBar1, new XYConstraints(0, 0, 1200, -1));
    }

    /**
     * Método que asigna a cada botón el icono que queremos que tenga
     */
    private void icons() {
        jButtonActor.setIcon(new ImageIcon(traduccion.Dialogo.class.getResource(
                "tb_actor.png")));
        jButtonLifeline.setIcon(new ImageIcon(traduccion.Dialogo.class.
                                              getResource(
                "tb_lifeline.png")));
        jButtonMessage.setIcon(new ImageIcon(traduccion.Dialogo.class.
                                             getResource(
                "tb_association_directed.png")));

    }

    /**
     * Método que dibuja un Lifeline sobre la ventana de diseño en las coordenadas
     * indicadas
     *
     * @param e ActionEvent
     */
    public void jButtonLifeline_actionPerformed(ActionEvent e) {
        XYConstraints c = new XYConstraints(0, 50, 100, 1000);
        MyLifeLine life = new MyLifeLine("LifeLine", c);
        jPanelDraw.add(life, c);
        statusBar.setText(" New LifeLine");
        int id = Math.abs(ident.nextInt());
        String idlifeline = "LF_" + id;
        life.setID(idlifeline);
        repaint();
    }


    /**
     * Método que dibuja un actor sobre la ventana de diseño en las coordenadas
     * indicadas
     *
     * @param e ActionEvent
     */
    public void jButtonActor_actionPerformed(ActionEvent e) {
        XYConstraints c = new XYConstraints(0, 50, 60, 1000);
        MyActor act = new MyActor("Actor", c);
        jPanelDraw.add(act, c);
        statusBar.setText("New Actor");

        int id = Math.abs(ident.nextInt());
        String idactor = "ACT_" + id;
        act.setID(idactor);
        repaint();

    }

    /**
     * Método que dibuja un mensaje sobre la ventana de diseño en las coordenadas
     * indicadas
     *
     * @param e ActionEvent
     */
    public void jButtonMessage_actionPerformed(ActionEvent e) {
        XYConstraints c = new XYConstraints(0, 0, 60,
                                            Constants.getFontsize() + 12);
        Shared.msg = new MyMessage(c);
        Shared.msg.setName("mensaje");
        Shared.state = Shared.State.WAITSOURCE;
        jPanelDraw.add(Shared.msg, c);
        statusBar.setText(
                " Click the first element and then click the second element");

        int id = Math.abs(ident.nextInt());
        String idmessage = "MSG_" + id;
        Shared.msg.setID(idmessage);

    }

      /**
       * Crea el documento XML, con el contenido XMI común a todos los elementos,
       * la cabecera del documento.
       *
       * @return Devuelve el documento xml que hemos creado
       */
      public Document XMLDocument() {
          //Fecha q va a controlar las modificaciones sobre el modelo
          Date dat_model = new Date();

          Document docxmi = DocumentHelper.createDocument();
          docxmi.setXMLEncoding("windows-1252");

          //Establecer la referencia a la DTD que se va a utilizar, en este caso
          //la DTD de UML versión 1.3
          docxmi.addDocType("XMI", "SYSTEM", "UMLX13.dtd");

          //Añadimos la raiz del documento xml con sus atributos correspondientes
          raiz = docxmi.addElement("XMI");
          raiz.addAttribute("xmi.version", "1.0");
          raiz.addAttribute("timestamp", dat.toString());

          //Creamos los nodos que cuelgan de la raiz del documento
          header = raiz.addElement("XMI.header");

          //Lo que cuelga de el nodo 'content' es el contenido del diagrama de secuencia que dibujemos
          content = raiz.addElement("XMI.content");

          //Creamos los nodos que cuelgan de la cabecera
          Element documentation = header.addElement("XMI.documentation");
          documentation.addElement("XMI.exporter").setText(
                  "Generacion de Coreografias");
          documentation.addElement("XMI.exporterVersion").setText("1.0");
          Element metamodel = header.addElement("XMI.metamodel");
          metamodel.addAttribute("xmi.name", "UML");
          metamodel.addAttribute("xmi.version", "1.3");

          //Creamos los nodos asociados para crear el modelo que va a contener los elementos
          content.addComment("Model information");
          int id = Math.abs(ident.nextInt());
          String idmodel = "G_" + id;

          content.addElement("Model_Management.Model").addAttribute("xmi.id",
                  idmodel);
          content.addElement("Foundation.Core.Model.Element.name").setText(
                  "SequenceModel");

          content.addElement("Foundation.Core.ModelElement.visibility").
                  addAttribute("xmi.value", "public");
          content.addElement("Foundation.Core.ModelElement.isSpecification").
                  addAttribute("xmi.value", "false");
          content.addElement("Foundation.Core.ModelElement.isRoot").
                  addAttribute(
                          "xmi.value", "false");
          content.addElement("Foundation.Core.ModelElement.isLeaf").
                  addAttribute(
                          "xmi.value", "false");
          content.addElement("Foundation.Core.ModelElement.isAbstract").
                  addAttribute("xmi.value", "false");

          //ETIQUETAS COMUNES A TODO EL MODELO PARA EL CONTROL DE VERSIONES
          content.addComment("TaggedValues of general model");
          //Fecha de creacion
          Element e1 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e1.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("created");
          e1.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat.toString());
          Element e2 = e1.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e2.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Fecha de modificación
          Element e3 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e3.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("modified");
          e3.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat_model.toString());
          Element e4 = e3.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e4.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Tiene control
          Element e5 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e5.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("iscontrolled");
          e5.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("TRUE");
          Element e6 = e5.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e6.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Ruta
          Element e7 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e7.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("xmlpath");
          e7.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(ruta);
          Element e8 = e7.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e8.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Version
          Element e9 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e9.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("version");
          e9.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e10 = e9.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e10.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Usa DTD
          Element e11 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e11.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("usedtd");
          e11.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("TRUE");
          Element e12 = e11.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e12.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Version de xmi
          Element e13 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e13.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("xmiver");
          e13.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("Generic XMI 1.0/UML 1.3");
          Element e14 = e13.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e14.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //logxml
          Element e15 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e15.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("logxml");
          e15.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("FALSE");
          Element e16 = e15.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e16.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Fase
          Element e17 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e17.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("phase");
          e17.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e18 = e17.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e18.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Estado
          Element e19 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e19.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("status");
          e19.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("Proposed");
          Element e20 = e19.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e20.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);
          //Complejidad
          Element e21 = content.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e21.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("complexity");
          e21.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1");
          Element e22 = e21.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e22.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  idmodel);

          //A partir de aqui se va a ir añadiendo el contenido del diagrama
          content.addComment("Contents of model");
          contmodelo = content.addElement(
                  "Foundation.Core.Namespace.ownedElement");
          return docxmi;
      }

      /**
       * Método que genera el código xmi asociado a los actores que se van a
       * introducir en el diagrama
       *
       * @return Documento con el contenido xmi de los 'actores'
       */
      public Document XMIActor(MyObjects act) {
          Document xmiactor = DocumentHelper.createDocument();
          Date dat_act = new Date();
          contmodelo.addComment("New Actor");
          Element e1 = contmodelo.addElement(
                  "Behavioral_Elements.Use_Cases.Actor");

          e1.addAttribute("xmi.id", act.getID());
          e1.addElement("Foundation.Core.ModelElement.name").setText(act.
                  getName());
          e1.addElement("Foundation.Core.ModelElement.visibility").
                  addAttribute(
                          "xmi.value", "public");
          e1.addElement("Foundation.Core.ModelElement.isSpecification").
                  addAttribute("xmi.value", "false");
          e1.addElement("Foundation.Core.GeneralizableElement.isRoot").
                  addAttribute("xmi.value", "false");
          e1.addElement("Foundation.Core.GeneralizableElement.isLeaf").
                  addAttribute("xmi.value", "false");
          e1.addElement("Foundation.Core.GeneralizableElement.isAbstract").
                  addAttribute("xmi.value", "false");

          //ETIQUETAS ASOCIADAS
          contmodelo.addComment("TaggedValues of Actor");
          //Version
          Element e2 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e2.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("version");
          e2.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e3 = e2.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e3.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());
          //Fecha de creación
          Element e4 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e4.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("date_created");
          e4.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat.toString());
          Element e5 = e4.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e5.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());
          //Fecha de modificacion
          Element e6 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e6.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("date_modified");
          e6.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat_act.toString());
          Element e7 = e6.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e7.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());
          //Fase
          Element e8 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e8.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("phase");
          e8.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e9 = e8.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e9.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());
          //Estado
          Element e10 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e10.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("status");
          e10.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("Proposed");
          Element e11 = e10.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e11.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());
          //Estilo
          Element e12 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e12.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("style");
          String style = "BackColor=-1;BorderColor=-1;BorderWidth=-1;FontColor=-1;VSwimLanes=0;HSwimLanes=0;BorderStyle=0";
          e12.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(style);
          Element e13 = e12.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e13.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  act.getID());

          return xmiactor;

      }

      /**
       * Método que genera el código xmi asociado a los lifelines que va a tener
       * el diagrama
       *
       * @return Documento con el contenido xmi de los 'lifelines'
       */
      public Document XMILifeLine(MyObjects life) {
          Document xmilifeline = DocumentHelper.createDocument();
          Date dat_life = new Date();
          contmodelo.addComment("New LifeLine");
          Element e2 = contmodelo.addElement(
                  "Behavioral_Elements.Collaborations.ClassifierRole");

          e2.addAttribute("xmi.id", life.getID());
          e2.addElement("Foundation.Core.ModelElement.name").setText(life.
                  getName());
          e2.addElement("Foundation.Core.ModelElement.visibility").
                  addAttribute(
                          "xmi.value", "public");
          e2.addElement("Foundation.Core.ModelElement.isSpecification").
                  addAttribute("xmi.value", "false");
          e2.addElement("Foundation.Core.GeneralizableElement.isRoot").
                  addAttribute("xmi.value", "false");
          e2.addElement("Foundation.Core.GeneralizableElement.isLeaf").
                  addAttribute("xmi.value", "false");
          e2.addElement("Foundation.Core.GeneralizableElement.isAbstract").
                  addAttribute("xmi.value", "false");
          Element e3 = e2.addElement(
                  "Behavioral_Elements.Collaborations.ClassifierRole.multiplicity");
          Element e4 = e3.addElement("Foundation.Data_Types.Multiplicity");
          Element e5 = e4.addElement(
                  "Foundation.Data_Types.Multiplicity.range");
          Element e6 = e5.addElement(
                  "Foundation.Data_Types.Multiplicity.range");
          e6.addElement("Foundation.Data_Types.MultiplicityRange.lower").
                  setText(
                          "1");
          e6.addElement("Foundation.Data_Types.MultiplicityRange.upper").
                  setText(
                          "1");

          contmodelo.addComment("TaggedValues of Lifeline");
          //Version
          Element e7 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e7.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("version");
          e7.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e8 = e7.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e8.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());
          //Fecha de creación
          Element e9 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e9.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("date_created");
          e9.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat.toString());
          Element e10 = e9.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e10.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());
          //Fecha de modificacion
          Element e11 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e11.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("date_modified");
          e11.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(dat_life.toString());
          Element e12 = e11.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e12.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());
          //Fase
          Element e13 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e13.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("phase");
          e13.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1.0");
          Element e14 = e13.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e14.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());
          //Estado
          Element e15 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e15.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("status");
          e15.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("Proposed");
          Element e16 = e15.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e16.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());
          //Estilo
          Element e17 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e17.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("style");
          String style = "BackColor=-1;BorderColor=-1;BorderWidth=-1;FontColor=-1;VSwimLanes=0;HSwimLanes=0;BorderStyle=0";
          e17.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText(style);
          Element e18 = e17.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e18.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  life.getID());

          return xmilifeline;
      }

      /**
       * Método que genera el código xmi asociado a los mensajes que va a contener
       * el diagrama.
       *
       * @return Documento con el contenido xmi de los 'mensajes'.
       */
      public Document XMIMessage(MyObjects msg) {
          Document xmimensaje = DocumentHelper.createDocument();
          MyMessage msgaux = (MyMessage) msg;
          String origen = msgaux.getSource();
          String destino = msgaux.getDest();

          interaction.addComment("New Message");

          //Código del mensaje
          Element e4 = interaction.addElement(
                  "Behavioral_Elements.Collaborations.Message").
                       addAttribute("xmi.id", msg.getID());
          e4.addElement("Foundation.Core.ModelElement.name").setText(msg.
                  getName());
          e4.addElement("Foundation.Core.ModelElement.visibility").
                  addAttribute(
                          "xmi.value", "private");
          e4.addElement("Foundation.Core.ModelElement.isSpecification").
                  addAttribute("xmi.value", "false");
          Element e5 = e4.addElement(
                  "Behavioral_Elements.Collaborations.Message.sender");
          e5.addElement("Behavioral_Elements.Collaborations.ClassifierRole").
                  addAttribute("xmi.idref", origen);
          Element e6 = e4.addElement(
                  "Behavioral_Elements.Collaborations.Message.receiver");
          e6.addElement("Behavioral_Elements.Collaborations.ClassifierRole").
                  addAttribute("xmi.idref", destino);
          Element e7 = e4.addElement(
                  "Behavioral_Elements.Collaborations.Message.action");
          e7.addElement(
                  "Behavioral_Elements.Common_Behavior.UninterpretedAction").
                  addAttribute("xmi.id", msg.getID());

          contmodelo.addComment("TaggedValues of Message");
          //Instante de envio
          Element e8 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e8.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("sendingInstant");
          e8.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("105");
          Element e9 = e8.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e9.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  msg.getID());
          //Duración del proceso
          Element e10 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e10.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("processingDuration");
          e10.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("20");
          Element e11 = e10.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e11.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  msg.getID());
          //Número de secuencia
          Element e12 = contmodelo.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue");
          e12.addElement("Foundation.Extension_Mechanisms.TaggedValue.tag").
                  setText("sequenceNumber");
          e12.addElement("Foundation.Extension_Mechanisms.TaggedValue.value").
                  setText("1");
          Element e13 = e12.addElement(
                  "Foundation.Extension_Mechanisms.TaggedValue.modelElement");
          e13.addElement("Foundation.Core.ModelElement").addAttribute(
                  "xmi.idref",
                  msg.getID());
          return xmimensaje;
      }

      /**
       * Método que añade al documento xmi que estamos creando la parte final, que
       * es común a todos los diagramas, independientemente de su contenido.
       */
      public void XMLfin() {
          Document fin = DocumentHelper.createDocument();
          XMIDiagram();
          Element difference = raiz.addElement("XMI.difference");
          Element extension = raiz.addElement("XMI.extensions");
          extension.addAttribute("xmi.extender", "Generacion de Coreografias");
          doc.appendContent(fin);
      }

      /**
       * Método que añade el código asociado al diagrama de secuencia general.
       */
      public void XMIDiagram() {
          Document diagramas = DocumentHelper.createDocument();
          contmodelo.addComment("Diagram xmi code");
          doc.appendContent(diagramas);
      }

      /**
       * Guarda el fichero creado en memoria, cuyo contenido es el código xmi
       * del diagrama que hayamos creado, con extension xml.
       *
       * @param doc Documento que se va a almacenar en memoria
       * @throws IOException
       */
      public void XMLwrite(Document doc, String fich) throws IOException {
          XMLWriter writer = new XMLWriter(new FileWriter(fich));
          writer.write(doc);
          writer.close();
      }


      /**
       * Limpia el contenido del JPanel, que muestra el diagrama de secuencia
       * generado.
       *
       * @param e ActionEvent
       */
      public void jButton4_actionPerformed(ActionEvent e) {
          int num= jPanelDraw.getComponentCount();
          if (num==1){
              JOptionPane optionPane= new JOptionPane();
              optionPane.showMessageDialog(jPanelDraw,"No exite ningún diagrama para borrar");
          }
          else{
              JOptionPane optionPane= new JOptionPane();
              int ret=optionPane.showOptionDialog(optionPane,"¿Desea borrar el diagrama?","Borrar el diagrama",optionPane.YES_NO_CANCEL_OPTION,optionPane.QUESTION_MESSAGE, null, null, null);
              if (ret==JOptionPane.YES_OPTION){
                  jPanelDraw.removeAll();
                  statusBar.setText("Borrando el diagrama...");
                  jPanelDraw.add(jToolBar1, new XYConstraints(0, 0, 1200, -1));
                  repaint();
              }
          }
      }
}

class Dialogo_jButton4_actionAdapter implements ActionListener {
    private Dialogo adaptee;
    Dialogo_jButton4_actionAdapter(Dialogo adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButton4_actionPerformed(e);
    }
}

class Dialogo_jButtonMessage_actionAdapter implements ActionListener {
    private Dialogo adaptee;
    Dialogo_jButtonMessage_actionAdapter(Dialogo adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButtonMessage_actionPerformed(e);
    }
}


class Dialogo_jButtonActor_actionAdapter implements ActionListener {
    private Dialogo adaptee;
    Dialogo_jButtonActor_actionAdapter(Dialogo adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButtonActor_actionPerformed(e);
    }
}


class Dialogo_jButtonLifeline_actionAdapter implements ActionListener {
    private Dialogo adaptee;
    Dialogo_jButtonLifeline_actionAdapter(Dialogo adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.jButtonLifeline_actionPerformed(e);
    }
}
