Skip to content

Commit b804320

Browse files
committed
[516809] Add check for cordova availability in new app wizard
Bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=516809 Signed-off-by: Rastislav Wagner <[email protected]>
1 parent 1159789 commit b804320

File tree

1 file changed

+108
-6
lines changed

1 file changed

+108
-6
lines changed

plugins/org.eclipse.thym.ui/src/org/eclipse/thym/ui/wizard/project/WizardNewHybridProjectCreationPage.java

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2013, 2014 Red Hat, Inc.
2+
* Copyright (c) 2013, 2017 Red Hat, Inc.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -10,30 +10,48 @@
1010
*******************************************************************************/
1111
package org.eclipse.thym.ui.wizard.project;
1212

13+
import static org.eclipse.jface.dialogs.Dialog.DLG_IMG_MESSAGE_WARNING;
14+
import static org.eclipse.jface.dialogs.Dialog.DLG_IMG_MESSAGE_ERROR;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.core.runtime.IProgressMonitor;
1318
import org.eclipse.core.runtime.IStatus;
19+
import org.eclipse.core.runtime.Status;
20+
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
21+
import org.eclipse.core.runtime.jobs.Job;
22+
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
1423
import org.eclipse.jface.dialogs.Dialog;
24+
import org.eclipse.jface.resource.JFaceResources;
1525
import org.eclipse.jface.viewers.IStructuredSelection;
1626
import org.eclipse.swt.SWT;
27+
import org.eclipse.swt.custom.CLabel;
1728
import org.eclipse.swt.events.ModifyEvent;
1829
import org.eclipse.swt.events.ModifyListener;
1930
import org.eclipse.swt.layout.GridData;
2031
import org.eclipse.swt.layout.GridLayout;
2132
import org.eclipse.swt.widgets.Composite;
33+
import org.eclipse.swt.widgets.Display;
2234
import org.eclipse.swt.widgets.Group;
2335
import org.eclipse.swt.widgets.Label;
2436
import org.eclipse.swt.widgets.Text;
2537
import org.eclipse.ui.IWorkingSet;
2638
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
2739
import org.eclipse.ui.dialogs.WorkingSetGroup;
40+
import org.osgi.framework.Version;
2841
import org.eclipse.thym.core.HybridProjectConventions;
42+
import org.eclipse.thym.core.internal.cordova.CordovaCLI;
43+
import org.eclipse.thym.core.internal.cordova.ErrorDetectingCLIResult;
44+
import org.eclipse.thym.ui.HybridUI;
2945

3046
public class WizardNewHybridProjectCreationPage extends WizardNewProjectCreationPage{
3147
private Text txtName;
3248
private Text txtID;
3349
private WorkingSetGroup workingSetGroup;
3450
private final PropertyModifyListener propertyModifyListener = new PropertyModifyListener();
3551
private IStructuredSelection currentSelection;
36-
52+
private CordovaIsAvailableJob cordovaIsAvailableJob;
53+
private CLabel cordovaLabel;
54+
private Boolean cordovaFound = null;
3755

3856
class PropertyModifyListener implements ModifyListener{
3957
private boolean skipValidation = false;
@@ -65,7 +83,7 @@ public WizardNewHybridProjectCreationPage(String pageName, IStructuredSelection
6583
setDescription("Create a hybrid mobile application using Apache Cordova for cross-platform mobile development");
6684
}
6785

68-
public void createControl(Composite parent ){
86+
public void createControl(Composite parent ){
6987
super.createControl(parent);
7088

7189
Group applicationGroup = new Group((Composite)getControl(), SWT.NONE);
@@ -93,21 +111,43 @@ public void createControl(Composite parent ){
93111
txtID.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
94112

95113
createWorkingSetGroup();
114+
115+
Group cordovaGroup = new Group((Composite)getControl(), SWT.NONE);
116+
cordovaGroup.setText("Cordova version");
117+
layout = new GridLayout();
118+
layout.numColumns = 1;
119+
cordovaGroup.setLayout(layout);
120+
cordovaGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
121+
122+
cordovaLabel = new CLabel(cordovaGroup, SWT.NONE);
123+
cordovaLabel.setImage(JFaceResources.getImage(DLG_IMG_MESSAGE_WARNING));
124+
cordovaLabel.setText("Checking cordova availability...");
125+
cordovaLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL,true, true, 1, 1));
96126

97-
98-
setPageComplete(validatePage());
99127
setErrorMessage(null);
100128
setMessage(null);
129+
setPageComplete(validatePage());
101130
Dialog.applyDialogFont(getControl());
131+
checkCordovaIsAvailable();
102132
}
103133

104134
@Override
105135
protected boolean validatePage() {
136+
if(cordovaFound == null){
137+
setMessage("Checking cordova availability...", WARNING);
138+
this.getContainer().updateMessage();
139+
return false;
140+
}
141+
if(!cordovaFound){
142+
setMessage("Cordova not found, please run 'npm install -g cordova' on a command line", ERROR);
143+
this.getContainer().updateMessage();
144+
return false;
145+
}
146+
106147
boolean superValidate = super.validatePage();
107148
if(!superValidate || txtID == null || txtName == null ){//validate is actually called first time on super.createControl()
108149
return superValidate; // in order to avoid NPEs for the half initialized UI we do a partial
109150
} // until all UI components are in place.
110-
111151
if( !propertyModifyListener.isNameOrIDChanged() ){
112152
String id = HybridProjectConventions.generateProjectID(getProjectName());
113153
String name = HybridProjectConventions.generateApplicationName(getProjectName());
@@ -147,6 +187,7 @@ protected boolean validatePage() {
147187

148188
if(status.getSeverity() == IStatus.ERROR ){
149189
setMessage(status.getMessage(), ERROR);
190+
this.getContainer().updateMessage();
150191
return false;
151192
}
152193
if (status.getSeverity() == IStatus.WARNING) {
@@ -174,4 +215,65 @@ public String getApplicationID(){
174215
public IWorkingSet[] getSelectedWorkingSets(){
175216
return workingSetGroup.getSelectedWorkingSets();
176217
}
218+
219+
class CordovaIsAvailableJob extends Job {
220+
221+
private String cordovaVersion;
222+
223+
public CordovaIsAvailableJob() {
224+
super("Checking cordova availability");
225+
}
226+
227+
@Override
228+
protected IStatus run(IProgressMonitor monitor) {
229+
try {
230+
ErrorDetectingCLIResult result = new CordovaCLI().version(monitor).convertTo(ErrorDetectingCLIResult.class);
231+
if(result.asStatus().isOK()){
232+
cordovaVersion = Version.parseVersion(result.getMessage()).toString();
233+
}
234+
} catch (CoreException e) {
235+
HybridUI.log(WARNING, "Unable to determine if cordova is available", e);
236+
}
237+
return Status.OK_STATUS;
238+
239+
}
240+
241+
public String getCordovaVersion(){
242+
return cordovaVersion;
243+
}
244+
245+
}
246+
247+
private void checkCordovaIsAvailable() {
248+
cordovaIsAvailableJob = new CordovaIsAvailableJob();
249+
cordovaIsAvailableJob.setUser(true);
250+
cordovaIsAvailableJob.schedule();
251+
cordovaIsAvailableJob.addJobChangeListener(new JobChangeAdapter() {
252+
253+
@Override
254+
public void done(IJobChangeEvent event) {
255+
if(!getControl().isDisposed()){
256+
final Display display = getControl().getDisplay();
257+
display.syncExec(new Runnable() {
258+
@Override
259+
public void run() {
260+
if(cordovaIsAvailableJob.getCordovaVersion() != null){
261+
cordovaLabel.setText(cordovaIsAvailableJob.getCordovaVersion());
262+
cordovaLabel.setImage(null);
263+
cordovaFound = true;
264+
} else {
265+
cordovaLabel.setText("Cordova not found, please run 'npm install -g cordova' on a command line");
266+
cordovaLabel.setImage(JFaceResources.getImage(DLG_IMG_MESSAGE_ERROR));
267+
cordovaLabel.setForeground(getControl().getDisplay().getSystemColor(SWT.COLOR_RED));
268+
cordovaFound = false;
269+
}
270+
setPageComplete(validatePage());
271+
}
272+
});
273+
}
274+
}
275+
});
276+
}
277+
278+
177279
}

0 commit comments

Comments
 (0)