<commit_before>package com.redhat.ceylon.eclipse.ui;

import static com.redhat.ceylon.eclipse.ui.CeylonPlugin.PLUGIN_ID;
import static org.eclipse.ui.PlatformUI.getWorkbench;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;

import com.redhat.ceylon.eclipse.code.editor.RecentFilesPopup;
import com.redhat.ceylon.eclipse.core.debug.CeylonDebugElementAdapterFactory;
import com.redhat.ceylon.eclipse.util.EditorUtil;

public class CeylonStartup implements IStartup {

    private static final class WindowListener implements IWindowListener {
        IContextActivation contextActivation1 = null;
        IContextActivation contextActivation2 = null;

        public void updateContext(IPerspectiveDescriptor perspective) {
            IContextService service = (IContextService) getWorkbench().getActiveWorkbenchWindow()
                    .getService(IContextService.class);
            // in case of previous crash, perspective may be null
            if (perspective != null && perspective.getId() != null && 
                    perspective.getId().equals(PLUGIN_ID + ".perspective")) {
                contextActivation1 = service.activateContext(PLUGIN_ID + ".perspectiveContext");
                contextActivation2 = service.activateContext(PLUGIN_ID + ".wizardContext");
            }
            else {
                if (contextActivation1!=null) {
                    service.deactivateContext(contextActivation1);
                }
                if (contextActivation2!=null) {
                    service.deactivateContext(contextActivation2);
                }
            }
        }

        @Override
        public void windowOpened(IWorkbenchWindow window) {
            updateContext(window.getActivePage().getPerspective());
            window.addPerspectiveListener(new IPerspectiveListener() {
                @Override
                public void perspectiveChanged(IWorkbenchPage page,
                        IPerspectiveDescriptor perspective, String changeId) {}
                @Override
                public void perspectiveActivated(IWorkbenchPage page,
                        IPerspectiveDescriptor perspective) {
                    updateContext(perspective);
                }
            });
        }

        @Override
        public void windowDeactivated(IWorkbenchWindow window) {}
        @Override
        public void windowClosed(IWorkbenchWindow window) {}
        @Override
        public void windowActivated(IWorkbenchWindow window) {}
    }

    @Override
    public void earlyStartup() {
        CeylonDebugElementAdapterFactory.installCeylonDebugElementAdapters();
        getWorkbench().getDisplay().asyncExec(new Runnable() {
            @Override
            public void run() {
                WindowListener listener = new WindowListener();
                getWorkbench().addWindowListener(listener);<commit_msg>More robust registering of overriden JDT Debug adapters.<commit_after>package com.redhat.ceylon.eclipse.ui;

import static com.redhat.ceylon.eclipse.core.debug.CeylonDebugElementAdapterFactory.installCeylonDebugElementAdapters;
import static com.redhat.ceylon.eclipse.ui.CeylonPlugin.PLUGIN_ID;
import static org.eclipse.ui.PlatformUI.getWorkbench;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IExecutionListener;
import org.eclipse.core.commands.NotHandledException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;

import com.redhat.ceylon.eclipse.code.editor.RecentFilesPopup;
import com.redhat.ceylon.eclipse.util.EditorUtil;

public class CeylonStartup implements IStartup {

    private static final class WindowListener implements IWindowListener {
        IContextActivation contextActivation1 = null;
        IContextActivation contextActivation2 = null;

        public void updateContext(IPerspectiveDescriptor perspective) {
            IContextService service = (IContextService) getWorkbench().getActiveWorkbenchWindow()
                    .getService(IContextService.class);
            // in case of previous crash, perspective may be null
            if (perspective != null && perspective.getId() != null && 
                    perspective.getId().equals(PLUGIN_ID + ".perspective")) {
                contextActivation1 = service.activateContext(PLUGIN_ID + ".perspectiveContext");
                contextActivation2 = service.activateContext(PLUGIN_ID + ".wizardContext");
            }
            else {
                if (contextActivation1!=null) {
                    service.deactivateContext(contextActivation1);
                }
                if (contextActivation2!=null) {
                    service.deactivateContext(contextActivation2);
                }
            }
        }

        @Override
        public void windowOpened(IWorkbenchWindow window) {
            updateContext(window.getActivePage().getPerspective());
            window.addPerspectiveListener(new IPerspectiveListener() {
                @Override
                public void perspectiveChanged(IWorkbenchPage page,
                        IPerspectiveDescriptor perspective, String changeId) {}
                @Override
                public void perspectiveActivated(IWorkbenchPage page,
                        IPerspectiveDescriptor perspective) {
                    updateContext(perspective);
                }
            });
        }

        @Override
        public void windowDeactivated(IWorkbenchWindow window) {}
        @Override
        public void windowClosed(IWorkbenchWindow window) {}
        @Override
        public void windowActivated(IWorkbenchWindow window) {}
    }

    @Override
    public void earlyStartup() {
        DebugPlugin.getDefault().getLaunchManager().addLaunchListener(new ILaunchListener() {
            Boolean activated = false;
            
            @Override
            public void launchRemoved(ILaunch launch) {
            }
            
            @Override
            public void launchChanged(ILaunch launch) {
                synchronized (activated) {
                    if (activated) {
                        return;
                    }
                    activated = true;
                    installCeylonDebugElementAdapters();
                    DebugPlugin.getDefault().getLaunchManager().removeLaunchListener(this);
                }
            }
            
            @Override
            public void launchAdded(ILaunch launch) {
                launchChanged(launch);
            }
        });
        
        getWorkbench().getDisplay().asyncExec(new Runnable() {
            @Override
            public void run() {
                WindowListener listener = new WindowListener();
                getWorkbench().addWindowListener(listener);
{'fraction_non_alphanumeric': 0.07991388044579534, 'fraction_numerical': 0.002026342451874367, 'mean_word_length': 2.4590451160753393, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '834730', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1902, 'n_words': 350}