KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > operations > ReplaceFeatureVersionOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.operations;
12
13 import org.eclipse.core.runtime.*;
14 import org.eclipse.update.core.*;
15 import org.eclipse.update.operations.*;
16
17 /**
18  * Swaps a feature.
19  * ReplaceFeatureVersionOperation
20  */

21 public class ReplaceFeatureVersionOperation
22     extends FeatureOperation
23     implements IConfigFeatureOperation {
24
25     private IFeature anotherFeature;
26     
27     public ReplaceFeatureVersionOperation(
28         IFeature feature,
29         IFeature anotherFeature) {
30         super(feature.getSite().getCurrentConfiguredSite(), feature);
31         this.anotherFeature = anotherFeature;
32     }
33
34     public boolean execute(IProgressMonitor pm, IOperationListener listener)
35         throws CoreException {
36
37         IStatus status =
38             OperationsManager.getValidator().validatePendingReplaceVersion(feature, anotherFeature);
39         if (status != null) {
40             throw new CoreException(status);
41         }
42
43         // unconfigure current feature first, then configure the other one
44

45         PatchCleaner cleaner = new PatchCleaner(targetSite, feature);
46         targetSite.unconfigure(feature);
47         cleaner.dispose();
48         
49         targetSite.configure(anotherFeature);
50 // ensureUnique();
51

52         try {
53             // Restart not needed
54
boolean restartNeeded = false;
55
56             // Check if this operation is cancelling one that's already pending
57
IOperation pendingOperation =
58                 OperationsManager.findPendingOperation(feature);
59
60             if (pendingOperation instanceof IConfigFeatureOperation) {
61                 // no need to do either pending change
62
OperationsManager.removePendingOperation(pendingOperation);
63             } else {
64                 OperationsManager.addPendingOperation(this);
65                 restartNeeded = true;
66             }
67             
68             pendingOperation =
69                 OperationsManager.findPendingOperation(anotherFeature);
70                 
71             if (pendingOperation instanceof IUnconfigFeatureOperation) {
72                 // no need to do either pending change
73
OperationsManager.removePendingOperation(pendingOperation);
74             } else {
75                 OperationsManager.addPendingOperation(this);
76                 restartNeeded = true;
77             }
78             
79             markProcessed();
80             if (listener != null)
81                 listener.afterExecute(this, null);
82
83             restartNeeded = SiteManager.getLocalSite().save() && restartNeeded;
84
85             // notify the model
86
OperationsManager.fireObjectChanged(feature, null);
87
88             return restartNeeded;
89         } catch (CoreException e) {
90             undo();
91             UpdateUtils.logException(e);
92             throw e;
93         }
94     }
95
96     public void undo() throws CoreException {
97         targetSite.unconfigure(anotherFeature);
98         targetSite.configure(feature);
99     }
100 }
101
Popular Tags