KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > views > FeaturesStateAction


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.views;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.jface.dialogs.ErrorDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.update.configuration.IConfiguredSite;
21 import org.eclipse.update.core.IFeature;
22 import org.eclipse.update.internal.core.InstallRegistry;
23 import org.eclipse.update.internal.operations.OperationFactory;
24 import org.eclipse.update.internal.operations.UpdateUtils;
25 import org.eclipse.update.internal.ui.UpdateUI;
26 import org.eclipse.update.internal.ui.UpdateUIMessages;
27 import org.eclipse.update.internal.ui.model.ConfiguredFeatureAdapter;
28 import org.eclipse.update.operations.IFeatureOperation;
29 import org.eclipse.update.operations.IOperation;
30 import org.eclipse.update.operations.OperationsManager;
31
32 public class FeaturesStateAction extends FeatureAction {
33
34     
35     private ConfiguredFeatureAdapter[] adapters;
36
37     public FeaturesStateAction(Shell shell, String JavaDoc text) {
38         super(shell, text);
39         setWindowTitle(UpdateUIMessages.FeatureStateAction_dialogTitle);
40     }
41     
42     public void setSelection(IStructuredSelection selection) {
43
44         this.adapters = (ConfiguredFeatureAdapter[]) selection.toList().toArray(new ConfiguredFeatureAdapter[selection.size()]);
45         if (canUnconfigure()) {
46             setText(UpdateUIMessages.FeatureStateAction_disable);
47         } else {
48             setText(UpdateUIMessages.FeatureStateAction_enable);
49         }
50
51     }
52     
53     public void run() {
54         try {
55             if ( (adapters == null) && (adapters.length == 0))
56                 return;
57             
58             IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
59             if (status != null)
60                 throw new CoreException(status);
61             
62             boolean isConfigured = canUnconfigure();
63             // Ask user to confirm the operation
64
String JavaDoc message =
65                 isConfigured
66                     ? UpdateUIMessages.FeatureStateAction_disableQuestion
67                     : UpdateUIMessages.FeatureStateAction_EnableQuestion;
68
69             if (!confirm(message))
70                 return;
71
72             // If current config is broken, confirm with the user to continue
73
if (OperationsManager.getValidator().validateCurrentState() != null &&
74                     !confirm(UpdateUIMessages.Actions_brokenConfigQuestion))
75                 return;
76             
77             IOperation toggleOperation = null;
78             
79             IFeature[] features = new IFeature[adapters.length];
80             IConfiguredSite[] configuredSites = new IConfiguredSite[adapters.length];
81             for( int i = 0; i < adapters.length; i++) {
82                 features[i] = adapters[i].getFeature(null);
83                 configuredSites[i] = adapters[i].getConfiguredSite();
84             }
85             
86             if (isConfigured) {
87                 toggleOperation = ((OperationFactory)OperationsManager.getOperationFactory()).createUnconfigureFeaturesOperation( configuredSites, features);
88             } else if (canConfigure()){
89                 toggleOperation = ((OperationFactory)OperationsManager.getOperationFactory()).createConfigureFeaturesOperation( configuredSites, features);
90             }
91             
92             if (toggleOperation != null) {
93                 boolean restartNeeded = toggleOperation.execute(null, null);
94                 UpdateUI.requestRestart(restartNeeded);
95             }
96
97         } catch (CoreException e) {
98             ErrorDialog.openError(shell,
99                     null, null, e.getStatus());
100         } catch (InvocationTargetException JavaDoc e) {
101             // This should not happen
102
UpdateUtils.logException(e.getTargetException());
103         }
104     }
105     
106     public boolean canExecuteAction() {
107         return canConfigure() || canUnconfigure();
108     }
109     
110     private boolean canConfigure() {
111         
112         if (adapters == null || adapters.length == 0)
113             return false;
114         
115         for( int i = 0; i < adapters.length; i++) {
116             
117             if (adapters[i].isConfigured())
118                 return false;
119         
120             try {
121                 // check for pending changes (e.g. if the feature has just been disabled)
122
IFeatureOperation pendingOperation = OperationsManager.findPendingOperation(adapters[i].getFeature(null));
123                 if (pendingOperation != null)
124                     return false;
125
126                 if (InstallRegistry.getInstance().get("feature_"+adapters[i].getFeature(null).getVersionedIdentifier()) == null) //$NON-NLS-1$
127
return false;
128             } catch (CoreException e) {
129                 return false;
130             }
131         }
132                 
133         return true;
134     }
135     
136     private boolean canUnconfigure() {
137         
138         if (adapters == null || adapters.length == 0)
139             return false;
140         
141         for( int i = 0; i < adapters.length; i++) {
142             
143             if (!adapters[i].isConfigured())
144                 return false;
145         
146             try {
147                 // check for pending changes (e.g. if the feature has just been disabled)
148
IFeatureOperation pendingOperation = OperationsManager.findPendingOperation(adapters[i].getFeature(null));
149                 if (pendingOperation != null)
150                     return false;
151
152                 if (InstallRegistry.getInstance().get("feature_"+adapters[i].getFeature(null).getVersionedIdentifier()) == null) //$NON-NLS-1$
153
return false;
154             } catch (CoreException e) {
155                 return false;
156             }
157         }
158                 
159         return true;
160     }
161     
162
163 }
164
Popular Tags