org.chromium.debug.core/src/org/chromium/debug/core/ChromiumDebugPlugin.java
changeset 2 e4420d2515f1
child 52 f577ea64429e
equal deleted inserted replaced
1:ef76fc2ac88c 2:e4420d2515f1
       
     1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
       
     2 // Use of this source code is governed by a BSD-style license that can be
       
     3 // found in the LICENSE file.
       
     4 
       
     5 package org.chromium.debug.core;
       
     6 
       
     7 import java.text.MessageFormat;
       
     8 
       
     9 import org.chromium.debug.core.model.ChromiumBreakpointWBAFactory;
       
    10 import org.chromium.debug.core.model.ChromiumLineBreakpoint;
       
    11 import org.eclipse.core.runtime.CoreException;
       
    12 import org.eclipse.core.runtime.IAdapterManager;
       
    13 import org.eclipse.core.runtime.IStatus;
       
    14 import org.eclipse.core.runtime.Platform;
       
    15 import org.eclipse.core.runtime.Plugin;
       
    16 import org.eclipse.core.runtime.Status;
       
    17 import org.osgi.framework.BundleContext;
       
    18 
       
    19 /**
       
    20  * The activator class that controls the plug-in life cycle.
       
    21  */
       
    22 public class ChromiumDebugPlugin extends Plugin {
       
    23 
       
    24   /** The plug-in ID. */
       
    25   public static final String PLUGIN_ID = "org.chromium.debug.core"; //$NON-NLS-1$
       
    26 
       
    27   /** The debug model ID. */
       
    28   public static final String DEBUG_MODEL_ID = "org.chromium.debug"; //$NON-NLS-1$
       
    29 
       
    30   /** The JavaScript line breakpoint marker. */
       
    31   public static final String BP_MARKER = PLUGIN_ID + ".LineBP"; //$NON-NLS-1$
       
    32 
       
    33   /** The shared instance. */
       
    34   private static ChromiumDebugPlugin plugin;
       
    35 
       
    36   private ChromiumBreakpointWBAFactory breakpointWorkbenchAdapterFactory;
       
    37 
       
    38   public ChromiumDebugPlugin() {
       
    39   }
       
    40 
       
    41   @Override
       
    42   public void start(BundleContext context) throws Exception {
       
    43     super.start(context);
       
    44     IAdapterManager manager = Platform.getAdapterManager();
       
    45     breakpointWorkbenchAdapterFactory = new ChromiumBreakpointWBAFactory();
       
    46     manager.registerAdapters(breakpointWorkbenchAdapterFactory, ChromiumLineBreakpoint.class);
       
    47     plugin = this;
       
    48   }
       
    49 
       
    50   @Override
       
    51   public void stop(BundleContext context) throws Exception {
       
    52     plugin = null;
       
    53     IAdapterManager manager = Platform.getAdapterManager();
       
    54     manager.unregisterAdapters(breakpointWorkbenchAdapterFactory);
       
    55     super.stop(context);
       
    56   }
       
    57 
       
    58   /**
       
    59    * @return the shared instance
       
    60    */
       
    61   public static ChromiumDebugPlugin getDefault() {
       
    62     return plugin;
       
    63   }
       
    64 
       
    65   public static boolean isDebug() {
       
    66     ChromiumDebugPlugin thePlugin = getDefault();
       
    67     return thePlugin != null && thePlugin.isDebugging();
       
    68   }
       
    69 
       
    70   public static boolean isTransportDebug() {
       
    71     return isDebug() &&
       
    72         Boolean.valueOf(Platform.getDebugOption(PLUGIN_ID + "/debug/transport")); //$NON-NLS-1$
       
    73   }
       
    74 
       
    75   public static boolean isV8DebuggerToolDebug() {
       
    76     return isDebug() &&
       
    77         Boolean.valueOf(Platform.getDebugOption(PLUGIN_ID + "/debug/v8DebuggerTool")); //$NON-NLS-1$
       
    78   }
       
    79 
       
    80   public static void log(IStatus status) {
       
    81     ChromiumDebugPlugin plugin = getDefault();
       
    82     if (plugin != null) {
       
    83       plugin.getLog().log(status);
       
    84     } else {
       
    85       System.err.println(status.getPlugin() + ": " + status.getMessage()); //$NON-NLS-1$
       
    86     }
       
    87   }
       
    88 
       
    89   public static void log(Throwable e) {
       
    90     if (e instanceof CoreException) {
       
    91       log(new Status(IStatus.ERROR, PLUGIN_ID,
       
    92           ((CoreException) e).getStatus().getSeverity(), e.getMessage(), e.getCause()));
       
    93     } else {
       
    94       log(new Status(IStatus.ERROR, PLUGIN_ID, Messages.ChromiumDebugPlugin_InternalError, e));
       
    95     }
       
    96   }
       
    97 
       
    98   public static void logError(String message, Object... arguments) {
       
    99     log(new Status(Status.ERROR, PLUGIN_ID, getPossiblyFormattedString(message, arguments)));
       
   100   }
       
   101 
       
   102   public static void logWarning(String message, Object... arguments) {
       
   103     log(new Status(Status.WARNING, PLUGIN_ID, getPossiblyFormattedString(message, arguments)));
       
   104   }
       
   105 
       
   106   private static String getPossiblyFormattedString(String message, Object... arguments) {
       
   107     return arguments.length > 0
       
   108         ? MessageFormat.format(message, arguments)
       
   109         : message;
       
   110   }
       
   111 
       
   112 }