Skip to content

Commit

Permalink
[426] Use the font defined in the GMF node to compute label size
Browse files Browse the repository at this point in the history
Bug: #426
  • Loading branch information
lredor committed Jul 26, 2024
1 parent 1d877b0 commit ba99866
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import org.eclipse.core.runtime.Path;
Expand All @@ -45,12 +46,14 @@
import org.eclipse.gmf.runtime.notation.Bounds;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.Edge;
import org.eclipse.gmf.runtime.notation.FontStyle;
import org.eclipse.gmf.runtime.notation.LayoutConstraint;
import org.eclipse.gmf.runtime.notation.Location;
import org.eclipse.gmf.runtime.notation.Node;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.RelativeBendpoints;
import org.eclipse.gmf.runtime.notation.Size;
import org.eclipse.gmf.runtime.notation.Style;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;
import org.eclipse.jface.resource.ImageDescriptor;
Expand All @@ -74,6 +77,7 @@
import org.eclipse.sirius.diagram.ui.business.internal.query.DNodeContainerQuery;
import org.eclipse.sirius.diagram.ui.business.internal.query.DNodeQuery;
import org.eclipse.sirius.diagram.ui.edit.api.part.AbstractDiagramElementContainerEditPart;
import org.eclipse.sirius.diagram.ui.edit.api.part.IDiagramNameEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.AbstractDNodeContainerCompartmentEditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNodeContainer2EditPart;
import org.eclipse.sirius.diagram.ui.internal.edit.parts.DNodeList2EditPart;
Expand Down Expand Up @@ -1086,7 +1090,16 @@ public static Dimension getLabelDimension(Node node, Dimension defaultDimension)
if (!new DDiagramElementQuery(dDiagramElement).isLabelHidden()) {
if (siriusStyle instanceof BasicLabelStyle) {
BasicLabelStyle bls = (BasicLabelStyle) siriusStyle;
Font defaultFont = VisualBindingManager.getDefault().getFontFromLabelStyle(bls, (String) viewQuery.getDefaultValue(NotationPackage.Literals.FONT_STYLE__FONT_NAME));
String fontName = (String) viewQuery.getDefaultValue(NotationPackage.Literals.FONT_STYLE__FONT_NAME);
Optional<Style> optionalStyle = getFontStyleOf(node);
if (optionalStyle.isPresent() && optionalStyle.get() instanceof FontStyle) {
String currentFontName = ((FontStyle) optionalStyle.get()).getFontName();
if (currentFontName != null && !currentFontName.isEmpty()) {
// Use the defined font name in the node if it is defined.
fontName = currentFontName;
}
}
Font defaultFont = VisualBindingManager.getDefault().getFontFromLabelStyle(bls, fontName);
try {
labelSize = FigureUtilities.getStringExtents(dDiagramElement.getName(), defaultFont);
if (bls.isShowIcon()) {
Expand All @@ -1106,6 +1119,23 @@ public static Dimension getLabelDimension(Node node, Dimension defaultDimension)
return labelSize;
}

/**
* Get the font style to use for this node. As in
* {@link org.eclipse.sirius.diagram.ui.edit.api.part.DiagramNameEditPartOperation#getFontStyle(IDiagramNameEditPart)},
* the parent style is used if no one is defined for the node.
*
* @param node
* The node to use
* @return The font style to use for this node
*/
private static Optional<Style> getFontStyleOf(Node node) {
Style style = node.getStyle(NotationPackage.eINSTANCE.getFontStyle());
if (style == null && node.eContainer() instanceof View container) {
style = container.getStyle(NotationPackage.eINSTANCE.getFontStyle());
}
return Optional.ofNullable(style);
}

private static Dimension getIconDimension(DSemanticDecorator dSemanticDecorator, BasicLabelStyle bls) {
ImageDescriptor descriptor = null;
EObject target = dSemanticDecorator.getTarget();
Expand Down

0 comments on commit ba99866

Please sign in to comment.