-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAngularCLIConfigurationBlock.java
353 lines (309 loc) · 11.8 KB
/
AngularCLIConfigurationBlock.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* Copyright (c) 2015-2016 Angelo ZERR.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package ts.eclipse.ide.angular.internal.cli.preferences;
import java.io.File;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.jface.dialogs.ControlEnableState;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
import ts.eclipse.ide.angular.cli.AngularCLIPlugin;
import ts.eclipse.ide.angular.cli.preferences.AngularCLIPreferenceConstants;
import ts.eclipse.ide.angular.cli.utils.CLIProcessHelper;
import ts.eclipse.ide.angular.cli.utils.CLIStatus;
import ts.eclipse.ide.angular.cli.utils.NgVersionJob;
import ts.eclipse.ide.angular.internal.cli.AngularCLIMessages;
import ts.eclipse.ide.core.utils.TypeScriptResourceUtil;
import ts.eclipse.ide.core.utils.WorkbenchResourceUtil;
import ts.eclipse.ide.ui.preferences.BrowseButtonsComposite;
import ts.eclipse.ide.ui.preferences.OptionsConfigurationBlock;
import ts.eclipse.ide.ui.preferences.ScrolledPageContent;
import ts.eclipse.ide.ui.preferences.StatusInfo;
import ts.eclipse.ide.ui.widgets.IStatusChangeListener;
import ts.utils.FileUtils;
import ts.utils.StringUtils;
/**
* @angular/cli configuration block.
*
*/
public class AngularCLIConfigurationBlock extends OptionsConfigurationBlock {
private ControlEnableState blockEnableState;
private Composite controlsComposite;
private Button ngUseGlobalInstallation;
private Combo ngCustomFilePath;
private BrowseButtonsComposite browseButtons;
private static final String[] DEFAULT_PATHS = new String[] { "${project_loc:node_modules/.bin}" };
private static final Key PREF_NG_USE_GLOBAL_INSTALLATION = getAngularCliKey(
AngularCLIPreferenceConstants.NG_USE_GLOBAL_INSTALLATION);
private static final Key PREF_NG_CUSTOM_FILE_PATH = getAngularCliKey(
AngularCLIPreferenceConstants.NG_CUSTOM_FILE_PATH);
private static final Key PREF_EXECUTE_NG_WITH_FILE = getAngularCliKey(
AngularCLIPreferenceConstants.EXECUTE_NG_WITH_FILE);
private Text cliPath;
private Text cliVersion;
private NgVersionJob ngVersionJob;
public AngularCLIConfigurationBlock(IStatusChangeListener context, IProject project,
IWorkbenchPreferenceContainer container) {
super(context, project, getKeys(), container);
ngVersionJob = new NgVersionJob();
ngVersionJob.setNodeFile(getNodejsPath(project));
ngVersionJob.addJobChangeListener(new JobChangeAdapter() {
@Override
public void done(IJobChangeEvent event) {
IStatus status = event.getResult();
if (!(status instanceof CLIStatus)) {
return;
}
final CLIStatus s = (CLIStatus) status;
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
if (cliVersion.isDisposed()) {
return;
}
if (!StringUtils.isEmpty(s.getVersion())) {
cliVersion.setText(s.getVersion());
} else {
cliPath.setText("");
cliVersion.setText("");
}
fContext.statusChanged(status);
}
});
}
});
blockEnableState = null;
}
/** Gets the NodeJS-File for this Project. */
private File getNodejsPath(IProject project) {
try {
return TypeScriptResourceUtil.getTypeScriptProject(project).getProjectSettings().getNodejsInstallPath();
} catch (Throwable e) {
return null;
}
}
private static Key[] getKeys() {
return new Key[] { PREF_NG_USE_GLOBAL_INSTALLATION, PREF_NG_CUSTOM_FILE_PATH, PREF_EXECUTE_NG_WITH_FILE };
}
protected final static Key getAngularCliKey(String key) {
return getKey(AngularCLIPlugin.PLUGIN_ID, key);
}
@Override
protected Composite createUI(Composite parent) {
final ScrolledPageContent pageContent = new ScrolledPageContent(parent);
Composite composite = pageContent.getBody();
GridLayout layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
composite.setLayout(layout);
controlsComposite = new Composite(composite, SWT.NONE);
controlsComposite.setFont(composite.getFont());
controlsComposite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
layout = new GridLayout();
layout.marginHeight = 0;
layout.marginWidth = 0;
layout.numColumns = 1;
controlsComposite.setLayout(layout);
createBody(controlsComposite);
createCLIInfo(composite);
return pageContent;
}
private void createBody(Composite parent) {
int nColumns = 2;
GridLayout layout = new GridLayout();
layout.numColumns = nColumns;
Group group = new Group(parent, SWT.NONE);
group.setFont(parent.getFont());
group.setText(AngularCLIMessages.AngularCLIConfigurationBlock_cli_group_label);
group.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
group.setLayout(layout);
createNgUseGlobalInstallation(group);
createNgUseCustomFile(group);
createExecuteNgWithFile(group);
updateComboBoxes();
}
private void createNgUseGlobalInstallation(Composite parent) {
// Create "Use ng global installation" checkbox
ngUseGlobalInstallation = addRadioBox(parent,
AngularCLIMessages.AngularCLIConfigurationBlock_ngUseGlobalInstallation_label,
PREF_NG_USE_GLOBAL_INSTALLATION, new String[] { "true", "true" }, 1);
ngUseGlobalInstallation.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateComboBoxes();
}
});
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gd.horizontalSpan = 2;
ngUseGlobalInstallation.setLayoutData(gd);
}
private void createNgUseCustomFile(Composite parent) {
// Create "Use ng custom file" field
Button ngUseCustomFile = addRadioBox(parent,
AngularCLIMessages.AngularCLIConfigurationBlock_ngUseCustomFile_label, PREF_NG_USE_GLOBAL_INSTALLATION,
new String[] { "false", "false" }, 0);
ngUseCustomFile.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateComboBoxes();
}
});
ngCustomFilePath = newComboControl(parent, PREF_NG_CUSTOM_FILE_PATH, getDefaultPaths(), getDefaultPaths(),
false);
ngCustomFilePath.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
// Create Browse buttons.
browseButtons = new BrowseButtonsComposite(parent, ngCustomFilePath, getProject(), SWT.NONE);
}
private void createExecuteNgWithFile(Composite parent) {
// Create "Execute Ng With File" checkbox
addCheckBox(parent, AngularCLIMessages.AngularCLIConfigurationBlock_executeNgWithFile_label,
PREF_EXECUTE_NG_WITH_FILE, new String[] { "true", "false" }, 0);
}
private void createCLIInfo(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout());
GridData gridData = new GridData(GridData.FILL_BOTH);
composite.setLayoutData(gridData);
// CLI version
Label cliVersionTitle = new Label(composite, SWT.NONE);
cliVersionTitle.setText(AngularCLIMessages.AngularCLIConfigurationBlock_cliVersion_label);
gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
cliVersionTitle.setLayoutData(gridData);
cliVersion = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
cliVersion.setText(""); //$NON-NLS-1$
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 2;
gridData.widthHint = 200;
cliVersion.setLayoutData(gridData);
// CLI path
Label cliPathTitle = new Label(composite, SWT.NONE);
cliPathTitle.setText(AngularCLIMessages.AngularCLIConfigurationBlock_cliPath_label);
gridData = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
cliPathTitle.setLayoutData(gridData);
cliPath = new Text(composite, SWT.WRAP | SWT.READ_ONLY);
cliPath.setText(""); //$NON-NLS-1$
gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
gridData.widthHint = 200;
cliPath.setLayoutData(gridData);
}
private String[] getDefaultPaths() {
return DEFAULT_PATHS;
}
@Override
protected void validateSettings(Key changedKey, String oldValue, String newValue) {
cliVersion.setText("");
cliPath.setText("");
ngVersionJob.cancel();
IStatus status = validateCLIPath();
if (status.isOK() && status instanceof CLIStatus) {
File ngFile = ((CLIStatus)status).getNgFile();
cliPath.setText(FileUtils.getPath(ngFile));
cliVersion.setText("Executing 'ng --version'...");
ngVersionJob.setNgFile(ngFile);
ngVersionJob.schedule();
} else {
fContext.statusChanged(status);
}
}
/**
* Returns the status of the ng path.
*
* @return the status of the ng path.
*/
private IStatus validateCLIPath() {
File ngFile = null;
boolean useGlobal = ngUseGlobalInstallation.getSelection();
boolean globalPrefs = getProject() == null;
if (useGlobal) {
ngFile = CLIProcessHelper.findNg();
if (ngFile == null) {
// ERROR: ng was not installed with "npm install @angular/cli
// -g"
return new CLIStatus(null, AngularCLIMessages.AngularCLIConfigurationBlock_ngGlobal_notFound_error);
}
} else if (!globalPrefs) {
String ngPath = ngCustomFilePath.getText();
if (StringUtils.isEmpty(ngPath)) {
// ERROR: the installed path is empty
return new CLIStatus(null, AngularCLIMessages.AngularCLIConfigurationBlock_ngCustomFile_required_error);
} else {
ngFile = WorkbenchResourceUtil.resolvePath(ngPath, getProject());
if (!ngFile.isDirectory()) {
// ERROR: the ng path must be a directory which contains the
// ng/ng.cmd
return new CLIStatus(null,
NLS.bind(AngularCLIMessages.AngularCLIConfigurationBlock_ngCustomFile_notDir_error,
FileUtils.getPath(ngFile)));
}
ngFile = new File(ngFile, CLIProcessHelper.getNgFileName());
}
}
if (!globalPrefs) {
if (!ngFile.exists()) {
// ERROR: ng file doesn't exists
return new CLIStatus(null,
NLS.bind(AngularCLIMessages.AngularCLIConfigurationBlock_ngCustomFile_exists_error,
FileUtils.getPath(ngFile)));
}
}
else {
return StatusInfo.OK_STATUS;
}
// ng path is valid
return new CLIStatus(ngFile, null);
}
@Override
protected String[] getFullBuildDialogStrings(boolean workspaceSettings) {
return null;
}
public void enablePreferenceContent(boolean enable) {
if (controlsComposite != null && !controlsComposite.isDisposed()) {
if (enable) {
if (blockEnableState != null) {
blockEnableState.restore();
blockEnableState = null;
}
} else {
if (blockEnableState == null) {
blockEnableState = ControlEnableState.disable(controlsComposite);
}
}
}
}
private void updateComboBoxes() {
boolean global = ngUseGlobalInstallation.getSelection();
ngCustomFilePath.setEnabled(!global);
browseButtons.setEnabled(!global);
}
@Override
public void dispose() {
super.dispose();
if (ngVersionJob != null) {
ngVersionJob.cancel();
ngVersionJob = null;
}
}
}