1 11 package org.eclipse.update.internal.ui.wizards; 12 13 import java.lang.reflect.*; 14 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.jface.dialogs.*; 17 import org.eclipse.jface.dialogs.Dialog; 18 import org.eclipse.jface.operation.*; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.jface.wizard.*; 21 import org.eclipse.swt.*; 22 import org.eclipse.swt.graphics.*; 23 import org.eclipse.swt.layout.*; 24 import org.eclipse.swt.widgets.*; 25 import org.eclipse.update.core.*; 26 import org.eclipse.update.core.model.*; 27 import org.eclipse.update.internal.ui.*; 28 import org.eclipse.update.operations.*; 29 30 31 public class ReplaceFeatureVersionWizardPage extends WizardPage { 32 33 private IFeature currentFeature; 34 private IFeature[] features; 35 private TableViewer tableViewer; 36 37 public ReplaceFeatureVersionWizardPage(IFeature currentFeature, IFeature[] features) { 38 super("SwapFeature"); setTitle(UpdateUIMessages.SwapFeatureWizardPage_title); 40 setDescription(UpdateUIMessages.SwapFeatureWizardPage_desc); 41 this.currentFeature = currentFeature; 42 this.features = features; 43 } 44 45 public void createControl(Composite parent) { 46 Composite tableContainer = new Composite(parent, SWT.NONE); 47 GridLayout layout = new GridLayout(); 48 layout.marginHeight = layout.marginWidth = 0; 49 tableContainer.setLayout(layout); 50 51 Label label = new Label(tableContainer, SWT.NONE); 52 label.setText(UpdateUIMessages.SwapFeatureWizardPage_label); 53 54 Table table = new Table(tableContainer, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL); 55 table.setLayoutData(new GridData(GridData.FILL_BOTH)); 56 57 tableViewer = new TableViewer(table); 58 tableViewer.setLabelProvider(new LabelProvider() { 59 public Image getImage(Object element) { 60 UpdateLabelProvider provider = 61 UpdateUI.getDefault().getLabelProvider(); 62 return provider.get(UpdateUIImages.DESC_UNCONF_FEATURE_OBJ, 0); 63 } 64 public String getText(Object element) { 65 IFeature feature = (IFeature) element; 66 return feature.getLabel() + " " + feature.getVersionedIdentifier().getVersion().toString(); } 68 }); 69 70 tableViewer.setContentProvider(new IStructuredContentProvider() { 71 public Object [] getElements(Object element) { 72 return features; 73 } 74 public void dispose() { 75 } 76 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 77 } 78 }); 79 80 tableViewer.setSorter(new ViewerSorter() { 81 public int compare(Viewer viewer, Object e1, Object e2) { 82 PluginVersionIdentifier v1 = ((IFeature)e1).getVersionedIdentifier().getVersion(); 83 PluginVersionIdentifier v2 = ((IFeature)e2).getVersionedIdentifier().getVersion(); 84 return v1.isGreaterOrEqualTo(v2) ? -1 : 1; 85 } 86 }); 87 88 tableViewer.addFilter(new ViewerFilter() { 89 public boolean select(Viewer viewer, Object parentElement, Object element) { 90 String version = 91 ((IFeature) element).getVersionedIdentifier().getVersion().toString(); 92 return !version.equals( 93 currentFeature.getVersionedIdentifier().getVersion().toString()); 94 } 95 }); 96 97 tableViewer.addSelectionChangedListener(new ISelectionChangedListener() { 98 public void selectionChanged(SelectionChangedEvent event) { 99 IStructuredSelection ssel = (IStructuredSelection)tableViewer.getSelection(); 100 if (ssel == null) 101 return; 102 IFeature chosenFeature = (IFeature)ssel.getFirstElement(); 103 IStatus validationStatus = 104 OperationsManager.getValidator().validatePendingReplaceVersion(currentFeature, chosenFeature); 105 setPageComplete(validationStatus == null || validationStatus.getCode() == IStatus.WARNING); 106 107 if (validationStatus == null) { 108 setErrorMessage(null); 109 } else if (validationStatus.getCode() == IStatus.WARNING) { 110 setErrorMessage(null); 111 setMessage(validationStatus.getMessage(), IMessageProvider.WARNING); 112 } else { 113 setErrorMessage(validationStatus.getMessage()); 114 } 115 } 116 }); 117 118 tableViewer.setInput(currentFeature); 119 tableViewer.getTable().select(0); 120 setControl(tableContainer); 121 122 Dialog.applyDialogFont(tableContainer); 123 } 124 125 public boolean performFinish() { 126 IStructuredSelection ssel = (IStructuredSelection)tableViewer.getSelection(); 127 IFeature chosenFeature = (IFeature)ssel.getFirstElement(); 128 129 return swap(currentFeature, chosenFeature); 130 } 131 132 private boolean swap(final IFeature currentFeature, final IFeature anotherFeature) { 133 144 IRunnableWithProgress operation = new IRunnableWithProgress() { 145 public void run(IProgressMonitor monitor) 146 throws InvocationTargetException { 147 IOperation revertOperation = 148 OperationsManager 149 .getOperationFactory() 150 .createReplaceFeatureVersionOperation(currentFeature, anotherFeature); 151 try { 152 boolean restartNeeded = revertOperation.execute(monitor, null); 153 UpdateUI.requestRestart(restartNeeded); 154 } catch (CoreException e) { 155 throw new InvocationTargetException(e); 156 } finally { 157 monitor.done(); 158 } 159 } 160 }; 161 try { 162 getContainer().run(false, true, operation); 163 return true; 164 } catch (InvocationTargetException e) { 165 Throwable targetException = e.getTargetException(); 166 if (targetException instanceof InstallAbortedException) { 167 return true; 168 } else { 169 UpdateUI.logException(e); 170 } 171 return false; 172 } catch (InterruptedException e) { 173 return false; 174 } 175 } 176 177 } 178 | Popular Tags |