<commit_before>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.setup.ui;

import org.eclipse.oomph.setup.internal.core.SetupContext;
import org.eclipse.oomph.setup.internal.core.util.SetupUtil;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.ui.viewer.IViewerProvider;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.ui.util.EditUIUtil;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.FileEditorInput;

import java.net.URISyntaxException;

/**
 * @author <NAME>
 */
public final class SetupEditorSupport
{
  public static final String EDITOR_ID = "org.eclipse.oomph.setup.presentation.SetupEditorID";

  public static IEditorPart findEditor(IWorkbenchPage page, URI uri)
  {
    URIConverter uriConverter = SetupUtil.createResourceSet().getURIConverter();
    URI normalizedURI = uriConverter.normalize(uri).trimFragment();

    IEditorInput input = getEditorInput(normalizedURI, null);
    return findEditor(page, uriConverter, input);
  }

  private static IEditorPart findEditor(IWorkbenchPage page, URIConverter uriConverter, IEditorInput input)
  {
    URI resourceURI = EditUIUtil.getURI(input);

    for (IEditorReference editorReference : page.getEditorReferences())
    {
      if (editorReference.getId().equals(EDITOR_ID))
      {
        try
        {
          IEditorInput editorInput = editorReference.getEditorInput();
          URI editorResourceURI = EditUIUtil.getURI(editorInput);
          if (editorResourceURI != null)
          {
            editorResourceURI = uriConverter.normalize(editorResourceURI).trimFragment();
            if (editorResourceURI.equals(resourceURI))
            {
              return editorReference.getEditor(true);
            }
          }
        }
        catch (PartInitException ex)
        {
          SetupUIPlugin.INSTANCE.log(ex);
        }
      }
    }

    return null;
  }

  public static void openEditor(final IWorkbenchPage page, URI uri)
  {
    openEditor(page, uri, null);
  }

  public static void openEditor(final IWorkbenchPage page, final URI uri, final Callback callback)
  {
    Display display = page.getWorkbenchWindow().getShell().getDisplay();
    display.asyncExec(new Runnable()
    {
      public void run()
      {
        try
        {
          URIConverter uriConverter = SetupUtil.createResourceSet().getURIConverter();
          final URI normalizedURI = uriConverter.normalize(uri);

          IEditorInput editorInput = getEditorInput(normalizedURI, new Callback()
          {
            public void modelCreated(IEditorPart editor)
            {
              postOpen(editor, normalizedURI, callback);
            }
          });

          IEditorPart editor = findEditor(page, uriConverter, editorInput);
          if (editor != null)
          {
            callback.modelCreated(editor);
          }
          else
          {
            page.openEditor(editorInput, EDITOR_ID);
          }
        }
        catch (Exception ex)
        {
          SetupUIPlugin.INSTANCE.log(ex);
        }
      }
    });
  }

  private static void postOpen(IEditorPart editor, URI normalizedURI, Callback callback)<commit_msg>[442823] Simplify the workflow for the recording of preference tasks<commit_after>/*
 * Copyright (c) 2014 <NAME> (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation
 */
package org.eclipse.oomph.setup.ui;

import org.eclipse.oomph.setup.internal.core.SetupContext;
import org.eclipse.oomph.setup.internal.core.util.SetupUtil;

import org.eclipse.emf.common.ui.URIEditorInput;
import org.eclipse.emf.common.ui.viewer.IViewerProvider;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.edit.domain.IEditingDomainProvider;
import org.eclipse.emf.edit.ui.util.EditUIUtil;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.FileEditorInput;

import java.net.URISyntaxException;

/**
 * @author <NAME>
 */
public final class SetupEditorSupport
{
  public static final String EDITOR_ID = "org.eclipse.oomph.setup.presentation.SetupEditorID";

  public static IEditorPart findEditor(IWorkbenchPage page, URI uri)
  {
    URIConverter uriConverter = SetupUtil.createResourceSet().getURIConverter();
    URI normalizedURI = uriConverter.normalize(uri).trimFragment();

    IEditorInput input = getEditorInput(normalizedURI, null);
    return findEditor(page, uriConverter, input);
  }

  private static IEditorPart findEditor(IWorkbenchPage page, URIConverter uriConverter, IEditorInput input)
  {
    URI resourceURI = EditUIUtil.getURI(input);

    for (IEditorReference editorReference : page.getEditorReferences())
    {
      if (editorReference.getId().equals(EDITOR_ID))
      {
        try
        {
          IEditorInput editorInput = editorReference.getEditorInput();
          URI editorResourceURI = EditUIUtil.getURI(editorInput);
          if (editorResourceURI != null)
          {
            editorResourceURI = uriConverter.normalize(editorResourceURI).trimFragment();
            if (editorResourceURI.equals(resourceURI))
            {
              return editorReference.getEditor(true);
            }
          }
        }
        catch (PartInitException ex)
        {
          SetupUIPlugin.INSTANCE.log(ex);
        }
      }
    }

    return null;
  }

  public static void openEditor(final IWorkbenchPage page, URI uri)
  {
    openEditor(page, uri, null);
  }

  public static void openEditor(final IWorkbenchPage page, final URI uri, final Callback callback)
  {
    Display display = page.getWorkbenchWindow().getShell().getDisplay();
    display.asyncExec(new Runnable()
    {
      public void run()
      {
        try
        {
          URIConverter uriConverter = SetupUtil.createResourceSet().getURIConverter();
          final URI normalizedURI = uriConverter.normalize(uri);

          IEditorInput editorInput = getEditorInput(normalizedURI, new Callback()
          {
            public void modelCreated(IEditorPart editor)
            {
              postOpen(editor, normalizedURI, callback);
            }
          });

          IEditorPart editor = findEditor(page, uriConverter, editorInput);
          if (editor != null)
          {
            page.activate(editor);
            callback.modelCreated(editor);
          }
          else
          {
            page.openEditor(editorInput, EDITOR_ID);
          }
        }
        catch (Exception ex)
        {
          SetupUIPlugin.INSTANCE.log(ex);
        }
      }
    });
  }

  private static void postOpen(IEditorPart editor, URI normalizedURI, Callback callback)
{'fraction_non_alphanumeric': 0.09666203059805285, 'fraction_numerical': 0.0025498377375985167, 'mean_word_length': 3.077977315689981, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '2409559', 'n_tokens_mistral': 2474, 'n_tokens_neox': 2432, 'n_words': 505}