Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 99676d4d851672ff1113d29b6b85f076d31e2bd2 (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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems 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:
 * QNX Software Systems - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.qt.core.qmltypes;

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

import org.eclipse.cdt.qt.core.qmljs.IJSObjectExpression;
import org.eclipse.cdt.qt.core.qmljs.IJSProperty;
import org.eclipse.cdt.qt.core.qmljs.IQmlObjectDefinition;
import org.eclipse.cdt.qt.core.qmljs.IQmlObjectMember;
import org.eclipse.cdt.qt.core.qmljs.IQmlPropertyBinding;
import org.eclipse.cdt.qt.core.qmljs.IQmlScriptBinding;
import org.eclipse.cdt.qt.core.qmljs.QMLExpressionEvaluator;
import org.eclipse.cdt.qt.core.qmljs.QMLExpressionEvaluator.InvalidExpressionException;

public class QMLEnumInfo {
	public static class EnumConst {
		private final String identifier;
		private final int value;

		private EnumConst(String ident, int val) {
			this.identifier = ident;
			this.value = val;
		}

		public String getIdentifier() {
			return identifier;
		}

		public int getValue() {
			return value;
		}
	}

	static final String IDENTIFIER = "Enum"; //$NON-NLS-1$

	static final String PROPERTY_NAME = "name"; //$NON-NLS-1$
	static final String PROPERTY_VALUE = "values"; //$NON-NLS-1$

	private String name;
	private List<EnumConst> constantList = new ArrayList<>();

	QMLEnumInfo(QMLModelBuilder builder, IQmlObjectDefinition obj) {
		if (builder.ensureIdentifier(obj.getIdentifier(), IDENTIFIER)) {
			for (IQmlObjectMember member : obj.getBody().getMembers()) {
				if (builder.ensureNode(member, IQmlPropertyBinding.class)) {
					IQmlPropertyBinding prop = (IQmlPropertyBinding) member;
					switch (prop.getIdentifier().getName()) {
					case PROPERTY_NAME:
						this.name = builder.getStringBinding(prop);
						break;
					case PROPERTY_VALUE:
						if (builder.ensureNode(prop.getBinding(), IQmlScriptBinding.class)) {
							IQmlScriptBinding binding = ((IQmlScriptBinding) prop.getBinding());
							if (builder.ensureNode(binding.getScript(), IJSObjectExpression.class)) {
								IJSObjectExpression objExpr = (IJSObjectExpression) binding.getScript();
								for (IJSProperty property : objExpr.getProperties()) {
									Object value;
									try {
										value = QMLExpressionEvaluator.evaluateConstExpr(property.getValue());
										if (value instanceof Number) {
											constantList.add(new EnumConst(property.getType(), ((Number) value).intValue()));
										}
									} catch (InvalidExpressionException e) {
										builder.handleException(e);
									}
								}
							}
						}
						break;
					default:
					}
				}
			}
		}
		constantList = Collections.unmodifiableList(constantList);
	}

	public String getName() {
		return name;
	}

	public List<EnumConst> getConstants() {
		return constantList;
	}
}

Back to the top