arcgissamples\cartography\PointDispersalRendererMain.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.TOC.TOCBean; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.FeatureLayer; import com.esri.arcgis.carto.IFeatureRenderer; import com.esri.arcgis.carto.IGeoFeatureLayer; 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 sample demonstrates how to create a custom renderer. The renderer draws * symbology for point, line and polygon feature layers and rendering all * features with the symbol. This sample demonstrates how to implement * ISimpleRenderer and ILegendInfo. Implementing ISimpleRenderer lets you assign * a symbol to the renderer before drawing and makes the renderer compatible * with the single symbol property page. Implementing ILegendInfo makes the * rendered layer compatible with the TOC. */ public class PointDispersalRendererMain extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; MapBean mapBean = new MapBean(); Button buttonOpenFile = new Button(); Button buttonCustomRenderer = new Button(); FeatureLayer featureLayer = null; ToolbarBean toolbarBean = null; TOCBean tocBean = new TOCBean(); static AoInitialize aoInit; public PointDispersalRendererMain() { } /** * Method to start the program execution. * @param s String[] */ public static void main(String s[]) { //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); if (!(new File(devKitHome).exists())) { System.out.println(devKitHome + " does not exist.\nExiting..."); System.exit(-1); } // // Change the following lines if you want to start with different data // String workspacePath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "usa"; String featureClassName = "wind"; File shapefileFile = new File(workspacePath, featureClassName + ".shp"); if (!shapefileFile.exists()) { System.out.println("Shapefile does not exist: "+ shapefileFile.getAbsolutePath()); System.exit(0); } try { EngineInitializer.initializeVisualBeans(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); PointDispersalRendererMain thisApp = new PointDispersalRendererMain(); thisApp.initUI(workspacePath, featureClassName); } 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(String path, String name) throws Exception { this.setTitle("PointDispersalRenderer - Engine Application"); this.setSize(new Dimension(700, 600)); this.getContentPane().setLayout(new BorderLayout()); Panel toolPanel = new Panel(new FlowLayout(FlowLayout.LEADING)); Label labelComment = new Label(); labelComment.setText("Render Point shapefile types."); this.buttonOpenFile.setLabel("Open new shapefile ..."); this.buttonCustomRenderer.setLabel("Apply Custom Renderer"); toolPanel.add(this.buttonOpenFile); toolPanel.add(this.buttonCustomRenderer); toolPanel.add(labelComment); try { this.mapBean.clearLayers(); this.mapBean.addShapeFile(path, name); this.featureLayer = (FeatureLayer) (this.mapBean.getLayer(0)); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null,null); tocBean.setBuddyControl(mapBean); } catch (IOException e1) { e1.printStackTrace(); } this.getContentPane().add(getToolbarBean(), BorderLayout.NORTH); this.getContentPane().add(this.tocBean, BorderLayout.WEST); this.getContentPane().add(this.mapBean, BorderLayout.CENTER); this.getContentPane().add(toolPanel, BorderLayout.SOUTH); this.buttonOpenFile.addActionListener(this); this.buttonCustomRenderer.addActionListener(this); this.setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { aoInit.shutdown(); } catch (IOException ex) { // exit anyway } System.exit(0); } }); } private ToolbarBean getToolbarBean() throws Exception { if (toolbarBean == null) { toolbarBean = new ToolbarBean(); toolbarBean.setItemsString("4|controls/ControlsAddDataCommand|0|-1|0|0|1;4|controls/ControlsOpenDocCommand|0|-1|0|0|1;4|controls/ControlsSaveAsDocCommand|0|-1|0|0|1;10|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomOutFixedCommand|0|-1|0|0|1;10|controls/ControlsMapZoomInTool|0|-1|0|0|1;10|controls/ControlsMapZoomOutTool|0|-1|0|0|1;10|controls/ControlsMapPanTool|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentBackCommand|0|-1|0|0|1;10|controls/ControlsMapZoomToLastExtentForwardCommand|0|-1|0|0|1;3|controls/ControlsClearSelectionCommand|0|-1|0|0|1;3|controls/ControlsSelectAllCommand|0|-1|0|0|1;3|controls/ControlsSelectByGraphicsCommand|0|-1|0|0|1;3|controls/ControlsZoomToSelectedCommand|0|-1|0|0|1;3|controls/ControlsSelectFeaturesTool|0|-1|0|0|1;4|controls/ControlsMapIdentifyTool|0|-1|0|0|1"); toolbarBean.setBuddyControl(mapBean); } return toolbarBean; } /** * @see ActionListener#actionPerformed * @param event */ public void actionPerformed(ActionEvent event) { Object evt = event.getSource(); if (evt == this.buttonOpenFile) { try { FileDialog fileDialog = new FileDialog(this, "Open ShapeFile",FileDialog.LOAD); fileDialog.setVisible(true); String path = fileDialog.getDirectory(); String name = fileDialog.getFile(); this.mapBean.clearLayers(); this.mapBean.addShapeFile(path, name); this.featureLayer = (FeatureLayer) (this.mapBean.getLayer(0)); this.mapBean.refresh(esriViewDrawPhase.esriViewGeography, null,null); } catch (IOException ex) { System.out.println("Couldn't load data. Try another shapefile."); } } if (evt == this.buttonCustomRenderer) { setCustomRenderer(); } } /** * The method creates CustomRenderer and sets properties depending on * feature type. */ private void setCustomRenderer() { try { // create a new PointDispersalRenderer IFeatureRenderer rend = (IFeatureRenderer) EngineContext.createObject(PointDispersalRenderer.class); IGeoFeatureLayer geoFeatureLayer = this.featureLayer; // finally, set the new renderer to the layer and refresh the map geoFeatureLayer.setRendererByRef(rend); this.mapBean.refresh(esriViewDrawPhase.esriViewBackground, null,null); this.tocBean.update(); } catch (java.lang.Exception ex) { ex.printStackTrace(); } } }