Does anyone use Java to make Add-in for Desktop Extension?

615
2
07-17-2011 11:41 AM
Marianne_BilstedWiese
New Contributor
Hello Forum

I am trying to make a custom renderer as a Java add-in. I made the following very simple code, which paints all polygons green.
The strange thing is, that each time i compile this program, it will work the first time I try it out in ArcMap.
If I close restart ArcMap, then all polygons get painted in red by this Add-in, which I guess is a sort of fall back behaviour.
No error is catched, and the printlines in the draw method are both printed to the console. I make the greenFillSymbol new each time. So I do not understand this behaviour at all.
My question is, if anyone out there is actually making Java Add-ins for Desktop extensions?
It may be, that I should revert to C++, and wait for another version of ArcGIS before changing to Java.

I have attached the sorce.

Best Regards

Marianne Wiese

package dk.geus;

import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.IOException;
import com.esri.arcgis.carto.*;
import com.esri.arcgis.display.*;
import com.esri.arcgis.geodatabase.*;
import com.esri.arcgis.geometry.*;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.*;
import com.esri.arcgis.interop.extn.ArcGISExtension;

@ArcGISExtension
public class LegendRenderer implements IFeatureRenderer {
 
 private static final long serialVersionUID = 1L;

 public LegendRenderer() { }

    public SimpleFillSymbol greenSymbol() {
     
     SimpleFillSymbol greenFillSymbol = null;
 
     try {
            greenFillSymbol = new SimpleFillSymbol();
         greenFillSymbol.setStyle(esriSimpleFillStyle.esriSFSSolid);
      greenFillSymbol.getOutline().setWidth(0.4);
            IColor greenColor = new RgbColor();
         greenColor.setRGB(0x00ff00);
         greenFillSymbol.setColor(greenColor);    
     } catch (Exception e) {
   e.printStackTrace();
  }
     return greenFillSymbol; 
    }
    
 // Available only for specific geometry types
 public boolean canRender(IFeatureClass fc, IDisplay display) throws IOException, AutomationException {

  if (fc.getShapeType() == esriGeometryType.esriGeometryPolygon)
         return true;
     else
         return false;
 }

 // Could be used for query filter
 public void prepareFilter(IFeatureClass fc, IQueryFilter qFilter)throws IOException, AutomationException { }

 // How to render
 public void draw(IFeatureCursor featureCursor, int drawPhase, IDisplay display, ITrackCancel trackCancel) throws IOException,AutomationException {
  System.out.println("Hello, here I am");
     
     try {      
   IFeature feature = featureCursor.nextFeature(); 
   display.setSymbol(greenSymbol());
   do{
    display.drawPolygon(feature.getShape());
    feature = featureCursor.nextFeature();
   }while (feature != null);

   System.out.println("Hello, here I was");
   
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
  return;
 }

 public ISymbol getSymbolByFeature(IFeature arg0) throws IOException,AutomationException {
  return null;
 }

 public boolean isRenderPhase(int drawPhase) throws IOException,AutomationException {
  if (drawPhase == esriDrawPhase.esriDPGeography) return true;
     else return false;
 }

 public void setExclusionSetByRef(IFeatureIDSet featureIDSet)throws IOException, AutomationException { }

 public void writeExternal(ObjectOutput out) throws IOException { }

 public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException { }
}

0 Kudos
2 Replies
SankarSinha
New Contributor
I think you need to persist the symbol in the writeExternal() and read it back in the readExternal()

An example is as below :-

                public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
                                sSymbol = (ISymbol) in.readObject(); // In your case the sSymbol will be his greenFillSymbol.
                }

                public void writeExternal(ObjectOutput out) throws IOException {
                                out.writeObject(sSymbol);
                }

-Sankar
0 Kudos
Marianne_BilstedWiese
New Contributor
Hello

Thanks for the answer Sankar.
However, I am not even trying to save the document yet, so I should not need to save the symbol.

I was testing it on several feature layers, and it would become green the first time I start ArcMap, but the following times it would go red.

Also, today there is nothing wrong, the green symbol is used every time, i try. :confused:
Really strange, I havent even rebooted. I checked in a license I had borrowed, thats all.

Sorry to have asked about a problem I can't reproduce now.
I tried at least 5 times the other day though.

I think I will continue with Java, as long as it works.

Best Regards
Marianne Wiese
0 Kudos