diff options
author | Nicolas FAUVERGUE | 2017-01-16 08:21:14 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2017-03-03 09:07:03 +0000 |
commit | c17bf6e6525851a6fdecee41c7e3a8b9dec59f86 (patch) | |
tree | c0d931deef732fdff99b94702f14f598da070126 | |
parent | dd8229e833e31ee60aa4219b8fa56b3a43b31311 (diff) | |
download | org.eclipse.papyrus-c17bf6e6525851a6fdecee41c7e3a8b9dec59f86.tar.gz org.eclipse.papyrus-c17bf6e6525851a6fdecee41c7e3a8b9dec59f86.tar.xz org.eclipse.papyrus-c17bf6e6525851a6fdecee41c7e3a8b9dec59f86.zip |
Bug 494531: [Property][Table] Editor is dirty after the creation of the
table in the Property View
https://bugs.eclipse.org/bugs/show_bug.cgi?id=494531
- The used command need to be execute with a GMFUnsafe because this
don't need to be in the command stack
- Manage the table as a proper Resource.
Change-Id: I4a3d418467516f9a4b4aea37d8eaa05c81e59dbb
Signed-off-by: Nicolas FAUVERGUE <nicolas.fauvergue@cea.fr>
6 files changed, 293 insertions, 27 deletions
diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/META-INF/MANIFEST.MF b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/META-INF/MANIFEST.MF index 5516bc6d1f7..ce9f44af5fc 100644 --- a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/META-INF/MANIFEST.MF +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/META-INF/MANIFEST.MF @@ -40,6 +40,7 @@ Export-Package: org.eclipse.papyrus.infra.nattable, org.eclipse.papyrus.infra.nattable.preferences, org.eclipse.papyrus.infra.nattable.provider, org.eclipse.papyrus.infra.nattable.reorder, + org.eclipse.papyrus.infra.nattable.resource, org.eclipse.papyrus.infra.nattable.selection, org.eclipse.papyrus.infra.nattable.sort, org.eclipse.papyrus.infra.nattable.style.configattribute, diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResource.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResource.java new file mode 100644 index 00000000000..8aed2b487c0 --- /dev/null +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResource.java @@ -0,0 +1,124 @@ +/***************************************************************************** + * Copyright (c) 2017 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.nattable.resource; + +import java.io.IOException; +import java.util.Collections; +import java.util.Map; + +import org.eclipse.emf.common.command.CompoundCommand; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.eclipse.emf.edit.command.SetCommand; +import org.eclipse.emf.transaction.RollbackException; +import org.eclipse.emf.transaction.TransactionalEditingDomain; +import org.eclipse.papyrus.infra.core.resource.ModelSet; +import org.eclipse.papyrus.infra.emf.gmf.util.GMFUnsafe; +import org.eclipse.papyrus.infra.nattable.Activator; +import org.eclipse.papyrus.infra.nattable.model.nattable.NattablePackage; +import org.eclipse.papyrus.infra.nattable.model.nattable.Table; +import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxisprovider.NattableaxisproviderPackage; + +/** + * The resource corresponding to the properties files. + * + * @since 3.0 + */ +public class TableResource extends XMIResourceImpl { + + /** + * Constructor. + * + * @param uri + * The uri of the resource. + */ + public TableResource(final URI uri) { + super(uri); + } + + /** + * {@inheritDoc} + * + * @see org.eclipse.emf.ecore.resource.impl.ResourceImpl#save(java.util.Map) + */ + @Override + public void save(final Map<?, ?> options) throws IOException { + + final TransactionalEditingDomain domain = ((ModelSet) getResourceSet()).getTransactionalEditingDomain(); + + final CompoundCommand compoundCommand = new CompoundCommand("Modify top tables"); //$NON-NLS-1$ + for (final EObject top : getContents()) { + if (top instanceof Table) { + compoundCommand.append(getDisposeTableCommand(domain, (Table) top)); + } + } + + try { + GMFUnsafe.write(domain, compoundCommand); + } catch (InterruptedException e) { + Activator.log.error(e); + } catch (RollbackException e) { + Activator.log.error(e); + } finally { + super.save(options); + } + } + + /** + * @see org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl#useUUIDs() + * + * @return + */ + @Override + protected boolean useUUIDs() { + return true; + } + + /** + * + * @param domain + * the editing domain + * @param table + * the table to clean before dispose + * @return + * the command to use to clean the table before disposing it + */ + protected void disposeTableCommand(final Table table) { + table.setContext(null); + table.setOwner(null); + table.getCurrentRowAxisProvider().eSet(NattableaxisproviderPackage.eINSTANCE.getAxisProvider_Axis(), Collections.emptyList()); + } + + /** + * This allows to delete the possible project dependencies. + * + * @param domain + * the editing domain. + * @param table + * the table to clean before dispose. + * @return + * the command to use to clean the table before disposing it. + * @since 2.0 + */ + protected CompoundCommand getDisposeTableCommand(final TransactionalEditingDomain domain, final Table table) { + final CompoundCommand disposeCommand = new CompoundCommand("Command used to clean the table before disposing it"); //$NON-NLS-1$ + disposeCommand.append(SetCommand.create(domain, table, NattablePackage.eINSTANCE.getTable_Context(), null)); + disposeCommand.append(SetCommand.create(domain, table, NattablePackage.eINSTANCE.getTable_Owner(), null)); + // assuming the table is synchronized and not inverted : + disposeCommand.append(SetCommand.create(domain, table.getCurrentRowAxisProvider(), NattableaxisproviderPackage.eINSTANCE.getAxisProvider_Axis(), Collections.emptyList())); + + return disposeCommand; + } +} diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceFactory.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceFactory.java new file mode 100644 index 00000000000..0777c8a5fc7 --- /dev/null +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceFactory.java @@ -0,0 +1,66 @@ +/***************************************************************************** + * Copyright (c) 2017 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.nattable.resource; + +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; +import org.eclipse.gmf.runtime.emf.core.internal.util.EMFCoreConstants; + +/** + * The resource factory for the properties files. + * + * @since 3.0 + */ +@SuppressWarnings("restriction") +public class TableResourceFactory extends XMIResourceFactoryImpl { + + /** The save options. */ + private static final Map<Object, Object> saveOptions = new HashMap<Object, Object>(); + + static { + saveOptions.put(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER); + saveOptions.put(Resource.OPTION_LINE_DELIMITER, Resource.OPTION_LINE_DELIMITER_UNSPECIFIED); + saveOptions.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, true); + saveOptions.put(XMIResource.OPTION_ENCODING,"UTF-8"); //$NON-NLS-1$ + saveOptions.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE); + saveOptions.put(XMLResource.OPTION_PROCESS_DANGLING_HREF, XMLResource.OPTION_PROCESS_DANGLING_HREF_DISCARD); + } + + + /** + * {@inheritDoc} + * + * @see org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl#createResource(org.eclipse.emf.common.util.URI) + */ + @Override + public Resource createResource(final URI uri) { + + final XMIResource resource = new TableResource(uri); + + if (!resource.getEncoding().equals(EMFCoreConstants.XMI_ENCODING)) { + resource.setEncoding(EMFCoreConstants.XMI_ENCODING); + } + + resource.getDefaultSaveOptions().putAll(saveOptions); + + return resource; + } + +} diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceHelper.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceHelper.java new file mode 100644 index 00000000000..a09f26fef4a --- /dev/null +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/resource/TableResourceHelper.java @@ -0,0 +1,36 @@ +/***************************************************************************** + * Copyright (c) 2017 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.nattable.resource; + +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.papyrus.infra.nattable.utils.TableResourceConstants; + +/** + * The table resource helper needed to install the table support for the '*.table' files resources. + */ +public class TableResourceHelper { + + /** + * This allows to install the correct resource factory corresponding to the table files. + * + * @param resourceSet + * The current resource set. + */ + public static void installTableSupport(final ResourceSet resourceSet) { + final TableResourceFactory factory = new TableResourceFactory(); + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() + .put(TableResourceConstants.TABLE_FILE_EXTENSION, factory); + } + +} diff --git a/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/TableResourceConstants.java b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/TableResourceConstants.java new file mode 100644 index 00000000000..bb4d436d01f --- /dev/null +++ b/plugins/infra/nattable/org.eclipse.papyrus.infra.nattable/src/org/eclipse/papyrus/infra/nattable/utils/TableResourceConstants.java @@ -0,0 +1,26 @@ +/***************************************************************************** + * Copyright (c) 2017 CEA LIST and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation + * + *****************************************************************************/ + +package org.eclipse.papyrus.infra.nattable.utils; + +/** + * This allows to define the table resource constants needed. + */ +public class TableResourceConstants { + + /** + * The file extension for the table. + */ + public static final String TABLE_FILE_EXTENSION = "table"; //$NON-NLS-1$ + +} diff --git a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/widgets/NattablePropertyEditor.java b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/widgets/NattablePropertyEditor.java index ef6f1fec61d..40eb6baa9f8 100644 --- a/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/widgets/NattablePropertyEditor.java +++ b/plugins/uml/properties/org.eclipse.papyrus.uml.properties/src/org/eclipse/papyrus/uml/properties/widgets/NattablePropertyEditor.java @@ -1,5 +1,5 @@ /***************************************************************************** - * Copyright (c) 2015, 2016 CEA LIST, Christian W. Damus, and others. + * Copyright (c) 2015, 2016, 2017 CEA LIST, Christian W. Damus, and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -7,9 +7,10 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * Nicolas FAUVERGUE (ALL4TEC) nicolas.fauvergue@all4tec.net - Initial API and implementation + * Nicolas FAUVERGUE (CEA LIST) nicolas.fauvergue@cea.fr - Initial API and implementation, Bug 502160, 494531 * Christian W. Damus - bugs 493858, 493853 * Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr - bugs 494537, 504745 + * *****************************************************************************/ package org.eclipse.papyrus.uml.properties.widgets; @@ -37,6 +38,7 @@ import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.emf.ecore.xmi.XMLResource; import org.eclipse.emf.edit.command.SetCommand; import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.RollbackException; import org.eclipse.emf.transaction.TransactionalEditingDomain; import org.eclipse.gef.EditPart; import org.eclipse.gef.editparts.AbstractEditPart; @@ -51,6 +53,7 @@ import org.eclipse.papyrus.infra.core.services.ServiceException; import org.eclipse.papyrus.infra.core.services.ServiceMultiException; import org.eclipse.papyrus.infra.core.services.ServiceStartKind; import org.eclipse.papyrus.infra.core.services.ServicesRegistry; +import org.eclipse.papyrus.infra.emf.gmf.util.GMFUnsafe; import org.eclipse.papyrus.infra.emf.nattable.selection.EObjectSelectionExtractor; import org.eclipse.papyrus.infra.emf.utils.EMFHelper; import org.eclipse.papyrus.infra.nattable.manager.table.INattableModelManager; @@ -70,8 +73,10 @@ import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.BooleanVa import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestyleFactory; import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.NattablestylePackage; import org.eclipse.papyrus.infra.nattable.model.nattable.nattablestyle.Style; +import org.eclipse.papyrus.infra.nattable.resource.TableResourceHelper; import org.eclipse.papyrus.infra.nattable.utils.NamedStyleConstants; import org.eclipse.papyrus.infra.nattable.utils.NattableModelManagerFactory; +import org.eclipse.papyrus.infra.nattable.utils.TableResourceConstants; import org.eclipse.papyrus.infra.properties.contexts.Property; import org.eclipse.papyrus.infra.properties.ui.modelelement.CompositeModelElement; import org.eclipse.papyrus.infra.properties.ui.modelelement.DataSource; @@ -96,10 +101,13 @@ import org.eclipse.ui.internal.views.properties.tabbed.view.TabbedPropertyCompos /** * The property editor for the nattable widget. */ +@SuppressWarnings("restriction") public class NattablePropertyEditor extends AbstractPropertyEditor { /** * The save options to use. + * + * @deprecated since 3.0. Use TableResourceFactory. */ private static final Map<Object, Object> saveOptions = new HashMap<Object, Object>(); @@ -108,7 +116,7 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { saveOptions.put(Resource.OPTION_LINE_DELIMITER, Resource.OPTION_LINE_DELIMITER_UNSPECIFIED); saveOptions.put(XMLResource.OPTION_SAVE_TYPE_INFORMATION, true); } - + /** * The folders in which we will save the table configured by the user. */ @@ -118,7 +126,10 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { * The file in which the table will be saved. * * It doesn't work using .notation as extension file. In this case, the commands are not executed, because it is read-only, but why ? + * + * @deprecated since 3.0. Use TableResourceConstants.TABLE_FILE_EXTENSION. */ + @SuppressWarnings("unused") private static final String FILE_EXTENSION = "table";//$NON-NLS-1$ /** @@ -407,9 +418,16 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { final ResourceSet resourceSet = getResourceSet(); // Bug 502160: Remove the resource from the resource set to execute the command without using the editing command stack resourceSet.getResources().remove(this.resource); - domain.getCommandStack().execute(cc); - // Bug 502160: Re-add the removed resource before the command execute - resourceSet.getResources().add(this.resource); + try { + GMFUnsafe.write(domain, cc); + } catch (InterruptedException e) { + Activator.log.error(e); + } catch (RollbackException e) { + Activator.log.error(e); + } finally { + // Bug 502160: Re-add the removed resource before the command execute + resourceSet.getResources().add(this.resource); + } if (this.table.getContext() == null) { displayError("The context of the table hasn't be set");//$NON-NLS-1$ @@ -558,9 +576,9 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { * @since 2.0 */ protected void configureLayout(final EObject sourceElement) { - //must be done first! + // must be done first! ((NattableModelManager) nattableManager).refreshNatTable(); - + // Configure the size of the parent container configureSize(sourceElement); @@ -646,6 +664,8 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { if (this.serviceRegistry != null) { URI tableURI = createTableURI(sourceElement, tableConfiguration); final ResourceSet resourceSet = getResourceSet(); + // Install the table support to manage the table as a correct Resource + TableResourceHelper.installTableSupport(resourceSet); ((ModelSet) resourceSet).createModels(tableURI); boolean exists = resourceSet.getURIConverter().exists(tableURI, Collections.emptyMap()); if (exists) { @@ -695,8 +715,10 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { /** * Create URI for the table configuration. * - * @param sourceElement The source Element - * @param tableConfiguration The tableConfiguration + * @param sourceElement + * The source Element + * @param tableConfiguration + * The tableConfiguration * @return The URI to use to save and load the table * @since 2.0 */ @@ -704,7 +726,7 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { // If the source element is an EClass, the table configuration file name // will be suffixed by the name of its eClass setRegisterTableConfigurationByEClass(null != sourceElement && sourceElement.eClass() instanceof EClass); - + IPath preferencePath = Activator.getDefault().getStateLocation(); // we create a folder to save the tables used by the property view and we start to create the name of the model owning the table preferencePath = preferencePath.append(TABLES_PREFERENCES_FOLDER_NAME).append(tableConfiguration.getType()); @@ -735,7 +757,7 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { b.append("_"); //$NON-NLS-1$ b.append(eClass.getName()); } - URI newURI = URI.createFileURI(b.toString()).appendFileExtension(FILE_EXTENSION); + URI newURI = URI.createFileURI(b.toString()).appendFileExtension(TableResourceConstants.TABLE_FILE_EXTENSION); return newURI; } @@ -950,19 +972,9 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { if (null != this.natTableWidget) { this.natTableWidget.dispose(); } - TransactionalEditingDomain domain = getTableEditingDomain(); - if (domain != null && this.table != null) { - final ResourceSet resourceSet = getResourceSet(); - // Bug 502160: Remove the resource from the resource set to execute the command without using the editing command stack - resourceSet.getResources().remove(this.resource); - Command cmd = getDisposeTableCommand(domain, this.table); - cmd.execute(); - // Bug 502160: Re-add the removed resource before the command execute - resourceSet.getResources().add(this.resource); - } if (NattablePropertyEditor.this.resource != null) { try { - NattablePropertyEditor.this.resource.save(saveOptions); + NattablePropertyEditor.this.resource.save(null); } catch (IOException e1) { Activator.log.error(e1); } @@ -987,6 +999,7 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { * @return * the command to use to clean the table before disposing it * @since 2.0 + * @deprecated since 3.0 */ protected CompoundCommand getDisposeTableCommand(final TransactionalEditingDomain domain, final Table table) { CompoundCommand disposeCommand = new CompoundCommand("Command used to clean the table before disposing it"); //$NON-NLS-1$ @@ -1076,14 +1089,14 @@ public class NattablePropertyEditor extends AbstractPropertyEditor { // Recreate the table widget, its adjuncts, and their layout createWidgets(sourceElement, feature, contexts); - + // We need to refresh the parent composite to get the needed space Composite parent = self.getParent(); boolean found = false; - while(null != parent && !found){ - if(parent instanceof TabbedPropertyComposite){ + while (null != parent && !found) { + if (parent instanceof TabbedPropertyComposite) { found = true; - }else{ + } else { parent.layout(true, true); parent.redraw(); parent.update(); |