Commit | Line | Data |
---|---|---|
d04ec5a7 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2013 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace; | |
14 | ||
15 | import java.lang.reflect.InvocationTargetException; | |
16 | import java.util.ArrayList; | |
17 | import java.util.List; | |
18 | ||
19 | import org.eclipse.core.runtime.IProgressMonitor; | |
20 | import org.eclipse.core.runtime.SubMonitor; | |
21 | import org.eclipse.jface.operation.IRunnableWithProgress; | |
22 | import org.eclipse.jface.wizard.IWizard; | |
23 | import org.eclipse.jface.wizard.IWizardPage; | |
24 | import org.eclipse.jface.wizard.ProgressMonitorPart; | |
25 | import org.eclipse.jface.wizard.WizardDialog; | |
26 | import org.eclipse.swt.layout.GridLayout; | |
27 | import org.eclipse.swt.widgets.Composite; | |
28 | import org.eclipse.swt.widgets.Shell; | |
29 | ||
30 | /** | |
31 | * Non-modal wizard allows background jobs to work in tandem with modal jobs. | |
32 | * | |
33 | * @author Matthew Khouzam | |
34 | * @since 2.0 | |
35 | */ | |
36 | public class NonModalWizardDialog extends WizardDialog { | |
37 | ||
38 | private ProgressMonitorPart fProgressMonitor; | |
39 | private IWizardPage fWizardPage; | |
40 | private List<HelperThread> fHelpers = new ArrayList<HelperThread>(); | |
41 | ||
42 | /** | |
43 | * Creates a new non modal wizard dialog for the given wizard. | |
44 | * | |
45 | * @param parentShell | |
46 | * the parent shell | |
47 | * @param newWizard | |
48 | * the wizard this dialog is working on | |
49 | */ | |
50 | public NonModalWizardDialog(Shell parentShell, IWizard newWizard) { | |
51 | super(parentShell, newWizard); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Copy constructor (be careful) | |
56 | * | |
57 | * @param wiz | |
58 | * the Wizard to clone | |
59 | */ | |
60 | public NonModalWizardDialog(WizardDialog wiz) { | |
61 | super(wiz.getShell(), wiz.getCurrentPage().getWizard()); | |
62 | } | |
63 | ||
64 | @Override | |
65 | public boolean close() { | |
66 | for(HelperThread t : fHelpers){ | |
67 | t.interrupt(); | |
68 | } | |
69 | for(HelperThread t : fHelpers){ | |
70 | try { | |
71 | t.join(100); | |
72 | } catch (InterruptedException e) { | |
73 | } | |
74 | } | |
75 | return super.close(); | |
76 | } | |
77 | /** | |
78 | * Gets the progress monitor, can be null | |
79 | * | |
80 | * @return a progress monitor that can be null | |
81 | */ | |
82 | public IProgressMonitor getMonitor() { | |
83 | return super.getProgressMonitor(); | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void updateButtons() { | |
88 | super.updateButtons(); | |
89 | if (fProgressMonitor != null) { | |
90 | fProgressMonitor.setVisible(this.getCurrentPage().equals(fWizardPage)); | |
91 | fProgressMonitor.getMonitor(); | |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * Background job to run | |
97 | * | |
98 | * @param pageForJob | |
99 | * the page to display the job on | |
100 | * @param runnable | |
101 | * the runnable that is a background job | |
102 | */ | |
103 | public void backgroundRun(IWizardPage pageForJob, IRunnableWithProgress runnable) { | |
104 | // create progress monitor; | |
105 | fWizardPage = pageForJob; | |
106 | IProgressMonitor x = getProgressMonitor(); | |
107 | SubMonitor y = SubMonitor.convert(x); | |
108 | // make a new thread with the runnable | |
109 | HelperThread h = new HelperThread(runnable, y); | |
110 | ||
111 | h.start(); | |
112 | fHelpers.add(h); | |
113 | ||
114 | } | |
115 | ||
116 | @Override | |
117 | protected ProgressMonitorPart createProgressMonitorPart(Composite composite, GridLayout pmlayout) { | |
118 | fProgressMonitor = new ProgressMonitorPart(composite, pmlayout); | |
119 | return fProgressMonitor; | |
120 | } | |
121 | ||
122 | private class HelperThread extends Thread { | |
123 | private IRunnableWithProgress runnable; | |
124 | private IProgressMonitor monitor; | |
125 | ||
126 | public HelperThread(IRunnableWithProgress i, IProgressMonitor s) { | |
127 | runnable = i; | |
128 | monitor = s; | |
129 | } | |
130 | ||
131 | @Override | |
132 | public void run() { | |
133 | try { | |
134 | runnable.run(monitor); | |
135 | } catch (InvocationTargetException e) { | |
136 | // skip over me | |
137 | } catch (InterruptedException e) { | |
138 | // skip over me | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | } |