Skip to main content
summaryrefslogtreecommitdiffstats
blob: a0eae910f5483ce19cef8ce04c1c5664aafcb1f0 (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
/*******************************************************************************
 * Copyright (c) 2014 TwelveTone LLC 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:
 *     Steven Spungin <steven@spungin.tv> - initial API and implementation, Bug 432372
 *******************************************************************************/

package org.eclipse.e4.tools.emf.ui.internal.handlers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.EmfUtil;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.IViewEObjects;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.E;
import org.eclipse.e4.tools.emf.ui.internal.common.component.tabs.empty.EmptyFilterOption;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CompoundCommand;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.edit.command.SetCommand;
import org.eclipse.emf.edit.domain.EditingDomain;

public class RepairDuplicateItemsHandler extends MarkDuplicateItemsBase {

	static public String name = "org.eclipse.e4.tools.active-object-viewer"; //$NON-NLS-1$
	static String attName = "elementId"; //$NON-NLS-1$

	@Override
	@Execute
	public void execute(IEclipseContext context) {
		IViewEObjects viewer = (IViewEObjects) context.get(name);
		Collection<EObject> all = viewer.getAllEObjects();
		Map<String, List<EObject>> map = MarkDuplicateElementIdsHandler.getDuplicateMap(attName, all);
		Iterator<Entry<String, List<EObject>>> it = map.entrySet().iterator();
		CompoundCommand compoundCommand = new CompoundCommand();
		EditingDomain editingDomain = viewer.getEditingDomain();

		while (it.hasNext()) {
			Entry<String, List<EObject>> entry = it.next();
			List<EObject> listDups = entry.getValue();
			if (listDups.size() > 1) {
				ArrayList<EObject> toFix = new ArrayList<EObject>(listDups.subList(1, listDups.size()));
				Iterator<EObject> itToFix = toFix.iterator();
				while (itToFix.hasNext()) {
					EObject eObject = itToFix.next();
					EAttribute att = EmfUtil.getAttribute(eObject, attName);
					String value = (String) eObject.eGet(att);
					// do not repair empty or null values
					if (E.isEmpty(value)) {
						break;
					}
					int index = -1;
					while (map.containsKey(value)) {
						index++;
						int lastDot = value.lastIndexOf('.');
						if (lastDot == -1) {
							lastDot = value.length();
						}
						try {
							String suffix = value.substring(lastDot + 1);
							Integer.parseInt(suffix);
							// ends with integer
							value = value.substring(0, lastDot);
						} catch (Exception e) {
						}
						value = value + "." + index; //$NON-NLS-1$
					}
					Command cmd = SetCommand.create(editingDomain, eObject, att, value);
					compoundCommand.append(cmd);
					// Note: If the compound command cannot execute, we
					// must revert these changes
					listDups.remove(eObject);
					List<EObject> newList = new ArrayList<EObject>();
					newList.add(eObject);
					map.put(value, newList);
					// Note: end

				}
			}
		}

		if (compoundCommand.isEmpty() == false) {
			if (compoundCommand.canExecute()) {
				editingDomain.getCommandStack().execute(compoundCommand);
			} else {
				// this will rollback our local map changes
				// Object obj =
				// ContextInjectionFactory.make(MarkDuplicateItemsHandler.class,
				// context);
				// ContextInjectionFactory.invoke(obj, Execute.class, context);
			}
			Collection<EObject> duplicateList = MarkDuplicateElementIdsHandler.getDuplicateList(attName, all);
			applyEmptyOption(duplicateList, attName, EmptyFilterOption.EXCLUDE);
			viewer.highlightEObjects(duplicateList);
		}
	}

	@CanExecute
	public boolean canExecute(IEclipseContext context) {
		IViewEObjects viewer = (IViewEObjects) context.get(name);
		return (viewer != null);
	}

}

Back to the top