Home » Modeling » Graphiti » IGaService.creatFont() ?
IGaService.creatFont() ? [message #709946] |
Thu, 04 August 2011 10:08  |
Eclipse User |
|
|
|
I downloaded a sample ("Libray Example" from "Getting Started with Graphiti", Michael Wenz, slide linked in http://www.eclipse.org/graphiti/documentation/ ) and it gives some compilation error
IGaService gaService = Graphiti.getGaService();
...
gaService.createFont(libraryNameText, "Arial", 16);
Apparently some older api version had this method, it doesn't exists now (Graphiti 0.8) I changed it (by guessing) to:
libraryNameText.setFont(gaService.manageFont(diagram, "Arial", 16));
Does this sound right? It sounded to me, but when I try to run the sample project it throws some (apparently related) exception:
Caused by: org.eclipse.emf.ecore.xmi.DanglingHREFException: The object 'org.eclipse.graphiti.mm.algorithms.styles.impl.FontImpl@14adad2 (name: Arial, size: 16, italic: false, bold: false)' is not contained in a resource.
at org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl.handleDanglingHREF(XMLHelperImpl.java:760)
Any clues?
PS: I'm not terribly interested in making that sample work, more in making sure that that is the right way to set the font.
[Updated on: Thu, 04 August 2011 10:21] by Moderator
|
|
|
Re: IGaService.creatFont() ? [message #710248 is a reply to message #709946] |
Thu, 04 August 2011 17:11   |
Eclipse User |
|
|
|
Hi Hernan,
this code is from org.eclipse.graphiti.tests.cases.GaServiceTest.deleteFont
Text text = gas.createDefaultText(d, s1);
Font font = gas.manageFont(d, FONTNAME, 10);
text.setFont(font);
assertEquals(font, text.getFont());
gas.deleteFont(font);
assertNull(text.getFont());
Be aware that you create your font only once, otherwise your diagram file will contain duplicate font entries. Although this causes no crash it is not intended.
<pi:Diagram ...>
<graphicsAlgorithm ... />
...
<children>
</children>
...
<connections>
...
</connections>
...
<fonts name="Arial" size="10"/>
<fonts name="Arial" size="10"/>
<fonts name="Arial" size="10"/>
<fonts name="Arial" size="10"/>
</pi:Diagram>
So call aService.manageFont(diagram, "Arial", 16) only once and use the reference in the setFont method.
Cheers,
Joerg
|
|
|
Re: IGaService.creatFont() ? [message #710357 is a reply to message #710248] |
Thu, 04 August 2011 20:04   |
Eclipse User |
|
|
|
Joerg Reichert wrote on Thu, 04 August 2011 18:11
Be aware that you create your font only once, otherwise your diagram file will contain duplicate font entries.
...
So call aService.manageFont(diagram, "Arial", 16) only once and use the reference in the setFont method.
Wow, thanks, that sounds somewhat scaring... First, I found a little strange that the service API has a deleteFont() method and not a createFont() method. I assumed that manageFont() was a "clever" createFont() version, in that it creates it if not found in the diagram, elsewhere finds and return it. From the javadocs, it "Provides a font instance by either creating a new one and aggregating it to the diagram or finding it in the diagrams list of fonts."
Doesn't this contradicts that ocurrence of duplicate fonts? Is this a bug?
Further, how can I make sure that I create the font only once?
|
|
|
Re: IGaService.creatFont() ? [message #710593 is a reply to message #710357] |
Fri, 05 August 2011 03:41   |
Eclipse User |
|
|
|
This is the actual implementation of manageFont in GaServiceImpl (manageFont(Diagram, String, int) delegates to it):
@Override
public Font manageFont(Diagram diagram, String name, int size, boolean isItalic, boolean isBold) {
EList<Font> fonts = diagram.getFonts();
for (Font font : fonts) {
if (font.getName().equals(name) && font.getSize() == size && font.isBold() == isBold && font.isItalic() == isItalic)
return font;
}
Font newFont = StylesFactory.eINSTANCE.createFont();
newFont.setName(name);
newFont.setSize(size);
newFont.setItalic(isItalic);
newFont.setBold(isBold);
fonts.add(newFont);
return newFont;
}
So actually this method does what JavaDocs says. So I had a look in my code now, why despite of that I have this behavior:
Text text = gaService.createDefaultText(getDiagram(), textShape);
createDefaultText Internally creates a font of type "Arial" in size 8.
But then I assign my own font attributes:
text.getFont().setName("Arial");
text.getFont().setSize(10);
text.getFont().setBold(false);
text.setHorizontalAlignment(Orientation.ALIGNMENT_RIGHT);
text.setVerticalAlignment(Orientation.ALIGNMENT_CENTER);
text.setForeground(gaService.manageColor(diagram,ColorConstant.BLACK));
I have a helper method doing this and I call that in every add feature. So what happens: createDefaultText creates a font of type "Arial", 8, then I change it to Arial, 10. The next time I call createDefaultText, there is no font Arial, 8 anymore, so it creates a new one, then it is changed to Arial, 10 again. So the duplicate fonts are created. How to overcome that?: Use gaService.createText instead of gaService.createDefaultText.
Joerg
|
|
| | | |
Goto Forum:
Current Time: Wed Jul 09 23:41:38 EDT 2025
Powered by FUDForum. Page generated in 0.05602 seconds
|