KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > wizards > ReplaceFeatureVersionWizardPage


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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.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"); //$NON-NLS-1$
39
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 JavaDoc element) {
60                 UpdateLabelProvider provider =
61                     UpdateUI.getDefault().getLabelProvider();
62                 return provider.get(UpdateUIImages.DESC_UNCONF_FEATURE_OBJ, 0);
63             }
64             public String JavaDoc getText(Object JavaDoc element) {
65                 IFeature feature = (IFeature) element;
66                 return feature.getLabel() + " " + feature.getVersionedIdentifier().getVersion().toString(); //$NON-NLS-1$
67
}
68         });
69         
70         tableViewer.setContentProvider(new IStructuredContentProvider() {
71             public Object JavaDoc[] getElements(Object JavaDoc element) {
72                 return features;
73             }
74             public void dispose() {
75             }
76             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
77             }
78         });
79         
80         tableViewer.setSorter(new ViewerSorter() {
81             public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc 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 JavaDoc parentElement, Object JavaDoc element) {
90                 String JavaDoc 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 // IStatus status =
134
// OperationsManager.getValidator().validatePendingReplaceVersion(currentFeature, anotherFeature);
135
// if (status != null) {
136
// ErrorDialog.openError(
137
// UpdateUI.getActiveWorkbenchShell(),
138
// null,
139
// null,
140
// status);
141
// return false;
142
// }
143

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 JavaDoc targetException = e.getTargetException();
166             if (targetException instanceof InstallAbortedException) {
167                 return true;
168             } else {
169                 UpdateUI.logException(e);
170             }
171             return false;
172         } catch (InterruptedException JavaDoc e) {
173             return false;
174         }
175     }
176
177 }
178
Popular Tags