PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/launch/NdkDebuggerTab.java

https://github.com/CyanogenMod/android_sdk
Java | 311 lines | 244 code | 43 blank | 24 comment | 27 complexity | d84486cc9c15732ebcfd347b977c73f6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2012 The Android Open Source Project
  3. *
  4. * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
  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. package com.android.ide.eclipse.ndk.internal.launch;
  17. import com.android.ide.eclipse.ndk.internal.NdkHelper;
  18. import com.android.ide.eclipse.ndk.internal.NdkManager;
  19. import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
  20. import org.eclipse.core.runtime.CoreException;
  21. import org.eclipse.debug.core.ILaunchConfiguration;
  22. import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
  23. import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
  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.events.SelectionListener;
  30. import org.eclipse.swt.layout.GridData;
  31. import org.eclipse.swt.layout.GridLayout;
  32. import org.eclipse.swt.layout.RowLayout;
  33. import org.eclipse.swt.widgets.Button;
  34. import org.eclipse.swt.widgets.Composite;
  35. import org.eclipse.swt.widgets.Control;
  36. import org.eclipse.swt.widgets.DirectoryDialog;
  37. import org.eclipse.swt.widgets.FileDialog;
  38. import org.eclipse.swt.widgets.Group;
  39. import org.eclipse.swt.widgets.Label;
  40. import org.eclipse.swt.widgets.Shell;
  41. import org.eclipse.swt.widgets.Text;
  42. import java.io.File;
  43. import java.util.Arrays;
  44. import java.util.Collections;
  45. import java.util.List;
  46. public class NdkDebuggerTab extends AbstractLaunchConfigurationTab {
  47. private static String sLastGdbPath;
  48. private static String sLastSolibPath;
  49. private Text mGdbPathText;
  50. private Text mGdbInitPathText;
  51. private Text mGdbRemotePortText;
  52. private org.eclipse.swt.widgets.List mSoliblist;
  53. private Button mAddSolibButton;
  54. private Button mDeleteSolibButton;
  55. /**
  56. * @wbp.parser.entryPoint (Window Builder Entry Point)
  57. */
  58. @Override
  59. public void createControl(Composite parent) {
  60. Composite comp = new Composite(parent, SWT.NONE);
  61. setControl(comp);
  62. comp.setLayout(new GridLayout(1, false));
  63. Group grpGdb = new Group(comp, SWT.NONE);
  64. grpGdb.setText("Launch Options");
  65. grpGdb.setLayout(new GridLayout(3, false));
  66. grpGdb.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
  67. Label lblDebugger = new Label(grpGdb, SWT.NONE);
  68. lblDebugger.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  69. lblDebugger.setText("Debugger:");
  70. mGdbPathText = new Text(grpGdb, SWT.BORDER);
  71. mGdbPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
  72. final Button btnBrowseGdb = new Button(grpGdb, SWT.NONE);
  73. btnBrowseGdb.setText("Browse...");
  74. Label lblNewLabel = new Label(grpGdb, SWT.NONE);
  75. lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  76. lblNewLabel.setText("GDB Command File:");
  77. mGdbInitPathText = new Text(grpGdb, SWT.BORDER);
  78. mGdbInitPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
  79. final Button btnBrowseGdbInit = new Button(grpGdb, SWT.NONE);
  80. btnBrowseGdbInit.setText("Browse...");
  81. SelectionListener browseListener = new SelectionAdapter() {
  82. @Override
  83. public void widgetSelected(SelectionEvent e) {
  84. Shell shell = ((Control) e.getSource()).getShell();
  85. if (e.getSource() == btnBrowseGdb) {
  86. browseForGdb(shell);
  87. } else {
  88. browseForGdbInit(shell);
  89. }
  90. checkParameters();
  91. updateLaunchConfigurationDialog();
  92. }
  93. };
  94. btnBrowseGdb.addSelectionListener(browseListener);
  95. btnBrowseGdbInit.addSelectionListener(browseListener);
  96. Label lblPort = new Label(grpGdb, SWT.NONE);
  97. lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
  98. lblPort.setText("Port:");
  99. mGdbRemotePortText = new Text(grpGdb, SWT.BORDER);
  100. GridData gd_text_2 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
  101. gd_text_2.widthHint = 100;
  102. mGdbRemotePortText.setLayoutData(gd_text_2);
  103. ModifyListener m = new ModifyListener() {
  104. @Override
  105. public void modifyText(ModifyEvent e) {
  106. checkParameters();
  107. updateLaunchConfigurationDialog();
  108. }
  109. };
  110. mGdbPathText.addModifyListener(m);
  111. mGdbInitPathText.addModifyListener(m);
  112. mGdbRemotePortText.addModifyListener(m);
  113. Group grpSharedLibraries = new Group(comp, SWT.NONE);
  114. grpSharedLibraries.setText("Shared Libraries");
  115. grpSharedLibraries.setLayout(new GridLayout(2, false));
  116. GridData gd_grpSharedLibraries = new GridData(GridData.FILL_BOTH);
  117. gd_grpSharedLibraries.verticalAlignment = SWT.TOP;
  118. gd_grpSharedLibraries.grabExcessVerticalSpace = true;
  119. grpSharedLibraries.setLayoutData(gd_grpSharedLibraries);
  120. mSoliblist = new org.eclipse.swt.widgets.List(grpSharedLibraries,
  121. SWT.BORDER | SWT.V_SCROLL | SWT.SINGLE);
  122. GridData gd_list = new GridData(GridData.FILL_BOTH);
  123. gd_list.heightHint = 133;
  124. gd_list.grabExcessVerticalSpace = false;
  125. gd_list.verticalSpan = 1;
  126. mSoliblist.setLayoutData(gd_list);
  127. Composite composite = new Composite(grpSharedLibraries, SWT.NONE);
  128. composite.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false, 1, 1));
  129. composite.setLayout(new RowLayout(SWT.VERTICAL));
  130. mAddSolibButton = new Button(composite, SWT.NONE);
  131. mAddSolibButton.setText("Add...");
  132. mDeleteSolibButton = new Button(composite, SWT.NONE);
  133. mDeleteSolibButton.setText("Remove");
  134. mDeleteSolibButton.setEnabled(false);
  135. SelectionListener l = new SelectionAdapter() {
  136. @Override
  137. public void widgetSelected(SelectionEvent e) {
  138. Control c = (Control) e.getSource();
  139. if (c == mSoliblist) {
  140. // enable delete only if there is a selection
  141. mDeleteSolibButton.setEnabled(mSoliblist.getSelectionCount() > 0);
  142. } else if (c == mAddSolibButton) {
  143. addSolib(c.getShell());
  144. } else {
  145. // delete current selection
  146. int index = mSoliblist.getSelectionIndex();
  147. if (index >= 0) {
  148. mSoliblist.remove(index);
  149. }
  150. }
  151. updateLaunchConfigurationDialog();
  152. }
  153. };
  154. mSoliblist.addSelectionListener(l);
  155. mAddSolibButton.addSelectionListener(l);
  156. mDeleteSolibButton.addSelectionListener(l);
  157. }
  158. private void addSolib(Shell shell) {
  159. DirectoryDialog dd = new DirectoryDialog(shell);
  160. if (sLastSolibPath != null) {
  161. dd.setFilterPath(sLastSolibPath);
  162. }
  163. String solibPath = dd.open();
  164. if (solibPath != null) {
  165. mSoliblist.add(solibPath);
  166. sLastSolibPath = new File(solibPath).getParent();
  167. }
  168. }
  169. private void browseForGdb(Shell shell) {
  170. if (sLastGdbPath == null) {
  171. sLastGdbPath = NdkManager.getNdkLocation();
  172. }
  173. FileDialog fd = new FileDialog(shell);
  174. fd.setFilterPath(sLastGdbPath);
  175. String gdbPath = fd.open();
  176. if (gdbPath != null) {
  177. mGdbPathText.setText(gdbPath);
  178. sLastGdbPath = new File(gdbPath).getParent();
  179. }
  180. }
  181. private void browseForGdbInit(Shell shell) {
  182. FileDialog fd = new FileDialog(shell);
  183. String gdbInit = fd.open();
  184. if (gdbInit != null) {
  185. mGdbInitPathText.setText(gdbInit);
  186. }
  187. }
  188. private void checkParameters() {
  189. // check gdb path
  190. String gdb = mGdbPathText.getText().trim();
  191. if (!gdb.equals(NdkLaunchConstants.DEFAULT_GDB)) {
  192. File f = new File(gdb);
  193. if (!f.exists() || !f.canExecute()) {
  194. setErrorMessage("Invalid gdb location.");
  195. return;
  196. }
  197. }
  198. // check gdb init path
  199. String gdbInit = mGdbInitPathText.getText().trim();
  200. if (!gdbInit.isEmpty()) {
  201. File f = new File(gdbInit);
  202. if (!f.exists() || !f.isFile()) {
  203. setErrorMessage("Invalid gdbinit location.");
  204. return;
  205. }
  206. }
  207. // port should be a valid integer
  208. String port = mGdbRemotePortText.getText().trim();
  209. try {
  210. Integer.parseInt(port, 10);
  211. } catch (NumberFormatException e) {
  212. setErrorMessage("Port should be a valid integer");
  213. return;
  214. }
  215. // no errors
  216. setErrorMessage(null);
  217. setMessage(null);
  218. }
  219. @Override
  220. public void setDefaults(ILaunchConfigurationWorkingCopy config) {
  221. NdkHelper.setLaunchConfigDefaults(config);
  222. }
  223. @Override
  224. public void initializeFrom(ILaunchConfiguration config) {
  225. mGdbPathText.setText(getAttribute(config, NdkLaunchConstants.ATTR_NDK_GDB,
  226. NdkLaunchConstants.DEFAULT_GDB));
  227. mGdbInitPathText.setText(getAttribute(config,
  228. IGDBLaunchConfigurationConstants.ATTR_GDB_INIT,
  229. NdkLaunchConstants.DEFAULT_GDBINIT));
  230. mGdbRemotePortText.setText(getAttribute(config, IGDBLaunchConfigurationConstants.ATTR_PORT,
  231. NdkLaunchConstants.DEFAULT_GDB_PORT));
  232. List<String> solibs = getAttribute(config, NdkLaunchConstants.ATTR_NDK_SOLIB,
  233. Collections.EMPTY_LIST);
  234. mSoliblist.removeAll();
  235. for (String s: solibs) {
  236. mSoliblist.add(s);
  237. }
  238. }
  239. private String getAttribute(ILaunchConfiguration config, String key, String defaultValue) {
  240. try {
  241. return config.getAttribute(key, defaultValue);
  242. } catch (CoreException e) {
  243. return defaultValue;
  244. }
  245. }
  246. private List<String> getAttribute(ILaunchConfiguration config, String key,
  247. List<String> defaultValue) {
  248. try {
  249. return config.getAttribute(key, defaultValue);
  250. } catch (CoreException e) {
  251. return defaultValue;
  252. }
  253. }
  254. @Override
  255. public void performApply(ILaunchConfigurationWorkingCopy config) {
  256. config.setAttribute(NdkLaunchConstants.ATTR_NDK_GDB, mGdbPathText.getText().trim());
  257. config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_GDB_INIT,
  258. mGdbInitPathText.getText().trim());
  259. config.setAttribute(IGDBLaunchConfigurationConstants.ATTR_PORT,
  260. mGdbRemotePortText.getText().trim());
  261. config.setAttribute(NdkLaunchConstants.ATTR_NDK_SOLIB,
  262. Arrays.asList(mSoliblist.getItems()));
  263. }
  264. @Override
  265. public String getName() {
  266. return "Debugger";
  267. }
  268. }