Skip to content

Package: NewWorkspaceProjectComposite$ImportWorkspaceAdapter$1

NewWorkspaceProjectComposite$ImportWorkspaceAdapter$1

nameinstructionbranchcomplexitylinemethod
validate(Object[])
M: 36 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
{...}
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%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2013 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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.workspace.internal.ui;
15:
16: import java.io.IOException;
17: import java.util.Collections;
18: import java.util.HashSet;
19:
20: import org.eclipse.core.resources.IFile;
21: import org.eclipse.core.resources.IResource;
22: import org.eclipse.core.resources.ResourcesPlugin;
23: import org.eclipse.core.runtime.IStatus;
24: import org.eclipse.core.runtime.Status;
25: import org.eclipse.emf.common.util.URI;
26: import org.eclipse.emf.ecore.EClass;
27: import org.eclipse.emf.ecore.EObject;
28: import org.eclipse.emf.ecore.EPackage;
29: import org.eclipse.emf.ecore.resource.Resource;
30: import org.eclipse.emf.ecore.resource.ResourceSet;
31: import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
32: import org.eclipse.emf.ecore.util.EcoreUtil;
33: import org.eclipse.emf.ecore.xmi.XMLResource;
34: import org.eclipse.emf.ecp.core.util.ECPProperties;
35: import org.eclipse.emf.ecp.core.util.ECPUtil;
36: import org.eclipse.emf.ecp.spi.common.ui.CompositeFactory;
37: import org.eclipse.emf.ecp.spi.common.ui.SelectModelElementWizard;
38: import org.eclipse.emf.ecp.spi.common.ui.composites.SelectionComposite;
39: import org.eclipse.emf.ecp.spi.ui.CompositeStateObserver;
40: import org.eclipse.emf.ecp.workspace.internal.core.WorkspaceProvider;
41: import org.eclipse.jface.dialogs.MessageDialog;
42: import org.eclipse.jface.window.Window;
43: import org.eclipse.jface.wizard.WizardDialog;
44: import org.eclipse.swt.SWT;
45: import org.eclipse.swt.custom.StackLayout;
46: import org.eclipse.swt.events.ModifyEvent;
47: import org.eclipse.swt.events.ModifyListener;
48: import org.eclipse.swt.events.SelectionAdapter;
49: import org.eclipse.swt.events.SelectionEvent;
50: import org.eclipse.swt.layout.GridData;
51: import org.eclipse.swt.layout.GridLayout;
52: import org.eclipse.swt.widgets.Button;
53: import org.eclipse.swt.widgets.Composite;
54: import org.eclipse.swt.widgets.Display;
55: import org.eclipse.swt.widgets.Event;
56: import org.eclipse.swt.widgets.FileDialog;
57: import org.eclipse.swt.widgets.Label;
58: import org.eclipse.swt.widgets.Listener;
59: import org.eclipse.swt.widgets.Text;
60: import org.eclipse.ui.dialogs.ElementTreeSelectionDialog;
61: import org.eclipse.ui.dialogs.ISelectionStatusValidator;
62: import org.eclipse.ui.model.BaseWorkbenchContentProvider;
63: import org.eclipse.ui.model.WorkbenchLabelProvider;
64:
65: /**
66: * The NewWorkspaceProjectComposite allows a user to provide the information needed for the
67: * creation of a WorkspaceProject.
68: *
69: * @author Tobias Verhoeven
70: */
71: public class NewWorkspaceProjectComposite extends Composite {
72:
73:         /**
74:          * @author Jonas
75:          *
76:          */
77:         private final class ImportWorkspaceAdapter extends SelectionAdapter {
78:                 @Override
79:                 public void widgetSelected(SelectionEvent e) {
80:                         final ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(Display.getDefault()
81:                                 .getActiveShell(), new WorkbenchLabelProvider(), new BaseWorkbenchContentProvider());
82:                         dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
83:                         dialog.setAllowMultiple(false);
84:                         dialog.setValidator(new ISelectionStatusValidator() {
85:
86:                                 @Override
87:                                 public IStatus validate(Object[] selection) {
88:•                                        if (selection.length == 1) {
89:•                                                if (selection[0] instanceof IFile) {
90:                                                         final IFile file = (IFile) selection[0];
91:•                                                        if (file.getType() == IResource.FILE) {
92:                                                                 return new Status(IStatus.OK, Activator.PLUGIN_ID, IStatus.OK, null, null);
93:                                                         }
94:                                                 }
95:                                         }
96:                                         return new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
97:                                                 Messages.NewWorkspaceProjectComposite_PLEASE_SELECT_FILE,
98:                                                 null);
99:                                 }
100:                         });
101:                         dialog.setTitle(Messages.NewWorkspaceProjectComposite_SELECT_XMI);
102:
103:                         if (dialog.open() == Window.OK) {
104:                                 if (dialog.getFirstResult() instanceof IFile) {
105:                                         final IFile file = (IFile) dialog.getFirstResult();
106:                                         final ResourceSet resourceSet = new ResourceSetImpl();
107:                                         final Resource resource = resourceSet.createResource(URI.createPlatformResourceURI(file
108:                                                 .getFullPath()
109:                                                 .toString(), true));
110:                                         try {
111:                                                 resource.load(null);
112:                                                 importFileText.setText(URI.createPlatformResourceURI(file.getFullPath().toString(), true)
113:                                                         .toString());
114:                                         } catch (final IOException ex) {
115:                                                 MessageDialog.openError(getShell(), Messages.NewWorkspaceProjectComposite_ERROR,
116:                                                         Messages.NewWorkspaceProjectComposite_ERROR_PARSINGXMIFILE);
117:                                         }
118:                                 }
119:                         }
120:                 }
121:         }
122:
123:         private final CompositeStateObserver compositeStateObserver;
124:         private final ECPProperties properties;
125:         private boolean complete;
126:
127:         private Text newFileText;
128:         private Text rootClassText;
129:         private Text importFileText;
130:         private Button createButton;
131:         private Button importButton;
132:         private StackLayout providerStackLayout;
133:         private Composite providerStackComposite;
134:         private Composite newProjectComposite;
135:         private Composite importProjectComposite;
136:
137:         private Resource resource;
138:         private EClass eClass;
139:         private Composite grpDoYouWant;
140:         private Button newWorkspaceButton;
141:         private Button importWorkspaceButton;
142:         private Label label;
143:
144:         /**
145:          * Instantiates a new new workspace project composite.
146:          *
147:          * @param parent a widget which will be the parent of the new instance (cannot be null)
148:          * @param observer the observer
149:          * @param projectProperties the project properties
150:          */
151:         public NewWorkspaceProjectComposite(Composite parent, CompositeStateObserver observer,
152:                 ECPProperties projectProperties) {
153:                 super(parent, SWT.NONE);
154:                 properties = projectProperties;
155:                 compositeStateObserver = observer;
156:                 setLayout(new GridLayout(1, false));
157:                 createStackComposite(parent);
158:                 notifyObserver();
159:         }
160:
161:         private void createStackComposite(Composite parent) {
162:
163:                 providerStackLayout = new StackLayout();
164:
165:                 grpDoYouWant = new Composite(this, SWT.BORDER);
166:                 grpDoYouWant.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
167:                 grpDoYouWant.setLayout(new GridLayout(2, false));
168:
169:                 createButton = new Button(grpDoYouWant, SWT.RADIO);
170:                 createButton.setText(Messages.NewWorkspaceProjectComposite_CREATE_EMPTY_PROJECT);
171:
172:                 createButton.addSelectionListener(new SelectionAdapter() {
173:                         @Override
174:                         public void widgetSelected(SelectionEvent e) {
175:                                 providerStackLayout.topControl = newProjectComposite;
176:                                 checkStatusChanged();
177:                                 providerStackComposite.layout();
178:                         }
179:                 });
180:
181:                 createButton.setSelection(true);
182:                 importButton = new Button(grpDoYouWant, SWT.RADIO);
183:                 importButton.setText(Messages.NewWorkspaceProjectComposite_IMPORT_XMI_FILE);
184:
185:                 importButton.addSelectionListener(new SelectionAdapter() {
186:                         @Override
187:                         public void widgetSelected(SelectionEvent e) {
188:                                 providerStackLayout.topControl = importProjectComposite;
189:                                 checkStatusChanged();
190:                                 providerStackComposite.layout();
191:                         }
192:                 });
193:
194:                 providerStackComposite = new Composite(this, SWT.NONE);
195:                 providerStackComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
196:                 providerStackComposite.setLayout(providerStackLayout);
197:
198:                 createNewWorkspaceProjectComposite(providerStackComposite);
199:                 createImportWorkspaceProjectComposite(providerStackComposite);
200:
201:                 providerStackLayout.topControl = newProjectComposite;
202:
203:                 final Button rootClassButton = new Button(newProjectComposite, SWT.ARROW);
204:                 rootClassButton.setText(Messages.NewWorkspaceProjectComposite_CHOOSE_ROOT_CLASS);
205:                 rootClassButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
206:
207:                 rootClassButton.addListener(SWT.Selection, new Listener() {
208:                         @Override
209:                         public void handleEvent(Event event) {
210:                                 eClass = openClassSelectionDialog(newProjectComposite);
211:                                 fillResource();
212:                                 if (eClass != null) {
213:                                         rootClassText.setText(eClass.getEPackage().getNsURI() + "/" + eClass.getName()); //$NON-NLS-1$
214:                                 }
215:                         }
216:                 });
217:         }
218:
219:         private void createImportWorkspaceProjectComposite(final Composite composite) {
220:
221:                 importProjectComposite = new Composite(composite, SWT.BORDER);
222:                 importProjectComposite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
223:                 importProjectComposite.setLayout(new GridLayout(3, false));
224:
225:                 final Label selectFileLabel = new Label(importProjectComposite, SWT.NONE);
226:                 selectFileLabel.setText(Messages.NewWorkspaceProjectComposite_SELECT_FILE);
227:
228:                 importFileText = new Text(importProjectComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
229:                 importFileText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
230:                 importFileText.addModifyListener(new ModifyListener() {
231:                         @Override
232:                         public void modifyText(ModifyEvent e) {
233:                                 checkStatusChanged();
234:                         }
235:                 });
236:                 new Label(importProjectComposite, SWT.NONE);
237:
238:                 final Button importFileButton = new Button(importProjectComposite, SWT.NONE);
239:                 importFileButton.addSelectionListener(new SelectionAdapter() {
240:                         @Override
241:                         public void widgetSelected(SelectionEvent e) {
242:                                 final FileDialog fileDialog = new FileDialog(composite.getShell(), SWT.OPEN);
243:                                 fileDialog.setText(Messages.NewWorkspaceProjectComposite_OPEN);
244:                                 fileDialog.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString());
245:
246:                                 final String path = fileDialog.open();
247:                                 if (path != null) {
248:                                         final ResourceSet resourceSet = new ResourceSetImpl();
249:                                         final Resource resource = resourceSet.createResource(URI.createFileURI(path));
250:                                         try {
251:                                                 resource.load(null);
252:                                                 importFileText.setText(URI.createFileURI(path).toString());
253:                                         } catch (final IOException ex) {
254:                                                 MessageDialog.openError(getShell(), Messages.NewWorkspaceProjectComposite_ERROR,
255:                                                         Messages.NewWorkspaceProjectComposite_ERROR_PARSINGXMIFILE);
256:                                         }
257:
258:                                 }
259:                         }
260:                 });
261:                 importFileButton.setText(Messages.NewWorkspaceProjectComposite_BROWSE_FILE_SYSTEM);
262:
263:                 importWorkspaceButton = new Button(importProjectComposite, SWT.NONE);
264:                 importWorkspaceButton.addSelectionListener(new ImportWorkspaceAdapter());
265:                 importWorkspaceButton.setText(Messages.NewWorkspaceProjectComposite_BROWSE_WORKSPACE);
266:         }
267:
268:         /**
269:          * @param composite
270:          */
271:         private void createNewWorkspaceProjectComposite(final Composite composite) {
272:
273:                 newProjectComposite = new Composite(composite, SWT.BORDER);
274:                 newProjectComposite.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false, 2, 1));
275:                 newProjectComposite.setLayout(new GridLayout(4, false));
276:
277:                 final Label filenameLabel = new Label(newProjectComposite, SWT.NONE);
278:                 filenameLabel.setText(Messages.NewWorkspaceProjectComposite_FILENAME);
279:
280:                 newFileText = new Text(newProjectComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
281:                 newFileText.addModifyListener(new ModifyListener() {
282:                         @Override
283:                         public void modifyText(ModifyEvent e) {
284:                                 checkStatusChanged();
285:                         }
286:                 });
287:                 newFileText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1));
288:                 new Label(newProjectComposite, SWT.NONE);
289:
290:                 final Button newFileButton = new Button(newProjectComposite, SWT.NONE);
291:                 newFileButton.setText(Messages.NewWorkspaceProjectComposite_BROWSE_FILE_SYSTEM);
292:
293:                 newFileButton.addSelectionListener(new SelectionAdapter() {
294:                         @Override
295:                         public void widgetSelected(SelectionEvent e) {
296:                                 final FileDialog fileDialog = new FileDialog(composite.getShell(), SWT.SAVE);
297:                                 fileDialog.setText(Messages.NewWorkspaceProjectComposite_OPEN);
298:                                 fileDialog.setOverwrite(true);
299:                                 fileDialog.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString());
300:                                 final String path = fileDialog.open();
301:
302:                                 if (path != null) {
303:                                         final URI pathURI = URI.createFileURI(path);
304:                                         final ResourceSet resourceSet = new ResourceSetImpl();
305:                                         resource = resourceSet.createResource(pathURI);
306:
307:                                         fillResource();
308:                                         newFileText.setText(pathURI.toString());
309:                                 }
310:                         }
311:                 });
312:
313:                 newWorkspaceButton = new Button(newProjectComposite, SWT.NONE);
314:                 newWorkspaceButton.addSelectionListener(new SelectionAdapter() {
315:                         @Override
316:                         public void widgetSelected(SelectionEvent e) {
317:
318:                                 final NewXMIFileWizard newXMIFileWizard = new NewXMIFileWizard();
319:                                 final WizardDialog wizardDialog = new WizardDialog(composite.getShell(), newXMIFileWizard);
320:                                 if (wizardDialog.open() == Window.OK) {
321:                                         final ResourceSet resourceSet = new ResourceSetImpl();
322:                                         resource = resourceSet.createResource(newXMIFileWizard.getFileURI());
323:                                         fillResource();
324:                                         newFileText.setText(newXMIFileWizard.getFileURI().toString());
325:                                 } else {
326:
327:                                 }
328:                         }
329:                 });
330:
331:                 newWorkspaceButton.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1));
332:                 newWorkspaceButton.setText(Messages.NewWorkspaceProjectComposite_BROWSE_WORKSPACE);
333:                 new Label(newProjectComposite, SWT.NONE);
334:
335:                 label = new Label(newProjectComposite, SWT.SEPARATOR | SWT.HORIZONTAL);
336:                 label.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 4, 1));
337:
338:                 final Label rootClassLabel = new Label(newProjectComposite, SWT.NONE);
339:                 rootClassLabel.setText("Root Class:");//$NON-NLS-1$
340:
341:                 rootClassText = new Text(newProjectComposite, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY);
342:                 rootClassText.addModifyListener(new ModifyListener() {
343:                         @Override
344:                         public void modifyText(ModifyEvent e) {
345:                                 checkStatusChanged();
346:                         }
347:                 });
348:                 rootClassText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
349:         }
350:
351:         private EClass openClassSelectionDialog(Composite composite) {
352:
353:                 final SelectionComposite helper = CompositeFactory.getSelectModelClassComposite(new HashSet<EPackage>(),
354:                         ECPUtil.getAllRegisteredEPackages(), new HashSet<EClass>());
355:
356:                 final SelectModelElementWizard wizard = new SelectModelElementWizard(
357:                         Messages.NewWorkspaceProjectComposite_CHOOSE_ROOT_CLASS,
358:                         Messages.NewWorkspaceProjectComposite_CHOOSE_ROOT_CLASS,
359:                         Messages.NewWorkspaceProjectComposite_CHOOSE_ROOT_CLASS,
360:                         Messages.NewWorkspaceProjectComposite_SELECT_ROOT_CLASS_DESCRIPTION);
361:                 wizard.setCompositeProvider(helper);
362:
363:                 final WizardDialog wd = new WizardDialog(composite.getShell(), wizard);
364:
365:                 final int wizardResult = wd.open();
366:                 if (wizardResult == Window.OK) {
367:                         final Object[] selection = helper.getSelection();
368:                         if (selection == null || selection.length == 0) {
369:                                 return null;
370:                         }
371:                         return (EClass) selection[0];
372:                 }
373:                 return null;
374:         }
375:
376:         private void notifyObserver() {
377:                 if (compositeStateObserver != null) {
378:                         compositeStateObserver.compositeChangedState(this, complete, properties);
379:                 }
380:         }
381:
382:         private void checkStatusChanged() {
383:
384:                 properties.addProperty(WorkspaceProvider.PROP_ROOT_URI, createButton.getSelection() ? newFileText.getText()
385:                         : importFileText.getText());
386:
387:                 final boolean pendingStatus = createButton.getSelection() && newFileTextStatus() && rootClassTextStatus()
388:                         || importButton.getSelection() && importFileTextStatus();
389:
390:                 if (pendingStatus != complete) {
391:                         complete = pendingStatus;
392:                         notifyObserver();
393:                 }
394:         }
395:
396:         private boolean newFileTextStatus() {
397:                 return nonEmptyString(newFileText.getText());
398:         }
399:
400:         private boolean importFileTextStatus() {
401:                 return nonEmptyString(importFileText.getText());
402:         }
403:
404:         private boolean rootClassTextStatus() {
405:                 return nonEmptyString(rootClassText.getText());
406:         }
407:
408:         private boolean nonEmptyString(String string) {
409:                 return string != null && string.length() > 0 && string.trim().length() == string.length();
410:         }
411:
412:         private void fillResource() {
413:                 if (resource != null && eClass != null) {
414:                         final EObject root = EcoreUtil.create(eClass);
415:
416:                         resource.getContents().clear();
417:
418:                         resource.getContents().add(root);
419:                         try {
420:                                 resource.save(Collections.singletonMap(XMLResource.OPTION_ENCODING, "UTF-8")); //$NON-NLS-1$
421:                         } catch (final IOException ex) {
422:                                 // TODO Auto-generated catch block
423:                                 ex.printStackTrace();
424:                         }
425:                 }
426:         }
427: }