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.uiautomator; 18 19 import org.eclipse.jface.dialogs.Dialog; 20 import org.eclipse.jface.dialogs.IDialogConstants; 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.graphics.Point; 23 import org.eclipse.swt.layout.GridData; 24 import org.eclipse.swt.layout.GridLayout; 25 import org.eclipse.swt.widgets.Button; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.Event; 29 import org.eclipse.swt.widgets.FileDialog; 30 import org.eclipse.swt.widgets.Group; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Shell; 33 import org.eclipse.swt.widgets.Text; 34 35 import java.io.File; 36 37 /** 38 * Implements a file selection dialog for both screen shot and xml dump file 39 * 40 * "OK" button won't be enabled unless both files are selected 41 * It also has a convenience feature such that if one file has been picked, and the other 42 * file path is empty, then selection for the other file will start from the same base folder 43 * 44 */ 45 public class OpenDialog extends Dialog { 46 47 private static final int FIXED_TEXT_FIELD_WIDTH = 300; 48 private static final int DEFAULT_LAYOUT_SPACING = 10; 49 private Text mScreenshotText; 50 private Text mXmlText; 51 private File mScreenshotFile; 52 private File mXmlDumpFile; 53 private boolean mFileChanged = false; 54 private Button mOkButton; 55 56 /** 57 * Create the dialog. 58 * @param parentShell 59 */ 60 public OpenDialog(Shell parentShell) { 61 super(parentShell); 62 setShellStyle(SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL); 63 } 64 65 /** 66 * Create contents of the dialog. 67 * @param parent 68 */ 69 @Override 70 protected Control createDialogArea(Composite parent) { 71 loadDataFromModel(); 72 73 Composite container = (Composite) super.createDialogArea(parent); 74 GridLayout gl_container = new GridLayout(1, false); 75 gl_container.verticalSpacing = DEFAULT_LAYOUT_SPACING; 76 gl_container.horizontalSpacing = DEFAULT_LAYOUT_SPACING; 77 gl_container.marginWidth = DEFAULT_LAYOUT_SPACING; 78 gl_container.marginHeight = DEFAULT_LAYOUT_SPACING; 79 container.setLayout(gl_container); 80 81 Group openScreenshotGroup = new Group(container, SWT.NONE); 82 openScreenshotGroup.setLayout(new GridLayout(2, false)); 83 openScreenshotGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 84 openScreenshotGroup.setText("Screenshot"); 85 86 mScreenshotText = new Text(openScreenshotGroup, SWT.BORDER | SWT.READ_ONLY); 87 if (mScreenshotFile != null) { 88 mScreenshotText.setText(mScreenshotFile.getAbsolutePath()); 89 } 90 GridData gd_screenShotText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1); 91 gd_screenShotText.minimumWidth = FIXED_TEXT_FIELD_WIDTH; 92 gd_screenShotText.widthHint = FIXED_TEXT_FIELD_WIDTH; 93 mScreenshotText.setLayoutData(gd_screenShotText); 94 95 Button openScreenshotButton = new Button(openScreenshotGroup, SWT.NONE); 96 openScreenshotButton.setText("..."); 97 openScreenshotButton.addListener(SWT.Selection, new Listener() { 98 @Override 99 public void handleEvent(Event event) { 100 handleOpenScreenshotFile(); 101 } 102 }); 103 104 Group openXmlGroup = new Group(container, SWT.NONE); 105 openXmlGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 106 openXmlGroup.setText("UI XML Dump"); 107 openXmlGroup.setLayout(new GridLayout(2, false)); 108 109 mXmlText = new Text(openXmlGroup, SWT.BORDER | SWT.READ_ONLY); 110 mXmlText.setEditable(false); 111 if (mXmlDumpFile != null) { 112 mXmlText.setText(mXmlDumpFile.getAbsolutePath()); 113 } 114 GridData gd_xmlText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1); 115 gd_xmlText.minimumWidth = FIXED_TEXT_FIELD_WIDTH; 116 gd_xmlText.widthHint = FIXED_TEXT_FIELD_WIDTH; 117 mXmlText.setLayoutData(gd_xmlText); 118 119 Button openXmlButton = new Button(openXmlGroup, SWT.NONE); 120 openXmlButton.setText("..."); 121 openXmlButton.addListener(SWT.Selection, new Listener() { 122 @Override 123 public void handleEvent(Event event) { 124 handleOpenXmlDumpFile(); 125 } 126 }); 127 128 return container; 129 } 130 131 /** 132 * Create contents of the button bar. 133 * @param parent 134 */ 135 @Override 136 protected void createButtonsForButtonBar(Composite parent) { 137 mOkButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); 138 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); 139 updateButtonState(); 140 } 141 142 /** 143 * Return the initial size of the dialog. 144 */ 145 @Override 146 protected Point getInitialSize() { 147 return new Point(368, 233); 148 } 149 150 @Override 151 protected void configureShell(Shell newShell) { 152 super.configureShell(newShell); 153 newShell.setText("Open UI Dump Files"); 154 } 155 156 private void loadDataFromModel() { 157 mScreenshotFile = UiAutomatorModel.getModel().getScreenshotFile(); 158 mXmlDumpFile = UiAutomatorModel.getModel().getXmlDumpFile(); 159 } 160 161 private void handleOpenScreenshotFile() { 162 FileDialog fd = new FileDialog(getShell(), SWT.OPEN); 163 fd.setText("Open Screenshot File"); 164 File initialFile = mScreenshotFile; 165 // if file has never been selected before, try to base initial path on the mXmlDumpFile 166 if (initialFile == null && mXmlDumpFile != null && mXmlDumpFile.isFile()) { 167 initialFile = mXmlDumpFile.getParentFile(); 168 } 169 if (initialFile != null) { 170 if (initialFile.isFile()) { 171 fd.setFileName(initialFile.getAbsolutePath()); 172 } else if (initialFile.isDirectory()) { 173 fd.setFilterPath(initialFile.getAbsolutePath()); 174 } 175 } 176 String[] filter = {"*.png"}; 177 fd.setFilterExtensions(filter); 178 String selected = fd.open(); 179 if (selected != null) { 180 mScreenshotFile = new File(selected); 181 mScreenshotText.setText(selected); 182 mFileChanged = true; 183 } 184 updateButtonState(); 185 } 186 187 private void handleOpenXmlDumpFile() { 188 FileDialog fd = new FileDialog(getShell(), SWT.OPEN); 189 fd.setText("Open UI Dump XML File"); 190 File initialFile = mXmlDumpFile; 191 // if file has never been selected before, try to base initial path on the mScreenshotFile 192 if (initialFile == null && mScreenshotFile != null && mScreenshotFile.isFile()) { 193 initialFile = mScreenshotFile.getParentFile(); 194 } 195 if (initialFile != null) { 196 if (initialFile.isFile()) { 197 fd.setFileName(initialFile.getAbsolutePath()); 198 } else if (initialFile.isDirectory()) { 199 fd.setFilterPath(initialFile.getAbsolutePath()); 200 } 201 } 202 String initialPath = mXmlText.getText(); 203 if (initialPath.isEmpty() && mScreenshotFile != null && mScreenshotFile.isFile()) { 204 initialPath = mScreenshotFile.getParentFile().getAbsolutePath(); 205 } 206 String[] filter = {"*.xml"}; 207 fd.setFilterExtensions(filter); 208 String selected = fd.open(); 209 if (selected != null) { 210 mXmlDumpFile = new File(selected); 211 mXmlText.setText(selected); 212 mFileChanged = true; 213 } 214 updateButtonState(); 215 } 216 217 private void updateButtonState() { 218 mOkButton.setEnabled(mScreenshotFile != null && mXmlDumpFile != null 219 && mScreenshotFile.isFile() && mXmlDumpFile.isFile()); 220 } 221 222 public boolean hasFileChanged() { 223 return mFileChanged; 224 } 225 226 public File getScreenshotFile() { 227 return mScreenshotFile; 228 } 229 230 public File getXmlDumpFile() { 231 return mXmlDumpFile; 232 } 233 } 234