<commit_before>/*******************************************************************************
 * Copyright (c) 2009 <NAME>.
 * 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
 * Contributor:  <NAME> - initial API and implementation
 *******************************************************************************/

package de.loskutov.anyedit.ui.editor;

import java.lang.reflect.Method;
import java.net.URI;

import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentRewriteSession;
import org.eclipse.jface.text.DocumentRewriteSessionType;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.IRewriteTarget;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.editors.text.IStorageDocumentProvider;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension;
import org.eclipse.ui.texteditor.ITextEditorExtension2;

import de.loskutov.anyedit.AnyEditToolsPlugin;
import de.loskutov.anyedit.util.EclipseUtils;

/**
 * @author Andrei
 */
public class AbstractEditor implements ITextEditorExtension2 {
    private IEditorPart editorPart;
    private boolean multipage;

    /**
     * Proxy for different editor types
     */
    public AbstractEditor(IEditorPart editorPart) {
        this();
        if(editorPart instanceof FormEditor){
            FormEditor fe = (FormEditor)editorPart;
            editorPart = fe.getActiveEditor();
            multipage = true;
        } else if(editorPart instanceof MultiPageEditorPart){
            MultiPageEditorPart me = (MultiPageEditorPart)editorPart;
            // followed is because "getActiveEditor" method is protected in
            // MultiPageEditorPart class.
            try {
                Method method = MultiPageEditorPart.class.getDeclaredMethod(
                        "getActiveEditor", null);
                method.setAccessible(true);
                editorPart = (IEditorPart) method.invoke(me, null);
                multipage = true;
            } catch (Exception e) {
                AnyEditToolsPlugin.logError("Can't get current page", e);
            }
        } else if(editorPart!= null
                && !(editorPart instanceof ITextEditor)) {
            /*
             * added to support different multipage editors which are not extending
             * MultiPageEditorPart, like adobe Flex family editors
             */
            try {
                Method[] declaredMethods = editorPart.getClass().getDeclaredMethods();
                for (int i = 0; i < declaredMethods.length; i++) {
                    Method method = declaredMethods[i];
                    String methodName = method.getName();
                    if(("getActiveEditor".equals(methodName)
                            // lines below are for Flex 3, above for Flex 2
                            ||  "getCodeEditor".equals(methodName)
                            ||  "getTextEditor".equals(methodName))
                            && method.getParameterTypes().length == 0){
                        method.setAccessible(true);
                        editorPart = (IEditorPart) method.invoke(editorPart, null);
                        if(editorPart == null) {
                            continue;
                        }
                        multipage = true;
                        break;
                    }
                }
            } catch (Exception e) {
                AnyEditToolsPlugin.logError("Can't get current page", e);
            }
        }
        this.editorPart = editorPart;
    }

    private AbstractEditor() {
        super();
    }

    public AbstractEditor recreate(){
        AbstractEditor ae = new AbstractEditor();
        ae.editorPart = editorPart;
        ae.multipage = multipage;
        return ae;
    }

    public boolean isMultiPage(){
        return multipage;
    }

    /**
     * @return may return null
     */
    public IDocumentProvider getDocumentProvider() {
        if (editorPart == null) {
            return null;
        }
        if (editorPart instanceof ITextEditor) {
            return ((ITextEditor) editorPart).getDocumentProvider();
        }

        IDocumentProvider docProvider =
            (IDocumentProvider) editorPart.getAdapter(IDocumentProvider.class);
        return docProvider;
    }

    /**
     * @return may return null
     */
    public IEditorInput getInput() {
        if (editorPart == null) {
            return null;
        }
        return editorPart.getEditorInput();
    }

    /**
     * @return may return null
     */
    public IFile getFile(){
        IEditorInput input = getInput();
        if(input == null){
            return null;
        }
        Object adapter = input.getAdapter(IFile.class);
        if(adapter instanceof IFile){
            return (IFile) adapter;
        }
        adapter = getAdapter(IFile.class);
        if(adapter instanceof IFile){
            return (IFile) adapter;
        }
        return null;
    }

    /**
     * @see ITypedElement#getType()
     */
    public String getContentType(){
        URI uri = getURI();
        if(uri == null){
            return null;
        }
        String path = uri.toString();
        int dot = path.lastIndexOf(".");
        if(dot >= 0){
            return path.substring(dot);
        }<commit_msg>fix for Issue 47: Comparing a java file with clipboard opens a compare view in "text compare" or "java source compare" mode depending on where it is launched from<commit_after>/*******************************************************************************
 * Copyright (c) 2009 <NAME>.
 * 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
 * Contributor:  <NAME> - initial API and implementation
 *******************************************************************************/

package de.loskutov.anyedit.ui.editor;

import java.lang.reflect.Method;
import java.net.URI;

import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentRewriteSession;
import org.eclipse.jface.text.DocumentRewriteSessionType;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentExtension;
import org.eclipse.jface.text.IDocumentExtension4;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.IRewriteTarget;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.editors.text.IStorageDocumentProvider;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorExtension;
import org.eclipse.ui.texteditor.ITextEditorExtension2;

import de.loskutov.anyedit.AnyEditToolsPlugin;
import de.loskutov.anyedit.util.EclipseUtils;

/**
 * @author Andrei
 */
public class AbstractEditor implements ITextEditorExtension2 {
    private IEditorPart editorPart;
    private boolean multipage;

    /**
     * Proxy for different editor types
     */
    public AbstractEditor(IEditorPart editorPart) {
        this();
        if(editorPart instanceof FormEditor){
            FormEditor fe = (FormEditor)editorPart;
            editorPart = fe.getActiveEditor();
            multipage = true;
        } else if(editorPart instanceof MultiPageEditorPart){
            MultiPageEditorPart me = (MultiPageEditorPart)editorPart;
            // followed is because "getActiveEditor" method is protected in
            // MultiPageEditorPart class.
            try {
                Method method = MultiPageEditorPart.class.getDeclaredMethod(
                        "getActiveEditor", null);
                method.setAccessible(true);
                editorPart = (IEditorPart) method.invoke(me, null);
                multipage = true;
            } catch (Exception e) {
                AnyEditToolsPlugin.logError("Can't get current page", e);
            }
        } else if(editorPart!= null
                && !(editorPart instanceof ITextEditor)) {
            /*
             * added to support different multipage editors which are not extending
             * MultiPageEditorPart, like adobe Flex family editors
             */
            try {
                Method[] declaredMethods = editorPart.getClass().getDeclaredMethods();
                for (int i = 0; i < declaredMethods.length; i++) {
                    Method method = declaredMethods[i];
                    String methodName = method.getName();
                    if(("getActiveEditor".equals(methodName)
                            // lines below are for Flex 3, above for Flex 2
                            ||  "getCodeEditor".equals(methodName)
                            ||  "getTextEditor".equals(methodName))
                            && method.getParameterTypes().length == 0){
                        method.setAccessible(true);
                        editorPart = (IEditorPart) method.invoke(editorPart, null);
                        if(editorPart == null) {
                            continue;
                        }
                        multipage = true;
                        break;
                    }
                }
            } catch (Exception e) {
                AnyEditToolsPlugin.logError("Can't get current page", e);
            }
        }
        this.editorPart = editorPart;
    }

    private AbstractEditor() {
        super();
    }

    public AbstractEditor recreate(){
        AbstractEditor ae = new AbstractEditor();
        ae.editorPart = editorPart;
        ae.multipage = multipage;
        return ae;
    }

    public boolean isMultiPage(){
        return multipage;
    }

    /**
     * @return may return null
     */
    public IDocumentProvider getDocumentProvider() {
        if (editorPart == null) {
            return null;
        }
        if (editorPart instanceof ITextEditor) {
            return ((ITextEditor) editorPart).getDocumentProvider();
        }

        IDocumentProvider docProvider =
            (IDocumentProvider) editorPart.getAdapter(IDocumentProvider.class);
        return docProvider;
    }

    /**
     * @return may return null
     */
    public IEditorInput getInput() {
        if (editorPart == null) {
            return null;
        }
        return editorPart.getEditorInput();
    }

    /**
     * @return may return null
     */
    public IFile getFile(){
        IEditorInput input = getInput();
        if(input == null){
            return null;
        }
        Object adapter = input.getAdapter(IFile.class);
        if(adapter instanceof IFile){
            return (IFile) adapter;
        }
        adapter = getAdapter(IFile.class);
        if(adapter instanceof IFile){
            return (IFile) adapter;
        }
        return null;
    }

    /**
     * @see ITypedElement#getType()
     */
    public String getContentType(){
        URI uri = getURI();
        if(uri == null){
            return null;
        }
        String path = uri.toString();
        int dot = path.lastIndexOf('.') + 1;
        if(dot >= 0){
            return path.substring(dot);
        }
{'fraction_non_alphanumeric': 0.11629587984900812, 'fraction_numerical': 0.0028110191952453616, 'mean_word_length': 2.154801114770712, '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': '6614651', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3115, 'n_words': 743}