Go to the documentation of this file.00001
00007 package org.best_of_robotics.model.datatypes.presentation;
00008
00009
00010 import java.util.ArrayList;
00011 import java.util.Arrays;
00012 import java.util.Collection;
00013 import java.util.Collections;
00014 import java.util.HashMap;
00015 import java.util.List;
00016 import java.util.Map;
00017 import java.util.MissingResourceException;
00018 import java.util.StringTokenizer;
00019
00020 import org.best_of_robotics.model.datatypes.DatatypesFactory;
00021 import org.best_of_robotics.model.datatypes.DatatypesPackage;
00022 import org.best_of_robotics.model.datatypes.provider.DataTypesEditPlugin;
00023 import org.eclipse.core.resources.IContainer;
00024 import org.eclipse.core.resources.IFile;
00025 import org.eclipse.core.resources.IFolder;
00026 import org.eclipse.core.resources.IProject;
00027 import org.eclipse.core.resources.IResource;
00028 import org.eclipse.core.resources.ResourcesPlugin;
00029 import org.eclipse.core.runtime.IProgressMonitor;
00030 import org.eclipse.core.runtime.Path;
00031 import org.eclipse.emf.common.CommonPlugin;
00032 import org.eclipse.emf.common.util.URI;
00033 import org.eclipse.emf.ecore.EClass;
00034 import org.eclipse.emf.ecore.EClassifier;
00035 import org.eclipse.emf.ecore.EObject;
00036 import org.eclipse.emf.ecore.resource.Resource;
00037 import org.eclipse.emf.ecore.resource.ResourceSet;
00038 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
00039 import org.eclipse.emf.ecore.xmi.XMLResource;
00040 import org.eclipse.emf.edit.ui.provider.ExtendedImageRegistry;
00041 import org.eclipse.jface.dialogs.MessageDialog;
00042 import org.eclipse.jface.viewers.ISelection;
00043 import org.eclipse.jface.viewers.IStructuredSelection;
00044 import org.eclipse.jface.viewers.StructuredSelection;
00045 import org.eclipse.jface.wizard.Wizard;
00046 import org.eclipse.jface.wizard.WizardPage;
00047 import org.eclipse.swt.SWT;
00048 import org.eclipse.swt.events.ModifyEvent;
00049 import org.eclipse.swt.events.ModifyListener;
00050 import org.eclipse.swt.layout.GridData;
00051 import org.eclipse.swt.layout.GridLayout;
00052 import org.eclipse.swt.widgets.Combo;
00053 import org.eclipse.swt.widgets.Composite;
00054 import org.eclipse.swt.widgets.Label;
00055 import org.eclipse.ui.INewWizard;
00056 import org.eclipse.ui.IWorkbench;
00057 import org.eclipse.ui.IWorkbenchPage;
00058 import org.eclipse.ui.IWorkbenchPart;
00059 import org.eclipse.ui.IWorkbenchWindow;
00060 import org.eclipse.ui.PartInitException;
00061 import org.eclipse.ui.actions.WorkspaceModifyOperation;
00062 import org.eclipse.ui.dialogs.WizardNewFileCreationPage;
00063 import org.eclipse.ui.part.FileEditorInput;
00064 import org.eclipse.ui.part.ISetSelectionTarget;
00065
00066
00073 public class DatatypesModelWizard extends Wizard implements INewWizard {
00080 public static final List<String> FILE_EXTENSIONS =
00081 Collections.unmodifiableList(Arrays.asList(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesEditorFilenameExtensions").split("\\s*,\\s*")));
00082
00089 public static final String FORMATTED_FILE_EXTENSIONS =
00090 DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesEditorFilenameExtensions").replaceAll("\\s*,\\s*", ", ");
00091
00098 protected DatatypesPackage datatypesPackage = DatatypesPackage.eINSTANCE;
00099
00106 protected DatatypesFactory datatypesFactory = datatypesPackage.getDatatypesFactory();
00107
00114 protected DatatypesModelWizardNewFileCreationPage newFileCreationPage;
00115
00122 protected DatatypesModelWizardInitialObjectCreationPage initialObjectCreationPage;
00123
00130 protected IStructuredSelection selection;
00131
00138 protected IWorkbench workbench;
00139
00146 protected List<String> initialObjectNames;
00147
00154 public void init(IWorkbench workbench, IStructuredSelection selection) {
00155 this.workbench = workbench;
00156 this.selection = selection;
00157 setWindowTitle(DataTypesEditorPlugin.INSTANCE.getString("_UI_Wizard_label"));
00158 setDefaultPageImageDescriptor(ExtendedImageRegistry.INSTANCE.getImageDescriptor(DataTypesEditorPlugin.INSTANCE.getImage("full/wizban/NewDatatypes")));
00159 }
00160
00168 protected Collection<String> getInitialObjectNames() {
00169 if (initialObjectNames == null) {
00170 initialObjectNames = new ArrayList<String>();
00171 for (EClassifier eClassifier : datatypesPackage.getEClassifiers()) {
00172 if (eClassifier instanceof EClass) {
00173 EClass eClass = (EClass)eClassifier;
00174
00175
00176 if (!eClass.isAbstract() && eClass.getName().equals("TypesLibrary")) {
00177 initialObjectNames.add(eClass.getName());
00178 }
00179 }
00180 }
00181 Collections.sort(initialObjectNames, CommonPlugin.INSTANCE.getComparator());
00182 }
00183 return initialObjectNames;
00184 }
00185
00193 protected EObject createInitialModel() {
00194 EClass eClass = (EClass)datatypesPackage.getEClassifier(initialObjectCreationPage.getInitialObjectName());
00195 EObject rootObject = datatypesFactory.create(eClass);
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211 return rootObject;
00212 }
00213
00220 @Override
00221 public boolean performFinish() {
00222 try {
00223
00224
00225 final IFile modelFile = getModelFile();
00226
00227
00228
00229 WorkspaceModifyOperation operation =
00230 new WorkspaceModifyOperation() {
00231 @Override
00232 protected void execute(IProgressMonitor progressMonitor) {
00233 try {
00234
00235
00236 ResourceSet resourceSet = new ResourceSetImpl();
00237
00238
00239
00240 URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true);
00241
00242
00243
00244 Resource resource = resourceSet.createResource(fileURI);
00245
00246
00247
00248 EObject rootObject = createInitialModel();
00249 if (rootObject != null) {
00250 resource.getContents().add(rootObject);
00251 }
00252
00253
00254
00255 Map<Object, Object> options = new HashMap<Object, Object>();
00256 options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
00257 resource.save(options);
00258 }
00259 catch (Exception exception) {
00260 DataTypesEditorPlugin.INSTANCE.log(exception);
00261 }
00262 finally {
00263 progressMonitor.done();
00264 }
00265 }
00266 };
00267
00268 getContainer().run(false, false, operation);
00269
00270
00271
00272 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
00273 IWorkbenchPage page = workbenchWindow.getActivePage();
00274 final IWorkbenchPart activePart = page.getActivePart();
00275 if (activePart instanceof ISetSelectionTarget) {
00276 final ISelection targetSelection = new StructuredSelection(modelFile);
00277 getShell().getDisplay().asyncExec
00278 (new Runnable() {
00279 public void run() {
00280 ((ISetSelectionTarget)activePart).selectReveal(targetSelection);
00281 }
00282 });
00283 }
00284
00285
00286
00287 try {
00288 page.openEditor
00289 (new FileEditorInput(modelFile),
00290 workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
00291 }
00292 catch (PartInitException exception) {
00293 MessageDialog.openError(workbenchWindow.getShell(), DataTypesEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
00294 return false;
00295 }
00296
00297 return true;
00298 }
00299 catch (Exception exception) {
00300 DataTypesEditorPlugin.INSTANCE.log(exception);
00301 return false;
00302 }
00303 }
00304
00311 public class DatatypesModelWizardNewFileCreationPage extends WizardNewFileCreationPage {
00318 public DatatypesModelWizardNewFileCreationPage(String pageId, IStructuredSelection selection) {
00319 super(pageId, selection);
00320 }
00321
00328 @Override
00329 protected boolean validatePage() {
00330 if (super.validatePage()) {
00331 String extension = new Path(getFileName()).getFileExtension();
00332 if (extension == null || !FILE_EXTENSIONS.contains(extension)) {
00333 String key = FILE_EXTENSIONS.size() > 1 ? "_WARN_FilenameExtensions" : "_WARN_FilenameExtension";
00334 setErrorMessage(DataTypesEditorPlugin.INSTANCE.getString(key, new Object [] { FORMATTED_FILE_EXTENSIONS }));
00335 return false;
00336 }
00337 return true;
00338 }
00339 return false;
00340 }
00341
00347 public IFile getModelFile() {
00348 return ResourcesPlugin.getWorkspace().getRoot().getFile(getContainerFullPath().append(getFileName()));
00349 }
00350 }
00351
00358 public class DatatypesModelWizardInitialObjectCreationPage extends WizardPage {
00364 protected Combo initialObjectField;
00365
00371 protected List<String> encodings;
00372
00378 protected Combo encodingField;
00379
00386 public DatatypesModelWizardInitialObjectCreationPage(String pageId) {
00387 super(pageId);
00388 }
00389
00395 public void createControl(Composite parent) {
00396 Composite composite = new Composite(parent, SWT.NONE); {
00397 GridLayout layout = new GridLayout();
00398 layout.numColumns = 1;
00399 layout.verticalSpacing = 12;
00400 composite.setLayout(layout);
00401
00402 GridData data = new GridData();
00403 data.verticalAlignment = GridData.FILL;
00404 data.grabExcessVerticalSpace = true;
00405 data.horizontalAlignment = GridData.FILL;
00406 composite.setLayoutData(data);
00407 }
00408
00409 Label containerLabel = new Label(composite, SWT.LEFT);
00410 {
00411 containerLabel.setText(DataTypesEditorPlugin.INSTANCE.getString("_UI_ModelObject"));
00412
00413 GridData data = new GridData();
00414 data.horizontalAlignment = GridData.FILL;
00415 containerLabel.setLayoutData(data);
00416 }
00417
00418 initialObjectField = new Combo(composite, SWT.BORDER);
00419 {
00420 GridData data = new GridData();
00421 data.horizontalAlignment = GridData.FILL;
00422 data.grabExcessHorizontalSpace = true;
00423 initialObjectField.setLayoutData(data);
00424 }
00425
00426 for (String objectName : getInitialObjectNames()) {
00427 initialObjectField.add(getLabel(objectName));
00428 }
00429
00430 if (initialObjectField.getItemCount() == 1) {
00431 initialObjectField.select(0);
00432 }
00433 initialObjectField.addModifyListener(validator);
00434
00435 Label encodingLabel = new Label(composite, SWT.LEFT);
00436 {
00437 encodingLabel.setText(DataTypesEditorPlugin.INSTANCE.getString("_UI_XMLEncoding"));
00438
00439 GridData data = new GridData();
00440 data.horizontalAlignment = GridData.FILL;
00441 encodingLabel.setLayoutData(data);
00442 }
00443 encodingField = new Combo(composite, SWT.BORDER);
00444 {
00445 GridData data = new GridData();
00446 data.horizontalAlignment = GridData.FILL;
00447 data.grabExcessHorizontalSpace = true;
00448 encodingField.setLayoutData(data);
00449 }
00450
00451 for (String encoding : getEncodings()) {
00452 encodingField.add(encoding);
00453 }
00454
00455 encodingField.select(0);
00456 encodingField.addModifyListener(validator);
00457
00458 setPageComplete(validatePage());
00459 setControl(composite);
00460 }
00461
00467 protected ModifyListener validator =
00468 new ModifyListener() {
00469 public void modifyText(ModifyEvent e) {
00470 setPageComplete(validatePage());
00471 }
00472 };
00473
00479 protected boolean validatePage() {
00480 return getInitialObjectName() != null && getEncodings().contains(encodingField.getText());
00481 }
00482
00488 @Override
00489 public void setVisible(boolean visible) {
00490 super.setVisible(visible);
00491 if (visible) {
00492 if (initialObjectField.getItemCount() == 1) {
00493 initialObjectField.clearSelection();
00494 encodingField.setFocus();
00495 }
00496 else {
00497 encodingField.clearSelection();
00498 initialObjectField.setFocus();
00499 }
00500 }
00501 }
00502
00508 public String getInitialObjectName() {
00509 String label = initialObjectField.getText();
00510
00511 for (String name : getInitialObjectNames()) {
00512 if (getLabel(name).equals(label)) {
00513 return name;
00514 }
00515 }
00516 return null;
00517 }
00518
00524 public String getEncoding() {
00525 return encodingField.getText();
00526 }
00527
00534 protected String getLabel(String typeName) {
00535 try {
00536 return DataTypesEditPlugin.INSTANCE.getString("_UI_" + typeName + "_type");
00537 }
00538 catch(MissingResourceException mre) {
00539 DataTypesEditorPlugin.INSTANCE.log(mre);
00540 }
00541 return typeName;
00542 }
00543
00549 protected Collection<String> getEncodings() {
00550 if (encodings == null) {
00551 encodings = new ArrayList<String>();
00552 for (StringTokenizer stringTokenizer = new StringTokenizer(DataTypesEditorPlugin.INSTANCE.getString("_UI_XMLEncodingChoices")); stringTokenizer.hasMoreTokens(); ) {
00553 encodings.add(stringTokenizer.nextToken());
00554 }
00555 }
00556 return encodings;
00557 }
00558 }
00559
00566 @Override
00567 public void addPages() {
00568
00569
00570 newFileCreationPage = new DatatypesModelWizardNewFileCreationPage("Whatever", selection);
00571 newFileCreationPage.setTitle(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesModelWizard_label"));
00572 newFileCreationPage.setDescription(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesModelWizard_description"));
00573 newFileCreationPage.setFileName(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesEditorFilenameDefaultBase") + "." + FILE_EXTENSIONS.get(0));
00574 addPage(newFileCreationPage);
00575
00576
00577
00578 if (selection != null && !selection.isEmpty()) {
00579
00580
00581 Object selectedElement = selection.iterator().next();
00582 if (selectedElement instanceof IResource) {
00583
00584
00585 IResource selectedResource = (IResource)selectedElement;
00586 if (selectedResource.getType() == IResource.FILE) {
00587 selectedResource = selectedResource.getParent();
00588 }
00589
00590
00591
00592 if (selectedResource instanceof IFolder || selectedResource instanceof IProject) {
00593
00594
00595 newFileCreationPage.setContainerFullPath(selectedResource.getFullPath());
00596
00597
00598
00599 String defaultModelBaseFilename = DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesEditorFilenameDefaultBase");
00600 String defaultModelFilenameExtension = FILE_EXTENSIONS.get(0);
00601 String modelFilename = defaultModelBaseFilename + "." + defaultModelFilenameExtension;
00602 for (int i = 1; ((IContainer)selectedResource).findMember(modelFilename) != null; ++i) {
00603 modelFilename = defaultModelBaseFilename + i + "." + defaultModelFilenameExtension;
00604 }
00605 newFileCreationPage.setFileName(modelFilename);
00606 }
00607 }
00608 }
00609 initialObjectCreationPage = new DatatypesModelWizardInitialObjectCreationPage("Whatever2");
00610 initialObjectCreationPage.setTitle(DataTypesEditorPlugin.INSTANCE.getString("_UI_DatatypesModelWizard_label"));
00611 initialObjectCreationPage.setDescription(DataTypesEditorPlugin.INSTANCE.getString("_UI_Wizard_initial_object_description"));
00612 addPage(initialObjectCreationPage);
00613 }
00614
00621 public IFile getModelFile() {
00622 return newFileCreationPage.getModelFile();
00623 }
00624
00625 }