Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvlorenzo2012-08-24 15:57:50 +0000
committervlorenzo2012-08-24 15:57:50 +0000
commit551ce8e8d93b6971eee3162057b3d3749479f4a0 (patch)
tree8ca0e317610a579f93fa7ce0650c26ae7dee69fd /incoming
parent50fec24e779d26f25be7ca0675b80b330c99926d (diff)
downloadorg.eclipse.papyrus-551ce8e8d93b6971eee3162057b3d3749479f4a0.tar.gz
org.eclipse.papyrus-551ce8e8d93b6971eee3162057b3d3749479f4a0.tar.xz
org.eclipse.papyrus-551ce8e8d93b6971eee3162057b3d3749479f4a0.zip
388005: [Diagram][table] the diagram/table are created without index
https://bugs.eclipse.org/bugs/show_bug.cgi?id=388005
Diffstat (limited to 'incoming')
-rw-r--r--incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/editor/EditorNameInitializer.java126
-rw-r--r--incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/handlers/AbstractCreateTableEditorHandler.java15
2 files changed, 137 insertions, 4 deletions
diff --git a/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/editor/EditorNameInitializer.java b/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/editor/EditorNameInitializer.java
new file mode 100644
index 00000000000..14e4fea8582
--- /dev/null
+++ b/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/editor/EditorNameInitializer.java
@@ -0,0 +1,126 @@
+/*****************************************************************************
+ * Copyright (c) 2012 CEA LIST.
+ *
+ *
+ * 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:
+ * Vincent Lorenzo (CEA LIST) Vincent.Lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.infra.table.efacet.common.editor;
+
+import java.util.Collection;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.eclipse.emf.ecore.EClass;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EStructuralFeature;
+import org.eclipse.emf.ecore.EStructuralFeature.Setting;
+import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
+
+/**
+ *
+ * This class provides useful method to get an unused name for a new editor
+ *
+ */
+//TODO move it in an upper plugin
+public class EditorNameInitializer {
+
+ private EditorNameInitializer() {
+ //to prenvent instanciation
+ }
+
+ /**
+ * This method returns the owner for the feature called "name". In some specific case (like PapyrusTable) the name is not owned by the eobject
+ * which provides the context of the editor!
+ *
+ * @param eobject
+ * an eobject
+ * @return
+ * the eobject which contains a feature called "name" or null if not found
+ */
+ private static final EObject getNameFeatureOwner(final EObject eobject) {
+ EObject owner = eobject;
+ while(owner.eContainer() != null) {
+ EStructuralFeature nameFeature = ((EObject)eobject).eClass().getEStructuralFeature("name");
+ if(nameFeature == null) {
+ owner = owner.eContainer();
+ } else {
+ return owner;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * This method returns the feature called "name". In some specific case (like PapyrusTable) the name is not owned by the eobject
+ * which provides the context of the editor!
+ *
+ * @param eobject
+ * an eobject
+ * @return
+ * the name feature or <code>null</code> if not found
+ */
+ private static final EStructuralFeature getNameFeature(final EObject eobject) {
+ EObject owner = eobject;
+ while(owner.eContainer() != null) {
+ EStructuralFeature nameFeature = ((EObject)eobject).eClass().getEStructuralFeature("name");
+ if(nameFeature == null) {
+ owner = owner.eContainer();
+ } else {
+ return nameFeature;
+ }
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param editorModelEClass
+ * the eclass of the model of the editor
+ * @param wantedNameWithoutIndex
+ * the wantedname for the new editor
+ * @param context
+ * the context of the editor
+ * @return
+ * the name for this editor
+ */
+ public static final String getNameWithIncrement(final EClass editorModelEClass, final String wantedNameWithoutIndex, final EObject context) {
+ //a set of the existing index for the wantedName
+ final SortedSet<Integer> existingIndex = new TreeSet<Integer>();
+ final ECrossReferenceAdapter crossRef = ECrossReferenceAdapter.getCrossReferenceAdapter(context);
+ final Collection<Setting> crossReference = crossRef.getNonNavigableInverseReferences(context, true);
+ for(Setting set : crossReference) {
+ final EObject eobject = set.getEObject();
+ if(eobject.eClass() == editorModelEClass) {
+ final EStructuralFeature nameFeature = getNameFeature(eobject);
+ if(nameFeature != null) {
+ final EObject featureOwner = getNameFeatureOwner(eobject);
+ Object currentName = featureOwner.eGet(nameFeature);
+ if(currentName instanceof String) {
+ String aName = (String)currentName;
+ if(aName.contains(wantedNameWithoutIndex)) {
+ String lastChar = aName.substring(aName.length() - 1, aName.length());
+ try {
+ Integer value = Integer.parseInt(lastChar);
+ existingIndex.add(value);
+ } catch (Exception e) {
+ //nothing to do
+ }
+ }
+ }
+ }
+ }
+ }
+ int index = 0;
+ if(!existingIndex.isEmpty()) {
+ index = existingIndex.last().intValue() + 1;
+ }
+ return wantedNameWithoutIndex + String.valueOf(index);
+ }
+}
diff --git a/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/handlers/AbstractCreateTableEditorHandler.java b/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/handlers/AbstractCreateTableEditorHandler.java
index 4d0308854ec..0a04068147d 100644
--- a/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/handlers/AbstractCreateTableEditorHandler.java
+++ b/incoming/TableEFacet/org.eclipse.papyrus.infra.table.efacet.common/src/org/eclipse/papyrus/infra/table/efacet/common/handlers/AbstractCreateTableEditorHandler.java
@@ -32,6 +32,7 @@ import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.Table;
+import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.table.TablePackage;
import org.eclipse.emf.facet.widgets.table.metamodel.v0_2_0.tableconfiguration.TableConfiguration;
import org.eclipse.emf.facet.widgets.table.ui.internal.exported.TableWidgetUtils;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
@@ -54,6 +55,7 @@ import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.core.utils.ServiceUtilsForActionHandlers;
import org.eclipse.papyrus.infra.table.efacet.common.Activator;
+import org.eclipse.papyrus.infra.table.efacet.common.editor.EditorNameInitializer;
import org.eclipse.papyrus.infra.table.efacet.common.modelresource.PapyrusTableModelResource;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrusTable;
import org.eclipse.papyrus.infra.table.efacet.metamodel.papyrustable.PapyrustableFactory;
@@ -125,6 +127,8 @@ public abstract class AbstractCreateTableEditorHandler extends AbstractHandler {
return true;
}
+
+
/**
* Run the command as a transaction. Create a Transaction and delegate the
* command to {@link #doExecute(ServicesRegistry)}.
@@ -133,8 +137,12 @@ public abstract class AbstractCreateTableEditorHandler extends AbstractHandler {
*
*/
public void runAsTransaction() throws ServiceException {
+ final ServicesRegistry serviceRegistry = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry();
+ final TransactionalEditingDomain domain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);
+
+
// default Value
- this.name = this.defaultName;
+ this.name = EditorNameInitializer.getNameWithIncrement(TablePackage.eINSTANCE.getTable(), defaultName, getTableContext());
if(shouldOpenNameDialog()) {// this test is used to allow the JUnit
// test without ui!
@@ -146,8 +154,7 @@ public abstract class AbstractCreateTableEditorHandler extends AbstractHandler {
return;
}
}
- final ServicesRegistry serviceRegistry = ServiceUtilsForActionHandlers.getInstance().getServiceRegistry();
- final TransactionalEditingDomain domain = ServiceUtils.getInstance().getTransactionalEditingDomain(serviceRegistry);
+
// Create the transactional command
final AbstractEMFOperation command = new AbstractEMFOperation(domain, "Create " + this.editorType) { //$NON-NLS-1$
@@ -169,7 +176,7 @@ public abstract class AbstractCreateTableEditorHandler extends AbstractHandler {
// Execute the command
try {
- CheckedOperationHistory.getInstance().execute(command, new NullProgressMonitor(), null);
+ CheckedOperationHistory.getInstance().execute(command, new NullProgressMonitor(), null);
} catch (final ExecutionException e) {
Activator.log.error("Can't create Table Editor", e); //$NON-NLS-1$
}

Back to the top