|
1 /** |
|
2 * Copyright (c) 2010 Symbian Foundation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Symbian Foundation - initial contribution. |
|
11 * Contributors: |
|
12 * Description: |
|
13 * Overview: |
|
14 * Details: |
|
15 * Platforms/Drives/Compatibility: |
|
16 * Assumptions/Requirement/Pre-requisites: |
|
17 * Failures and causes: |
|
18 */ |
|
19 package org.symbian.tools.tmw.internal.deployment; |
|
20 |
|
21 import java.lang.reflect.InvocationTargetException; |
|
22 import java.text.MessageFormat; |
|
23 import java.util.HashMap; |
|
24 import java.util.HashSet; |
|
25 import java.util.Map; |
|
26 import java.util.Set; |
|
27 |
|
28 import org.eclipse.core.runtime.CoreException; |
|
29 import org.eclipse.core.runtime.IProgressMonitor; |
|
30 import org.eclipse.core.runtime.IStatus; |
|
31 import org.eclipse.core.runtime.SubProgressMonitor; |
|
32 import org.eclipse.jface.operation.IRunnableWithProgress; |
|
33 import org.eclipse.jface.util.Policy; |
|
34 import org.eclipse.jface.viewers.ArrayContentProvider; |
|
35 import org.eclipse.jface.viewers.DoubleClickEvent; |
|
36 import org.eclipse.jface.viewers.IDoubleClickListener; |
|
37 import org.eclipse.jface.viewers.ISelection; |
|
38 import org.eclipse.jface.viewers.ISelectionChangedListener; |
|
39 import org.eclipse.jface.viewers.IStructuredSelection; |
|
40 import org.eclipse.jface.viewers.SelectionChangedEvent; |
|
41 import org.eclipse.jface.viewers.StructuredSelection; |
|
42 import org.eclipse.jface.viewers.TableViewer; |
|
43 import org.eclipse.jface.viewers.ViewerSorter; |
|
44 import org.eclipse.jface.wizard.IWizardContainer; |
|
45 import org.eclipse.jface.wizard.WizardDialog; |
|
46 import org.eclipse.jface.wizard.WizardPage; |
|
47 import org.eclipse.swt.SWT; |
|
48 import org.eclipse.swt.events.SelectionAdapter; |
|
49 import org.eclipse.swt.events.SelectionEvent; |
|
50 import org.eclipse.swt.layout.FormAttachment; |
|
51 import org.eclipse.swt.layout.FormData; |
|
52 import org.eclipse.swt.layout.FormLayout; |
|
53 import org.eclipse.swt.widgets.Button; |
|
54 import org.eclipse.swt.widgets.Composite; |
|
55 import org.eclipse.swt.widgets.Control; |
|
56 import org.eclipse.ui.model.WorkbenchLabelProvider; |
|
57 import org.eclipse.ui.part.PageBook; |
|
58 import org.symbian.tools.tmw.core.TMWCore; |
|
59 import org.symbian.tools.tmw.core.runtimes.IPackager; |
|
60 import org.symbian.tools.tmw.ui.TMWCoreUI; |
|
61 import org.symbian.tools.tmw.ui.ProjectMemo; |
|
62 import org.symbian.tools.tmw.ui.deployment.IDeploymentTarget; |
|
63 import org.symbian.tools.tmw.ui.deployment.IDeploymentTargetType; |
|
64 import org.symbian.tools.tmw.ui.deployment.ITargetDetailsPane; |
|
65 |
|
66 public class DeploymentTargetWizardPage extends WizardPage implements ITargetDetailsPane.Context { |
|
67 private final DeployWizardContext context; |
|
68 private PageBook descriptions; |
|
69 private Control emptyness; |
|
70 private final Set<IDeploymentTarget> inited = new HashSet<IDeploymentTarget>(); |
|
71 private TableViewer list; |
|
72 private final Map<IDeploymentTargetType, ITargetDetailsPane> panes = new HashMap<IDeploymentTargetType, ITargetDetailsPane>(); |
|
73 private final IDeploymentTarget prev; |
|
74 |
|
75 public DeploymentTargetWizardPage(DeployWizardContext context, ProjectMemo memo) { |
|
76 super("TargetPage", "Select Deployment Target", null); |
|
77 this.context = context; |
|
78 prev = memo.getPreviousDeploymentTarget(); |
|
79 setDescription(MessageFormat.format("Select emulator or device to deploy the application ''{0}''", context |
|
80 .getProject().getProject().getName())); |
|
81 } |
|
82 |
|
83 public void createControl(Composite parent) { |
|
84 Composite root = new Composite(parent, SWT.NONE); |
|
85 |
|
86 root.setLayout(new FormLayout()); |
|
87 |
|
88 list = new TableViewer(root, SWT.BORDER); |
|
89 list.setContentProvider(new ArrayContentProvider()); |
|
90 list.setLabelProvider(new WorkbenchLabelProvider()); |
|
91 list.setSorter(new ViewerSorter() { |
|
92 @Override |
|
93 public int category(Object element) { |
|
94 return ((DeploymentTargetWrapper) element).getCategory(); |
|
95 } |
|
96 }); |
|
97 list.addSelectionChangedListener(new ISelectionChangedListener() { |
|
98 public void selectionChanged(SelectionChangedEvent event) { |
|
99 IStructuredSelection selection = ((IStructuredSelection) event.getSelection()); |
|
100 DeploymentTargetWrapper target = (DeploymentTargetWrapper) selection.getFirstElement(); |
|
101 selectDeploymentTarget(target); |
|
102 } |
|
103 }); |
|
104 list.addDoubleClickListener(new IDoubleClickListener() { |
|
105 public void doubleClick(DoubleClickEvent event) { |
|
106 if (getWizard().performFinish()) { |
|
107 IWizardContainer container = getWizard().getContainer(); |
|
108 if (container instanceof WizardDialog) { |
|
109 ((WizardDialog) container).close(); |
|
110 } |
|
111 } |
|
112 } |
|
113 }); |
|
114 final Button search = new Button(root, SWT.NONE); |
|
115 search.addSelectionListener(new SelectionAdapter() { |
|
116 @Override |
|
117 public void widgetSelected(SelectionEvent e) { |
|
118 doBluetoothSearch(search); |
|
119 } |
|
120 }); |
|
121 search.setText("Discover"); |
|
122 search.setImage(TMWCoreUI.getImages().getDiscoverButtonIcon()); |
|
123 |
|
124 descriptions = new PageBook(root, SWT.BORDER); |
|
125 emptyness = new Composite(descriptions, SWT.NONE); |
|
126 |
|
127 FormData data = new FormData(); |
|
128 data.left = new FormAttachment(0, 5); |
|
129 data.right = new FormAttachment(100, -5); |
|
130 data.bottom = new FormAttachment(100, -5); |
|
131 data.height = 80; |
|
132 descriptions.setLayoutData(data); |
|
133 |
|
134 data = new FormData(); |
|
135 data.top = new FormAttachment(0, 5); |
|
136 data.right = new FormAttachment(100, -5); |
|
137 search.setLayoutData(data); |
|
138 |
|
139 data = new FormData(); |
|
140 data.left = new FormAttachment(0, 5); |
|
141 data.top = new FormAttachment(0, 5); |
|
142 data.bottom = new FormAttachment(descriptions, -10); |
|
143 data.right = new FormAttachment(search, -10); |
|
144 |
|
145 list.getControl().setLayoutData(data); |
|
146 list.setInput(context.getDeploymentTargets()); |
|
147 setPageComplete(false); |
|
148 |
|
149 if (prev != null) { |
|
150 list.setSelection(new StructuredSelection(prev)); |
|
151 } |
|
152 setControl(root); |
|
153 } |
|
154 |
|
155 protected void doBluetoothSearch(final Button search) { |
|
156 try { |
|
157 final ISelection sel = list.getSelection(); |
|
158 getContainer().run(true, true, new IRunnableWithProgress() { |
|
159 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
|
160 monitor.beginTask("Searching for Bluetooth devices", IProgressMonitor.UNKNOWN); |
|
161 try { |
|
162 context.doSearch(new SubProgressMonitor(monitor, 100)); |
|
163 } catch (CoreException e) { |
|
164 Policy.getStatusHandler().show(e.getStatus(), "Application Deployment"); |
|
165 } |
|
166 monitor.done(); |
|
167 search.getDisplay().asyncExec(new Runnable() { |
|
168 public void run() { |
|
169 list.setInput(context.getDeploymentTargets()); |
|
170 if (!sel.isEmpty()) { |
|
171 list.setSelection(sel); |
|
172 selectDeploymentTarget((DeploymentTargetWrapper) ((IStructuredSelection) sel) |
|
173 .getFirstElement()); |
|
174 } else { |
|
175 DeploymentTargetWrapper[] deploymentTargets = context.getDeploymentTargets(); |
|
176 if (deploymentTargets.length == 0) { |
|
177 selectDeploymentTarget(null); |
|
178 } else { |
|
179 list.setSelection(new StructuredSelection(deploymentTargets[0])); |
|
180 selectDeploymentTarget(deploymentTargets[0]); |
|
181 } |
|
182 } |
|
183 } |
|
184 }); |
|
185 } |
|
186 }); |
|
187 } catch (InvocationTargetException e) { |
|
188 TMWCoreUI.log(e); |
|
189 } catch (InterruptedException e) { |
|
190 TMWCoreUI.log(e); |
|
191 } |
|
192 } |
|
193 |
|
194 private IPackager getPackager() { |
|
195 return TMWCore.getDefault().getRuntimesManager().getPackager(context.getProject()); |
|
196 } |
|
197 |
|
198 protected void selectDeploymentTarget(DeploymentTargetWrapper target) { |
|
199 if (target != null) { |
|
200 if (!inited.contains(target)) { |
|
201 target.init(context.getProject(), getPackager(), |
|
202 TMWCoreUI.getMemo(context.getProject()).getMemo(target.getType().getId(), target)); |
|
203 inited.add(target); |
|
204 } |
|
205 context.setTarget(target); |
|
206 |
|
207 IDeploymentTargetType type = target.getType(); |
|
208 ITargetDetailsPane pane = panes.get(type); |
|
209 if (pane == null) { |
|
210 pane = TMWCoreUI.getDefault().getPresentations().createDetailsPane(type); |
|
211 pane.createControl(descriptions); |
|
212 pane.init(this); |
|
213 panes.put(type, pane); |
|
214 } |
|
215 pane.setTarget(target.getActualTarget()); |
|
216 descriptions.showPage(pane.getControl()); |
|
217 } else { |
|
218 descriptions.showPage(emptyness); |
|
219 context.setTarget(null); |
|
220 setPageComplete(false); |
|
221 setMessage(null); |
|
222 } |
|
223 revalidate(); |
|
224 } |
|
225 |
|
226 protected void toggleLogging(boolean logging) { |
|
227 context.setLogging(logging); |
|
228 } |
|
229 |
|
230 public void revalidate() { |
|
231 String error = null; |
|
232 String warning = !context.areTargetsReady() ? "Press \"Discover\" to find more deployment targets" : null; |
|
233 if (context.getTarget() == null) { |
|
234 error = "Select device or emulator to deploy the application"; |
|
235 } else { |
|
236 final IStatus validate = panes.get(context.getTarget().getType()).validate(); |
|
237 if (validate.getSeverity() == IStatus.ERROR) { |
|
238 error = validate.getMessage(); |
|
239 } else if (validate.getSeverity() == IStatus.WARNING) { |
|
240 warning = validate.getMessage(); |
|
241 } |
|
242 } |
|
243 setErrorMessage(error); |
|
244 setMessage(warning, IStatus.WARNING); |
|
245 setPageComplete(error == null); |
|
246 } |
|
247 |
|
248 } |