11
11
*/
12
12
package ts .eclipse .ide .internal .ui .wizards ;
13
13
14
+ import java .io .File ;
15
+
14
16
import org .eclipse .core .resources .IResource ;
17
+ import org .eclipse .core .runtime .IStatus ;
18
+ import org .eclipse .core .runtime .Status ;
15
19
import org .eclipse .jface .resource .JFaceResources ;
16
20
import org .eclipse .jface .viewers .ArrayContentProvider ;
17
21
import org .eclipse .jface .viewers .ComboViewer ;
18
22
import org .eclipse .jface .window .Window ;
23
+ import org .eclipse .osgi .util .NLS ;
19
24
import org .eclipse .swt .SWT ;
20
25
import org .eclipse .swt .events .SelectionAdapter ;
21
26
import org .eclipse .swt .events .SelectionEvent ;
32
37
import ts .eclipse .ide .core .TypeScriptCorePlugin ;
33
38
import ts .eclipse .ide .core .nodejs .IDENodejsProcessHelper ;
34
39
import ts .eclipse .ide .core .nodejs .IEmbeddedNodejs ;
40
+ import ts .eclipse .ide .core .utils .WorkbenchResourceUtil ;
35
41
import ts .eclipse .ide .internal .ui .TypeScriptUIMessages ;
36
42
import ts .eclipse .ide .internal .ui .dialogs .WorkspaceResourceSelectionDialog ;
37
43
import ts .eclipse .ide .internal .ui .dialogs .WorkspaceResourceSelectionDialog .Mode ;
44
+ import ts .eclipse .ide .ui .preferences .StatusInfo ;
38
45
import ts .eclipse .ide .ui .widgets .NPMInstallWidget ;
39
46
import ts .eclipse .ide .ui .wizards .AbstractWizardPage ;
47
+ import ts .nodejs .NodejsProcessHelper ;
40
48
import ts .repository .ITypeScriptRepository ;
49
+ import ts .utils .FileUtils ;
41
50
import ts .utils .StringUtils ;
42
51
43
52
public class TypeScriptRuntimeAndNodejsWizardPage extends AbstractWizardPage {
@@ -112,6 +121,7 @@ private void createEmbeddedTypeScriptField(Composite parent) {
112
121
useEmbeddedTsRuntimeButton = new Button (parent , SWT .RADIO );
113
122
useEmbeddedTsRuntimeButton
114
123
.setText (TypeScriptUIMessages .TypeScriptRuntimeAndNodejsWizardPage_useEmbeddedTsRuntime_label );
124
+ useEmbeddedTsRuntimeButton .addListener (SWT .Selection , this );
115
125
useEmbeddedTsRuntimeButton .addSelectionListener (new SelectionAdapter () {
116
126
@ Override
117
127
public void widgetSelected (SelectionEvent e ) {
@@ -133,13 +143,15 @@ private void createInstallScriptField(Composite parent) {
133
143
Button useInstallTsRuntime = new Button (parent , SWT .RADIO );
134
144
useInstallTsRuntime
135
145
.setText (TypeScriptUIMessages .TypeScriptRuntimeAndNodejsWizardPage_useInstallTsRuntime_label );
146
+ useInstallTsRuntime .addListener (SWT .Selection , this );
136
147
useInstallTsRuntime .addSelectionListener (new SelectionAdapter () {
137
148
@ Override
138
149
public void widgetSelected (SelectionEvent e ) {
139
150
updateTsRuntimeMode ();
140
151
}
141
152
});
142
153
installTsRuntime = new NPMInstallWidget ("typescript" , this , parent , SWT .NONE );
154
+ installTsRuntime .getVersionText ().addListener (SWT .Modify , this );
143
155
}
144
156
145
157
private void updateTsRuntimeMode () {
@@ -148,6 +160,13 @@ private void updateTsRuntimeMode() {
148
160
installTsRuntime .setEnabled (!useEmbeddedTsRuntime );
149
161
}
150
162
163
+ private IStatus validateTypeScriptRuntime () {
164
+ if (useEmbeddedTsRuntimeButton .getSelection ()) {
165
+ return Status .OK_STATUS ;
166
+ }
167
+ return installTsRuntime .getStatus ();
168
+ }
169
+
151
170
// ------------------- Node.js content
152
171
153
172
private void createNodejsBody (Composite parent ) {
@@ -173,6 +192,7 @@ private void createEmbeddedNodejsField(Composite parent) {
173
192
useEmbeddedNodeJsButton = new Button (parent , SWT .RADIO );
174
193
useEmbeddedNodeJsButton
175
194
.setText (TypeScriptUIMessages .TypeScriptRuntimeAndNodejsWizardPage_useEmbeddedNodeJs_label );
195
+ useEmbeddedNodeJsButton .addListener (SWT .Selection , this );
176
196
useEmbeddedNodeJsButton .addSelectionListener (new SelectionAdapter () {
177
197
@ Override
178
198
public void widgetSelected (SelectionEvent e ) {
@@ -195,27 +215,22 @@ public void widgetSelected(SelectionEvent e) {
195
215
}
196
216
embeddedNodeJs .setItems (valueLabels );
197
217
embeddedNodeJs .setFont (JFaceResources .getDialogFont ());
198
-
218
+ embeddedNodeJs . addListener ( SWT . Modify , this );
199
219
}
200
220
201
221
private void createInstalledNodejsField (Composite parent ) {
202
222
Button useInstalledNodejs = new Button (parent , SWT .RADIO );
203
223
useInstalledNodejs .setText (TypeScriptUIMessages .TypeScriptRuntimeAndNodejsWizardPage_useInstalledNodeJs_label );
204
- useInstalledNodejs .addSelectionListener (new SelectionAdapter () {
205
- @ Override
206
- public void widgetSelected (SelectionEvent e ) {
207
- updateNodeJsMode ();
208
- }
209
- });
224
+ useInstalledNodejs .addListener (SWT .Selection , this );
210
225
211
226
String [] defaultPaths = IDENodejsProcessHelper .getAvailableNodejsPaths ();
212
227
installedNodeJs = new Combo (parent , SWT .NONE );
213
228
installedNodeJs .setLayoutData (new GridData (GridData .FILL_HORIZONTAL ));
214
229
installedNodeJs .setItems (defaultPaths );
230
+ installedNodeJs .addListener (SWT .Modify , this );
215
231
216
232
// Create Browse buttons.
217
233
createBrowseButtons (parent , installedNodeJs );
218
-
219
234
}
220
235
221
236
protected void createBrowseButtons (final Composite parent , final Combo filePathCombo ) {
@@ -250,8 +265,8 @@ public void widgetSelected(SelectionEvent e) {
250
265
Mode .FILE );
251
266
if (dialog .open () == Window .OK ) {
252
267
IResource resource = (IResource ) dialog .getFirstResult ();
253
- filePathCombo .setText (TypeScriptCorePlugin . getTypeScriptRepositoryManager ()
254
- .generateFileName (resource , null ));
268
+ filePathCombo .setText (
269
+ TypeScriptCorePlugin . getTypeScriptRepositoryManager () .generateFileName (resource , null ));
255
270
}
256
271
257
272
}
@@ -289,7 +304,6 @@ private void createNodePathInfo(Composite parent) {
289
304
gridData .horizontalSpan = 2 ;
290
305
gridData .widthHint = 200 ;
291
306
nodePath .setLayoutData (gridData );
292
-
293
307
}
294
308
295
309
@ Override
@@ -303,7 +317,6 @@ protected void initializeDefaultValues() {
303
317
embeddedNodeJs .select (0 );
304
318
useEmbeddedNodeJsButton .setSelection (true );
305
319
updateNodeJsMode ();
306
-
307
320
}
308
321
309
322
private void updateNodeJsMode () {
@@ -312,13 +325,107 @@ private void updateNodeJsMode() {
312
325
installedNodeJs .setEnabled (!useEmbeddedNodeJs );
313
326
browseFileSystemButton .setEnabled (!useEmbeddedNodeJs );
314
327
browseWorkspaceButton .setEnabled (!useEmbeddedNodeJs );
328
+ }
329
+
330
+ private class NodeJsStatus extends StatusInfo {
331
+
332
+ private final File nodeFile ;
333
+ private final String version ;
315
334
335
+ public NodeJsStatus (File nodeFile , String version , String errorMessage ) {
336
+ if (errorMessage != null ) {
337
+ setError (errorMessage );
338
+ }
339
+ this .nodeFile = nodeFile ;
340
+ this .version = version ;
341
+ }
342
+
343
+ public File getNodeFile () {
344
+ return nodeFile ;
345
+ }
346
+
347
+ public String getNodeVersion () {
348
+ return version ;
349
+ }
350
+ }
351
+
352
+ /**
353
+ * Update the node version, path labels and returns the validation status of
354
+ * the nodejs path.
355
+ *
356
+ * @return the validation status of the nodejs path.
357
+ */
358
+ private IStatus validateAndUpdateNodejsPath () {
359
+ // Compute node.j status
360
+ NodeJsStatus status = validateNodejsPath ();
361
+ // Update node version & path
362
+ if (status .isOK ()) {
363
+ nodeVersion .setText (status .getNodeVersion ());
364
+ nodePath .setText (FileUtils .getPath (status .getNodeFile ()));
365
+ } else {
366
+ nodeVersion .setText ("" );
367
+ nodePath .setText ("" );
368
+ }
369
+ return status ;
370
+ }
371
+
372
+ /**
373
+ * Returns the status of the node.js path.
374
+ *
375
+ * @return the status of the node.js path.
376
+ */
377
+ private NodeJsStatus validateNodejsPath () {
378
+ File nodeFile = null ;
379
+ String version = null ;
380
+ boolean embedded = useEmbeddedNodeJsButton .getSelection ();
381
+ if (embedded ) {
382
+ int selectedIndex = embeddedNodeJs .getSelectionIndex ();
383
+ /*
384
+ * if (selectedIndex == 0) { // ERROR: the embedded node.js combo is
385
+ * not selected. return new NodeJsStatus(null, null,
386
+ * TypeScriptUIMessages.
387
+ * NodejsConfigurationBlock_embeddedNode_required_error); } else {
388
+ */
389
+ IEmbeddedNodejs [] installs = TypeScriptCorePlugin .getNodejsInstallManager ().getNodejsInstalls ();
390
+ IEmbeddedNodejs install = installs [selectedIndex ];
391
+ nodeFile = install .getPath ();
392
+ // }
393
+ } else {
394
+ String nodeJsPath = installedNodeJs .getText ();
395
+ if (StringUtils .isEmpty (nodeJsPath )) {
396
+ // ERROR: the installed path is empty
397
+ return new NodeJsStatus (null , null ,
398
+ TypeScriptUIMessages .NodejsConfigurationBlock_installedNode_required_error );
399
+ } else {
400
+ nodeFile = WorkbenchResourceUtil .resolvePath (nodeJsPath , null );
401
+ }
402
+ }
403
+
404
+ if (!nodeFile .exists ()) {
405
+ // ERROR: node.js file doesn't exists
406
+ return new NodeJsStatus (null , null , NLS .bind (
407
+ TypeScriptUIMessages .NodejsConfigurationBlock_nodeFile_exists_error , FileUtils .getPath (nodeFile )));
408
+ } else {
409
+ version = NodejsProcessHelper .getNodeVersion (nodeFile );
410
+ if (StringUtils .isEmpty (version )) {
411
+ // ERROR: the file path is not a node.exe
412
+ return new NodeJsStatus (null , null ,
413
+ NLS .bind (TypeScriptUIMessages .NodejsConfigurationBlock_nodeFile_invalid_error ,
414
+ FileUtils .getPath (nodeFile )));
415
+ }
416
+ }
417
+ // Node.js path is valid
418
+ return new NodeJsStatus (nodeFile , version , null );
316
419
}
317
420
318
421
@ Override
319
- protected boolean validatePage () {
320
- // TODO Auto-generated method stub
321
- return false ;
422
+ protected IStatus [] validatePage () {
423
+ IStatus [] status = new IStatus [2 ];
424
+ // Validate TypeScript Runtime
425
+ status [0 ] = validateTypeScriptRuntime ();
426
+ // Validate Node
427
+ status [1 ] = validateAndUpdateNodejsPath ();
428
+ return status ;
322
429
}
323
430
324
431
public String getNpmInstallCommand () {
0 commit comments