Scale renderer
arcgissamples\cartography\ScaleRendererMain.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.cartography;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.UIManager;

import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.carto.IFeatureRenderer;
import com.esri.arcgis.carto.IGeoFeatureLayer;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineContext;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * This class provides a framework for demo application.
 * Load shapefile and make scale changes.
 * The sample works on polygon features.
 */
public class ScaleRendererMain extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;
  static AoInitialize aoInit;
  MapBean mapBean = new MapBean();
  Label labelComment = new Label();
  Button buttonOpenFile = new Button();
  Button buttonZoomIn = new Button();
  Button buttonZoomOut = new Button();

  /**
   * Constructor
   */
  public ScaleRendererMain() {
    initUI();
  }

  /**
   * Method to start the program execution.
   * @param s String[]
   */
  public static void main(String s[]) {
    try {
      EngineInitializer.initializeVisualBeans();
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      initializeArcGISLicenses();
      new ScaleRendererMain();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  static void initializeArcGISLicenses() {
    try {
      aoInit = new AoInitialize();
      
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) 
          == esriLicenseStatus.esriLicenseAvailable)
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
    } catch (Exception e) {e.printStackTrace();}
  }

  /*
   * Lays out the User Interface, and sets up event listeners
   */
  private void initUI() {
    this.setTitle("ScaleRenderer Java Sample");
    this.setSize(new Dimension(600, 500));
    this.getContentPane().setLayout(new BorderLayout());

    this.labelComment.setText("Open java/samples/data/usa/states.shp file.");
    this.buttonOpenFile.setLabel("Open File ...");
    this.buttonZoomIn.setLabel("ZoomIn");
    this.buttonZoomOut.setLabel("ZoomOut");

    Panel toolPanel = new Panel(new FlowLayout(FlowLayout.LEADING));

    toolPanel.add(this.labelComment);
    toolPanel.add(this.buttonOpenFile);
    toolPanel.add(this.buttonZoomIn);
    toolPanel.add(this.buttonZoomOut);

    this.getContentPane().add(this.mapBean, BorderLayout.CENTER);
    this.getContentPane().add(toolPanel, BorderLayout.SOUTH);
    this.setVisible(true);

    // set listeners
    this.buttonOpenFile.addActionListener(this);
    this.buttonZoomIn.addActionListener(this);
    this.buttonZoomOut.addActionListener(this);
    this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        try {
          aoInit.shutdown();
        } catch(IOException ex) {
          // exit anyway
        }
        System.exit(0);
      }
    });
  }

  /**
   * implements interface ActionListener
   * @see ActionListener#actionPerformed
   * @param event
   */
  public void actionPerformed(ActionEvent event) {
    Object eventSource = event.getSource();
    if (eventSource == this.buttonOpenFile) {
      try {
        FileDialog fileDialog = new FileDialog(this, "Open File", FileDialog.LOAD);
        fileDialog.setVisible(true);
        String fullFilename = fileDialog.getDirectory() + fileDialog.getFile();
        String path = fullFilename.substring(0, fullFilename.lastIndexOf(File.separator));
        String name = fullFilename.substring(fullFilename.lastIndexOf(File.separator) + 1);
        this.mapBean.clearLayers();
        this.mapBean.addShapeFile(path, name);
        ILayer layer = this.mapBean.getLayer(0);
        IGeoFeatureLayer geoFeatureLayer = (IGeoFeatureLayer)(layer);
        IFeatureRenderer scaleRenderer = (IFeatureRenderer) EngineContext.createObject(ScaleRenderer.class);
        geoFeatureLayer.setRendererByRef(scaleRenderer);
        this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null, null);
      } catch (java.io.IOException ex) {
        System.out.println("Can't load data, exception:" + ex);
      }
    }
    try {
      if (eventSource == this.buttonZoomIn) {
        double mapScale = this.mapBean.getMapScale();
        this.mapBean.setMapScale(mapScale / 1.2);
        this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
      }
      if (eventSource == this.buttonZoomOut) {
        double mapScale = this.mapBean.getMapScale();
        this.mapBean.setMapScale(mapScale * 1.2);
        this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
      }
    } catch (java.io.IOException ex) {
      ex.printStackTrace(); // never happened
    }
  }
}