Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f188a52bf00f176323b65f84097e386326ef3e9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.externaltools.internal.ant.view.actions;


import java.util.Iterator;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.externaltools.internal.ant.launchConfigurations.AntLaunchShortcut;
import org.eclipse.ui.externaltools.internal.ant.model.AntUtil;
import org.eclipse.ui.externaltools.internal.ant.view.AntView;
import org.eclipse.ui.externaltools.internal.ant.view.elements.AntNode;
import org.eclipse.ui.externaltools.internal.ant.view.elements.ProjectNode;
import org.eclipse.ui.externaltools.internal.ant.view.elements.TargetNode;
import org.eclipse.ui.externaltools.internal.model.IExternalToolConstants;
import org.eclipse.ui.externaltools.internal.model.IExternalToolsHelpContextIds;
import org.eclipse.ui.help.WorkbenchHelp;
import org.eclipse.ui.texteditor.IUpdate;

/**
 * Action for activating a target node selected in the ant view
 */
public class EditLaunchConfigurationAction extends Action implements IUpdate {
	
	private AntView view;
	private ProjectNode projectNode;
	
	public EditLaunchConfigurationAction(AntView view) {
		super(AntViewActionMessages.getString("EditLaunchConfigurationAction.Properties")); //$NON-NLS-1$
		setDescription(AntViewActionMessages.getString("EditLaunchConfigurationAction.Edit")); //$NON-NLS-1$
		this.view= view;
		WorkbenchHelp.setHelp(this, IExternalToolsHelpContextIds.EDIT_LAUNCH_CONFIGURATION_ACTION);
	}
	
	public void run() {
		IFile file= AntUtil.getFile(projectNode.getBuildFileName());
		ILaunchConfiguration configuration= null;
		List configurations = AntLaunchShortcut.findExistingLaunchConfigurations(file);
		if (configurations.isEmpty()) {
			configuration = AntLaunchShortcut.createDefaultLaunchConfiguration(file);
		} else {
			if (configurations.size() == 1) {
				configuration= (ILaunchConfiguration)configurations.get(0);
			} else {
				configuration= AntLaunchShortcut.chooseConfig(configurations);
				if (configuration == null) {
					// User cancelled selection
					return;
				}
			}
		}
		if (configuration != null) {
			DebugUITools.openLaunchConfigurationPropertiesDialog(view.getSite().getShell(), configuration, IExternalToolConstants.ID_EXTERNAL_TOOLS_BUILDER_LAUNCH_GROUP);
		}
	}

	/**
	 * Updates the enablement of this action based on the user's selection
	 */
	public void update() {
		boolean enabled= false;
		IStructuredSelection selection= (IStructuredSelection) view.getProjectViewer().getSelection();
		if (selection.size() == 1) {
			Object element= selection.getFirstElement();
			if (element instanceof ProjectNode) {
				if (!((ProjectNode)element).isErrorNode()) {
					enabled= true;
					projectNode= (ProjectNode)element;
				}
			} else if (element instanceof TargetNode) {
				if (!((TargetNode)element).isErrorNode()) {
					enabled= true;
					projectNode= getProjectNode((AntNode)element);
				}
			} else if (element instanceof AntNode) {
				projectNode= getProjectNode((AntNode)element);
				enabled= true;
			}
		} else if (!selection.isEmpty()){
			//all non-error nodes from the same project
			Iterator iter= selection.iterator();
			Object data= null;
			enabled= true;
			ProjectNode tempProjectNode= null;
			while (iter.hasNext()) {
				data= iter.next();
				if (!(data instanceof AntNode)) {
					continue;
				}
				if (data instanceof TargetNode) {
					TargetNode targetNode= (TargetNode) data;
					if(targetNode.isErrorNode()) {
						enabled= false;
						break;
					}
				}
				if (data instanceof ProjectNode) {
					ProjectNode projectNode= (ProjectNode) data;
					if(projectNode.isErrorNode()) {
						enabled= false;
						break;
					}
				}
			
				tempProjectNode= getProjectNode((AntNode)data);
				if (projectNode != null) {
					if (!(projectNode.equals(tempProjectNode))) {
						enabled= false;
						break;
					}
				} else {
					projectNode= tempProjectNode;
				}
			}
		}
		
		if (!enabled) {
			projectNode= null;
		}
		setEnabled(enabled);
	}

	/**
	 * Method getProjectNode.
	 * @param antNode
	 * @return ProjectNode
	 */
	private ProjectNode getProjectNode(AntNode antNode) {
		AntNode parentNode= null;
		if (antNode instanceof ProjectNode) {
			parentNode= antNode;
		} else {
			parentNode= antNode.getParent();
			while (parentNode != null && !(parentNode instanceof ProjectNode)) {
				parentNode= parentNode.getParent();
			}
		}
		return (ProjectNode)parentNode;
	}
}

Back to the top