Skip to main content



      Home
Home » Modeling » Graphiti » IGaService.creatFont() ?
IGaService.creatFont() ? [message #709946] Thu, 04 August 2011 10:08 Go to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: IGaService.creatFont() ? [message #710793 is a reply to message #710593] Fri, 05 August 2011 08:16 Go to previous messageGo to next message
Eclipse UserFriend
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 09:28] by Moderator

Re: IGaService.creatFont() ? [message #716497 is a reply to message #710793] Wed, 17 August 2011 11:00 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

can you open a bugzilla to track this.
Maybe we should even deprecate these methods if people get confused
and they are not really necessary anymore..

Best, Tim
Re: IGaService.creatFont() ? [message #717743 is a reply to message #716497] Mon, 22 August 2011 04:39 Go to previous message
Eclipse UserFriend
DONE: https://bugs.eclipse.org/bugs/show_bug.cgi?id=355347
Previous Topic:Layout Feature with RoundedRectangle as a child
Next Topic:Problems with respect to printers: Slow performance
Goto Forum:
  


Current Time: Wed Jul 09 23:41:38 EDT 2025

Powered by FUDForum. Page generated in 0.05602 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top