KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > views > actions > RunTargetAction


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.ant.internal.ui.views.actions;
12
13 import java.util.Iterator JavaDoc;
14 import org.eclipse.ant.internal.ui.AntUIImages;
15 import org.eclipse.ant.internal.ui.IAntUIConstants;
16 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds;
17 import org.eclipse.ant.internal.ui.launchConfigurations.AntLaunchShortcut;
18 import org.eclipse.ant.internal.ui.model.AntElementNode;
19 import org.eclipse.ant.internal.ui.model.AntProjectNode;
20 import org.eclipse.ant.internal.ui.model.AntTargetNode;
21 import org.eclipse.ant.internal.ui.model.AntTaskNode;
22 import org.eclipse.ant.internal.ui.views.AntView;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.debug.core.ILaunchManager;
27 import org.eclipse.jface.action.Action;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.progress.UIJob;
31 import org.eclipse.ui.texteditor.IUpdate;
32
33 /**
34  * Action which runs the selected target or the default target of the selected
35  * project in the AntView.
36  */

37 public class RunTargetAction extends Action implements IUpdate {
38     
39     private AntView fView;
40     
41     /**
42      * Creates a new <code>RunTargetAction</code> which will execute
43      * targets in the given view.
44      * @param view the Ant view whose selection this action will use when
45      * determining which target to run.
46      */

47     public RunTargetAction(AntView view) {
48         
49         setText(AntViewActionMessages.RunTargetAction_Run_1);
50         setImageDescriptor(AntUIImages.getImageDescriptor(IAntUIConstants.IMG_RUN));
51         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IAntUIHelpContextIds.RUN_TARGET_ACTION);
52
53         setToolTipText(AntViewActionMessages.RunTargetAction_3);
54         fView= view;
55     }
56
57     /**
58      * Executes the appropriate target based on the selection in the Ant view.
59      */

60     public void run() {
61         run(getSelectedElement());
62     }
63     
64     /**
65      * @param selectedElement The element to use as the context for launching
66      */

67     public void run(final AntElementNode selectedElement) {
68         UIJob job= new UIJob(AntViewActionMessages.RunTargetAction_2) {
69             public IStatus runInUIThread(IProgressMonitor monitor) {
70                 launch(selectedElement);
71                 return Status.OK_STATUS;
72             }
73         };
74         job.schedule();
75     }
76
77     /**
78      * Launches the given Ant element node
79      * @param node the node to use to launch
80      * @see AntLaunchShortcut#launch(AntElementNode, String)
81      */

82     public void launch(AntElementNode node) {
83         AntLaunchShortcut shortcut= new AntLaunchShortcut();
84         shortcut.setShowDialog(false);
85         shortcut.launch(node, ILaunchManager.RUN_MODE);
86     }
87
88     /**
89      * Updates the enablement of this action based on the user's selection
90      */

91     public void update() {
92         AntElementNode selection= getSelectedElement();
93         boolean enabled= false;
94         if (selection instanceof AntTargetNode) {
95             if (!((AntTargetNode) selection).isErrorNode()) {
96                 setToolTipText(AntViewActionMessages.RunTargetAction_4);
97                 enabled= true;
98             }
99         } else if (selection instanceof AntProjectNode) {
100             if (!((AntProjectNode) selection).isErrorNode()) {
101                 enabled= true;
102                 setToolTipText(AntViewActionMessages.RunTargetAction_3);
103             }
104         } else if (selection instanceof AntTaskNode) {
105             if (!((AntTaskNode) selection).isErrorNode()) {
106                 enabled= true;
107                 setToolTipText(AntViewActionMessages.RunTargetAction_0);
108             }
109         }
110         setEnabled(enabled);
111     }
112     
113     /**
114      * Returns the selected node or <code>null</code> if more than one element is selected.
115      *
116      * @return AntElementNode the selected node
117      */

118     private AntElementNode getSelectedElement() {
119         IStructuredSelection selection= (IStructuredSelection) fView.getViewer().getSelection();
120         if (selection.isEmpty()) {
121             return null;
122         }
123         Iterator JavaDoc iter= selection.iterator();
124         Object JavaDoc data= iter.next();
125         if (iter.hasNext()) {
126             return null;
127         }
128         return (AntElementNode) data;
129     }
130 }
131
Popular Tags