• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.ddms.editors;
18 
19 import com.android.ide.eclipse.base.InstallDetails;
20 import com.android.uiautomator.UiAutomatorHelper.UiAutomatorResult;
21 import com.android.uiautomator.UiAutomatorModel;
22 import com.android.uiautomator.UiAutomatorView;
23 
24 import org.eclipse.core.filesystem.EFS;
25 import org.eclipse.core.filesystem.IFileStore;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.jface.dialogs.MessageDialog;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.ui.IEditorInput;
35 import org.eclipse.ui.IEditorPart;
36 import org.eclipse.ui.IEditorSite;
37 import org.eclipse.ui.IURIEditorInput;
38 import org.eclipse.ui.IWorkbench;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.IWorkbenchWindow;
41 import org.eclipse.ui.PartInitException;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.WorkbenchException;
44 import org.eclipse.ui.ide.IDE;
45 import org.eclipse.ui.part.EditorPart;
46 
47 import java.io.File;
48 import java.util.concurrent.atomic.AtomicBoolean;
49 
50 public class UiAutomatorViewer extends EditorPart {
51     private String mFilePath;
52     private UiAutomatorView mView;
53 
54     @Override
doSave(IProgressMonitor arg0)55     public void doSave(IProgressMonitor arg0) {
56     }
57 
58     @Override
doSaveAs()59     public void doSaveAs() {
60     }
61 
62     @Override
isSaveAsAllowed()63     public boolean isSaveAsAllowed() {
64         return false;
65     }
66 
67     @Override
isDirty()68     public boolean isDirty() {
69         return false;
70     }
71 
72     @Override
init(IEditorSite site, IEditorInput input)73     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
74         // we use a IURIEditorInput to allow opening files not within the workspace
75         if (!(input instanceof IURIEditorInput)) {
76             throw new PartInitException("UI Automator Hierarchy View: unsupported input type.");
77         }
78 
79         setSite(site);
80         setInput(input);
81         mFilePath = ((IURIEditorInput) input).getURI().getPath();
82 
83         // set the editor part name to be the name of the file.
84         File f = new File(mFilePath);
85         setPartName(f.getName());
86     }
87 
88     @Override
createPartControl(Composite parent)89     public void createPartControl(Composite parent) {
90         Composite c = new Composite(parent, SWT.NONE);
91         c.setLayout(new GridLayout(1, false));
92         GridData gd = new GridData(GridData.FILL_BOTH);
93         c.setLayoutData(gd);
94 
95         mView = new UiAutomatorView(c, SWT.BORDER);
96         mView.setLayoutData(new GridData(GridData.FILL_BOTH));
97 
98         if (mFilePath == null) {
99             return;
100         }
101 
102         UiAutomatorModel model = null;
103         File modelFile = new File(mFilePath);
104         try {
105             model = new UiAutomatorModel(modelFile);
106         } catch (Exception e) {
107             MessageDialog.openError(parent.getShell(), "Error opening " + mFilePath,
108                     "Unexpected error while parsing input: " + e.getMessage());
109             return;
110         }
111 
112         mView.setModel(model, modelFile, null);
113     }
114 
115     @Override
setFocus()116     public void setFocus() {
117     }
118 
openEditor(final UiAutomatorResult r)119     public static boolean openEditor(final UiAutomatorResult r) {
120         final IFileStore fileStore =  EFS.getLocalFileSystem().getStore(
121                 new Path(r.uiHierarchy.getAbsolutePath()));
122         if (!fileStore.fetchInfo().exists()) {
123             return false;
124         }
125 
126         final AtomicBoolean status = new AtomicBoolean(false);
127 
128         final IWorkbench workbench = PlatformUI.getWorkbench();
129         workbench.getDisplay().syncExec(new Runnable() {
130             @Override
131             public void run() {
132                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
133                 if (window == null) {
134                     return;
135                 }
136 
137                 IWorkbenchPage page = window.getActivePage();
138                 if (page == null) {
139                     return;
140                 }
141 
142                 // try to switch perspectives if possible
143                 if (page.isEditorAreaVisible() == false && InstallDetails.isAdtInstalled()) {
144                     try {
145                         workbench.showPerspective("org.eclipse.jdt.ui.JavaPerspective", window); //$NON-NLS-1$
146                     } catch (WorkbenchException e) {
147                     }
148                 }
149 
150                 IEditorPart editor = null;
151                 try {
152                     editor = IDE.openEditorOnFileStore(page, fileStore);
153                 } catch (PartInitException e) {
154                     return;
155                 }
156 
157                 if (!(editor instanceof UiAutomatorViewer)) {
158                     return;
159                 }
160 
161                 ((UiAutomatorViewer) editor).setModel(r.model, r.uiHierarchy, r.screenshot);
162                 status.set(true);
163             }
164         });
165 
166         return status.get();
167     }
168 
setModel(UiAutomatorModel model, File modelFile, Image screenshot)169     protected void setModel(UiAutomatorModel model, File modelFile, Image screenshot) {
170         mView.setModel(model, modelFile, screenshot);
171     }
172 }
173