Skip to content

Commit 195c995

Browse files
committed
[#1668] simplify work with launch attributes
* identify launch attribute * connect it with preference metadata (to supply defaults/label/description) * read attribute from configuration * write attribute to configuration working copy * demonstrate usage for ExternalTools
1 parent dc18b24 commit 195c995

File tree

11 files changed

+416
-144
lines changed

11 files changed

+416
-144
lines changed

ant/org.eclipse.ant.ui/Ant Tools Support/org/eclipse/ant/internal/ui/launchConfigurations/AntMainTab.java

+25-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2013 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,9 +10,12 @@
1010
*
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
13+
* Alexander Fedorov (ArSysOp) - https://github.com/eclipse-platform/eclipse.platform/issues/1668
1314
*******************************************************************************/
1415
package org.eclipse.ant.internal.ui.launchConfigurations;
1516

17+
import java.util.Optional;
18+
1619
import org.eclipse.ant.internal.core.IAntCoreConstants;
1720
import org.eclipse.ant.internal.ui.AntUIPlugin;
1821
import org.eclipse.ant.internal.ui.AntUtil;
@@ -24,7 +27,6 @@
2427
import org.eclipse.core.resources.IResource;
2528
import org.eclipse.core.resources.ResourcesPlugin;
2629
import org.eclipse.core.runtime.CoreException;
27-
import org.eclipse.core.variables.IStringVariableManager;
2830
import org.eclipse.core.variables.VariablesPlugin;
2931
import org.eclipse.debug.core.ILaunchConfiguration;
3032
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
@@ -45,44 +47,27 @@
4547

4648
public class AntMainTab extends ExternalToolsMainTab {
4749

48-
private String fCurrentLocation = null;
50+
private Optional<String> fCurrentLocation;
4951
private Button fSetInputHandlerButton;
5052
private IFile fNewFile;
5153

5254
@Override
5355
public void initializeFrom(ILaunchConfiguration configuration) {
5456
super.initializeFrom(configuration);
55-
try {
56-
fCurrentLocation = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
57-
}
58-
catch (CoreException e) {
59-
// do nothing
60-
}
57+
fCurrentLocation = IExternalToolConstants.LAUNCH_ATTRIBUTE_LOCATION.read(configuration);
6158
updateCheckButtons(configuration);
6259
}
6360

6461
@Override
6562
public void performApply(ILaunchConfigurationWorkingCopy configuration) {
6663
super.performApply(configuration);
67-
try {
68-
// has the location changed
69-
String newLocation = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
70-
if (newLocation != null) {
71-
if (!newLocation.equals(fCurrentLocation)) {
72-
updateTargetsTab();
73-
fCurrentLocation = newLocation;
74-
updateProjectName(configuration);
75-
}
76-
} else if (fCurrentLocation != null) {
77-
updateTargetsTab();
78-
fCurrentLocation = newLocation;
79-
updateProjectName(configuration);
80-
}
81-
}
82-
catch (CoreException e) {
83-
// do nothing
64+
// has the location changed
65+
Optional<String> newLocation = IExternalToolConstants.LAUNCH_ATTRIBUTE_LOCATION.read(configuration);
66+
if (!newLocation.equals(fCurrentLocation)) {
67+
updateTargetsTab();
68+
fCurrentLocation = newLocation;
69+
updateProjectName(configuration);
8470
}
85-
8671
setMappedResources(configuration);
8772
setAttribute(IAntUIConstants.SET_INPUTHANDLER, configuration, fSetInputHandlerButton.getSelection(), true);
8873
}
@@ -110,26 +95,21 @@ private void updateProjectName(ILaunchConfigurationWorkingCopy configuration) {
11095
}
11196

11297
private IFile getIFile(ILaunchConfigurationWorkingCopy configuration) {
113-
IFile file = null;
11498
if (fNewFile != null) {
115-
file = fNewFile;
99+
IFile file = fNewFile;
116100
fNewFile = null;
117-
} else {
118-
IStringVariableManager manager = VariablesPlugin.getDefault().getStringVariableManager();
119-
try {
120-
String location = configuration.getAttribute(IExternalToolConstants.ATTR_LOCATION, (String) null);
121-
if (location != null) {
122-
String expandedLocation = manager.performStringSubstitution(location);
123-
if (expandedLocation != null) {
124-
file = AntUtil.getFileForLocation(expandedLocation, null);
125-
}
126-
}
127-
}
128-
catch (CoreException e) {
129-
// do nothing
130-
}
101+
return file;
102+
}
103+
return IExternalToolConstants.LAUNCH_ATTRIBUTE_LOCATION.read(configuration).flatMap(this::resolve).map(exp -> AntUtil.getFileForLocation(exp, null)).orElse(null);
104+
}
105+
106+
private Optional<String> resolve(String location) {
107+
try {
108+
return Optional.of(VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(location));
109+
}
110+
catch (CoreException e) {
111+
return Optional.empty();
131112
}
132-
return file;
133113
}
134114

135115
@Override
@@ -153,7 +133,7 @@ public void createControl(Composite parent) {
153133

154134
/**
155135
* Creates the controls needed to edit the set input handler attribute of an Ant build
156-
*
136+
*
157137
* @param parent
158138
* the composite to create the controls in
159139
*/

debug/org.eclipse.core.externaltools/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Bundle-ManifestVersion: 2
33
Bundle-Name: %pluginName
44
Bundle-Localization: plugin
55
Bundle-SymbolicName: org.eclipse.core.externaltools;singleton:=true
6-
Bundle-Version: 1.3.400.qualifier
6+
Bundle-Version: 1.3.500.qualifier
77
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.29.0,4.0.0)",
88
org.eclipse.debug.core;bundle-version="[3.9.0,4.0.0)",
99
org.eclipse.core.variables;bundle-version="[3.2.800,4.0.0)"

debug/org.eclipse.core.externaltools/src/org/eclipse/core/externaltools/internal/IExternalToolConstants.java

+97-44
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2013 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -11,9 +11,16 @@
1111
* Contributors:
1212
* IBM Corporation - initial API and implementation
1313
* [email protected] - bug 165371
14+
* Alexander Fedorov (ArSysOp) - https://github.com/eclipse-platform/eclipse.platform/issues/1668
1415
*******************************************************************************/
1516

1617
package org.eclipse.core.externaltools.internal;
18+
19+
import org.eclipse.core.externaltools.internal.launchConfigurations.ExternalToolsProgramMessages;
20+
import org.eclipse.core.runtime.preferences.PreferenceMetadata;
21+
import org.eclipse.debug.core.ILaunchAttribute;
22+
import org.eclipse.debug.core.ILaunchAttributeIdentity;
23+
1724
/**
1825
* Defines the constants available for client use.
1926
* <p>
@@ -30,71 +37,77 @@ public interface IExternalToolConstants {
3037
String EMPTY_STRING = ""; //$NON-NLS-1$
3138

3239
/**
33-
* Plug-in identifier for external tools UI (value <code>org.eclipse.ui.externaltools</code>).
40+
* Plug-in identifier for external tools UI (value
41+
* <code>org.eclipse.ui.externaltools</code>).
3442
*/
35-
String UI_PLUGIN_ID = "org.eclipse.ui.externaltools"; //$NON-NLS-1$;
43+
String UI_PLUGIN_ID = "org.eclipse.ui.externaltools"; //$NON-NLS-1$ ;
3644

3745
/**
38-
* Plug-in identifier for external tools core (value <code>org.eclipse.core.externaltools</code>).
46+
* Plug-in identifier for external tools core (value
47+
* <code>org.eclipse.core.externaltools</code>).
3948
*/
40-
String PLUGIN_ID = "org.eclipse.core.externaltools"; //$NON-NLS-1$;
49+
String PLUGIN_ID = "org.eclipse.core.externaltools"; //$NON-NLS-1$ ;
4150

4251
// ------- Refresh Variables -------
4352
/**
44-
* Variable that expands to the workspace root object (value <code>workspace</code>).
53+
* Variable that expands to the workspace root object (value
54+
* <code>workspace</code>).
4555
*/
4656
String VAR_WORKSPACE = "workspace"; //$NON-NLS-1$
4757
/**
48-
* Variable that expands to the project resource (value <code>project</code>).
58+
* Variable that expands to the project resource (value
59+
* <code>project</code>).
4960
*/
5061
String VAR_PROJECT = "project"; //$NON-NLS-1$
5162
/**
52-
* Variable that expands to the container resource (value <code>container</code>).
63+
* Variable that expands to the container resource (value
64+
* <code>container</code>).
5365
*/
5466
String VAR_CONTAINER = "container"; //$NON-NLS-1$
5567
/**
5668
* Variable that expands to a resource (value <code>resource</code>).
5769
*/
5870
String VAR_RESOURCE = "resource"; //$NON-NLS-1$
5971
/**
60-
* Variable that expands to the working set object (value <code>working_set</code>).
72+
* Variable that expands to the working set object (value
73+
* <code>working_set</code>).
6174
*/
6275
String VAR_WORKING_SET = "working_set"; //$NON-NLS-1$
6376
// ------- Tool Types -------
6477
/**
65-
* External tool type for programs such as executables, batch files,
66-
* shell scripts, etc (value <code>programType</code>).
78+
* External tool type for programs such as executables, batch files, shell
79+
* scripts, etc (value <code>programType</code>).
6780
*/
68-
String TOOL_TYPE_PROGRAM = "programType"; //$NON-NLS-1$;
81+
String TOOL_TYPE_PROGRAM = "programType"; //$NON-NLS-1$ ;
6982

7083
// ------- Build Types -------
7184
/**
72-
* Build type indicating an incremental project build request for
73-
* the external tool running as a builder (value <code>incremental</code>).
85+
* Build type indicating an incremental project build request for the
86+
* external tool running as a builder (value <code>incremental</code>).
7487
*/
7588
String BUILD_TYPE_INCREMENTAL = "incremental"; //$NON-NLS-1$
7689

7790
/**
78-
* Build type indicating a full project build request for
79-
* the external tool running as a builder (value <code>full</code>).
91+
* Build type indicating a full project build request for the external tool
92+
* running as a builder (value <code>full</code>).
8093
*/
8194
String BUILD_TYPE_FULL = "full"; //$NON-NLS-1$
8295

8396
/**
84-
* Build type indicating an automatic project build request for
85-
* the external tool running as a builder (value <code>auto</code>).
97+
* Build type indicating an automatic project build request for the external
98+
* tool running as a builder (value <code>auto</code>).
8699
*/
87100
String BUILD_TYPE_AUTO = "auto"; //$NON-NLS-1$
88101

89102
/**
90-
* Build type indicating a clean project build request for
91-
* the external tool running as a builder (value <code>clean</code>).
103+
* Build type indicating a clean project build request for the external tool
104+
* running as a builder (value <code>clean</code>).
92105
*/
93106
String BUILD_TYPE_CLEAN = "clean"; //$NON-NLS-1$
94107

95108
/**
96-
* Build type indicating no project build request for
97-
* the external tool running as a builder (value <code>none</code>).
109+
* Build type indicating no project build request for the external tool
110+
* running as a builder (value <code>none</code>).
98111
*/
99112
String BUILD_TYPE_NONE = "none"; //$NON-NLS-1$
100113

@@ -129,32 +142,35 @@ public interface IExternalToolConstants {
129142
/**
130143
* Boolean attribute indicating if external tool output should be captured.
131144
* Default value is <code>false</code>.
132-
* @deprecated since 3.1 Replaced by <code>org.eclipse.debug.core.DebugPlugin.ATTR_CAPTURE_OUTPUT</code>
145+
*
146+
* @deprecated since 3.1 Replaced by
147+
* <code>org.eclipse.debug.core.DebugPlugin.ATTR_CAPTURE_OUTPUT</code>
133148
*/
134-
@Deprecated String ATTR_CAPTURE_OUTPUT = UI_PLUGIN_ID + ".ATTR_CAPTURE_OUTPUT"; //$NON-NLS-1$
149+
@Deprecated
150+
String ATTR_CAPTURE_OUTPUT = UI_PLUGIN_ID + ".ATTR_CAPTURE_OUTPUT"; //$NON-NLS-1$
135151
/**
136152
* String attribute identifying the location of an external. Default value
137153
* is <code>null</code>. Encoding is tool specific.
138154
*/
139155
String ATTR_LOCATION = UI_PLUGIN_ID + ".ATTR_LOCATION"; //$NON-NLS-1$
140156

141157
/**
142-
* Boolean attribute indicating if the user should be prompted for
143-
* arguments before running a tool. Default value is <code>false</code>.
144-
* THIS ATTRIBUTE IS NOT USED.
158+
* Boolean attribute indicating if the user should be prompted for arguments
159+
* before running a tool. Default value is <code>false</code>. THIS
160+
* ATTRIBUTE IS NOT USED.
145161
*/
146162
String ATTR_PROMPT_FOR_ARGUMENTS = UI_PLUGIN_ID + ".ATTR_PROMPT_FOR_ARGUMENTS"; //$NON-NLS-1$
147163

148164
/**
149-
* String attribute identifying the scope of resources that should trigger an
150-
* external tool to run. Default value is <code>null</code>
151-
* indicating that the builder will be triggered for all changes.
165+
* String attribute identifying the scope of resources that should trigger
166+
* an external tool to run. Default value is <code>null</code> indicating
167+
* that the builder will be triggered for all changes.
152168
*/
153169
String ATTR_BUILDER_SCOPE = UI_PLUGIN_ID + ".ATTR_BUILD_SCOPE"; //$NON-NLS-1$
154170

155171
/**
156-
* String attribute containing an array of build kinds for which an
157-
* external tool builder should be run.
172+
* String attribute containing an array of build kinds for which an external
173+
* tool builder should be run.
158174
*/
159175
String ATTR_RUN_BUILD_KINDS = UI_PLUGIN_ID + ".ATTR_RUN_BUILD_KINDS"; //$NON-NLS-1$
160176

@@ -178,16 +194,17 @@ public interface IExternalToolConstants {
178194
String ATTR_WORKING_DIRECTORY = UI_PLUGIN_ID + ".ATTR_WORKING_DIRECTORY"; //$NON-NLS-1$
179195

180196
/**
181-
* String attribute identifying whether an external tool builder configuration
182-
* is enabled. The default value is <code>true</code>, which indicates
183-
* that the configuration will be executed as appropriate by the builder.
197+
* String attribute identifying whether an external tool builder
198+
* configuration is enabled. The default value is <code>true</code>, which
199+
* indicates that the configuration will be executed as appropriate by the
200+
* builder.
184201
*/
185202
String ATTR_BUILDER_ENABLED = UI_PLUGIN_ID + ".ATTR_BUILDER_ENABLED"; //$NON-NLS-1$
186203

187204
/**
188-
* Boolean attribute identifying whether an external tool launcher should execute
189-
* synchronously (value <code>false</code>) or asynchronously (value <code>true</code>).
190-
* Default value is
205+
* Boolean attribute identifying whether an external tool launcher should
206+
* execute synchronously (value <code>false</code>) or asynchronously (value
207+
* <code>true</code>). Default value is
191208
*/
192209
String ATTR_LAUNCH_IN_BACKGROUND = "org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND"; //$NON-NLS-1$
193210

@@ -197,17 +214,20 @@ public interface IExternalToolConstants {
197214
int ERR_INTERNAL_ERROR = 150;
198215

199216
/**
200-
* String attribute identifying a non-external tool builder launch configuration that is disabled
201-
* The value is the name of the disabled builder.
217+
* String attribute identifying a non-external tool builder launch
218+
* configuration that is disabled The value is the name of the disabled
219+
* builder.
202220
*/
203-
String ATTR_DISABLED_BUILDER = UI_PLUGIN_ID + ".ATTR_DISABLED_BUILDER"; //$NON-NLS-1$
221+
String ATTR_DISABLED_BUILDER = UI_PLUGIN_ID + ".ATTR_DISABLED_BUILDER"; //$NON-NLS-1$
204222

205223
/**
206-
* boolean attribute identifying that an external tool builder has been configured for triggering
207-
* using the <code>ICommand.setBuilding(int)</code> mechanism
224+
* boolean attribute identifying that an external tool builder has been
225+
* configured for triggering using the
226+
* <code>ICommand.setBuilding(int)</code> mechanism
227+
*
208228
* @since 3.1
209229
*/
210-
String ATTR_TRIGGERS_CONFIGURED = UI_PLUGIN_ID + ".ATTR_TRIGGERS_CONFIGURED"; //$NON-NLS-1$
230+
String ATTR_TRIGGERS_CONFIGURED = UI_PLUGIN_ID + ".ATTR_TRIGGERS_CONFIGURED"; //$NON-NLS-1$
211231

212232
/**
213233
* String attribute identifying the build scope for a launch configuration.
@@ -221,4 +241,37 @@ public interface IExternalToolConstants {
221241
* <code>true</code>.
222242
*/
223243
String ATTR_INCLUDE_REFERENCED_PROJECTS = UI_PLUGIN_ID + ".ATTR_INCLUDE_REFERENCED_PROJECTS"; //$NON-NLS-1$
244+
245+
/**
246+
* {@link ILaunchAttribute<String>} identifying the location of an external.
247+
* Default value is <code>null</code>. Encoding is tool specific.
248+
*/
249+
ILaunchAttribute<String> LAUNCH_ATTRIBUTE_LOCATION = ILaunchAttribute.DEFAULT.create(//
250+
new PreferenceMetadata<>(String.class, //
251+
ILaunchAttributeIdentity.DEFAULT.create(ATTR_LOCATION).id(), //
252+
(String) null, // unspecified by default
253+
ExternalToolsProgramMessages.LaunchAttributeLocation_name));
254+
255+
/**
256+
* {@link ILaunchAttribute<String>} identifying the working directory of an
257+
* external tool. Default value is <code>null</code>, which indicates a
258+
* default working directory, which is tool specific.
259+
*/
260+
ILaunchAttribute<String> LAUNCH_ATTRIBUTE_WORKING_DIRECTORY = ILaunchAttribute.DEFAULT.create(//
261+
new PreferenceMetadata<>(String.class, //
262+
ILaunchAttributeIdentity.DEFAULT.create(ATTR_WORKING_DIRECTORY).id(), //
263+
(String) null, // unspecified by default
264+
ExternalToolsProgramMessages.LaunchAttributeWorkingDirectory_name));
265+
266+
/**
267+
* {@link ILaunchAttribute<String>} containing the arguments that should be
268+
* passed to the tool. Default value is <code>null</code>, and encoding is
269+
* tool specific.
270+
*/
271+
ILaunchAttribute<String> LAUNCH_ATTRIBUTE_ARGUMENTS = ILaunchAttribute.DEFAULT.create(//
272+
new PreferenceMetadata<>(String.class, //
273+
ILaunchAttributeIdentity.DEFAULT.create(ATTR_TOOL_ARGUMENTS).id(), //
274+
(String) null, // unspecified by default
275+
ExternalToolsProgramMessages.LaunchAttributeArguments_name));
276+
224277
}

0 commit comments

Comments
 (0)