View | Details | Raw Unified | Return to bug 196685 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/pde/internal/ui/util/PDELabelUtility.java (-26 / +73 lines)
Lines 96-102 Link Here
96
	 * @param base
96
	 * @param base
97
	 * @param set
97
	 * @param set
98
	 */
98
	 */
99
	private static void addNumberToBase(StringBuffer base, HashSet set) {
99
	private static void addNumberToBase(StringBuffer base, boolean bracketed, HashSet set) {
100
		if (set.size() > 0) {
100
		if (set.size() > 0) {
101
			// Limit on the number of auto-generated item numbers to check for
101
			// Limit on the number of auto-generated item numbers to check for
102
			int limit = 100;
102
			int limit = 100;
Lines 109-117 Link Here
109
					// Check if the number was already used to auto-generate an
109
					// Check if the number was already used to auto-generate an
110
					// existing item
110
					// existing item
111
					if (set.contains(new Integer(x)) == false) {
111
					if (set.contains(new Integer(x)) == false) {
112
						base.append(" ("); //$NON-NLS-1$
112
						if (bracketed)
113
							base.append(" ("); //$NON-NLS-1$
113
						base.append(x);
114
						base.append(x);
114
						base.append(")"); //$NON-NLS-1$
115
						if (bracketed)
116
							base.append(")"); //$NON-NLS-1$
115
						break;
117
						break;
116
					}
118
					}
117
				}
119
				}
Lines 124-157 Link Here
124
	 * @param set
126
	 * @param set
125
	 * @param title
127
	 * @param title
126
	 */
128
	 */
127
	private static void compareTitleWithBase(String base, HashSet set, String title) {
129
	private static void compareTitleWithBase(String base, boolean bracketed, HashSet set, String title) {
128
		// Check to see it the name starts with the base
130
		// Check to see it the name starts with the prefix
129
		if (title.startsWith(base)) {
131
		if (title.startsWith(base)) {
130
			// space, (, number, )
132
			// with brackets add on is: space, (, #, )
131
			int minSizeNumAddOn = 4;				
133
			int minSizeNumAddOn = 4;
134
			if (!bracketed)
135
				// without brackets and space add on is just number
136
				minSizeNumAddOn = 1;
132
			// We found a possible auto-generated name
137
			// We found a possible auto-generated name
133
			// Determine number
138
			// Determine number
134
			if (title.length() >= (base.length() + minSizeNumAddOn)) {
139
			if (title.length() >= (base.length() + minSizeNumAddOn)) {
135
				// We skipped the space
140
				String numPart;
136
				String numPart = title.substring(base.length() + 1);
141
				if (bracketed && title.charAt(base.length()) == ' ') {
142
					// We skipped the space since we already checked
143
					numPart = title.substring(base.length() + 1);
144
				} else if (!bracketed) {
145
					// without brackets, the numPart is everything after the prefix
146
					numPart = title.substring(base.length());
147
				} else {
148
					// We are using brackets and there was no space
149
					return;
150
				}
151
				if (bracketed) {
152
					if (numPart.charAt(0) == '(') {
153
						// We are using brackets and confirmed that the open bracket exists
154
						// move on to just the number part
155
						numPart = numPart.substring(1);
156
					}
157
					else {
158
						// We are using brackets and there is no opening bracket
159
						return;
160
					}
161
				}
137
				// We found an auto-generated name
162
				// We found an auto-generated name
138
				if (numPart.charAt(0) == '(') {
163
				StringBuffer buffer = new StringBuffer();
139
					StringBuffer buffer = new StringBuffer();
164
				// Parse the number between the brackets
140
					// Parse the number between the brackets
165
				for (int j = 0; j < numPart.length(); j++) {
141
					for (int j = 1; j < numPart.length(); j++) {
166
					char current = numPart.charAt(j);
142
						char current = numPart.charAt(j);
167
					// Make sure its a digit
143
						// Make sure its a digit
168
					if (Character.isDigit(current)) {
144
						if (Character.isDigit(current)) {
169
						buffer.append(current);
145
							buffer.append(current);
170
					} else {
146
						} else {
171
						if (!bracketed || numPart.charAt(j) != ')' || j != numPart.length() - 1) {
147
							// Break on non digits including ')'
172
							// without brackets, a non digits means this will not conflict
148
							break;
173
							// with brackets, anything other than a ')' means this will not conflict
174
							// with brackets, if this is not the last character it will not conflict
175
							return;
149
						}
176
						}
177
						// if all conditions passed, this is the last loop, no need to break
150
					}
178
					}
151
					// Convert the number we found into an actual number
179
				}
152
					if (buffer.length() > 0) {
180
				// Convert the number we found into an actual number
153
						set.add(new Integer(buffer.toString()));
181
				if (buffer.length() > 0) {
154
					}
182
					set.add(new Integer(buffer.toString()));
155
				}
183
				}
156
				
184
				
157
			} else {
185
			} else {
Lines 166-171 Link Here
166
	 * @return
194
	 * @return
167
	 */
195
	 */
168
	public static String generateName(String[] names, String base) {
196
	public static String generateName(String[] names, String base) {
197
		return generateName(names, base, true);
198
	}
199
	
200
	/**
201
	 * <p>Generates a name that does not conflict with any of the given names with one of two forms:
202
	 * <ol><li>&quot;&lt;base&gt; (#)&quot;</li><li>&quot;&lt;base&gt;#&quot;</li></ol>
203
	 * The number will be omitted if the base name alone is available.</p>
204
	 * 
205
	 * @param names
206
	 * 			the existing names that should not be conflicted
207
	 * @param base
208
	 * 			the base name to add numbers to
209
	 * @param bracketed
210
	 * 			if true use the first form, otherwise use the second
211
	 * @return
212
	 * 			the non-conflicting name
213
	 */
214
	public static String generateName(String[] names, String base, boolean bracketed){
169
		StringBuffer result = new StringBuffer(base);
215
		StringBuffer result = new StringBuffer(base);
170
		// Used to track auto-generated numbers used
216
		// Used to track auto-generated numbers used
171
		HashSet set = new HashSet();
217
		HashSet set = new HashSet();
Lines 174-184 Link Here
174
		// Performance hit unnoticeable because number of items per cheatsheet
220
		// Performance hit unnoticeable because number of items per cheatsheet
175
		// should be minimal.
221
		// should be minimal.
176
		for (int i = 0; i < names.length; i++) {
222
		for (int i = 0; i < names.length; i++) {
177
			PDELabelUtility.compareTitleWithBase(base, set, names[i]);
223
			PDELabelUtility.compareTitleWithBase(base, bracketed, set, names[i]);
178
		}
224
		}
179
		// Add an auto-generated number
225
		// Add an auto-generated number
180
		PDELabelUtility.addNumberToBase(result, set);
226
		PDELabelUtility.addNumberToBase(result, bracketed, set);
181
		
227
		
182
		return result.toString();
228
		return result.toString();
229
		
183
	}
230
	}
184
}
231
}
(-)src/org/eclipse/pde/internal/ui/pderesources.properties (-1 / +1 lines)
Lines 395-401 Link Here
395
SchemaElementDetails_description= Properties for the "{0}" element.
395
SchemaElementDetails_description= Properties for the "{0}" element.
396
SchemaEditor_NewElement_tooltip = New Global Element
396
SchemaEditor_NewElement_tooltip = New Global Element
397
SchemaIncludesSection_addButton=Add...
397
SchemaIncludesSection_addButton=Add...
398
SchemaEditor_NewElement_initialName = new_element{0}
398
SchemaEditor_NewElement_initialName = new_element
399
SchemaIncludesSection_dialogMessage=Select an extension point schema file:
399
SchemaIncludesSection_dialogMessage=Select an extension point schema file:
400
SchemaIncludesSection_missingWarningTitle=Missing Schema Include
400
SchemaIncludesSection_missingWarningTitle=Missing Schema Include
401
SchemaIncludesSection_missingWarningMessage={0} could not be found.
401
SchemaIncludesSection_missingWarningMessage={0} could not be found.
(-)src/org/eclipse/pde/internal/ui/editor/schema/NewElementAction.java (-14 / +12 lines)
Lines 10-46 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.pde.internal.ui.editor.schema;
11
package org.eclipse.pde.internal.ui.editor.schema;
12
12
13
import java.util.Hashtable;
14
15
import org.eclipse.jface.action.Action;
13
import org.eclipse.jface.action.Action;
16
import org.eclipse.osgi.util.NLS;
17
import org.eclipse.pde.internal.core.ischema.ISchema;
14
import org.eclipse.pde.internal.core.ischema.ISchema;
15
import org.eclipse.pde.internal.core.ischema.ISchemaElement;
18
import org.eclipse.pde.internal.core.schema.Schema;
16
import org.eclipse.pde.internal.core.schema.Schema;
19
import org.eclipse.pde.internal.core.schema.SchemaElement;
17
import org.eclipse.pde.internal.core.schema.SchemaElement;
20
import org.eclipse.pde.internal.core.schema.SchemaRootElement;
18
import org.eclipse.pde.internal.core.schema.SchemaRootElement;
21
import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
19
import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
22
import org.eclipse.pde.internal.ui.PDEPlugin;
23
import org.eclipse.pde.internal.ui.PDEPluginImages;
20
import org.eclipse.pde.internal.ui.PDEPluginImages;
24
import org.eclipse.pde.internal.ui.PDEUIMessages;
21
import org.eclipse.pde.internal.ui.PDEUIMessages;
22
import org.eclipse.pde.internal.ui.util.PDELabelUtility;
25
23
26
public class NewElementAction extends Action {
24
public class NewElementAction extends Action {
27
	private Schema schema;
25
	private Schema schema;
28
	private static final String NAME_COUNTER_KEY = "__schema_element_name"; //$NON-NLS-1$
29
	public NewElementAction() {
26
	public NewElementAction() {
30
		setText(PDEUIMessages.SchemaEditor_NewElement_label);
27
		setText(PDEUIMessages.SchemaEditor_NewElement_label);
31
		setImageDescriptor(PDEPluginImages.DESC_GEL_SC_OBJ);
28
		setImageDescriptor(PDEPluginImages.DESC_GEL_SC_OBJ);
32
		setToolTipText(PDEUIMessages.SchemaEditor_NewElement_tooltip);
29
		setToolTipText(PDEUIMessages.SchemaEditor_NewElement_tooltip);
33
	}
30
	}
34
	private String getInitialName() {
31
	private String getInitialName() {
35
		Hashtable counters = PDEPlugin.getDefault().getDefaultNameCounters();
32
		return PDELabelUtility.generateName(getElementNames(), PDEUIMessages.SchemaEditor_NewElement_initialName, false);
36
		Integer counter = (Integer) counters.get(NAME_COUNTER_KEY);
33
	}
37
		if (counter == null) {
34
	private String[] getElementNames() {
38
			counter = new Integer(1);
35
		if (schema == null)
39
		} else {
36
			return new String[0];
40
			counter = new Integer(counter.intValue() + 1);
37
		ISchemaElement[] elements = schema.getElements();
41
		}
38
		String[] names = new String[elements.length];
42
		counters.put(NAME_COUNTER_KEY, counter);
39
		for (int i = 0; i < elements.length; i++)
43
		return NLS.bind(PDEUIMessages.SchemaEditor_NewElement_initialName, counter.intValue() + ""); //$NON-NLS-1$
40
			names[i] = elements[i].getName();
41
		return names;
44
	}
42
	}
45
	public org.eclipse.pde.internal.core.schema.Schema getSchema() {
43
	public org.eclipse.pde.internal.core.schema.Schema getSchema() {
46
		return schema;
44
		return schema;

Return to bug 196685