Skip to content

Commit

Permalink
[test] Add styled string support in the explorer for Papaya models
Browse files Browse the repository at this point in the history
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Aug 26, 2024
1 parent 6003599 commit f851b2f
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Annotation;
import org.eclipse.sirius.components.papaya.provider.AnnotationItemProvider;
import org.eclipse.sirius.components.papaya.provider.spec.images.VisibilityOverlayImageProvider;
Expand Down Expand Up @@ -44,10 +45,12 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof Annotation annotation && annotation.getName() != null && !annotation.getName().isBlank()) {
return annotation.getName();
StyledString styledLabel = new StyledString();
styledLabel.append(annotation.getName());
return styledLabel;
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Attribute;
import org.eclipse.sirius.components.papaya.provider.AttributeItemProvider;
import org.eclipse.sirius.components.papaya.provider.PapayaItemProviderAdapterFactory;
Expand Down Expand Up @@ -46,19 +47,21 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof Attribute attribute && attribute.getName() != null && !attribute.getName().isBlank()) {
var text = attribute.getName();
StyledString styledLabel = new StyledString();
styledLabel.append(attribute.getName());

if (attribute.getType() != null) {
var adapter = new PapayaItemProviderAdapterFactory().adapt(attribute.getType(), IItemLabelProvider.class);
if (adapter instanceof IItemLabelProvider itemLabelProvider) {
text = text + ": " + itemLabelProvider.getText(attribute.getType());
styledLabel.append(": ", PapayaStyledStringStyles.DECORATOR_STYLE);
styledLabel.append(itemLabelProvider.getText(attribute.getType()), PapayaStyledStringStyles.TYPE_STYLE);
}
}

return text;
return styledLabel;
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
package org.eclipse.sirius.components.papaya.provider.spec;

import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Class;
import org.eclipse.sirius.components.papaya.TypeParameter;
import org.eclipse.sirius.components.papaya.provider.ClassItemProvider;
import org.eclipse.sirius.components.papaya.provider.spec.images.VisibilityOverlayImageProvider;

Expand Down Expand Up @@ -46,17 +45,26 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof Class aClass && aClass.getName() != null && !aClass.getName().isBlank()) {
var typeParameters = "";
StyledString styledLabel = new StyledString();
styledLabel.append(aClass.getName());

if (!aClass.getTypeParameters().isEmpty()) {
typeParameters = aClass.getTypeParameters()
.stream()
.map(TypeParameter::getName)
.collect(Collectors.joining(", ", "<", ">"));
styledLabel.append("<", PapayaStyledStringStyles.DECORATOR_STYLE);

for (var i = 0; i < aClass.getTypeParameters().size(); i++) {
var typeParameter = aClass.getTypeParameters().get(i);
styledLabel.append(typeParameter.getName(), PapayaStyledStringStyles.GENERIC_TYPE_STYLE);
if (i < aClass.getTypeParameters().size() - 1) {
styledLabel.append(", ", PapayaStyledStringStyles.DECORATOR_STYLE);
}
}

styledLabel.append(">", PapayaStyledStringStyles.DECORATOR_STYLE);
}
return aClass.getName() + typeParameters;
return styledLabel;
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
package org.eclipse.sirius.components.papaya.provider.spec;

import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemStyledLabelProvider;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Constructor;
import org.eclipse.sirius.components.papaya.NamedElement;
import org.eclipse.sirius.components.papaya.provider.ConstructorItemProvider;
Expand Down Expand Up @@ -48,22 +48,34 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof Constructor constructor) {
if (constructor.eContainer() instanceof NamedElement namedElement && namedElement.getName() != null && !namedElement.getName().isBlank()) {
var text = namedElement.getName();
StyledString styledLabel = new StyledString();
styledLabel.append(namedElement.getName());

return text + constructor.getParameters().stream()
.map(parameter -> {
var adapter = new PapayaItemProviderAdapterFactory().adapt(parameter, IItemLabelProvider.class);
if (adapter instanceof IItemLabelProvider itemLabelProvider) {
return itemLabelProvider.getText(parameter);
}
return "";
})
.collect(Collectors.joining(", ", "(", ")"));
styledLabel.append("(", PapayaStyledStringStyles.DECORATOR_STYLE);

for (var i = 0; i < constructor.getParameters().size(); i++) {
var parameter = constructor.getParameters().get(i);

var adapter = new PapayaItemProviderAdapterFactory().adapt(parameter, IItemStyledLabelProvider.class);
if (adapter instanceof IItemStyledLabelProvider itemStyledLabelProvider) {
var rawStyledString = itemStyledLabelProvider.getStyledText(parameter);
if (rawStyledString instanceof StyledString styledString) {
styledLabel.append(styledString);
}
}

if (i < constructor.getParameters().size() - 1) {
styledLabel.append(", ", PapayaStyledStringStyles.DECORATOR_STYLE);
}
}

styledLabel.append(")", PapayaStyledStringStyles.DECORATOR_STYLE);
return styledLabel;
}
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
package org.eclipse.sirius.components.papaya.provider.spec;

import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.GenericType;
import org.eclipse.sirius.components.papaya.Type;
import org.eclipse.sirius.components.papaya.provider.GenericTypeItemProvider;
Expand All @@ -36,20 +36,32 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof GenericType genericType) {
StyledString styledLabel = new StyledString();

var text = Optional.ofNullable(genericType.getRawType()).map(Type::getName).orElse("");
styledLabel.append(text);

if (!genericType.getTypeArguments().isEmpty()) {
text = text + "<";
styledLabel.append("<", PapayaStyledStringStyles.DECORATOR_STYLE);

for (var i = 0; i < genericType.getTypeArguments().size(); i++) {
var typeParameter = genericType.getTypeArguments().get(i);
var typeArgumentLabel = this.getStyledText(typeParameter);
if (typeArgumentLabel instanceof StyledString styledString) {
styledLabel.append(styledString);

text = text + genericType.getTypeArguments().stream()
.map(this::getText)
.collect(Collectors.joining(", "));
if (i < genericType.getTypeArguments().size() - 1) {
styledLabel.append(", ", PapayaStyledStringStyles.DECORATOR_STYLE);
}
}
}

text = text + ">";
styledLabel.append(">", PapayaStyledStringStyles.DECORATOR_STYLE);
}
return text;
return styledLabel;
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
package org.eclipse.sirius.components.papaya.provider.spec;

import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Interface;
import org.eclipse.sirius.components.papaya.TypeParameter;
import org.eclipse.sirius.components.papaya.provider.InterfaceItemProvider;
import org.eclipse.sirius.components.papaya.provider.spec.images.VisibilityOverlayImageProvider;

Expand Down Expand Up @@ -46,20 +45,26 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
var text = this.getString("_UI_Interface_type");
if (object instanceof Interface anInterface) {
if (anInterface.getName() != null && !anInterface.getName().isBlank()) {
var typeParameters = "";
if (!anInterface.getTypeParameters().isEmpty()) {
typeParameters = anInterface.getTypeParameters()
.stream()
.map(TypeParameter::getName)
.collect(Collectors.joining(", ", "<", ">"));
public Object getStyledText(Object object) {
if (object instanceof Interface anInterface && anInterface.getName() != null && !anInterface.getName().isBlank()) {
StyledString styledLabel = new StyledString();
styledLabel.append(anInterface.getName());

if (!anInterface.getTypeParameters().isEmpty()) {
styledLabel.append("<", PapayaStyledStringStyles.DECORATOR_STYLE);

for (var i = 0; i < anInterface.getTypeParameters().size(); i++) {
var typeParameter = anInterface.getTypeParameters().get(i);
styledLabel.append(typeParameter.getName(), PapayaStyledStringStyles.GENERIC_TYPE_STYLE);
if (i < anInterface.getTypeParameters().size() - 1) {
styledLabel.append(", ", PapayaStyledStringStyles.DECORATOR_STYLE);
}
}
text = anInterface.getName() + typeParameters;

styledLabel.append(">", PapayaStyledStringStyles.DECORATOR_STYLE);
}
return styledLabel;
}
return text;
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
package org.eclipse.sirius.components.papaya.provider.spec;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.provider.ComposedImage;
import org.eclipse.emf.edit.provider.IItemLabelProvider;
import org.eclipse.emf.edit.provider.IItemStyledLabelProvider;
import org.eclipse.emf.edit.provider.StyledString;
import org.eclipse.sirius.components.papaya.Operation;
import org.eclipse.sirius.components.papaya.provider.OperationItemProvider;
import org.eclipse.sirius.components.papaya.provider.PapayaItemProviderAdapterFactory;
Expand Down Expand Up @@ -48,29 +48,40 @@ public Object getImage(Object object) {
}

@Override
public String getText(Object object) {
public Object getStyledText(Object object) {
if (object instanceof Operation operation && operation.getName() != null && !operation.getName().isBlank()) {
var text = operation.getName();
StyledString styledLabel = new StyledString();
styledLabel.append(operation.getName());

var parameters = operation.getParameters().stream()
.map(parameter -> Optional.ofNullable(new PapayaItemProviderAdapterFactory().adapt(parameter, IItemLabelProvider.class))
.filter(IItemLabelProvider.class::isInstance)
.map(IItemLabelProvider.class::cast)
.map(itemLabelProvider -> itemLabelProvider.getText(parameter))
.orElse("")
)
.collect(Collectors.joining(", ", "(", ")"));
styledLabel.append("(", PapayaStyledStringStyles.DECORATOR_STYLE);

for (var i = 0; i < operation.getParameters().size(); i++) {
var parameter = operation.getParameters().get(i);
var adapter = new PapayaItemProviderAdapterFactory().adapt(parameter, IItemStyledLabelProvider.class);
if (adapter instanceof IItemStyledLabelProvider itemStyledLabelProvider) {
var rawStyledText = itemStyledLabelProvider.getStyledText(parameter);
if (rawStyledText instanceof StyledString styledString) {
styledLabel.append(styledString);

if (i < operation.getParameters().size() - 1) {
styledLabel.append(", ", PapayaStyledStringStyles.DECORATOR_STYLE);
}
}
}
}

styledLabel.append(")", PapayaStyledStringStyles.DECORATOR_STYLE);

var type = "";
if (operation.getType() != null) {
var adapter = new PapayaItemProviderAdapterFactory().adapt(operation.getType(), IItemLabelProvider.class);
if (adapter instanceof IItemLabelProvider itemLabelProvider) {
type = ": " + itemLabelProvider.getText(operation.getType());
styledLabel.append(": ", PapayaStyledStringStyles.DECORATOR_STYLE);
styledLabel.append(itemLabelProvider.getText(operation.getType()), PapayaStyledStringStyles.TYPE_STYLE);
}
}

return text + parameters + type;
return styledLabel;
}
return super.getText(object);
return super.getStyledText(object);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.papaya.provider.spec;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.edit.provider.StyledString;

/**
* Used to contain the various styles of StyledString used in Papaya.
*
* @author sbegaudeau
*/
public final class PapayaStyledStringStyles {
public static final StyledString.Style DECORATOR_STYLE = StyledString.Style.newBuilder()
.setForegroundColor(URI.createURI("color://rgb/120/120/120"))
.toStyle();

public static final StyledString.Style TYPE_STYLE = StyledString.Style.newBuilder()
.setForegroundColor(URI.createURI("color://rgb/40/77/181"))
.toStyle();

public static final StyledString.Style GENERIC_TYPE_STYLE = StyledString.Style.newBuilder()
.setForegroundColor(URI.createURI("color://rgb/40/100/120"))
.toStyle();

private PapayaStyledStringStyles() {
// Prevent instantiation
}
}
Loading

0 comments on commit f851b2f

Please sign in to comment.