Skip to main content
summaryrefslogtreecommitdiffstats
blob: a328e3d83e3a3662ac7c1cef4ef7681a688c70dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Patrick Tessier (CEA LIST) Patrick.tessier@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.service.types.utils;

import java.util.Collection;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.uml2.uml.Element;

/**
 * This singleton is used to find a new element name with
 * no duplication in the same workspace.
 * 
 * Copied in current plug-in to avoid dependencies with oep.diagram.common.
 */
public class NamedElementHelper {

	/** Singleton instance */
	@Deprecated
	public static NamedElementHelper EINSTANCE = new NamedElementHelper();

	@Deprecated
	private String baseString = "default";

	/**
	 * Get the base string to use for default name creation
	 * 
	 * @return the base string
	 * @deprecated should not be used.
	 */
	@Deprecated
	public String getBaseString() {
		return baseString;
	}

	/**
	 * Generic method that returns a new unique name within a {@link Namespace}.
	 * 
	 * @param umlParent
	 *        the parent of the element to create
	 * @param eclass
	 *        the eClass of the element to name
	 * 
	 * @return a distinguishable name within the {@link Namespace} of the umlParent
	 * @deprecated should not be used.
	 */
	@Deprecated
	public String getNewUMLElementName(Element umlParent, EClass eclass) {
		return getNewUMLElementName(umlParent, eclass.getName());
	}

	/**
	 * Generic method that returns a new unique name within a {@link Namespace}.
	 * 
	 * @param umlParent
	 *        the parent of the element to create
	 * @param baseString
	 *        the base string for the new element name
	 * 
	 * @return a distinguishable name within the {@link Namespace} of the umlParent
	 * @deprecated use {@link #getDefaultNameWithIncrementFromBase(String, Collection)} directly.
	 */
	@Deprecated
	public String getNewUMLElementName(Element umlParent, String baseString) {
		return getDefaultNameWithIncrementFromBase(baseString, umlParent.eContents());
//		this.setBaseString(baseString);
//		String name = ""; //$NON-NLS-1$
//
//		boolean found = false;
//		// i <10000: avoid infinite loops
//		for(int i = 1; i < 5; i++) {
//			found = false;
//			name = getBaseString() + i;
//			
//			Iterator<?> it = umlParent.getOwnedElements().iterator();
//			while(it.hasNext() && !found) {
//				Object o = it.next();
//				if(o instanceof NamedElement) {
//					if(name.equals(((NamedElement)o).getName())) {
//						found = true;
//					}
//				}
//			}
//			if(!found) {
//				return name;
//			}
//		}
//		return getBaseString() + "X"; //$NON-NLS-1$
	}

	/**
	 * set the base string for the name
	 * 
	 * @param baseString
	 *        a string that is the prefix
	 * @deprecated should not be used.
	 */
	@Deprecated
	public void setBaseString(String baseString) {
		this.baseString = baseString;
	}
	
	@SuppressWarnings("rawtypes")
	public static String getDefaultNameWithIncrementFromBase(String base, Collection contents) {
		int nextNumber = 1;

		for(Object o : contents) {
			if(o instanceof EObject) {
				String name = EMFCoreUtil.getName((EObject)o);
				if(name != null && name.startsWith(base)) {
					String end = name.substring(base.length());
					int nextNumberTmp = 1;

					try {
						nextNumberTmp = Integer.parseInt(end) + 1;
					} catch (NumberFormatException ex) {
					}

					if(nextNumberTmp > nextNumber) {
						nextNumber = nextNumberTmp;
					}
				}
			}
		}

		return base + nextNumber;
	}
}

Back to the top