Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 212a61dda0c050bd26515268276aa3489f0772d3 (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
/*****************************************************************************
 * Copyright (c) 2014, 2015 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.profile.types.generator.ui.internal.wizards;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.uml2.common.util.UML2Util.getValidJavaIdentifier;
import static org.eclipse.xtext.xbase.lib.StringExtensions.toFirstLower;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;

import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;

/**
 * @author damus
 *
 */
class GeneratorParametersBlock {

	private static final String IDENTIFIER_PREFIX = "identifierPrefix"; //$NON-NLS-1$

	private final GeneratorWizardModel model;

	private Text idText;

	public GeneratorParametersBlock(GeneratorWizardModel model) {
		super();

		this.model = model;
	}

	public void createControl(Composite parent) {
		new Label(parent, SWT.NONE).setText("Identifier:");
		idText = new Text(parent, SWT.BORDER | SWT.SINGLE);
		idText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		idText.setText(getDefaultIdentifier());
		model.setIdentifier(getIdentifier());

		idText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				model.setIdentifier(getIdentifier());
				model.validatePage();
			}
		});
	}

	private String getIdentifier() {
		return idText.getText().trim();
	}

	void save() {
		String identifier = getIdentifier();
		if (!getDefaultIdentifier().equals(identifier)) {
			// Only a custom default prefix if the user deviated from the default
			final String suffix = "." + getDefaultQualifier(); //$NON-NLS-1$
			if (identifier.endsWith(suffix)) {
				// This looks like a custom prefix
				model.getDialogSettings().put(IDENTIFIER_PREFIX, identifier.substring(0, identifier.length() - suffix.length()));
			}
		}
	}

	private String getDefaultIdentifier() {
		return String.format("%s.%s", getDefaultIdentifierPrefix(), getDefaultQualifier());
	}

	private String getDefaultQualifier() {
		return Joiner.on('.').join(Iterables.transform(Splitter.on(NamedElement.SEPARATOR).split(model.getProfile().getQualifiedName()), new Function<String, String>() {
			@Override
			public String apply(String input) {
				return toFirstLower(getValidJavaIdentifier(input));
			}
		}));
	}

	private String getDefaultIdentifierPrefix() {
		String result = model.getDialogSettings().get(IDENTIFIER_PREFIX);
		Profile profile = model.getProfile();

		Stereotype ePackage = profile.getApplicableStereotype(String.format("%s::%s", UMLUtil.PROFILE__ECORE, UMLUtil.STEREOTYPE__E_PACKAGE)); //$NON-NLS-1$
		if ((ePackage != null) && profile.isStereotypeApplied(ePackage)) {
			String basePackage = (String) profile.getValue(ePackage, UMLUtil.TAG_DEFINITION__BASE_PACKAGE);
			if (!isNullOrEmpty(basePackage)) {
				result = basePackage;
			}
		}

		if (result == null) {
			result = "com.example"; //$NON-NLS-1$
		}

		return result;
	}
}

Back to the top