Home » Modeling » Graphiti » IGaService.creatFont() ?
IGaService.creatFont() ? [message #709946] |
Thu, 04 August 2011 14:08 |
Hernan Gonzalez Messages: 188 Registered: October 2010 Location: Buenos Aires, Argentina |
Senior Member |
|
|
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 14:21] Report message to a moderator
|
|
|
Re: IGaService.creatFont() ? [message #710248 is a reply to message #709946] |
Thu, 04 August 2011 21:11 |
Joerg Reichert Messages: 80 Registered: July 2009 Location: Leipzig |
Member |
|
|
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
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
|
|
|
Re: IGaService.creatFont() ? [message #710357 is a reply to message #710248] |
Fri, 05 August 2011 00:04 |
Hernan Gonzalez Messages: 188 Registered: October 2010 Location: Buenos Aires, Argentina |
Senior Member |
|
|
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 07:41 |
Joerg Reichert Messages: 80 Registered: July 2009 Location: Leipzig |
Member |
|
|
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
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
|
|
|
Re: IGaService.creatFont() ? [message #710793 is a reply to message #710593] |
Fri, 05 August 2011 12:16 |
Hernan Gonzalez Messages: 188 Registered: October 2010 Location: Buenos Aires, Argentina |
Senior Member |
|
|
Aha...
If I got it right, you could also change in your helper method the lines
text.getFont().setName("Arial");
text.getFont().setSize(10);
text.getFont().setBold(false);
to (as in the testcase posted earlier)
text.setFont(gaService.manageFont(getDiagram(), "Arial", 10,false,false));
It looks as if the Font.setXXX() methods should never called by the programmer.
Perhaps this should be explained in some wiki, or as a warning in the javadocs of the Font class.
[Updated on: Fri, 05 August 2011 13:28] Report message to a moderator
|
|
| | |
Goto Forum:
Current Time: Tue Apr 23 13:08:21 GMT 2024
Powered by FUDForum. Page generated in 0.03807 seconds
|