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.monitor; 18 19 import com.android.ide.eclipse.monitor.SdkToolsLocator.SdkInstallStatus; 20 21 import org.eclipse.jface.dialogs.Dialog; 22 import org.eclipse.jface.dialogs.IDialogConstants; 23 import org.eclipse.jface.layout.GridDataFactory; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.ModifyEvent; 26 import org.eclipse.swt.events.ModifyListener; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.layout.GridData; 30 import org.eclipse.swt.layout.GridLayout; 31 import org.eclipse.swt.widgets.Button; 32 import org.eclipse.swt.widgets.Composite; 33 import org.eclipse.swt.widgets.Control; 34 import org.eclipse.swt.widgets.DirectoryDialog; 35 import org.eclipse.swt.widgets.Label; 36 import org.eclipse.swt.widgets.Shell; 37 import org.eclipse.swt.widgets.Text; 38 39 import java.io.File; 40 41 public class SdkLocationChooserDialog extends Dialog { 42 private static final String TITLE = "Android Device Monitor"; 43 private static final String DEFAULT_MESSAGE = "Provide the path to the Android SDK"; 44 45 private Label mStatusLabel; 46 private Text mTextBox; 47 private String mPath; 48 49 public SdkLocationChooserDialog(Shell parentShell) { 50 super(parentShell); 51 } 52 53 @Override 54 protected Control createDialogArea(Composite parent) { 55 getShell().setText(TITLE); 56 57 Composite c = new Composite((Composite) super.createDialogArea(parent), SWT.NONE); 58 c.setLayout(new GridLayout(2, false)); 59 c.setLayoutData(new GridData(GridData.FILL_BOTH)); 60 61 Label l = new Label(c, SWT.NONE); 62 l.setText(DEFAULT_MESSAGE); 63 GridDataFactory.fillDefaults().span(2, 1).applyTo(l); 64 65 mTextBox = new Text(c, SWT.BORDER); 66 mTextBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 67 GridDataFactory.fillDefaults() 68 .hint(SwtUtils.getFontWidth(mTextBox) * 80, SWT.DEFAULT) 69 .applyTo(mTextBox); 70 mTextBox.addModifyListener(new ModifyListener() { 71 @Override 72 public void modifyText(ModifyEvent e) { 73 validateInstall(); 74 } 75 }); 76 77 Button browse = new Button(c, SWT.PUSH); 78 browse.setText("Browse"); 79 browse.addSelectionListener(new SelectionAdapter() { 80 @Override 81 public void widgetSelected(SelectionEvent e) { 82 DirectoryDialog dlg = new DirectoryDialog(getShell(), SWT.OPEN); 83 dlg.setText("Android SDK location"); 84 String dir = dlg.open(); 85 if (dir != null) { 86 mTextBox.setText(dir); 87 validateInstall(); 88 } 89 } 90 }); 91 92 mStatusLabel = new Label(c, SWT.WRAP); 93 mStatusLabel.setText(""); 94 mStatusLabel.setForeground(getShell().getDisplay().getSystemColor(SWT.COLOR_RED)); 95 GridDataFactory.fillDefaults().span(2, 1).applyTo(mStatusLabel); 96 97 return super.createDialogArea(parent); 98 } 99 100 private void validateInstall() { 101 SdkToolsLocator locator = new SdkToolsLocator(new File(mTextBox.getText())); 102 SdkInstallStatus status = locator.isValidInstallation(); 103 if (status.isValid()) { 104 mStatusLabel.setText(""); 105 getButton(IDialogConstants.OK_ID).setEnabled(true); 106 } else { 107 mStatusLabel.setText(status.getErrorMessage()); 108 mStatusLabel.pack(); 109 getButton(IDialogConstants.OK_ID).setEnabled(false); 110 } 111 } 112 113 @Override 114 public boolean close() { 115 mPath = mTextBox.getText(); 116 return super.close(); 117 } 118 119 public String getPath() { 120 return mPath; 121 } 122 } 123