|
1 package org.symbian.tools.tmw.internal.ui.wizard; |
|
2 |
|
3 import java.io.File; |
|
4 import java.net.URI; |
|
5 |
|
6 import org.eclipse.core.databinding.DataBindingContext; |
|
7 import org.eclipse.core.databinding.beans.BeansObservables; |
|
8 import org.eclipse.core.databinding.observable.value.IObservableValue; |
|
9 import org.eclipse.core.databinding.validation.IValidator; |
|
10 import org.eclipse.core.resources.IProject; |
|
11 import org.eclipse.core.resources.IResource; |
|
12 import org.eclipse.core.resources.IWorkspace; |
|
13 import org.eclipse.core.resources.ResourcesPlugin; |
|
14 import org.eclipse.core.runtime.IPath; |
|
15 import org.eclipse.core.runtime.IStatus; |
|
16 import org.eclipse.core.runtime.Path; |
|
17 import org.eclipse.core.runtime.Status; |
|
18 import org.eclipse.jface.databinding.viewers.IViewerObservableValue; |
|
19 import org.eclipse.jface.databinding.viewers.ViewersObservables; |
|
20 import org.eclipse.jface.databinding.wizard.WizardPageSupport; |
|
21 import org.eclipse.jface.dialogs.Dialog; |
|
22 import org.eclipse.jface.viewers.ArrayContentProvider; |
|
23 import org.eclipse.jface.viewers.ComboViewer; |
|
24 import org.eclipse.jface.viewers.LabelProvider; |
|
25 import org.eclipse.jface.wizard.WizardPage; |
|
26 import org.eclipse.swt.SWT; |
|
27 import org.eclipse.swt.layout.GridData; |
|
28 import org.eclipse.swt.layout.GridLayout; |
|
29 import org.eclipse.swt.widgets.Composite; |
|
30 import org.eclipse.swt.widgets.Event; |
|
31 import org.eclipse.swt.widgets.Label; |
|
32 import org.eclipse.swt.widgets.Listener; |
|
33 import org.eclipse.swt.widgets.Text; |
|
34 import org.eclipse.ui.PlatformUI; |
|
35 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; |
|
36 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; |
|
37 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea; |
|
38 import org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea.IErrorMessageReporter; |
|
39 import org.symbian.tools.tmw.core.TMWCore; |
|
40 import org.symbian.tools.tmw.core.runtimes.IMobileWebRuntime; |
|
41 import org.symbian.tools.tmw.ui.TMWCoreUI; |
|
42 |
|
43 @SuppressWarnings("restriction") |
|
44 public final class NewApplicationDetailsWizardPage extends WizardPage { |
|
45 public static final class ProjectNameValidator implements IValidator { |
|
46 |
|
47 public IStatus validate(Object value) { |
|
48 IWorkspace workspace = ResourcesPlugin.getWorkspace(); |
|
49 |
|
50 String projectFieldContents = (String) value; |
|
51 IStatus nameStatus = workspace.validateName(projectFieldContents, IResource.PROJECT); |
|
52 if (!nameStatus.isOK()) { |
|
53 return nameStatus; |
|
54 } |
|
55 |
|
56 IProject handle = ResourcesPlugin.getWorkspace().getRoot().getProject(projectFieldContents); |
|
57 if (handle.exists()) { |
|
58 return new Status(IStatus.ERROR, TMWCoreUI.PLUGIN_ID, |
|
59 IDEWorkbenchMessages.WizardNewProjectCreationPage_projectExistsMessage); |
|
60 } |
|
61 return Status.OK_STATUS; |
|
62 } |
|
63 |
|
64 } |
|
65 |
|
66 private ProjectContentsLocationArea locationArea; |
|
67 private final DataBindingContext bindingContext; |
|
68 private final WizardContext context; |
|
69 |
|
70 public NewApplicationDetailsWizardPage(WizardContext context, DataBindingContext bindingContext) { |
|
71 super("WRTApp"); |
|
72 setImageDescriptor(null); |
|
73 setTitle("Application Details"); |
|
74 setDescription("Specify application details"); |
|
75 this.context = context; |
|
76 this.bindingContext = bindingContext; |
|
77 } |
|
78 |
|
79 public void createControl(Composite parent) { |
|
80 Composite root = new Composite(parent, SWT.NONE); |
|
81 |
|
82 initializeDialogUnits(parent); |
|
83 |
|
84 PlatformUI.getWorkbench().getHelpSystem().setHelp(root, IIDEHelpContextIds.NEW_PROJECT_WIZARD_PAGE); |
|
85 |
|
86 WizardPageSupport.create(this, bindingContext); |
|
87 root.setLayout(new GridLayout(2, false)); |
|
88 createProjectNameGroup(root); |
|
89 |
|
90 createLabel(root, ""); |
|
91 createLabel(root, ""); |
|
92 createLabel(root, "Mobile Runtime:"); |
|
93 |
|
94 ComboViewer viewer = new ComboViewer(root); |
|
95 viewer.setContentProvider(new ArrayContentProvider()); |
|
96 viewer.setLabelProvider(new LabelProvider() { |
|
97 @Override |
|
98 public String getText(Object element) { |
|
99 return ((IMobileWebRuntime) element).getName(); |
|
100 } |
|
101 }); |
|
102 viewer.setInput(TMWCore.getRuntimesManager().getAllRuntimes()); |
|
103 |
|
104 final IViewerObservableValue observeSelection = ViewersObservables.observeSingleSelection(viewer); |
|
105 final IObservableValue observableValue = BeansObservables.observeValue(context, WizardContext.RUNTIME); |
|
106 bindingContext.bindValue(observeSelection, observableValue); |
|
107 |
|
108 createLabel(root, ""); |
|
109 createLabel(root, ""); |
|
110 createLabel(root, "Application identifier:"); |
|
111 |
|
112 context.createText(root, WizardContext.WIDGET_ID, "applicatoin identifier", bindingContext, null, |
|
113 new RegexpValidator("[\\w]*(\\.\\w[\\w]*)*", "{0} is not a valid applicatoin ID", true)); |
|
114 createLabel(root, ""); |
|
115 createLabel(root, "This id should be unique for successful installation of application on the device"); |
|
116 |
|
117 createLabel(root, ""); |
|
118 createLabel(root, ""); |
|
119 |
|
120 createLabel(root, "Application name:"); |
|
121 |
|
122 context.createText(root, WizardContext.WIDGET_NAME, "application name", bindingContext, null, |
|
123 new RegexpValidator("[^\\w\\. ]", "Application name cannot contain {0} character", false)); |
|
124 |
|
125 createLabel(root, ""); |
|
126 createLabel(root, "This will be the application display name on the device"); |
|
127 |
|
128 Composite composite = new Composite(root, SWT.NONE); |
|
129 GridLayout gridLayout = new GridLayout(1, false); |
|
130 gridLayout.marginWidth = 0; |
|
131 composite.setLayout(gridLayout); |
|
132 composite.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, true, 2, 1)); |
|
133 |
|
134 locationArea = new ProjectContentsLocationArea(getErrorReporter(), composite); |
|
135 if (context.getProjectName() != null && context.getProjectName().trim().length() > 0) { |
|
136 locationArea.updateProjectName(context.getProjectName()); |
|
137 } |
|
138 |
|
139 // Scale the button based on the rest of the dialog |
|
140 setButtonLayoutData(locationArea.getBrowseButton()); |
|
141 |
|
142 setPageComplete(validatePage()); |
|
143 // Show description on opening |
|
144 setErrorMessage(null); |
|
145 setMessage(null); |
|
146 setControl(root); |
|
147 Dialog.applyDialogFont(root); |
|
148 } |
|
149 |
|
150 private void createLabel(Composite root, String string) { |
|
151 new Label(root, SWT.NONE).setText(string); |
|
152 } |
|
153 |
|
154 /** |
|
155 * Creates the project name specification controls. |
|
156 * |
|
157 * @param parent |
|
158 * the parent composite |
|
159 */ |
|
160 private final void createProjectNameGroup(Composite parent) { |
|
161 // new project label |
|
162 Label projectLabel = new Label(parent, SWT.NONE); |
|
163 projectLabel.setText(IDEWorkbenchMessages.WizardNewProjectCreationPage_nameLabel); |
|
164 projectLabel.setFont(parent.getFont()); |
|
165 |
|
166 Text projectNameField = context.createText(parent, WizardContext.PROJECT_NAME, "project name", bindingContext, |
|
167 null, new ProjectNameValidator()); |
|
168 |
|
169 projectNameField.setFont(parent.getFont()); |
|
170 |
|
171 projectNameField.addListener(SWT.Modify, nameModifyListener); |
|
172 } |
|
173 |
|
174 /** |
|
175 * Returns the current project location path as entered by the user, or its |
|
176 * anticipated initial value. Note that if the default has been returned the |
|
177 * path in a project description used to create a project should not be set. |
|
178 * |
|
179 * @return the project location path or its anticipated initial value. |
|
180 */ |
|
181 public IPath getLocationPath() { |
|
182 return locationArea.isDefault() ? null : new Path(locationArea.getProjectLocation()); |
|
183 } |
|
184 |
|
185 /** |
|
186 * /** Returns the current project location URI as entered by the user, or |
|
187 * <code>null</code> if a valid project location has not been entered. |
|
188 * |
|
189 * @return the project location URI, or <code>null</code> |
|
190 * @since 3.2 |
|
191 */ |
|
192 public URI getLocationURI() { |
|
193 return locationArea.getProjectLocationURI(); |
|
194 } |
|
195 |
|
196 private final Listener nameModifyListener = new Listener() { |
|
197 public void handleEvent(Event e) { |
|
198 if (isPageComplete()) { |
|
199 setLocationForSelection(); |
|
200 boolean valid = validatePage(); |
|
201 setPageComplete(valid); |
|
202 } |
|
203 } |
|
204 }; |
|
205 |
|
206 /** |
|
207 * Get an error reporter for the receiver. |
|
208 * |
|
209 * @return IErrorMessageReporter |
|
210 */ |
|
211 private IErrorMessageReporter getErrorReporter() { |
|
212 return new IErrorMessageReporter() { |
|
213 /* |
|
214 * (non-Javadoc) |
|
215 * |
|
216 * @see |
|
217 * org.eclipse.ui.internal.ide.dialogs.ProjectContentsLocationArea |
|
218 * .IErrorMessageReporter#reportError(java.lang.String) |
|
219 */ |
|
220 public void reportError(String errorMessage, boolean infoOnly) { |
|
221 if (infoOnly) { |
|
222 setMessage(errorMessage, IStatus.INFO); |
|
223 setErrorMessage(null); |
|
224 } else { |
|
225 setErrorMessage(errorMessage); |
|
226 } |
|
227 boolean valid = errorMessage == null; |
|
228 if (valid) { |
|
229 valid = validatePage(); |
|
230 } |
|
231 |
|
232 setPageComplete(valid); |
|
233 } |
|
234 }; |
|
235 } |
|
236 |
|
237 /** |
|
238 * Set the location to the default location if we are set to useDefaults. |
|
239 */ |
|
240 void setLocationForSelection() { |
|
241 locationArea.updateProjectName(context.getProjectName()); |
|
242 } |
|
243 |
|
244 /** |
|
245 * Returns whether this page's controls currently all contain valid values. |
|
246 * |
|
247 * @return <code>true</code> if all controls are valid, and |
|
248 * <code>false</code> if at least one is invalid |
|
249 */ |
|
250 protected boolean validatePage() { |
|
251 if (isPageComplete() || context.getProjectName().trim().length() == 0) { |
|
252 return false; |
|
253 } |
|
254 String projectName = context.getProjectName(); |
|
255 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); |
|
256 locationArea.setExistingProject(project); |
|
257 |
|
258 String validLocationMessage = locationArea.checkValidLocation(); |
|
259 if (validLocationMessage != null) { // there is no destination location |
|
260 // given |
|
261 setErrorMessage(validLocationMessage); |
|
262 return false; |
|
263 } |
|
264 File file = new File(locationArea.getProjectLocationURI()); |
|
265 if (file.isFile()) { |
|
266 setErrorMessage(String.format("%s is an existing file", file)); |
|
267 return false; |
|
268 } else if (file.isDirectory() && file.listFiles().length > 0) { |
|
269 setErrorMessage(String.format("%s is a non-empty folder", file)); |
|
270 return false; |
|
271 } |
|
272 |
|
273 setErrorMessage(null); |
|
274 setMessage(null); |
|
275 return true; |
|
276 } |
|
277 |
|
278 /** |
|
279 * Returns the useDefaults. |
|
280 * |
|
281 * @return boolean |
|
282 */ |
|
283 public boolean useDefaults() { |
|
284 return locationArea.isDefault(); |
|
285 } |
|
286 |
|
287 @Override |
|
288 public void setVisible(boolean visible) { |
|
289 // TODO Auto-generated method stub |
|
290 super.setVisible(visible); |
|
291 setErrorMessage(null); |
|
292 } |
|
293 } |