]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.project/src/org/simantics/project/management/install/SWTInstallAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.project / src / org / simantics / project / management / install / SWTInstallAdvisor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2008 IBM Corporation and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     IBM Corporation - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.project.management.install;\r
12 \r
13 import org.eclipse.core.runtime.IStatus;\r
14 import org.eclipse.equinox.internal.provisional.p2.installer.IInstallOperation;\r
15 import org.eclipse.equinox.internal.provisional.p2.installer.InstallAdvisor;\r
16 import org.eclipse.equinox.internal.provisional.p2.installer.InstallDescription;\r
17 import org.eclipse.swt.widgets.Display;\r
18 \r
19 /**\r
20  * Install context that creates a simple SWT-based UI and interacts with a user.\r
21  */\r
22 public class SWTInstallAdvisor extends InstallAdvisor {\r
23         private InstallDialog dialog;\r
24         private boolean started = false;\r
25         private boolean stopped = false;\r
26 \r
27         public IStatus performInstall(IInstallOperation operation) {\r
28                 return dialog.run(operation);\r
29         }\r
30 \r
31         public InstallDescription prepareInstallDescription(InstallDescription description) {\r
32                 if (description.getInstallLocation() == null)\r
33                         dialog.promptForLocations(description);\r
34                 return description;\r
35         }\r
36 \r
37         public boolean promptForLaunch(InstallDescription description) {\r
38                 return dialog.promptForLaunch(description);\r
39         }\r
40 \r
41         public void setResult(IStatus status) {\r
42                 String message;\r
43                 if (status.getSeverity() == IStatus.CANCEL) {\r
44                         message = "Canceled";\r
45                 } else {\r
46                         message = status.getMessage();\r
47                 }\r
48                 dialog.promptForClose(message);\r
49         }\r
50 \r
51         public synchronized void start() {\r
52                 if (stopped || started)\r
53                         return;\r
54                 started = true;\r
55                 Display display = Display.getCurrent();\r
56                 if (display == null)\r
57                         display = new Display();\r
58                 dialog = new InstallDialog();\r
59                 dialog.setMessage("Preparing");\r
60         }\r
61 \r
62         public synchronized void stop() {\r
63                 if (stopped || !started)\r
64                         return;\r
65                 stopped = true;\r
66                 final InstallDialog activeDialog = dialog;\r
67                 if (activeDialog == null)\r
68                         return;\r
69                 //clear the window now, so the reference is gone no matter what happens during cleanup\r
70                 dialog = null;\r
71                 final Display display = activeDialog.getDisplay();\r
72                 if (display == null || display.isDisposed())\r
73                         return;\r
74                 display.syncExec(new Runnable() {\r
75                         public void run() {\r
76                                 activeDialog.close();\r
77                         }\r
78                 });\r
79                 display.dispose();\r
80         }\r
81 \r
82 }\r