Skip to main content
summaryrefslogtreecommitdiffstats
blob: d1c452edf1a1aecd9ff4fec16e179502edcffb8d (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 *   Gregoire Dupe (Mia-Software) - initial API and implementation
 *   Nicolas Bros (Mia-Software) - Bug 339664 - org.eclipse.papyrus.emf.facet.widgets.celleditors API cleaning
 *   Gregoire Dupe (Mia-Software) - Bug 344563 - NPE with Enum Editor
 *****************************************************************************/
package org.eclipse.papyrus.emf.facet.widgets.celleditors.internal.core.composite;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.swt.widgets.Composite;

public class EnumeratorComposite<T extends Enumerator> extends EnumComposite<T> {

	public EnumeratorComposite(final Composite parent) {
		super(parent);

	}

	private EEnum eenum = null;

	public void setEEnum(final EEnum eenum2) {
		this.eenum = eenum2;
		getCombo().setItems(getEnumStrings());
	}

	@SuppressWarnings("unchecked")
	@Override
	protected T getEnumValue(final String text) {
		if (this.eenum.getEEnumLiteralByLiteral(text) == null) {
			throw new RuntimeException("Invalid enumeration literal"); //$NON-NLS-1$
		}
		Object result = this.eenum.getEPackage().getEFactoryInstance()
				.createFromString(this.eenum, this.eenum.getEEnumLiteralByLiteral(text).getName());
		if (result instanceof Enumerator) {
			return (T) result;
		}
		return null;
	}

	@Override
	public String getEnumString(final T value) {
		return value.getLiteral();
	}

	@Override
	protected String[] getEnumStrings() {
		List<String> result = new ArrayList<String>();
		if (this.eenum != null) {
			for (EEnumLiteral eeNumLiteral : this.eenum.getELiterals()) {
				result.add(eeNumLiteral.getLiteral());
			}
		}
		return result.toArray(new String[] {});
	}
}

Back to the top