Skip to main content
summaryrefslogtreecommitdiffstats
blob: 050ed818562d27cad56e62cd97d15ce4b2ba88e8 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2005-2009 itemis AG (http://www.itemis.eu) 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
 *
 *******************************************************************************/
package org.eclipse.xtend.util.stdlib;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent2;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;

/**
 * This component copies an element from one slot to a list contained in another
 * slot.
 * 
 * <h2>Properties</h2>
 * <table border="1">
 * <tr>
 * <th>Property</th>
 * <th>Type</th>
 * <th>Mandatory</th>
 * <th>Description</th>
 * </tr>
 * <td><tt>modelSlot</tt></td>
 * <td>String</td>
 * <td>yes</td>
 * <td>Source slot name.</td>
 * </tr>
 * <tr>
 * <td><tt>listSlot</tt></td>
 * <td>String</td>
 * <td>yes</td>
 * <td>Target slot name. This slot contains a list of elements.</td>
 * </tr>
 * <tr>
 * <tr>
 * <td><tt>uniqueNames</tt></td>
 * <td>boolean</td>
 * <td>no</td>
 * <td>If <code>true</code>, names have to be unique, otherwise not. Requires
 * that <tt>modelSlot</tt> contains an <tt>EObject</tt>.</td>
 * </tr>
 * </table>
 */
public class SlotListAdder extends AbstractWorkflowComponent2 {

	private static final String COMPONENT_NAME = "Slot List Adder";

	private String modelSlot;
	private String listSlot;
	private Set<String> nameSet = new HashSet<String>();
	private boolean uniqueNames;

	/**
	 * Sets the list slot.
	 * 
	 * @param listSlot
	 *            name of slot
	 */
	public void setListSlot(String listSlot) {
		this.listSlot = listSlot;
	}

	/**
	 * Sets the model slot.
	 * 
	 * @param modelSlot
	 *            name of slot
	 */
	public void setModelSlot(String modelSlot) {
		this.modelSlot = modelSlot;
	}

	/**
	 * Sets if names have to be unique.
	 * 
	 * @param uniqueNames
	 *            If <code>true</code>, names have to be unique, otherwise not.
	 */
	public void setUniqueNames(boolean uniqueNames) {
		this.uniqueNames = uniqueNames;
	}

	@Override
	protected void checkConfigurationInternal(Issues issues) {
		if (modelSlot == null) {
			issues.addError(this, "no modelSlot specified.");
		}
		if (listSlot == null) {
			issues.addError(this, "no listSlot specified.");
		}
	}

	/**
	 * @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getLogMessage()
	 */
	@Override
	public String getLogMessage() {
		return "adding contents of slot '" + modelSlot + "' to the list of stuff in '" + listSlot + "'";
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void invokeInternal(WorkflowContext ctx, ProgressMonitor mon, Issues issues) {
		Object listContent = ctx.get(listSlot);
		if (listContent == null) {
			issues.addWarning("'" + listSlot + "' is empty, creating a new list.", this);
			listContent = new ArrayList();
			ctx.set(listSlot, listContent);
		}
		if (!(listContent instanceof Collection)) {
			issues.addError("contents of '" + listSlot + "' slot is not a collection, but rather a '"
					+ listSlot.getClass().getName() + "'", this);
			return;
		}
		Object modelContent = ctx.get(modelSlot);
		if (modelContent == null) {
			issues
					.addWarning("'" + modelSlot + "' is empty; not adding anything to the '" + listSlot + "' slot.",
							this);
			return;
		}
		if (uniqueNames) {
			EObject eo = (EObject) modelContent;
			DynamicEcoreHelper h = new DynamicEcoreHelper(eo);
			String name = h.getName(eo);
			if (!nameSet.contains(name)) {
				((Collection) listContent).add(modelContent);
				nameSet.add(name);
			}
		}
		else {
			((Collection) listContent).add(modelContent);
		}
	}

	/**
	 * @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getComponentName()
	 */
	@Override
	public String getComponentName() {
		return COMPONENT_NAME;
	}
}

Back to the top