Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0b5cfa78ee520483c828e881012bdf3d7d50589a (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.profile.ui.dialogs;

import java.util.List;

import org.eclipse.emf.common.util.Enumerator;
import org.eclipse.emf.ecore.EEnumLiteral;
import org.eclipse.papyrus.profile.Message;
import org.eclipse.papyrus.profile.utils.Util;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Property;


// TODO: Auto-generated Javadoc
/**
 * The Class InputDialogEnumeration.
 */
public class InputDialogEnumeration {

	/**
	 * The Constant OK.
	 */
	public static final int OK = ComboSelectionDialog.OK;

	/**
	 * The Constant CANCEL.
	 */
	public static final int CANCEL = ComboSelectionDialog.CANCEL;

	/**
	 * The i dialog.
	 */
	private ComboSelectionDialog iDialog;

	//private final String TITLE = "Property value editing";
	/**
	 * The LABEL.
	 */
	private final String LABEL = "Value:";

	/**
	 * The literals.
	 */
	private String[] literals;

	/**
	 * The Constructor.
	 * 
	 * @param value
	 *        the value
	 * @param property
	 *        the property
	 * @param shell
	 *        the shell
	 */
	public InputDialogEnumeration(Shell shell, Property property, Object value) {
		// Create literals list
		createLiterals(property);

		// Create Combo
		if(literals != null) {

			String initialValue = literals[0];
			EnumerationLiteral eLiteral = null;

			// Prepare Item data
			if(value instanceof EnumerationLiteral) {
				eLiteral = (EnumerationLiteral)value;

			} else if(value instanceof EEnumLiteral) {
				EEnumLiteral eEnumLiteral = (EEnumLiteral)value;
				Object tmp = Util.getValueObjectFromString(eEnumLiteral.getName(), property.getType());
				eLiteral = ((EnumerationLiteral)tmp);

			} else if(value instanceof Enumerator) { // Enumeration in static profile
				String literalString = ((Enumerator)value).getLiteral();
				Object tmp = Util.getValueObjectFromString(literalString, property.getType());
				eLiteral = ((EnumerationLiteral)tmp);

			} else if (value == null) {
				initialValue = literals[0];
			}
			else { // Error
				String err = "Value " + value.toString() + " of Property " + property.getName() + " is not an EnumerationLiteral.";
				Message.error(err);
			}

			if((eLiteral != null) && (eLiteral.isSetName())) {
				initialValue = eLiteral.getQualifiedName();
			}

			// Create Combo
			iDialog = new ComboSelectionDialog(shell, LABEL, literals, initialValue);

		}
	}

	/**
	 * Open.
	 * 
	 * @return the int
	 */
	public int open() {
		if(iDialog != null) {
			return iDialog.open();
		}

		return (ComboSelectionDialog.CANCEL);
	}

	/**
	 * Close.
	 */
	public void close() {
		if(iDialog != null) {
			iDialog.close();
		}
	}

	/**
	 * Gets the value.
	 * 
	 * @return the value
	 */
	public Object getValue() {
		if(iDialog != null) {
			return iDialog.getValue();
		}
		return null;
	}

	/**
	 * Gets the selection index.
	 * 
	 * @return the selection index
	 */
	public int getSelectionIndex() {
		if(iDialog != null) {
			return iDialog.indexOfSelection;
		} else {
			return -1;
		}
	}

	/**
	 * Creates the literals.
	 * 
	 * @param property
	 *        the property
	 */
	private void createLiterals(Property property) {
		// combo dialog construction
		List tmp = ((Enumeration)property.getType()).getOwnedLiterals();
		String[] enumLiteralNames = Util.getStringArrayFromList(tmp);
		// if no possible selection : abort
		if(enumLiteralNames == null) {
			Message.warning("No literal was found for Enumeration in the model.");
			return;
		}
		literals = enumLiteralNames;
	}
}

Back to the top