KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.runtime.*;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.operation.*;
20 import org.eclipse.jface.viewers.*;
21 import org.eclipse.jface.wizard.*;
22 import org.eclipse.swt.*;
23 import org.eclipse.swt.custom.*;
24 import org.eclipse.swt.events.*;
25 import org.eclipse.swt.graphics.*;
26 import org.eclipse.swt.layout.*;
27 import org.eclipse.swt.widgets.*;
28 import org.eclipse.ui.*;
29 import org.eclipse.update.configuration.*;
30 import org.eclipse.update.core.*;
31 import org.eclipse.update.core.model.*;
32 import org.eclipse.update.internal.core.*;
33 import org.eclipse.update.internal.ui.*;
34 import org.eclipse.update.operations.*;
35
36
37 public class RevertConfigurationWizardPage extends WizardPage {
38
39     private TableViewer activitiesViewer;
40     private TableViewer configViewer;
41     public static Color blueBGColor;
42     private SashForm sashForm;
43
44     public RevertConfigurationWizardPage() {
45         super("RevertConfiguration"); //$NON-NLS-1$
46
setTitle(UpdateUIMessages.RevertConfigurationWizardPage_title);
47         setDescription(UpdateUIMessages.RevertConfigurationWizardPage_desc);
48         blueBGColor = new Color(null, 238,238,255);
49         
50     }
51
52     public void createControl(Composite parent) {
53         sashForm = new SashForm(parent, SWT.VERTICAL);
54         sashForm.setLayout(new GridLayout());
55         sashForm.setLayoutData(new GridData(GridData.FILL_BOTH));
56
57         createConfigurationsSection(sashForm);
58         createActivitiesSection(sashForm);
59         setControl(sashForm);
60
61         Object JavaDoc element = configViewer.getElementAt(0);
62         if (element != null)
63             configViewer.setSelection(new StructuredSelection(element));
64         Dialog.applyDialogFont(sashForm);
65     }
66
67     private void createConfigurationsSection(Composite parent) {
68         Composite tableContainer = new Composite(parent, SWT.NONE);
69         GridLayout layout = new GridLayout();
70         layout.marginHeight = layout.marginWidth = 0;
71         tableContainer.setLayout(layout);
72         tableContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
73
74         Label label = new Label(tableContainer, SWT.NONE);
75         label.setText(UpdateUIMessages.RevertConfigurationWizardPage_label);
76
77         Table table = new Table(tableContainer, SWT.BORDER | SWT.V_SCROLL);
78         table.setLayoutData(new GridData(GridData.FILL_BOTH));
79
80         configViewer = new TableViewer(table);
81         configViewer.setLabelProvider(new LabelProvider() {
82             public Image getImage(Object JavaDoc element) {
83                 UpdateLabelProvider provider =
84                     UpdateUI.getDefault().getLabelProvider();
85                 return provider.get(UpdateUIImages.DESC_CONFIG_OBJ, 0);
86             }
87             public String JavaDoc getText(Object JavaDoc element) {
88                 return Utilities.format(
89                     ((IInstallConfiguration) element).getCreationDate());
90             }
91
92         });
93         configViewer.setContentProvider(new IStructuredContentProvider() {
94             public Object JavaDoc[] getElements(Object JavaDoc element) {
95                 ArrayList JavaDoc result = new ArrayList JavaDoc();
96                 ILocalSite localSite = (ILocalSite) element;
97                 IInstallConfiguration current = localSite.getCurrentConfiguration();
98                 IInstallConfiguration[] configurations = localSite.getConfigurationHistory();
99                 for (int i = configurations.length - 1; i >= 0; i--) {
100                     if (!current.equals(configurations[i])) {
101                         result.add(configurations[i]);
102                     }
103                 }
104                 return result.toArray();
105             }
106             
107             public void dispose() {
108             }
109
110             public void inputChanged(
111                 Viewer viewer,
112                 Object JavaDoc oldInput,
113                 Object JavaDoc newInput) {
114             }
115
116         });
117
118         configViewer
119             .addSelectionChangedListener(new ISelectionChangedListener() {
120             public void selectionChanged(SelectionChangedEvent e) {
121                 IStructuredSelection ssel = (IStructuredSelection) e.getSelection();
122                 InstallConfiguration currentConfig = (InstallConfiguration)ssel.getFirstElement();
123                 activitiesViewer.setInput(currentConfig);
124                 activitiesViewer.refresh();
125                 TableItem[] items = activitiesViewer.getTable().getItems();
126                 for (int i =0; i<items.length; i++){
127                     IActivity activity = (IActivity)items[i].getData();
128                     // for now, we test exact config match. If needed, we can also compare dates, etc.
129
if (((InstallConfiguration)activity.getInstallConfiguration()).equals(currentConfig))
130                         items[i].setBackground(blueBGColor);
131                     else
132                         items[i].setBackground(activitiesViewer.getControl().getBackground());
133                 }
134             }
135         });
136
137         try {
138             configViewer.setInput(SiteManager.getLocalSite());
139         } catch (CoreException e1) {
140         }
141     }
142
143     private void createActivitiesSection(Composite parent) {
144         final Composite composite = new Composite(parent, SWT.NONE);
145         GridLayout gridLayout = new GridLayout();
146         gridLayout.marginHeight = gridLayout.marginWidth = 0;
147         composite.setLayout(gridLayout);
148         GridData gd = new GridData(GridData.FILL_BOTH);
149         composite.setLayoutData(gd);
150
151
152
153         Label label = new Label(composite, SWT.NONE);
154         label.setText(
155             UpdateUIMessages.RevertConfigurationWizardPage_activities);
156
157         TableLayoutComposite tlComposite= new TableLayoutComposite(composite, SWT.NONE);
158         tlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
159         activitiesViewer = ActivitiesTableViewer.createViewer(tlComposite, false);
160
161         tlComposite.addColumnData(new ColumnPixelData(16, true, true));
162         tlComposite.addColumnData(new ColumnWeightData(2, 150, true));
163         tlComposite.addColumnData(new ColumnWeightData(5, 200, true));
164         tlComposite.addColumnData(new ColumnWeightData(4, 100, true));
165
166         //activitiesViewer.getTable().setLayout(layout);
167

168         TableItem[] configs = configViewer.getTable().getItems();
169         if (configs.length >0)
170             activitiesViewer.setInput(configs[0].getData());
171         
172         composite.addControlListener(new ControlAdapter() {
173             public void controlResized(ControlEvent e) {
174                 int sashHeight = getSashForm().getSize().y;
175                 int sashWidth = getSashForm().getSize().x;
176                 if (composite.getSize().y > (sashHeight*0.85) && composite.getSize().x > (sashWidth*0.5)){
177                     getSashForm().setOrientation(SWT.HORIZONTAL);
178                 } else {
179                     getSashForm().setOrientation(SWT.VERTICAL);
180                 }
181             }
182         });
183     }
184
185     /* (non-Javadoc)
186      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
187      */

188     public void dispose() {
189         blueBGColor.dispose();
190     }
191     
192     public SashForm getSashForm(){
193         return sashForm;
194     }
195     public boolean performFinish() {
196         Shell shell = getContainer().getShell();
197         boolean result =
198             MessageDialog.openQuestion(
199                 shell,
200                 shell.getText(),
201                 UpdateUIMessages.RevertConfigurationWizardPage_question);
202         if (!result)
203             return false;
204
205         boolean finish = performRevert();
206         if (finish) {
207             PlatformUI.getWorkbench().restart();
208         }
209         return finish;
210     }
211
212     public boolean performRevert() {
213
214         IStructuredSelection ssel =
215             (IStructuredSelection) configViewer.getSelection();
216         final IInstallConfiguration target =
217             (IInstallConfiguration) ssel.getFirstElement();
218
219         IStatus status =
220             OperationsManager.getValidator().validatePendingRevert(target);
221         if (status != null && status.getCode() == IStatus.ERROR) {
222             ErrorDialog.openError(
223                 UpdateUI.getActiveWorkbenchShell(),
224                 null,
225                 null,
226                 status);
227             return false;
228         }
229
230         IRunnableWithProgress operation = new IRunnableWithProgress() {
231             public void run(IProgressMonitor monitor)
232                 throws InvocationTargetException {
233                 IOperation revertOperation =
234                     OperationsManager
235                         .getOperationFactory()
236                         .createRevertConfigurationOperation(
237                         target,
238                         new UIProblemHandler());
239                 try {
240                     revertOperation.execute(monitor, null);
241                 } catch (CoreException e) {
242                     throw new InvocationTargetException(e);
243                 } finally {
244                     monitor.done();
245                 }
246             }
247         };
248         try {
249             getContainer().run(false, true, operation);
250             return true;
251         } catch (InvocationTargetException e) {
252             Throwable JavaDoc targetException = e.getTargetException();
253             if (targetException instanceof InstallAbortedException) {
254                 return true;
255             } else {
256                 UpdateUI.logException(e);
257             }
258             return false;
259         } catch (InterruptedException JavaDoc e) {
260             return false;
261         }
262     }
263 }
264
Popular Tags