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