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
|
/*******************************************************************************
* Copyright (c) 2010 protos software gmbh (http://www.protos.de).
* 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:
* Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.core.ui.contentassist;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.etrice.core.ui.contentassist.AbstractRoomProposalProvider;
import org.eclipse.etrice.core.validation.ValidationUtil;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.swt.graphics.Image;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.ui.editor.contentassist.ContentAssistContext;
import com.google.common.base.Function;
import org.eclipse.etrice.core.room.ActorClass;
import org.eclipse.etrice.core.room.ActorRef;
import org.eclipse.etrice.core.room.RoomPackage;
/**
* see http://www.eclipse.org/Xtext/documentation/latest/xtext.html#contentAssist on how to customize content assistant
*/
public class RoomProposalProvider extends AbstractRoomProposalProvider {
protected class FilteredProposalCreator implements Function<IEObjectDescription, ICompletionProposal> {
private IProposalFilter filter;
private final ContentAssistContext contentAssistContext;
private final String ruleName;
protected FilteredProposalCreator(IProposalFilter filter, ContentAssistContext contentAssistContext, String ruleName) {
this.filter = filter;
this.contentAssistContext = contentAssistContext;
this.ruleName = ruleName;
}
public ICompletionProposal apply(IEObjectDescription candidate) {
if (candidate == null)
return null;
ICompletionProposal result = null;
String proposal = candidate.getName();
if (ruleName != null)
proposal = getValueConverter().toString(proposal, ruleName);
EObject objectOrProxy = candidate.getEObjectOrProxy();
// three new lines in code taken from org.eclipse.xtext.ui.editor.contentassist.AbstractJavaBasedContentProposalProvider.DefaultProposalCreator
if (!objectOrProxy.eIsProxy() && filter!=null)
if (!filter.accept(contentAssistContext, candidate))
return null;
StyledString displayString = getStyledDisplayString(objectOrProxy, candidate.getQualifiedName(), candidate.getName());
Image image = getImage(objectOrProxy);
result = createCompletionProposal(proposal, displayString, image, contentAssistContext);
getPriorityHelper().adjustCrossReferencePriority(result, contentAssistContext.getPrefix());
return result;
}
}
protected class ActorRefFilter implements IProposalFilter {
@Override
public boolean accept(ContentAssistContext context, IEObjectDescription candidate) {
if (!(context.getCurrentModel() instanceof ActorRef))
// unexpected call??
return false;
ActorRef ar = (ActorRef) context.getCurrentModel();
if (!(ar.eContainer() instanceof ActorClass))
// can not filter due to lack of information
return true;
ActorClass ac = (ActorClass) ar.eContainer();
EObject objectOrProxy = candidate.getEObjectOrProxy();
if (objectOrProxy instanceof ActorClass) {
ActorClass referenced = (ActorClass) objectOrProxy;
return !ValidationUtil.isReferencing(referenced, ac);
}
return false;
}
}
protected Function<IEObjectDescription, ICompletionProposal> getProposalFactory(String ruleName, ContentAssistContext contentAssistContext) {
if (contentAssistContext!=null && contentAssistContext.getCurrentModel().eClass()==RoomPackage.eINSTANCE.getActorRef())
return new FilteredProposalCreator(new ActorRefFilter(), contentAssistContext, ruleName);
// delegate to default
return super.getProposalFactory(ruleName, contentAssistContext);
}
// public void completeActorRef_Type(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
// super.completeActorRef_Type(
// model,
// assignment,
// context,
// acceptor);
// }
}
|