KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.internal.operations.UpdateUtils;
21 import org.eclipse.update.internal.ui.UpdateUI;
22 import org.eclipse.update.internal.ui.UpdateUIMessages;
23 import org.eclipse.update.internal.ui.model.ConfiguredFeatureAdapter;
24 import org.eclipse.update.operations.IOperation;
25 import org.eclipse.update.operations.OperationsManager;
26
27 public class FeatureStateAction extends FeatureAction {
28
29     private ConfiguredFeatureAdapter adapter;
30
31     public FeatureStateAction(Shell shell, String JavaDoc text) {
32         super(shell, text);
33         setWindowTitle(UpdateUIMessages.FeatureStateAction_dialogTitle);
34     }
35     
36     public void setSelection(IStructuredSelection selection) {
37         
38         this.adapter = (ConfiguredFeatureAdapter) selection.getFirstElement();
39         if (adapter.isConfigured()) {
40             setText(UpdateUIMessages.FeatureStateAction_disable);
41         } else {
42             setText(UpdateUIMessages.FeatureStateAction_enable);
43         }
44     }
45
46     public void run() {
47         try {
48             if (adapter == null)
49                 return;
50             
51             IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
52             if (status != null)
53                 throw new CoreException(status);
54             
55             boolean isConfigured = adapter.isConfigured();
56             // Ask user to confirm the operation
57
String JavaDoc message =
58                 isConfigured
59                     ? UpdateUIMessages.FeatureStateAction_disableQuestion
60                     : UpdateUIMessages.FeatureStateAction_EnableQuestion;
61
62             if (!confirm(message))
63                 return;
64
65             // If current config is broken, confirm with the user to continue
66
if (OperationsManager.getValidator().validateCurrentState() != null &&
67                     !confirm(UpdateUIMessages.Actions_brokenConfigQuestion))
68                 return;
69             
70             IOperation toggleOperation =
71                 isConfigured
72                     ? (IOperation)OperationsManager
73                         .getOperationFactory()
74                         .createUnconfigOperation(
75                         adapter.getConfiguredSite(),
76                         adapter.getFeature(null))
77                     : OperationsManager
78                         .getOperationFactory()
79                         .createConfigOperation(
80                         adapter.getConfiguredSite(),
81                         adapter.getFeature(null));
82
83             boolean restartNeeded = toggleOperation.execute(null, null);
84             UpdateUI.requestRestart(restartNeeded);
85
86         } catch (CoreException e) {
87             ErrorDialog.openError(shell,
88                     null, null, e.getStatus());
89         } catch (InvocationTargetException JavaDoc e) {
90             // This should not happen
91
UpdateUtils.logException(e.getTargetException());
92         }
93     }
94     
95     public boolean canExecuteAction() {
96         return true;
97     }
98
99 }
100
Popular Tags