Home | History | Annotate | Download | only in traceview
      1 /*
      2  * Copyright (C) 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.traceview;
     18 
     19 import com.android.ide.eclipse.ddms.ITraceviewLauncher;
     20 
     21 import org.eclipse.core.filesystem.EFS;
     22 import org.eclipse.core.filesystem.IFileStore;
     23 import org.eclipse.core.resources.ResourcesPlugin;
     24 import org.eclipse.core.runtime.IAdaptable;
     25 import org.eclipse.core.runtime.Path;
     26 import org.eclipse.swt.widgets.Display;
     27 import org.eclipse.ui.IWorkbench;
     28 import org.eclipse.ui.IWorkbenchPage;
     29 import org.eclipse.ui.IWorkbenchWindow;
     30 import org.eclipse.ui.PartInitException;
     31 import org.eclipse.ui.PlatformUI;
     32 import org.eclipse.ui.WorkbenchException;
     33 import org.eclipse.ui.ide.IDE;
     34 
     35 public class TraceviewLauncher implements ITraceviewLauncher {
     36 
     37     @Override
     38     public boolean openFile(String osPath) {
     39         final IFileStore fileStore =  EFS.getLocalFileSystem().getStore(new Path(osPath));
     40         if (!fileStore.fetchInfo().isDirectory() && fileStore.fetchInfo().exists()) {
     41             // before we open the file in an editor window, we make sure the current
     42             // workbench page has an editor area (typically the ddms perspective doesn't).
     43             final IWorkbench workbench = PlatformUI.getWorkbench();
     44             Display display = workbench.getDisplay();
     45             final boolean[] result = new boolean[] { false };
     46             display.syncExec(new Runnable() {
     47                 @Override
     48                 public void run() {
     49                     IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
     50                     IWorkbenchPage page = window.getActivePage();
     51                     if (page.isEditorAreaVisible() == false) {
     52                         IAdaptable input;
     53                         if (page != null)
     54                             input= page.getInput();
     55                         else
     56                             input= ResourcesPlugin.getWorkspace().getRoot();
     57                         try {
     58                             workbench.showPerspective("org.eclipse.debug.ui.DebugPerspective",
     59                                     window, input);
     60                         } catch (WorkbenchException e) {
     61                         }
     62                     }
     63 
     64                     try {
     65                         result[0] = IDE.openEditorOnFileStore(page, fileStore) != null;
     66                     } catch (PartInitException e) {
     67                         // return false below
     68                     }
     69                 }
     70             });
     71 
     72             return result[0];
     73         }
     74 
     75         return false;
     76     }
     77 }
     78