UiAutomatorViewer.java revision 07750e98d91da6a1a906c73e2a2b92e46aedf4cf
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
17package com.android.ide.eclipse.ddms.editors;
18
19import com.android.ide.eclipse.base.InstallDetails;
20import com.android.uiautomator.UiAutomatorHelper.UiAutomatorResult;
21import com.android.uiautomator.UiAutomatorModel;
22import com.android.uiautomator.UiAutomatorView;
23
24import org.eclipse.core.filesystem.EFS;
25import org.eclipse.core.filesystem.IFileStore;
26import org.eclipse.core.runtime.IProgressMonitor;
27import org.eclipse.core.runtime.Path;
28import org.eclipse.jface.dialogs.MessageDialog;
29import org.eclipse.swt.SWT;
30import org.eclipse.swt.graphics.Image;
31import org.eclipse.swt.layout.GridData;
32import org.eclipse.swt.layout.GridLayout;
33import org.eclipse.swt.widgets.Composite;
34import org.eclipse.ui.IEditorInput;
35import org.eclipse.ui.IEditorPart;
36import org.eclipse.ui.IEditorSite;
37import org.eclipse.ui.IURIEditorInput;
38import org.eclipse.ui.IWorkbench;
39import org.eclipse.ui.IWorkbenchPage;
40import org.eclipse.ui.IWorkbenchWindow;
41import org.eclipse.ui.PartInitException;
42import org.eclipse.ui.PlatformUI;
43import org.eclipse.ui.WorkbenchException;
44import org.eclipse.ui.ide.IDE;
45import org.eclipse.ui.part.EditorPart;
46
47import java.io.File;
48import java.util.concurrent.atomic.AtomicBoolean;
49
50public class UiAutomatorViewer extends EditorPart {
51    private String mFilePath;
52    private UiAutomatorView mView;
53
54    @Override
55    public void doSave(IProgressMonitor arg0) {
56    }
57
58    @Override
59    public void doSaveAs() {
60    }
61
62    @Override
63    public boolean isSaveAsAllowed() {
64        return false;
65    }
66
67    @Override
68    public boolean isDirty() {
69        return false;
70    }
71
72    @Override
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
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
116    public void setFocus() {
117    }
118
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
169    protected void setModel(UiAutomatorModel model, File modelFile, Image screenshot) {
170        mView.setModel(model, modelFile, screenshot);
171    }
172}
173