Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7128f71e5d92ba50d68abf5849ee917b167176d4 (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
/*****************************************************************************
 * Copyright (c) 2014 Cedric Dumoulin.
 *
 *    
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.uml.profile.drafter.ui.dialog;

import org.eclipse.jface.bindings.keys.KeyStroke;
import org.eclipse.jface.fieldassist.ContentProposalAdapter;
import org.eclipse.jface.fieldassist.IContentProposal;
import org.eclipse.jface.fieldassist.IContentProposalListener;
import org.eclipse.jface.fieldassist.IContentProposalProvider;
import org.eclipse.jface.fieldassist.TextContentAdapter;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.papyrus.uml.profile.drafter.ui.contentassist.TypeContentProposalBase;
import org.eclipse.papyrus.uml.profile.drafter.ui.contentassist.TypeContentProposalProvider;
import org.eclipse.papyrus.uml.profile.drafter.ui.model.ITypeCatalog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.uml2.uml.Type;


/**
 * A Label used to select a uml {@link Type}.
 * 
 * @author cedric dumoulin
 * 
 * TODO : 20141126 not used - can be removed
 *
 */
public class TypeSelectorLabel extends Text {

	/**
	 * Type catalog used to propose existing types.
	 */
	protected ITypeCatalog typeProvider;
	
	protected ILabelProvider typeLabelProvider;
	
	protected Type type;
	
	/**
	 * Constructor.
	 *
	 * @param parent
	 * @param style
	 */
	public TypeSelectorLabel(Composite parent, int style, ITypeCatalog typeProvider, ILabelProvider typeLabelProvider, Type defaultType) {
		super(parent, style);
		
		this.typeProvider = typeProvider;
		this.typeLabelProvider = typeLabelProvider;
		// TODO: set first type
		setType(defaultType);
		
		// Set content assist
		installContentAssist();
	}

	/**
	 * Associate a ContentAssist based on the {@link ITypeCatalog}.
	 */
	private void installContentAssist() {
		installContentAssistantProvider( new TypeContentProposalProvider( typeProvider ));
	}

	/**
	 * Install content assistant provider.
	 * 
	 * @param control The control to which content assist is installed.
	 *
	 * @param contentProposalProvider The associated {@link ContentProposalAdapter}.
	 */
	protected void installContentAssistantProvider( IContentProposalProvider contentProposalProvider) {
//		KeyStroke keyStroke = KeyStroke.getInstance("Ctrl+Space");
		KeyStroke keyStroke = null;
		char[] autoActivationCharacters = null;
		int autoActivationDelay = 500;

		ContentProposalAdapter adapter = new ContentProposalAdapter(this, new TextContentAdapter(), contentProposalProvider, keyStroke, autoActivationCharacters);
		adapter.setAutoActivationDelay(autoActivationDelay);

		// filter proposals as keys are pressed and proposals popup is present
		adapter.setFilterStyle(ContentProposalAdapter.FILTER_NONE);

		// replace all text
		adapter.setProposalAcceptanceStyle(ContentProposalAdapter.PROPOSAL_REPLACE);

		// Listener called when a selection is made.
		// Use the associated type to set the Type.
		adapter.addContentProposalListener(new IContentProposalListener() {
			
			@Override
			public void proposalAccepted(IContentProposal proposal) {
				// set the corresponding type
				if( proposal instanceof TypeContentProposalBase ) {
					TypeContentProposalBase typeProposal = (TypeContentProposalBase)proposal;
					setType(typeProposal.getType());
				}
				
			}
		});

	}

	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}

	
	/**
	 * @param type the type to set
	 */
	public void setType(Type type) {
		
		String label = typeLabelProvider.getText(type);
		setText(label);
		
		setTypeInternal(type);
	}

	/**
	 * Set the type and throw an event.
	 * 
	 * @param type the type to set
	 */
	public void setTypeInternal(Type type) {
		this.type = type;
	}

	
}

Back to the top