Using SVG to save JGoImages

I have a class that extends JGoNode that has an icon image loaded with a local resource within the JAR file (this.getClass().getResource(“images/cs2000Box.GIF”)…).

Here is part of the save file, notice that JGoImage saves the pathing information to the image:

:        :        :        :
: : : :

The problem is when you give this to someone else that has a differing path it won’t load the images. Is the solution to just save off the resource name and not the JGoImage or is there a better way???

dWiGhT

Saving the resource name and not the image is certainly a reasonable solution.
Another possible approch would be to save a relative path in the generated XML. The setDefaultBase method in JGoImage specifies the base location that things are relative to. This needs to be set dynamically by your application both at save and load time. Something like the following:
image = new JGoImage(myStdPoint, getStdSize());
JGoImage.setDefaultBase(ActivityNode.class.getResource(""));
image.loadImage("images/star.gif", true);

Ok…Ermm tried this and I still am getting absolute pathing in the SVG file:

Here is the SVG Stuff…

public void SVGWriteObject(DomDoc svgDoc, DomElement jGoElementGroup)
{
if (svgDoc.JGoXMLOutputEnabled()) {
DomElement multiPortNode = svgDoc.createJGoClassElement(“com.cloudshield.vppc.CSPortNode”, jGoElementGroup);

  if (myIcon != null) {
      JGoImage.setDefaultBase(VPPCApp.class.getResource(""));
    svgDoc.registerReferencingNode(multiPortNode, "multiporticon", myIcon);
  }
}

super.SVGWriteObject(svgDoc, jGoElementGroup);

}

public DomNode SVGReadObject(DomDoc svgDoc, JGoDocument jGoDoc, DomElement svgElement, DomElement jGoChildElement)
{
if (jGoChildElement != null) {
JGoImage.setDefaultBase(VPPCApp.class.getResource(""));
String multiporticon = jGoChildElement.getAttribute(“multiporticon”);
svgDoc.registerReferencingObject(this, “multiporticon”, multiporticon);

  super.SVGReadObject(svgDoc, jGoDoc, svgElement, jGoChildElement.getNextSiblingElement());
}
return svgElement.getNextSibling();

}

public void SVGUpdateReference(String attr, Object referencedObject)
{
super.SVGUpdateReference(attr, referencedObject);
if (attr.equals(“multiporticon”)) {
JGoImage.setDefaultBase(VPPCApp.class.getResource(""));
myIcon = (JGoImage)referencedObject;
}
}

Here is how the image is originally created:

public CSPortNode( Point loc, VPPCView parent )
{
super();
view = parent;
init( loc );
}

public void init( Point loc )
{
: : : :
// Set the default pathing for JGoImages
JGoImage.setDefaultBase(VPPCApp.class.getResource(""));

// Load the CS2000 image to make this node look nice
myIcon = new JGoImage();

myIcon.loadImage(this.getClass().getResource(“images/cs2000Box.GIF”), true);
: : : :

}

The resultant SVG snippet:

:        :        :            :
: : : :

Ok… solved my own problem. Clap

After looking at the source code for JGoImage I was confused why loadImage( URL ) wasn’t using the “default base URL”. After looking at the loadImage( String ) I noticed it was!!! Looking back at the example that was given I would just have to change my code to the following:

myIcon.loadImage("images/cs2000Box.GIF", true);

…all is good now in JGoUserLand().

dWiGhT

Ouch problem.

This works great when it isn’t a JAR file but when it is JAR’d up getResource("") returns null and then every loadImage() fails.

…is there another way to setDefaultBase() when a JAR file is involved?

dWiGhT

Hmmm… guess there is no solution, huh?

Anyone have any suggestions for how to redesign this? Could I extract and save the images in to a temp directory and setDefaultBase() that way?

dWiGhT

This is apparently a Java bug:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4337703
Synopsis: URLClassLoader.getResource() fails (return null) on .jar file.
Sorry, I wish I had better news for you.