Skip to content

Package: SelectLocationPage$LocationValidator

SelectLocationPage$LocationValidator

nameinstructionbranchcomplexitylinemethod
SelectLocationPage.LocationValidator(SelectLocationPage)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
validate(Object)
M: 77 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 20 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Stefan Dirix - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.emf2web.ui.wizard;
15:
16: import java.io.File;
17:
18: import org.eclipse.core.databinding.DataBindingContext;
19: import org.eclipse.core.databinding.UpdateValueStrategy;
20: import org.eclipse.core.databinding.beans.typed.PojoProperties;
21: import org.eclipse.core.databinding.conversion.IConverter;
22: import org.eclipse.core.databinding.observable.value.IObservableValue;
23: import org.eclipse.core.databinding.validation.IValidator;
24: import org.eclipse.core.databinding.validation.ValidationStatus;
25: import org.eclipse.core.resources.IContainer;
26: import org.eclipse.core.resources.IFile;
27: import org.eclipse.core.resources.IResource;
28: import org.eclipse.core.resources.IWorkspaceRoot;
29: import org.eclipse.core.resources.ResourcesPlugin;
30: import org.eclipse.core.runtime.IStatus;
31: import org.eclipse.emf.common.ui.dialogs.WorkspaceResourceDialog;
32: import org.eclipse.emf.common.util.URI;
33: import org.eclipse.emf.ecp.emf2web.controller.xtend.GenerationInfo;
34: import org.eclipse.emf.ecp.emf2web.ui.messages.Messages;
35: import org.eclipse.jface.databinding.swt.typed.WidgetProperties;
36: import org.eclipse.jface.databinding.wizard.WizardPageSupport;
37: import org.eclipse.jface.fieldassist.ControlDecoration;
38: import org.eclipse.jface.fieldassist.FieldDecoration;
39: import org.eclipse.jface.fieldassist.FieldDecorationRegistry;
40: import org.eclipse.jface.window.Window;
41: import org.eclipse.jface.wizard.WizardPage;
42: import org.eclipse.swt.SWT;
43: import org.eclipse.swt.events.SelectionAdapter;
44: import org.eclipse.swt.events.SelectionEvent;
45: import org.eclipse.swt.layout.FillLayout;
46: import org.eclipse.swt.layout.GridData;
47: import org.eclipse.swt.layout.GridLayout;
48: import org.eclipse.swt.widgets.Button;
49: import org.eclipse.swt.widgets.Composite;
50: import org.eclipse.swt.widgets.FileDialog;
51: import org.eclipse.swt.widgets.Group;
52: import org.eclipse.swt.widgets.Label;
53: import org.eclipse.swt.widgets.Text;
54: import org.eclipse.ui.model.WorkbenchContentProvider;
55: import org.eclipse.ui.model.WorkbenchLabelProvider;
56:
57: /**
58: * A page for a single generated file which's main purpose is to select a location for the file.
59: *
60: * @author Stefan Dirix
61: *
62: */
63: public class SelectLocationPage extends WizardPage {
64:         private DataBindingContext bindingContext;
65:
66:         private final GenerationInfo generationInfo;
67:         private Text locationText;
68:         private Text generatedText;
69:         private Button btnWrap;
70:         private ControlDecoration requiredLocationDecoration;
71:
72:         private boolean wasAlreadyVisible;
73:
74:         /**
75:          * Constructor.
76:          *
77:          * @param generationInfo The {@link GenerationInfo} for which this page is responsible.
78:          */
79:         public SelectLocationPage(GenerationInfo generationInfo) {
80:                 super("wizardPage"); //$NON-NLS-1$
81:                 this.generationInfo = generationInfo;
82:
83:                 wasAlreadyVisible = false;
84:
85:                 setTitleAndDescription();
86:         }
87:
88:         /**
89:          * Sets title and description of this page according to the {@link GenerationInfo}.
90:          */
91:         protected void setTitleAndDescription() {
92:                 if (GenerationInfo.MODEL_TYPE.equals(generationInfo.getType())) {
93:                         setTitle(Messages.getString("SelectLocationPage.Title_ModelType")); //$NON-NLS-1$
94:                         setDescription(Messages.getString("SelectLocationPage.Description_ModelType")); //$NON-NLS-1$
95:                 } else if (GenerationInfo.VIEW_TYPE.equals(generationInfo.getType())) {
96:                         setTitle(Messages.getString("SelectLocationPage.Title_ViewType")); //$NON-NLS-1$
97:                         setDescription(Messages.getString("SelectLocationPage.Description_ViewType")); //$NON-NLS-1$
98:                 } else if (GenerationInfo.MODEL_AND_VIEW_TYPE.equals(generationInfo.getType())) {
99:                         setTitle(Messages.getString("SelectLocationPage.Title_ModelAndViewType")); //$NON-NLS-1$
100:                         setDescription(Messages.getString("SelectLocationPage.Description_ModelAndViewType")); //$NON-NLS-1$
101:                 } else {
102:                         setTitle(Messages.getString("SelectLocationPage.Title_OtherType") + generationInfo.getNameProposal()); //$NON-NLS-1$
103:                         setDescription(Messages.getString("SelectLocationPage.Description_OtherType")); //$NON-NLS-1$
104:                 }
105:         }
106:
107:         /**
108:          * Returns the linked {@link GenerationInfo}.
109:          *
110:          * @return The {@link GenerationInfo}.
111:          */
112:         public GenerationInfo getGenerationInfo() {
113:                 return generationInfo;
114:         }
115:
116:         /**
117:          * Returns the bindingContext of this page.
118:          *
119:          * @return The {@link DataBindingContext}.
120:          */
121:         public DataBindingContext getBindingContext() {
122:                 return bindingContext;
123:         }
124:
125:         @Override
126:         public void createControl(Composite parent) {
127:                 final Composite container = new Composite(parent, SWT.NULL);
128:
129:                 setControl(container);
130:                 container.setLayout(new GridLayout(3, false));
131:
132:                 final Label lblLocation = new Label(container, SWT.NONE);
133:                 lblLocation.setText(Messages.getString("SelectLocationPage.LocationLabel")); //$NON-NLS-1$
134:                 new Label(container, SWT.NONE);
135:                 new Label(container, SWT.NONE);
136:
137:                 locationText = new Text(container, SWT.BORDER);
138:                 locationText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
139:
140:                 requiredLocationDecoration = new ControlDecoration(locationText, SWT.LEFT | SWT.TOP);
141:                 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault()
142:                         .getFieldDecoration(FieldDecorationRegistry.DEC_ERROR);
143:                 requiredLocationDecoration.setImage(fieldDecoration.getImage());
144:                 requiredLocationDecoration.setDescriptionText(Messages.getString("SelectLocationPage.RequiredDecorationText")); //$NON-NLS-1$
145:                 requiredLocationDecoration.hide();
146:
147:                 final Button browseWorkspaceButton = new Button(container, SWT.NONE);
148:                 browseWorkspaceButton.addSelectionListener(new BrowseWorkspaceButtonSelectionListener());
149:                 browseWorkspaceButton.setText(Messages.getString("SelectLocationPage.BrowseWorkspaceButtonLabel")); //$NON-NLS-1$
150:
151:                 final Button browseFilesystemButton = new Button(container, SWT.NONE);
152:                 browseFilesystemButton.addSelectionListener(new BrowseFilesystemButtonSelectionListener());
153:                 browseFilesystemButton.setText(Messages.getString("SelectLocationPage.BrowseFilesystemButtonLabel")); //$NON-NLS-1$
154:                 new Label(container, SWT.NONE);
155:
156:                 if (generationInfo.getWrapper() != null) {
157:                         final Group grpWrapper = new Group(container, SWT.NONE);
158:                         grpWrapper.setLayout(new GridLayout(1, false));
159:                         grpWrapper.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1));
160:                         grpWrapper.setText(Messages.getString("SelectLocationPage.OptionalSettingsGroupText")); //$NON-NLS-1$
161:
162:                         btnWrap = new Button(grpWrapper, SWT.CHECK);
163:                         btnWrap.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 1, 1));
164:                         final String buttonText = Messages.getString("SelectLocationPage.WrapButtonText") //$NON-NLS-1$
165:                                 + ' ' + generationInfo.getWrapper().getName();
166:                         btnWrap.setText(buttonText);
167:                 }
168:
169:                 final Group grpPreview = new Group(container, SWT.NONE);
170:                 grpPreview.setLayout(new FillLayout(SWT.HORIZONTAL));
171:                 grpPreview.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));
172:                 grpPreview.setText(Messages.getString("SelectLocationPage.ContentGroupText")); //$NON-NLS-1$
173:
174:                 generatedText = new Text(grpPreview, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
175:                 bindingContext = initDataBindings();
176:
177:                 WizardPageSupport.create(this, bindingContext);
178:         }
179:
180:         /**
181:          * {@inheritDoc}
182:          *
183:          * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
184:          */
185:         @Override
186:         public void setVisible(boolean visible) {
187:                 super.setVisible(visible);
188:                 if (visible && !wasAlreadyVisible) {
189:                         wasAlreadyVisible = true;
190:                 }
191:         }
192:
193:         /**
194:          * Indicates if this page was already shown to the user.
195:          *
196:          * @return
197:          * {@code true} if this page was already shown to the user, {@code false} if this page was never shown to
198:          * the user.
199:          */
200:         public boolean wasAlreadyVisible() {
201:                 return wasAlreadyVisible;
202:         }
203:
204:         /**
205:          * Selection Listener for the Browse Workspace button opening a WorkspaceResourceDialog.
206:          */
207:         private class BrowseWorkspaceButtonSelectionListener extends SelectionAdapter {
208:                 @Override
209:                 public void widgetSelected(final SelectionEvent e) {
210:                         final WorkspaceResourceDialog dialog = new WorkspaceResourceDialog(getShell(), new WorkbenchLabelProvider(),
211:                                 new WorkbenchContentProvider());
212:                         dialog.setAllowMultiple(false);
213:                         dialog.setTitle(Messages.getString("SelectLocationPage.SelectLocationDialogTitle")); //$NON-NLS-1$
214:                         dialog.setShowNewFolderControl(true);
215:                         dialog.setShowFileControl(true);
216:
217:                         // preselect if file exists in workspace
218:                         if (generationInfo.getLocation() != null && generationInfo.getLocation().isPlatform()) {
219:                                 final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
220:                                 final IResource resource = root
221:                                         .findMember(generationInfo.getLocation().trimSegments(1).toPlatformString(true));
222:                                 if (resource != null && resource.isAccessible()) {
223:                                         dialog.setInitialSelection(resource);
224:                                         final int segment = generationInfo.getLocation().segmentCount() > 0
225:                                                 ? generationInfo.getLocation().segmentCount() - 1
226:                                                 : 0;
227:                                         dialog.setFileText(generationInfo.getLocation().segment(segment));
228:                                 }
229:                         }
230:
231:                         dialog.loadContents();
232:
233:                         if (dialog.open() == Window.OK) {
234:                                 final IFile file = dialog.getFile();
235:                                 final URI newLocation = URI.createPlatformResourceURI(file.getFullPath().toString(), true);
236:                                 setNewLocation(newLocation);
237:                         }
238:                 }
239:         }
240:
241:         /**
242:          * Selection Listener for the Browse Filesystem button opening a FileDialog.
243:          */
244:         private class BrowseFilesystemButtonSelectionListener extends SelectionAdapter {
245:                 @Override
246:                 public void widgetSelected(final SelectionEvent e) {
247:                         final FileDialog fileDialog = new FileDialog(getShell(), SWT.SINGLE | SWT.SAVE);
248:                         // preselect if proposal is not in workspace
249:                         if (generationInfo.getLocation() != null && !generationInfo.getLocation().isPlatform()) {
250:                                 final File file = new File(generationInfo.getLocation().toFileString());
251:                                 final File parent = file.getParentFile();
252:                                 if (parent != null && parent.exists()) {
253:                                         fileDialog.setFilterPath(parent.getAbsolutePath());
254:                                 }
255:                                 fileDialog.setFileName(file.getName());
256:                         }
257:                         final String result = fileDialog.open();
258:                         if (result != null) {
259:                                 final URI newLocation = URI.createFileURI(result);
260:                                 setNewLocation(newLocation);
261:                         }
262:                 }
263:         }
264:
265:         /**
266:          * Handles the propagation of a new location.
267:          *
268:          * @param location
269:          * The new location for the generated file.
270:          */
271:         private void setNewLocation(URI location) {
272:                 generationInfo.setLocation(location);
273:                 bindingContext.updateTargets();
274:                 requiredLocationDecoration.hide();
275:         }
276:
277:         /**
278:          * Validates the given location.
279:          */
280:         private class LocationValidator implements IValidator {
281:                 @Override
282:                 public IStatus validate(Object value) {
283:•                        if (value == null) {
284:                                 requiredLocationDecoration.show();
285:                                 return ValidationStatus.error(Messages.getString("SelectLocationPage.LocationError")); //$NON-NLS-1$
286:                         }
287:•                        if (URI.class.isInstance(value)) {
288:                                 final URI uri = URI.class.cast(value);
289:•                                if (uri.isPlatform()) {
290:                                         final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
291:                                         final IResource resource = root.findMember(uri.toPlatformString(true));
292:•                                        if (IContainer.class.isInstance(resource)) {
293:                                                 requiredLocationDecoration.show();
294:                                                 return ValidationStatus.error(Messages.getString("SelectLocationPage.LocationDirectoryError")); //$NON-NLS-1$
295:                                         }
296:                                 } else {
297:•                                        if (uri.toFileString() == null) {
298:                                                 requiredLocationDecoration.show();
299:                                                 return ValidationStatus.error(Messages.getString("SelectLocationPage.LocationValidError")); //$NON-NLS-1$
300:                                         }
301:                                         final File file = new File(uri.toFileString());
302:•                                        if (file.isDirectory()) {
303:                                                 requiredLocationDecoration.show();
304:                                                 return ValidationStatus.error(Messages.getString("SelectLocationPage.LocationDirectoryError")); //$NON-NLS-1$
305:                                         }
306:                                 }
307:                         }
308:                         requiredLocationDecoration.hide();
309:                         return ValidationStatus.ok();
310:                 }
311:         }
312:
313:         /**
314:          * Converts a manually entered string to an {@link URI}.
315:          */
316:         private class StringToURIConverter implements IConverter {
317:                 @Override
318:                 public Object getToType() {
319:                         return URI.class;
320:                 }
321:
322:                 @Override
323:                 public Object getFromType() {
324:                         return String.class;
325:                 }
326:
327:                 @Override
328:                 public Object convert(Object fromObject) {
329:                         if (fromObject == null || "".equals(fromObject)) { //$NON-NLS-1$
330:                                 return null;
331:                         }
332:                         final String path = (String) fromObject;
333:                         if (path.startsWith("platform:") || path.startsWith("file:")) { //$NON-NLS-1$ //$NON-NLS-2$
334:                                 return URI.createURI(path, false);
335:                         }
336:                         return URI.createFileURI(path);
337:                 }
338:         }
339:
340:         /**
341:          * Converts an URI to a String.
342:          */
343:         private class URItoStringConverter implements IConverter {
344:                 @Override
345:                 public Object getToType() {
346:                         return String.class;
347:                 }
348:
349:                 @Override
350:                 public Object getFromType() {
351:                         return URI.class;
352:                 }
353:
354:                 @Override
355:                 public Object convert(Object fromObject) {
356:                         if (fromObject == null) {
357:                                 return ""; //$NON-NLS-1$
358:                         }
359:                         final URI path = (URI) fromObject;
360:                         if (path.isFile()) {
361:                                 return path.toFileString();
362:                         }
363:                         return path.toString();
364:                 }
365:         }
366:
367:         /**
368:          * Creates and initializes the used data bindings.
369:          *
370:          * @return The initialized {@link DataBindingContext}.
371:          */
372:         protected DataBindingContext initDataBindings() {
373:                 final DataBindingContext bindingContext = new DataBindingContext();
374:
375:                 final IObservableValue observeTextLocationTextObserveWidget = WidgetProperties.text(SWT.Modify)
376:                         .observe(locationText);
377:                 final IObservableValue locationGenerationInfoObserveValue = PojoProperties.value("location") //$NON-NLS-1$
378:                         .observe(generationInfo);
379:                 bindingContext.bindValue(
380:                         observeTextLocationTextObserveWidget, locationGenerationInfoObserveValue, new UpdateValueStrategy()
381:                                 .setConverter(new StringToURIConverter()).setAfterConvertValidator(new LocationValidator()),
382:                         new UpdateValueStrategy().setConverter(new URItoStringConverter()));
383:
384:                 final IObservableValue observeTextGeneratedTextObserveWidget = WidgetProperties.text(SWT.Modify)
385:                         .observe(generatedText);
386:                 final IObservableValue generatedStringGenerationInfoObserveValue = PojoProperties.value("generatedString") //$NON-NLS-1$
387:                         .observe(generationInfo);
388:                 bindingContext.bindValue(observeTextGeneratedTextObserveWidget, generatedStringGenerationInfoObserveValue, null,
389:                         null);
390:
391:                 if (generationInfo.getWrapper() != null) {
392:                         final IObservableValue observeSelectionBtnWrapObserveWidget = WidgetProperties.buttonSelection()
393:                                 .observe(btnWrap);
394:                         final IObservableValue wrapGenerationInfoObserveValue = PojoProperties.value("wrap") //$NON-NLS-1$
395:                                 .observe(generationInfo);
396:                         bindingContext.bindValue(observeSelectionBtnWrapObserveWidget, wrapGenerationInfoObserveValue, null, null);
397:                 }
398:
399:                 return bindingContext;
400:         }
401: }