|
Re: Palette-Items Customize before/at Editor start [message #1028732 is a reply to message #1027668] |
Thu, 28 March 2013 16:59 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
I read the content of an existing (template) model and create palette
entries so you can drag'n drop them into new models. You essentially
read the model using normal API and create an appropriate palette
structure. I've pasted code from my tool behavior provider below,
hopefully you'll be able to understand the structure even though there's
a lot of noise...
Hallvard
@Override
public IPaletteCompartmentEntry[] getPalette() {
IPaletteCompartmentEntry[] superCompartments = super.getPalette();
List<IPaletteCompartmentEntry> libraryCompartments =
getLibraryCompartments();
List<IPaletteCompartmentEntry> allCompartments = new
ArrayList<IPaletteCompartmentEntry>(Arrays.asList(superCompartments));
allCompartments.addAll(libraryCompartments);
return allCompartments.toArray(new
IPaletteCompartmentEntry[allCompartments.size()]);
}
private Collection<EObject> defaultLibraryContainers = null;
private Collection<EObject> getDefaultLibraryContainers() {
if (defaultLibraryContainers == null) {
EObject libraryContainer =
modelFacade.createEntity(ModelFacade.EntityKind.ACTOR_CONTAINER,
"Default library", null);
EObject actor1 =
modelFacade.createEntity(ModelFacade.EntityKind.ATOMIC_ACTOR,
"Input1Output1Actor", libraryContainer);
modelFacade.createPort(ModelFacade.PortKind.INPUT, "input", actor1);
modelFacade.createPort(ModelFacade.PortKind.OUTPUT, "output",
actor1);
EObject actor2 =
modelFacade.createEntity(ModelFacade.EntityKind.ATOMIC_ACTOR,
"Input2Output1Actor", libraryContainer);
modelFacade.createPort(ModelFacade.PortKind.INPUT, "input1", actor2);
modelFacade.createPort(ModelFacade.PortKind.INPUT, "input2", actor2);
modelFacade.createPort(ModelFacade.PortKind.OUTPUT, "output",
actor2);
defaultLibraryContainers = new ArrayList<EObject>();
defaultLibraryContainers.add(libraryContainer);
}
return defaultLibraryContainers;
}
private List<IPaletteCompartmentEntry> getLibraryCompartments() {
List<IPaletteCompartmentEntry> compartments = new
ArrayList<IPaletteCompartmentEntry>();
DiagramBehavior diagramBehavior = (DiagramBehavior)
getDiagramTypeProvider().getDiagramBehavior();
EObject bo =
Graphiti.getLinkService().getBusinessObjectForLinkedPictogramElement(diagramBehavior.getDiagramTypeProvider().getDiagram());
EObject model = EcoreUtil.getRootContainer(bo);
// assume only the root can contain imports
if (modelFacade.isEntityKind(model,
ModelFacade.EntityKind.ACTOR_CONTAINER) && model.eContainer() == null) {
IScopeProvider scopeProvider =
getDslInjector().getInstance(IScopeProvider.class);
IQualifiedNameConverter qualifiedNameConverter =
getDslInjector().getInstance(IQualifiedNameConverter.class);
// for (ImportDirective importDirective : ((ActorModel)
model).getImports()) {
// String qName = importDirective.getImportedNamespace();
// if (qName.endsWith(".*")) {
// qName = qName.substring(0, qName.length() - 2);
// }
// IScope scope = scopeProvider.getScope(model,
XactorPackage.eINSTANCE.getEntityFolder_EntityContainers());
// for (IEObjectDescription element :
scope.getElements(qualifiedNameConverter.toQualifiedName(qName))) {
// EObject eObject = EcoreUtil.resolve(element.getEObjectOrProxy(),
model);
// if (eObject instanceof EntityContainer<?>) {
// addCompartmentEntries((EntityContainer<?>) eObject, compartments);
// }
// }
// }
}
if (compartments.isEmpty()) {
for (EObject entityContainer : getDefaultLibraryContainers()) {
addCompartmentEntries(entityContainer, compartments);
}
}
return compartments;
}
protected void addCompartmentEntries(EObject container,
List<IPaletteCompartmentEntry> compartments) {
if (! modelFacade.isEntityKind(container,
ModelFacade.EntityKind.COMPOSITE_ACTOR)) {
IQualifiedNameConverter qualifiedNameConverter =
getDslInjector().getInstance(IQualifiedNameConverter.class);
QualifiedName qName =
qualifiedNameConverter.toQualifiedName(modelFacade.getQualifiedEntityName(container));
if (qName.getSegmentCount() > 2) {
qName = qName.skipFirst(qName.getSegmentCount() - 2);
}
PaletteCompartmentEntry compartmentEntry = new
PaletteCompartmentEntry(qualifiedNameConverter.toString(qName), null);
for (EObject entity : modelFacade.getEntities(container)) {
compartmentEntry.addToolEntry(createEntityCreationToolEntry(entity));
}
compartments.add(compartmentEntry);
}
}
@Inject
private Injector editorInjector;
private IObjectCreationToolEntry createEntityCreationToolEntry(EObject
actor) {
String label = modelFacade.getName(actor);
AbstractCreateActorFeature createEntityFeature = new
CreateActorInstanceFeature(getFeatureProvider(),
modelFacade.getName(actor), actor, true);
editorInjector.injectMembers(createEntityFeature);
ObjectCreationToolEntry toolEntry = new ObjectCreationToolEntry(label,
"Create a " + label, null, null, createEntityFeature);
return toolEntry;
}
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05044 seconds