|
1 | 1 | /*******************************************************************************
|
2 |
| - * Copyright (c) 2013, 2014 Red Hat, Inc. |
| 2 | + * Copyright (c) 2013, 2017 Red Hat, Inc. |
3 | 3 | * All rights reserved. This program and the accompanying materials
|
4 | 4 | * are made available under the terms of the Eclipse Public License v1.0
|
5 | 5 | * which accompanies this distribution, and is available at
|
|
10 | 10 | *******************************************************************************/
|
11 | 11 | package org.eclipse.thym.ui.wizard.project;
|
12 | 12 |
|
| 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; |
13 | 18 | 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; |
14 | 23 | import org.eclipse.jface.dialogs.Dialog;
|
| 24 | +import org.eclipse.jface.resource.JFaceResources; |
15 | 25 | import org.eclipse.jface.viewers.IStructuredSelection;
|
16 | 26 | import org.eclipse.swt.SWT;
|
| 27 | +import org.eclipse.swt.custom.CLabel; |
17 | 28 | import org.eclipse.swt.events.ModifyEvent;
|
18 | 29 | import org.eclipse.swt.events.ModifyListener;
|
19 | 30 | import org.eclipse.swt.layout.GridData;
|
20 | 31 | import org.eclipse.swt.layout.GridLayout;
|
21 | 32 | import org.eclipse.swt.widgets.Composite;
|
| 33 | +import org.eclipse.swt.widgets.Display; |
22 | 34 | import org.eclipse.swt.widgets.Group;
|
23 | 35 | import org.eclipse.swt.widgets.Label;
|
24 | 36 | import org.eclipse.swt.widgets.Text;
|
25 | 37 | import org.eclipse.ui.IWorkingSet;
|
26 | 38 | import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
|
27 | 39 | import org.eclipse.ui.dialogs.WorkingSetGroup;
|
| 40 | +import org.osgi.framework.Version; |
28 | 41 | 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; |
29 | 45 |
|
30 | 46 | public class WizardNewHybridProjectCreationPage extends WizardNewProjectCreationPage{
|
31 | 47 | private Text txtName;
|
32 | 48 | private Text txtID;
|
33 | 49 | private WorkingSetGroup workingSetGroup;
|
34 | 50 | private final PropertyModifyListener propertyModifyListener = new PropertyModifyListener();
|
35 | 51 | private IStructuredSelection currentSelection;
|
36 |
| - |
| 52 | + private CordovaIsAvailableJob cordovaIsAvailableJob; |
| 53 | + private CLabel cordovaLabel; |
| 54 | + private Boolean cordovaFound = null; |
37 | 55 |
|
38 | 56 | class PropertyModifyListener implements ModifyListener{
|
39 | 57 | private boolean skipValidation = false;
|
@@ -65,7 +83,7 @@ public WizardNewHybridProjectCreationPage(String pageName, IStructuredSelection
|
65 | 83 | setDescription("Create a hybrid mobile application using Apache Cordova for cross-platform mobile development");
|
66 | 84 | }
|
67 | 85 |
|
68 |
| - public void createControl(Composite parent ){ |
| 86 | + public void createControl(Composite parent ){ |
69 | 87 | super.createControl(parent);
|
70 | 88 |
|
71 | 89 | Group applicationGroup = new Group((Composite)getControl(), SWT.NONE);
|
@@ -93,21 +111,43 @@ public void createControl(Composite parent ){
|
93 | 111 | txtID.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
|
94 | 112 |
|
95 | 113 | 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)); |
96 | 126 |
|
97 |
| - |
98 |
| - setPageComplete(validatePage()); |
99 | 127 | setErrorMessage(null);
|
100 | 128 | setMessage(null);
|
| 129 | + setPageComplete(validatePage()); |
101 | 130 | Dialog.applyDialogFont(getControl());
|
| 131 | + checkCordovaIsAvailable(); |
102 | 132 | }
|
103 | 133 |
|
104 | 134 | @Override
|
105 | 135 | 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 | + |
106 | 147 | boolean superValidate = super.validatePage();
|
107 | 148 | if(!superValidate || txtID == null || txtName == null ){//validate is actually called first time on super.createControl()
|
108 | 149 | return superValidate; // in order to avoid NPEs for the half initialized UI we do a partial
|
109 | 150 | } // until all UI components are in place.
|
110 |
| - |
111 | 151 | if( !propertyModifyListener.isNameOrIDChanged() ){
|
112 | 152 | String id = HybridProjectConventions.generateProjectID(getProjectName());
|
113 | 153 | String name = HybridProjectConventions.generateApplicationName(getProjectName());
|
@@ -147,6 +187,7 @@ protected boolean validatePage() {
|
147 | 187 |
|
148 | 188 | if(status.getSeverity() == IStatus.ERROR ){
|
149 | 189 | setMessage(status.getMessage(), ERROR);
|
| 190 | + this.getContainer().updateMessage(); |
150 | 191 | return false;
|
151 | 192 | }
|
152 | 193 | if (status.getSeverity() == IStatus.WARNING) {
|
@@ -174,4 +215,65 @@ public String getApplicationID(){
|
174 | 215 | public IWorkingSet[] getSelectedWorkingSets(){
|
175 | 216 | return workingSetGroup.getSelectedWorkingSets();
|
176 | 217 | }
|
| 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 | + |
177 | 279 | }
|
0 commit comments