|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
|
2 // Use of this source code is governed by a BSD-style license that can be |
|
3 // found in the LICENSE file. |
|
4 |
|
5 package org.chromium.debug.ui; |
|
6 |
|
7 import java.io.IOException; |
|
8 import java.util.ArrayList; |
|
9 import java.util.HashMap; |
|
10 import java.util.List; |
|
11 import java.util.Map; |
|
12 |
|
13 import org.chromium.debug.core.model.TabSelector; |
|
14 import org.chromium.sdk.Browser; |
|
15 import org.chromium.sdk.Browser.TabConnector; |
|
16 import org.chromium.sdk.Browser.TabFetcher; |
|
17 import org.eclipse.swt.widgets.Display; |
|
18 import org.eclipse.swt.widgets.Shell; |
|
19 import org.eclipse.ui.PlatformUI; |
|
20 |
|
21 /** |
|
22 * A TabSelector which brings up a dialog allowing users to select which target |
|
23 * browser tab to debug. |
|
24 */ |
|
25 public class DialogBasedTabSelector implements TabSelector { |
|
26 |
|
27 public TabConnector selectTab(TabFetcher tabFetcher) throws IOException { |
|
28 List<? extends Browser.TabConnector> allTabs = tabFetcher.getTabs(); |
|
29 |
|
30 List<Browser.TabConnector> filteredTabs = new ArrayList<TabConnector>(allTabs.size()); |
|
31 |
|
32 for (Browser.TabConnector tab : allTabs) { |
|
33 if (!tab.isAlreadyAttached()) { |
|
34 filteredTabs.add(tab); |
|
35 } |
|
36 } |
|
37 |
|
38 if (autoSelectSingleTab()) { |
|
39 if (allTabs.size() == 1 && filteredTabs.size() == 1) { |
|
40 // if all crystal clear -- choose by default |
|
41 // disable auto-select if there are some already attached tabs: |
|
42 // user has already seen this dialog and might have got used to it |
|
43 // he might not understand why it didn't show up this time |
|
44 return allTabs.get(0); |
|
45 } |
|
46 } |
|
47 |
|
48 final Map<Integer, Browser.TabConnector> map = new HashMap<Integer, Browser.TabConnector>(); |
|
49 final List<String> urls = new ArrayList<String>(filteredTabs.size()); |
|
50 for (int i = 0; i < filteredTabs.size(); ++i) { |
|
51 Browser.TabConnector connector = filteredTabs.get(i); |
|
52 map.put(i, connector); |
|
53 urls.add(connector.getUrl()); |
|
54 } |
|
55 final Browser.TabConnector[] result = { null }; |
|
56 Display.getDefault().syncExec(new Runnable() { |
|
57 public void run() { |
|
58 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); |
|
59 final ChromiumTabSelectionDialog dialog = new ChromiumTabSelectionDialog(shell, urls); |
|
60 dialog.setBlockOnOpen(true); |
|
61 int dialogResult = dialog.open(); |
|
62 if (dialogResult == ChromiumTabSelectionDialog.OK) { |
|
63 result[0] = map.get(dialog.getSelectedLine()); |
|
64 } |
|
65 // otherwise (result[0] == null) which means "Do not attach" |
|
66 } |
|
67 }); |
|
68 return result[0]; |
|
69 } |
|
70 |
|
71 private boolean autoSelectSingleTab() { |
|
72 return true; |
|
73 } |
|
74 |
|
75 } |