<commit_before>package eu.modelwriter.marker.ui.internal;

import java.awt.Frame;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4.Util;
import edu.mit.csail.sdg.alloy4graph.GraphViewer;
import edu.mit.csail.sdg.alloy4viz.AlloyAtom;
import edu.mit.csail.sdg.alloy4viz.AlloyInstance;
import edu.mit.csail.sdg.alloy4viz.MagicColor;
import edu.mit.csail.sdg.alloy4viz.MagicLayout;
import edu.mit.csail.sdg.alloy4viz.StaticInstanceReader;
import edu.mit.csail.sdg.alloy4viz.VizGraphPanel;
import edu.mit.csail.sdg.alloy4viz.VizState;
import eu.modelwriter.configuration.alloy.AlloyParserForMetamodel;
import eu.modelwriter.configuration.internal.AlloyUtilities;
import eu.modelwriter.marker.internal.MarkUtilities;
import eu.modelwriter.marker.internal.MarkerFactory;
import eu.modelwriter.marker.ui.internal.views.visualizationview.Visualization;

public class MetaModelEditor extends MultiPageEditorPart {

  private class Editor extends TextEditor {

    public Editor() {
      super();
      this.setSourceViewerConfiguration(new MetaModelEditorSourceViewerConfig());
    }

    @Override
    protected boolean affectsTextPresentation(final PropertyChangeEvent event) {
      return super.affectsTextPresentation(event);
    }

    @Override
    public boolean isEditable() {
      return super.isEditable();
    }
  }

  public static final String ID = "eu.modelwriter.marker.ui.views.metamodelview";
  private VizState myState = null;
  private VizGraphPanel graph;
  private Frame frame;
  private File file = null;
  public static Object rightClickedAnnotation;
  static String xmlFileName = null;
  Composite modelEditor;

  private Editor textEditor;

  private void addDropListener() {
    final int acceptableOps = DnDConstants.ACTION_COPY;
    @SuppressWarnings("unused")
    final DropTarget dropTarget =
        new DropTarget(graph.alloyGetViewer(), acceptableOps, new DropTargetListener() {
          ISelection selection;
          IFile file;

          private void createMarker(final String type) {
            Display.getDefault().asyncExec(new Runnable() {

              @Override
              public void run() {
                file = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .getActiveEditor().getEditorInput().getAdapter(IFile.class);
                selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                    .getSelectionService().getSelection();
                if (selection instanceof ITextSelection) {
                  IMarker marker;
                  final ITextSelection textSelection = (ITextSelection) selection;
                  final int start = textSelection.getOffset();
                  final int end = textSelection.getOffset() + textSelection.getLength();

                  marker = MarkerFactory.findMarkerWithAbsolutePosition(file, start, end);
                  if (marker != null) {
                    MarkUtilities.setType(marker, type);
                  } else {
                    marker = MarkerFactory.createMarker(file, (ITextSelection) selection);
                    MarkUtilities.setType(marker, type);
                    AlloyUtilities.addMarkerToRepository(marker);
                  }
                  AlloyUtilities.addTypeToMarker(marker);
                  Visualization.showViz(modelEditor);
                }
              }
            });
          }

          @Override
          public void dragEnter(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          @Override
          public void dragExit(final DropTargetEvent dte) {}

          @Override
          public void dragOver(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          @SuppressWarnings("deprecation")
          @Override
          public void drop(final DropTargetDropEvent dtde) {
            final DataFlavor flavor = DataFlavor.stringFlavor;
            DataFlavor chosen = null;
            if (dtde.isLocalTransfer() == false) {
              chosen = DataFlavor.plainTextFlavor;
            } else {
              if (dtde.isDataFlavorSupported(flavor)) {
                chosen = flavor;
              }
            }
            if (chosen == null) {
              dtde.rejectDrop();
              return;
            }

            final int sa = dtde.getSourceActions();
            if ((sa & acceptableOps) == 0) {
              dtde.rejectDrop();
              return;
            }

            Object data = null;
            try {
              dtde.acceptDrop(acceptableOps);
              data = dtde.getTransferable().getTransferData(chosen);
              if (data == null) {
                throw new NullPointerException();
              }
            } catch (final Throwable t) {
              t.printStackTrace();
              dtde.dropComplete(false);
              return;
            }

            final Point mousePoint = dtde.getLocation();
            final GraphViewer graphViewer =
                (GraphViewer) dtde.getDropTargetContext().getComponent();
            final Object annotation =
                graphViewer.alloyGetAnnotationAtXY(mousePoint.x, mousePoint.y);
            if (annotation instanceof AlloyAtom) {
              final AlloyAtom atom = (AlloyAtom) annotation;
              this.createMarker(atom.toString());
              dtde.dropComplete(true);
            }
          }

          @Override
          public void dropActionChanged(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          private boolean isDragOk(final DropTargetDragEvent dtde) {
            final DataFlavor flavor = DataFlavor.stringFlavor;
            DataFlavor chosen = null;

            if (dtde.isDataFlavorSupported(flavor)) {
              chosen = flavor;
            }

            if (chosen == null) {
              return false;
            }

            final int sourceActions = dtde.getSourceActions();

            if ((sourceActions & acceptableOps) == 0) {
              return false;
            }

            final Point mousePoint = dtde.getLocation();
            final GraphViewer graphViewer =
                (GraphViewer) dtde.getDropTargetContext().getComponent();
            final Object annotation =
                graphViewer.alloyGetAnnotationAtXY(mousePoint.x, mousePoint.y);
            if (annotation instanceof AlloyAtom) {
              final AlloyAtom atom = (AlloyAtom) annotation;
              if (atom.getType().isAbstract) {
                return false;
              }
            } else {
              return false;
            }
            return true;
          }
        }, true);
  }

  public void create() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
        | UnsupportedLookAndFeelException e1) {

      e1.printStackTrace();
    }

    int index;
    this.textEditor = new Editor();

    modelEditor = new Composite(this.getContainer(), SWT.EMBEDDED);
    index = this.addPage(modelEditor);
    this.setPageText(index, "Specification");

    try {
      index = this.addPage(this.textEditor, this.getEditorInput());
      this.setPageText(index, "Source");
      this.setPartName(this.textEditor.getTitle());
    } catch (final PartInitException e) {
      ErrorDialog.openError(this.getSite().getShell(), " Error creating nested text editor", null,
          e.getStatus());
    }

    @SuppressWarnings("unused")
    final AlloyParserForMetamodel alloyParserForMetamodel = new AlloyParserForMetamodel(
        ((FileEditorInput) this.textEditor.getEditorInput()).getPath().toString());

    frame = null;
    myState = null;
    graph = null;
    file = null;

    this.showMetamodel(true);
  }

  @Override
  protected void createPages() {
    this.create();
    this.addDropListener();
  }

  @Override
  public void doSave(final IProgressMonitor monitor) {
    // nothing
  }

  @Override
  public void doSaveAs() {
    // nothing
  }

  @Override
  public boolean isSaveAsAllowed() {
    return false;
  }

  private void showMetamodel(final boolean isMagicLayout) {
    MetaModelEditor.xmlFileName =
        Util.canon(AlloyUtilities.getLocationForMetamodel(this.textEditor.getTitle()));

    frame = SWT_AWT.new_Frame(modelEditor);
    frame.add(new JPanel());

    file = new File(MetaModelEditor.xmlFileName);
    try {
      if (!file.exists()) {
        throw new IOException("File " + MetaModelEditor.xmlFileName + " does not exist.");
      }
      // AlloyUtilities.setMetamodel(this.editor1.getTitle(), true);
      final AlloyInstance instance = StaticInstanceReader.parseInstance(file);

      myState = new VizState(instance);
      if (isMagicLayout == true) {
        MagicLayout.magic(myState);
        MagicColor.magic(myState);
      } else {
        myState.resetTheme();
      }

      if (frame == null) {
        frame = SWT_AWT.new_Frame(modelEditor);
      }

      if (graph != null && frame.getComponent(0) != null) {
        frame.remove(graph);
      }

      graph = new VizGraphPanel(myState, false);
      frame.removeAll();
      frame.add(graph);
      frame.setVisible(true);
      frame.setAlwaysOnTop(true);
      graph.alloyGetViewer().alloyRepaint();

      final JMenuItem magicLayoutMenuItem = new JMenuItem("Magic Layout");
      final JMenuItem resetThemeMenuItem = new JMenuItem("Reset Theme");

      graph.alloyGetViewer().pop.add(magicLayoutMenuItem, 0);
      graph.alloyGetViewer().pop.add(resetThemeMenuItem, 1);

      magicLayoutMenuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
          MetaModelEditor.this.showMetamodel(true);
        }<commit_msg>minor change for github conflict.<commit_after>package eu.modelwriter.marker.ui.internal;

import java.awt.Frame;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.part.MultiPageEditorPart;

import edu.mit.csail.sdg.alloy4.Err;
import edu.mit.csail.sdg.alloy4.Util;
import edu.mit.csail.sdg.alloy4graph.GraphViewer;
import edu.mit.csail.sdg.alloy4viz.AlloyAtom;
import edu.mit.csail.sdg.alloy4viz.AlloyInstance;
import edu.mit.csail.sdg.alloy4viz.MagicColor;
import edu.mit.csail.sdg.alloy4viz.MagicLayout;
import edu.mit.csail.sdg.alloy4viz.StaticInstanceReader;
import edu.mit.csail.sdg.alloy4viz.VizGraphPanel;
import edu.mit.csail.sdg.alloy4viz.VizState;
import eu.modelwriter.configuration.alloy.AlloyParserForMetamodel;
import eu.modelwriter.configuration.internal.AlloyUtilities;
import eu.modelwriter.marker.internal.MarkUtilities;
import eu.modelwriter.marker.internal.MarkerFactory;
import eu.modelwriter.marker.ui.internal.views.visualizationview.Visualization;

public class MetaModelEditor extends MultiPageEditorPart {

  private class Editor extends TextEditor {

    public Editor() {
      super();
      this.setSourceViewerConfiguration(new MetaModelEditorSourceViewerConfig());
    }

    @Override
    protected boolean affectsTextPresentation(final PropertyChangeEvent event) {
      return super.affectsTextPresentation(event);
    }

    @Override
    public boolean isEditable() {
      return super.isEditable();
    }
  }

  public static final String ID = "eu.modelwriter.marker.ui.views.metamodelview";
  public static Object rightClickedAnnotation;
  static String xmlFileName = null;
  private VizState myState = null;
  private VizGraphPanel graph;
  private Frame frame;
  private File file = null;
  Composite modelEditor;

  private Editor textEditor;

  private void addDropListener() {
    final int acceptableOps = DnDConstants.ACTION_COPY;
    @SuppressWarnings("unused")
    final DropTarget dropTarget =
        new DropTarget(this.graph.alloyGetViewer(), acceptableOps, new DropTargetListener() {
          ISelection selection;
          IFile file;

          private void createMarker(final String type) {
            Display.getDefault().asyncExec(new Runnable() {

              @Override
              public void run() {
                file = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                    .getActiveEditor().getEditorInput().getAdapter(IFile.class);
                selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
                    .getSelectionService().getSelection();
                if (selection instanceof ITextSelection) {
                  IMarker marker;
                  final ITextSelection textSelection = (ITextSelection) selection;
                  final int start = textSelection.getOffset();
                  final int end = textSelection.getOffset() + textSelection.getLength();

                  marker = MarkerFactory.findMarkerWithAbsolutePosition(file, start, end);
                  if (marker != null) {
                    MarkUtilities.setType(marker, type);
                  } else {
                    marker = MarkerFactory.createMarker(file, (ITextSelection) selection);
                    MarkUtilities.setType(marker, type);
                    AlloyUtilities.addMarkerToRepository(marker);
                  }
                  AlloyUtilities.addTypeToMarker(marker);
                  Visualization.showViz(MetaModelEditor.this.modelEditor);
                }
              }
            });
          }

          @Override
          public void dragEnter(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          @Override
          public void dragExit(final DropTargetEvent dte) {}

          @Override
          public void dragOver(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          @SuppressWarnings("deprecation")
          @Override
          public void drop(final DropTargetDropEvent dtde) {
            final DataFlavor flavor = DataFlavor.stringFlavor;
            DataFlavor chosen = null;
            if (dtde.isLocalTransfer() == false) {
              chosen = DataFlavor.plainTextFlavor;
            } else {
              if (dtde.isDataFlavorSupported(flavor)) {
                chosen = flavor;
              }
            }
            if (chosen == null) {
              dtde.rejectDrop();
              return;
            }

            final int sa = dtde.getSourceActions();
            if ((sa & acceptableOps) == 0) {
              dtde.rejectDrop();
              return;
            }

            Object data = null;
            try {
              dtde.acceptDrop(acceptableOps);
              data = dtde.getTransferable().getTransferData(chosen);
              if (data == null) {
                throw new NullPointerException();
              }
            } catch (final Throwable t) {
              t.printStackTrace();
              dtde.dropComplete(false);
              return;
            }

            final Point mousePoint = dtde.getLocation();
            final GraphViewer graphViewer =
                (GraphViewer) dtde.getDropTargetContext().getComponent();
            final Object annotation =
                graphViewer.alloyGetAnnotationAtXY(mousePoint.x, mousePoint.y);
            if (annotation instanceof AlloyAtom) {
              final AlloyAtom atom = (AlloyAtom) annotation;
              this.createMarker(atom.toString());
              dtde.dropComplete(true);
            }
          }

          @Override
          public void dropActionChanged(final DropTargetDragEvent dtde) {
            if (this.isDragOk(dtde) == false) {
              dtde.rejectDrag();
              return;
            }
            dtde.acceptDrag(acceptableOps);
          }

          private boolean isDragOk(final DropTargetDragEvent dtde) {
            final DataFlavor flavor = DataFlavor.stringFlavor;
            DataFlavor chosen = null;

            if (dtde.isDataFlavorSupported(flavor)) {
              chosen = flavor;
            }

            if (chosen == null) {
              return false;
            }

            final int sourceActions = dtde.getSourceActions();

            if ((sourceActions & acceptableOps) == 0) {
              return false;
            }

            final Point mousePoint = dtde.getLocation();
            final GraphViewer graphViewer =
                (GraphViewer) dtde.getDropTargetContext().getComponent();
            final Object annotation =
                graphViewer.alloyGetAnnotationAtXY(mousePoint.x, mousePoint.y);
            if (annotation instanceof AlloyAtom) {
              final AlloyAtom atom = (AlloyAtom) annotation;
              if (atom.getType().isAbstract) {
                return false;
              }
            } else {
              return false;
            }
            return true;
          }
        }, true);
  }

  public void create() {
    try {
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
        | UnsupportedLookAndFeelException e1) {

      e1.printStackTrace();
    }

    int index;
    this.textEditor = new Editor();

    this.modelEditor = new Composite(this.getContainer(), SWT.EMBEDDED);
    index = this.addPage(this.modelEditor);
    this.setPageText(index, "Specification");

    try {
      index = this.addPage(this.textEditor, this.getEditorInput());
      this.setPageText(index, "Source");
      this.setPartName(this.textEditor.getTitle());
    } catch (final PartInitException e) {
      ErrorDialog.openError(this.getSite().getShell(), " Error creating nested text editor", null,
          e.getStatus());
    }

    @SuppressWarnings("unused")
    final AlloyParserForMetamodel alloyParserForMetamodel = new AlloyParserForMetamodel(
        ((FileEditorInput) this.textEditor.getEditorInput()).getPath().toString());

    this.frame = null;
    this.myState = null;
    this.graph = null;
    this.file = null;

    this.showMetamodel(true);
  }

  @Override
  protected void createPages() {
    this.create();
    this.addDropListener();
  }

  @Override
  public void doSave(final IProgressMonitor monitor) {
    // nothing
  }

  @Override
  public void doSaveAs() {
    // nothing
  }

  @Override
  public boolean isSaveAsAllowed() {
    return false;
  }

  private void showMetamodel(final boolean isMagicLayout) {
    MetaModelEditor.xmlFileName =
        Util.canon(AlloyUtilities.getLocationForMetamodel(this.textEditor.getTitle()));

    this.frame = SWT_AWT.new_Frame(this.modelEditor);
    this.frame.add(new JPanel());

    this.file = new File(MetaModelEditor.xmlFileName);
    try {
      if (!this.file.exists()) {
        throw new IOException("File " + MetaModelEditor.xmlFileName + " does not exist.");
      }
      // AlloyUtilities.setMetamodel(this.editor1.getTitle(), true);
      final AlloyInstance instance = StaticInstanceReader.parseInstance(this.file);

      this.myState = new VizState(instance);
      if (isMagicLayout == true) {
        MagicLayout.magic(this.myState);
        MagicColor.magic(this.myState);
      } else {
        this.myState.resetTheme();
      }

      if (this.frame == null) {
        this.frame = SWT_AWT.new_Frame(this.modelEditor);
      }

      if (this.graph != null && this.frame.getComponent(0) != null) {
        this.frame.remove(this.graph);
      }

      this.graph = new VizGraphPanel(this.myState, false);
      this.frame.removeAll();
      this.frame.add(this.graph);
      this.frame.setVisible(true);
      this.frame.setAlwaysOnTop(true);
      this.graph.alloyGetViewer().alloyRepaint();

      final JMenuItem magicLayoutMenuItem = new JMenuItem("Magic Layout");
      final JMenuItem resetThemeMenuItem = new JMenuItem("Reset Theme");

      this.graph.alloyGetViewer().pop.add(magicLayoutMenuItem, 0);
      this.graph.alloyGetViewer().pop.add(resetThemeMenuItem, 1);

      magicLayoutMenuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
          MetaModelEditor.this.showMetamodel(true);
        }
{'fraction_non_alphanumeric': 0.09713644020213363, 'fraction_numerical': 0.0015548740983889777, 'mean_word_length': 2.3870684610883557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '423950', 'n_tokens_mistral': 6549, 'n_tokens_neox': 6377, 'n_words': 1196}