package org.eclipse.update.internal.ui.wizards; | |
/* | |
* (c) Copyright IBM Corp. 2000, 2001. | |
* All Rights Reserved. | |
*/ | |
import java.lang.reflect.InvocationTargetException; | |
import java.text.DateFormat; | |
import org.eclipse.core.runtime.*; | |
import org.eclipse.jface.operation.IRunnableWithProgress; | |
import org.eclipse.jface.wizard.IWizardPage; | |
import org.eclipse.jface.wizard.Wizard; | |
import org.eclipse.update.configuration.*; | |
import org.eclipse.update.core.*; | |
import org.eclipse.update.internal.ui.UpdateUIPlugin; | |
import org.eclipse.update.internal.ui.UpdateUIPluginImages; | |
import org.eclipse.update.internal.ui.model.PendingChange; | |
import org.eclipse.update.internal.ui.model.UpdateModel; | |
import org.eclipse.update.internal.ui.security.JarVerificationService; | |
public class InstallWizard extends Wizard { | |
private ReviewPage reviewPage; | |
private TargetPage targetPage; | |
private PendingChange job; | |
private boolean successfulInstall = false; | |
private IInstallConfiguration config; | |
public InstallWizard(PendingChange job) { | |
setDialogSettings(UpdateUIPlugin.getDefault().getDialogSettings()); | |
setDefaultPageImageDescriptor(UpdateUIPluginImages.DESC_INSTALL_WIZ); | |
setForcePreviousAndNextButtons(true); | |
setNeedsProgressMonitor(true); | |
this.job = job; | |
} | |
public boolean isSuccessfulInstall() { | |
return successfulInstall; | |
} | |
private boolean hasLicense() { | |
IFeature feature = job.getFeature(); | |
IURLEntry info = feature.getLicense(); | |
if (info == null) | |
return false; | |
return info.getAnnotation() != null && info.getAnnotation().length() > 0; | |
} | |
/** | |
* @see Wizard#performFinish() | |
*/ | |
public boolean performFinish() { | |
final IConfiguredSite targetSite = | |
(targetPage == null) ? null : targetPage.getTargetSite(); | |
IRunnableWithProgress operation = new IRunnableWithProgress() { | |
public void run(IProgressMonitor monitor) { | |
try { | |
successfulInstall = false; | |
makeConfigurationCurrent(); | |
execute(targetSite, monitor); | |
saveLocalSite(); | |
successfulInstall = true; | |
} catch (CoreException e) { | |
UpdateUIPlugin.logException(e); | |
} finally { | |
monitor.done(); | |
} | |
} | |
}; | |
try { | |
getContainer().run(false, true, operation); | |
} catch (InvocationTargetException e) { | |
UpdateUIPlugin.logException(e); | |
return false; | |
} catch (InterruptedException e) { | |
return false; | |
} | |
return true; | |
} | |
public void addPages() { | |
reviewPage = new ReviewPage(job); | |
addPage(reviewPage); | |
config = createInstallConfiguration(); | |
if (job.getJobType() == PendingChange.INSTALL) { | |
if (hasLicense()) { | |
addPage(new LicensePage(job)); | |
} | |
targetPage = new TargetPage(job.getFeature(), config); | |
addPage(targetPage); | |
} | |
} | |
private IInstallConfiguration createInstallConfiguration() { | |
try { | |
ILocalSite localSite = SiteManager.getLocalSite(); | |
IInstallConfiguration config = localSite.cloneCurrentConfiguration(); | |
config.setLabel(Utilities.format(config.getCreationDate())); | |
return config; | |
} catch (CoreException e) { | |
UpdateUIPlugin.logException(e); | |
return null; | |
} | |
} | |
private void makeConfigurationCurrent() throws CoreException { | |
ILocalSite localSite = SiteManager.getLocalSite(); | |
localSite.addConfiguration(config); | |
} | |
private void saveLocalSite() throws CoreException { | |
ILocalSite localSite = SiteManager.getLocalSite(); | |
localSite.save(); | |
} | |
public boolean canFinish() { | |
IWizardPage page = getContainer().getCurrentPage(); | |
return page.getNextPage() == null && super.canFinish(); | |
} | |
public IWizardPage getPreviousPage(IWizardPage page) { | |
return super.getPreviousPage(page); | |
} | |
public IWizardPage getNextPage(IWizardPage page) { | |
return super.getNextPage(page); | |
} | |
/* | |
* When we are uninstalling, there is not targetSite | |
*/ | |
private void execute(IConfiguredSite targetSite, IProgressMonitor monitor) | |
throws CoreException { | |
IFeature feature = job.getFeature(); | |
if (job.getJobType() == PendingChange.UNINSTALL) { | |
//find the config site of this feature | |
IConfiguredSite site = findConfigSite(feature); | |
if (site != null) { | |
site.remove(feature, monitor); | |
} else { | |
// we should do something here | |
String message = "Unable to locate configuration site for the feature"; | |
IStatus status = | |
new Status( | |
IStatus.ERROR, | |
UpdateUIPlugin.getPluginId(), | |
IStatus.OK, | |
message, | |
null); | |
throw new CoreException(status); | |
} | |
} else if (job.getJobType() == PendingChange.INSTALL) { | |
IFeature oldFeature = job.getOldFeature(); | |
boolean success = true; | |
if (oldFeature != null) { | |
success = unconfigure(oldFeature); | |
} | |
if (success) | |
targetSite.install(feature, getVerificationListener(), monitor); | |
else { | |
// FIXME should also throw error | |
return; | |
} | |
} else if (job.getJobType() == PendingChange.CONFIGURE) { | |
configure(job.getFeature()); | |
} else if (job.getJobType() == PendingChange.UNCONFIGURE) { | |
unconfigure(job.getFeature()); | |
} else { | |
return; | |
} | |
UpdateModel model = UpdateUIPlugin.getDefault().getUpdateModel(); | |
model.addPendingChange(job); | |
} | |
private IConfiguredSite findConfigSite(IFeature feature) throws CoreException { | |
ILocalSite localSite = SiteManager.getLocalSite(); | |
IConfiguredSite[] configSite = | |
localSite.getCurrentConfiguration().getConfiguredSites(); | |
for (int i = 0; i < configSite.length; i++) { | |
IConfiguredSite site = configSite[i]; | |
if (site.getSite().equals(feature.getSite())) { | |
return site; | |
} | |
} | |
return null; | |
} | |
private boolean unconfigure(IFeature feature) throws CoreException { | |
IConfiguredSite site = findConfigSite(feature); | |
if (site != null) { | |
return site.unconfigure(feature); | |
} | |
return false; | |
} | |
private void configure(IFeature feature) throws CoreException { | |
IConfiguredSite site = findConfigSite(feature); | |
if (site != null) { | |
site.configure(feature); | |
} | |
} | |
private IVerificationListener getVerificationListener() { | |
return new JarVerificationService(this.getShell()); | |
} | |
} |