package org.framed.orm.ui.command;

import java.util.ArrayList;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.WorkbenchException;
import org.framed.orm.ui.editor.ORMMultiPageEditor;

/**
 * 
 * @author Kay Bierzynski
 * 
 */
public class StepCommand extends Command {

  private AbstractGraphicalEditPart editpart;
  private ORMMultiPageEditor editorPart;
  private Object newContent;
  private boolean isNewWindowCommand;

  @Override
  public boolean canExecute() {

    return editpart != null && editorPart != null && newContent != null;
  }

  @Override
  public void execute() {

    IWorkbenchPage page = null;

    if (isNewWindowCommand) {

      try {
        page = editorPart.getSite().getWorkbenchWindow().openPage(editorPart.getSite());
      } catch (WorkbenchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

    } else {
      page = editorPart.getSite().getWorkbenchWindow().getActivePage();
    }

    IEditorInput input = editorPart.getEditorInput();

    IEditorPart[] editorlist = page.getEditors();
    ArrayList<IEditorPart> editors = new ArrayList<IEditorPart>();

    // get all editor instances with specific input
    for (IEditorPart part : editorlist) {

      if (part.getEditorInput().equals(input)) {
        editors.add(part);
      }

    }

    if (editors.size() > 1 && !isNewWindowCommand) {
      for (IEditorPart part : editors) {
        if (!part.equals(editorPart)) {
          ORMMultiPageEditor multiPart = (ORMMultiPageEditor) part;
          multiPart.setContents(editpart.getViewer().getContents().getModel());
        }
      }
    } else {

      try {
        // open new editor instance with old content edipart model
        ORMMultiPageEditor newPart =
            (ORMMultiPageEditor) page.openEditor(input, "ORMEditor.editorID", false,
                IWorkbenchPage.MATCH_NONE);

        if (isNewWindowCommand) {
          newPart.setContents(newContent);
        } else {
          newPart.setContents(editpart.getViewer().getContents().getModel());
          // set focus on the editor instance with new content
          page.activate(editorPart);
        }

      } catch (PartInitException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }

    if (!isNewWindowCommand) {
      editorPart.setContents(newContent);
    } else {
      editorPart.setContents(editpart.getViewer().getContents().getModel());
    }

  }

  /**
   * This command can for now not be undone.
   */
  @Override
  public boolean canUndo() {
    return false;
  }

  public void setEditPart(AbstractGraphicalEditPart editpart) {
    this.editpart = editpart;
  }

  public void setEditorPart(ORMMultiPageEditor editorPart) {
    this.editorPart = editorPart;
  }

  public void setNewContent(Object content) {
    this.newContent = content;
  }

  public void setIsNewWindowCommand(boolean isNewWindowCommand) {
    this.isNewWindowCommand = isNewWindowCommand;
  }

}

@@ -343,32 +343,181 @@
 benchException;%0A
+import org.framed.orm.model.CompartmentDiagram;%0Aimport org.framed.orm.model.Grouping;%0Aimport org.framed.orm.ui.editor.ORMGraphicalEditor.EditorType;%0A
 import org.frame
@@ -1967,32 +1967,369 @@
 ().getModel());%0A
+%0A          if (editpart.getViewer().getContents().getModel() instanceof CompartmentDiagram%0A              %7C%7C editpart.getViewer().getContents().getModel() instanceof Grouping)%0A            multiPart.getEditorBeh().setEditorType(EditorType.COMPARTMENT);%0A          else%0A            multiPart.getEditorBeh().setEditorType(EditorType.ROLES);%0A%0A
         %7D%0A      
@@ -2671,102 +2671,684 @@
 t);%0A
-        %7D else %7B%0A          newPart.setContents(editpart.getViewer().getContents().getModel());
+%0A          if (editpart.getViewer().getContents().getModel() instanceof CompartmentDiagram%0A              %7C%7C editpart.getViewer().getContents().getModel() instanceof Grouping)%0A            newPart.getEditorBeh().setEditorType(EditorType.COMPARTMENT);%0A          else%0A            newPart.getEditorBeh().setEditorType(EditorType.ROLES);%0A%0A        %7D else %7B%0A          newPart.setContents(editpart.getViewer().getContents().getModel());%0A%0A          if (newContent instanceof CompartmentDiagram %7C%7C newContent instanceof Grouping)%0A            newPart.getEditorBeh().setEditorType(EditorType.COMPARTMENT);%0A          else%0A            newPart.getEditorBeh().setEditorType(EditorType.ROLES);%0A
 %0A   
@@ -3440,24 +3440,25 @@
 ditorPart);%0A
+%0A
         %7D%0A%0A 
@@ -3452,24 +3452,26 @@
 %0A        %7D%0A%0A
+%0A%0A
       %7D catc
@@ -3654,24 +3654,261 @@
 ewContent);%0A
+      if (newContent instanceof CompartmentDiagram %7C%7C newContent instanceof Grouping)%0A        editorPart.getEditorBeh().setEditorType(EditorType.COMPARTMENT);%0A      else%0A        editorPart.getEditorBeh().setEditorType(EditorType.ROLES);%0A
     %7D else %7B

add code which was delete by mistake