Home | History | Annotate | Download | only in gltrace
      1 /*
      2  ** Copyright 2011, The Android Open Source Project
      3  **
      4  ** Licensed under the Apache License, Version 2.0 (the "License");
      5  ** you may not use this file except in compliance with the License.
      6  ** You may obtain a copy of the License at
      7  **
      8  **     http://www.apache.org/licenses/LICENSE-2.0
      9  **
     10  ** Unless required by applicable law or agreed to in writing, software
     11  ** distributed under the License is distributed on an "AS IS" BASIS,
     12  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  ** See the License for the specific language governing permissions and
     14  ** limitations under the License.
     15  */
     16 
     17 package com.android.ide.eclipse.gltrace;
     18 
     19 import org.eclipse.jface.resource.ImageDescriptor;
     20 import org.eclipse.swt.widgets.Display;
     21 import org.eclipse.ui.IWorkbenchPage;
     22 import org.eclipse.ui.IWorkbenchWindow;
     23 import org.eclipse.ui.PartInitException;
     24 import org.eclipse.ui.PlatformUI;
     25 import org.eclipse.ui.console.ConsolePlugin;
     26 import org.eclipse.ui.console.IConsole;
     27 import org.eclipse.ui.console.IConsoleConstants;
     28 import org.eclipse.ui.console.MessageConsole;
     29 import org.eclipse.ui.console.MessageConsoleStream;
     30 import org.eclipse.ui.plugin.AbstractUIPlugin;
     31 import org.osgi.framework.BundleContext;
     32 
     33 /**
     34  * The activator class controls the plug-in life cycle
     35  */
     36 public class GlTracePlugin extends AbstractUIPlugin {
     37 
     38     // The plug-in ID
     39     public static final String PLUGIN_ID = "com.android.ide.eclipse.gldebugger"; //$NON-NLS-1$
     40 
     41     // The shared instance
     42     private static GlTracePlugin plugin;
     43 
     44     private MessageConsole mConsole;
     45     private MessageConsoleStream mConsoleStream;
     46 
     47     /**
     48      * The constructor
     49      */
     50     public GlTracePlugin() {
     51     }
     52 
     53     /*
     54      * (non-Javadoc)
     55      * @see
     56      * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
     57      * )
     58      */
     59     @Override
     60     public void start(BundleContext context) throws Exception {
     61         super.start(context);
     62         plugin = this;
     63 
     64         mConsole = new MessageConsole("OpenGL Trace View", null);
     65         ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] {
     66                 mConsole });
     67 
     68         mConsoleStream = mConsole.newMessageStream();
     69     }
     70 
     71     /*
     72      * (non-Javadoc)
     73      * @see
     74      * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
     75      * )
     76      */
     77     @Override
     78     public void stop(BundleContext context) throws Exception {
     79         plugin = null;
     80         super.stop(context);
     81     }
     82 
     83     /**
     84      * Returns the shared instance
     85      *
     86      * @return the shared instance
     87      */
     88     public static GlTracePlugin getDefault() {
     89         return plugin;
     90     }
     91 
     92     /**
     93      * Returns an image descriptor for the image file at the given plug-in
     94      * relative path
     95      *
     96      * @param path the path
     97      * @return the image descriptor
     98      */
     99     public static ImageDescriptor getImageDescriptor(String path) {
    100         return imageDescriptorFromPlugin(PLUGIN_ID, path);
    101     }
    102 
    103     public void logMessage(String message) {
    104         mConsoleStream.println(message);
    105 
    106         Display.getDefault().asyncExec(sShowConsoleRunnable);
    107     }
    108 
    109     private static Runnable sShowConsoleRunnable = new Runnable() {
    110         @Override
    111         public void run() {
    112             showConsoleView();
    113         };
    114     };
    115 
    116     private static void showConsoleView() {
    117         IWorkbenchWindow w = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    118         if (w != null) {
    119             IWorkbenchPage page = w.getActivePage();
    120             if (page != null) {
    121                 try {
    122                     page.showView(IConsoleConstants.ID_CONSOLE_VIEW, null,
    123                             IWorkbenchPage.VIEW_VISIBLE);
    124                 } catch (PartInitException e) {
    125                     // ignore
    126                 }
    127             }
    128         }
    129     }
    130 }
    131