aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.ddms/src/com/android/ide/eclipse/ddms/editors/UiAutomatorViewer.java
blob: 0843018aaa0c19b3f6b939994e12c93dc084a053 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.ddms.editors;

import com.android.ide.eclipse.base.InstallDetails;
import com.android.uiautomator.UiAutomatorHelper.UiAutomatorResult;
import com.android.uiautomator.UiAutomatorModel;
import com.android.uiautomator.UiAutomatorView;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IURIEditorInput;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.part.EditorPart;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

public class UiAutomatorViewer extends EditorPart {
    private String mFilePath;
    private UiAutomatorView mView;

    @Override
    public void doSave(IProgressMonitor arg0) {
    }

    @Override
    public void doSaveAs() {
    }

    @Override
    public boolean isSaveAsAllowed() {
        return false;
    }

    @Override
    public boolean isDirty() {
        return false;
    }

    @Override
    public void init(IEditorSite site, IEditorInput input) throws PartInitException {
        // we use a IURIEditorInput to allow opening files not within the workspace
        if (!(input instanceof IURIEditorInput)) {
            throw new PartInitException("UI Automator Hierarchy View: unsupported input type.");
        }

        setSite(site);
        setInput(input);
        mFilePath = ((IURIEditorInput) input).getURI().getPath();

        // set the editor part name to be the name of the file.
        File f = new File(mFilePath);
        setPartName(f.getName());
    }

    @Override
    public void createPartControl(Composite parent) {
        Composite c = new Composite(parent, SWT.NONE);
        c.setLayout(new GridLayout(1, false));
        GridData gd = new GridData(GridData.FILL_BOTH);
        c.setLayoutData(gd);

        mView = new UiAutomatorView(c, SWT.BORDER);
        mView.setLayoutData(new GridData(GridData.FILL_BOTH));

        if (mFilePath == null) {
            return;
        }

        UiAutomatorModel model = null;
        File modelFile = new File(mFilePath);
        try {
            model = new UiAutomatorModel(modelFile);
        } catch (Exception e) {
            MessageDialog.openError(parent.getShell(), "Error opening " + mFilePath,
                    "Unexpected error while parsing input: " + e.getMessage());
            return;
        }

        mView.setModel(model, modelFile, null);
    }

    @Override
    public void setFocus() {
    }

    public static boolean openEditor(final UiAutomatorResult r) {
        final IFileStore fileStore =  EFS.getLocalFileSystem().getStore(
                new Path(r.uiHierarchy.getAbsolutePath()));
        if (!fileStore.fetchInfo().exists()) {
            return false;
        }

        final AtomicBoolean status = new AtomicBoolean(false);

        final IWorkbench workbench = PlatformUI.getWorkbench();
        workbench.getDisplay().syncExec(new Runnable() {
            @Override
            public void run() {
                IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
                if (window == null) {
                    return;
                }

                IWorkbenchPage page = window.getActivePage();
                if (page == null) {
                    return;
                }

                // try to switch perspectives if possible
                if (page.isEditorAreaVisible() == false && InstallDetails.isAdtInstalled()) {
                    try {
                        workbench.showPerspective("org.eclipse.jdt.ui.JavaPerspective", window); //$NON-NLS-1$
                    } catch (WorkbenchException e) {
                    }
                }

                IEditorPart editor = null;
                try {
                    editor = IDE.openEditorOnFileStore(page, fileStore);
                } catch (PartInitException e) {
                    return;
                }

                if (!(editor instanceof UiAutomatorViewer)) {
                    return;
                }

                ((UiAutomatorViewer) editor).setModel(r.model, r.uiHierarchy, r.screenshot);
                status.set(true);
            }
        });

        return status.get();
    }

    protected void setModel(UiAutomatorModel model, File modelFile, Image screenshot) {
        mView.setModel(model, modelFile, screenshot);
    }
}