scouter.window.util.RCPUtil.java Source code

Java tutorial

Introduction

Here is the source code for scouter.window.util.RCPUtil.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package scouter.window.util;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceNode;
import org.eclipse.jface.preference.PreferenceManager;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveRegistry;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.registry.ActionSetRegistry;
import org.eclipse.ui.internal.registry.IActionSetDescriptor;
import org.eclipse.ui.views.IViewDescriptor;
import org.eclipse.ui.views.IViewRegistry;

@SuppressWarnings("restriction")
public class RCPUtil {

    public static boolean isEclipseIdeRunning() {
        IProduct product = Platform.getProduct();
        if (product == null)
            return false;
        // ("Eclipse SDK".equals(product.getName()));
        return "org.eclipse.sdk.ide".equals(product.getId())
                && "org.eclipse.ui.ide.workbench".equals(product.getApplication());
    }

    public static void preLoadingPerspective(String[] ids) {
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        if (page != null) {
            IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
            IPerspectiveDescriptor active = page.getPerspective();
            for (int idx = ids.length - 1; idx >= 0; idx--) {
                if (active == null || !active.getId().equals(ids[idx])) {
                    IPerspectiveDescriptor perspective = registry.findPerspectiveWithId(ids[idx]);
                    page.setPerspective(perspective);
                    // PlatformUI.getWorkbench().showPerspective("scouter.window.perspective.utility",
                    // PlatformUI.getWorkbench().getActiveWorkbenchWindow());
                }
            }
            page.setPerspective(registry.findPerspectiveWithId(ids[0]));
        }

    }

    public static void hideActions(String[] ids) {
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        for (int idx = 0; idx < ids.length; idx++) {
            page.hideActionSet(ids[idx]);
        }
    }

    public static void hidePreference(String[] ids) {
        List<String> list = Arrays.asList(ids);
        PreferenceManager preferenceManager = PlatformUI.getWorkbench().getPreferenceManager();
        @SuppressWarnings("unchecked")
        List<IPreferenceNode> preferenceNodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER);
        for (Iterator<IPreferenceNode> it = preferenceNodes.iterator(); it.hasNext();) {
            IPreferenceNode preferenceNode = (IPreferenceNode) it.next();
            if (list.contains(preferenceNode.getId())) {
                preferenceManager.remove(preferenceNode);
            }
        }
    }

    public static void printPreferencePages() {
        System.out.println("=== PreferencePages ===");
        PreferenceManager preferenceManager = PlatformUI.getWorkbench().getPreferenceManager();
        @SuppressWarnings("unchecked")
        List<IPreferenceNode> preferenceNodes = preferenceManager.getElements(PreferenceManager.PRE_ORDER);
        for (Iterator<IPreferenceNode> it = preferenceNodes.iterator(); it.hasNext();) {
            IPreferenceNode preferenceNode = (IPreferenceNode) it.next();
            System.out.println(preferenceNode.getId());
        }
    }

    public static void printPerspectives() {
        System.out.println("=== Perspectives ===");
        IPerspectiveRegistry registry = PlatformUI.getWorkbench().getPerspectiveRegistry();
        IPerspectiveDescriptor[] descriptors = registry.getPerspectives();
        for (int idx = 0; idx < descriptors.length; idx++) {
            System.out.println(descriptors[idx].getId());
        }
    }

    public static void printViews() {
        System.out.println("=== Views ===");
        IViewRegistry registry = PlatformUI.getWorkbench().getViewRegistry();
        IViewDescriptor[] descriptors = registry.getViews();
        for (int idx = 0; idx < descriptors.length; idx++) {
            System.out.println(descriptors[idx].getId());
        }

    }

    public static void exit() {
        IWorkbench workbench = PlatformUI.getWorkbench();
        if (workbench != null && !workbench.isClosing()) {
            workbench.close();
        }
    }

    public static void printActionSet() {
        System.out.println("=== ActionSets ===");
        ActionSetRegistry reg = WorkbenchPlugin.getDefault().getActionSetRegistry();
        IActionSetDescriptor[] actionSets = reg.getActionSets();
        for (int i = 0; i < actionSets.length; i++) {
            System.out.println(actionSets[i].getId());
            //            IExtension ext = actionSets[i].getConfigurationElement().getDeclaringExtension();
            //            reg.removeExtension(ext, new Object[] { actionSets[i] });
        }
    }
}