KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.action.*;
17 import org.eclipse.jface.dialogs.*;
18 import org.eclipse.osgi.util.NLS;
19 import org.eclipse.swt.widgets.*;
20 import org.eclipse.update.configuration.*;
21 import org.eclipse.update.internal.ui.*;
22 import org.eclipse.update.operations.*;
23
24 /**
25  * Toggles a site's enabled state
26  */

27
28 public class SiteStateAction extends Action {
29     private IConfiguredSite site;
30     private Shell shell;
31
32     public SiteStateAction(Shell shell) {
33         this.shell = shell;
34     }
35
36     public void setSite(IConfiguredSite site) {
37         this.site = site;
38         boolean state = site.isEnabled();
39         setText(state ? UpdateUIMessages.SiteStateAction_disableLabel : UpdateUIMessages.SiteStateAction_enableLabel);
40     }
41
42     public void run() {
43         try {
44             if (site == null)
45                 return;
46             
47             IStatus status = OperationsManager.getValidator().validatePlatformConfigValid();
48             if (status != null) {
49                 ErrorDialog.openError(shell, null, null, status);
50                 return;
51             }
52             
53             boolean oldValue = site.isEnabled();
54             if (!confirm(!oldValue))
55                 return;
56             
57             IOperation toggleSiteOperation = OperationsManager.getOperationFactory().createToggleSiteOperation(site);
58             boolean restartNeeded = toggleSiteOperation.execute(null, null);
59                     
60             UpdateUI.requestRestart(restartNeeded);
61
62         } catch (CoreException e) {
63             ErrorDialog.openError(shell, null, null, e.getStatus());
64         } catch (InvocationTargetException e) {
65             UpdateUI.logException(e);
66         }
67     }
68
69     private boolean confirm(boolean newState) {
70         String JavaDoc name = site.getSite().getURL().toString();
71         String JavaDoc enableMessage = NLS.bind(UpdateUIMessages.SiteStateAction_enableMessage, name);
72         String JavaDoc disableMessage = NLS.bind(UpdateUIMessages.SiteStateAction_disableMessage, name);
73
74         String JavaDoc message = newState ? enableMessage : disableMessage;
75         return MessageDialog.openConfirm(shell, UpdateUIMessages.SiteStateAction_dialogTitle, message);
76     }
77 }
78
Popular Tags