arcgissamples\defensesolutions\MoleLeaderStacker.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.defensesolutions; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.UnknownHostException; import java.util.*; import javax.swing.*; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JFrame; import javax.swing.JSplitPane; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.carto.*; import com.esri.arcgis.geometry.*; import com.esri.arcgis.defensesolutions.*; import com.esri.arcgis.display.*; public class MOLELeaderStacker extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private ToolbarBean toolbarBean = null; private JSplitPane jSplitPane = null; private TOCBean TOCBean = null; private MapBean mapBean = null; private JButton addSymbolsBtn = null; private JButton leaderBtn = null; private JButton stackBtn = null; private JButton clearBtn = null; private JButton undoBtn = null; private Random randomNum = new Random(); String[] sic = { "SFAPC----------", "SFAPCF---------", "SFAPFH---------", "SFAPCL---------", "SFAPM----------" }; private MoleGroupElement moleGroup ; private IElement element; // actionPerformed // calls the event handler method that corresponds to the button // that was clicked public void actionPerformed( ActionEvent e) { try { if (e.getSource() == addSymbolsBtn) addSymbols(); else if (e.getSource() == stackBtn) stackSymbols(); else if (e.getSource() == leaderBtn) leaderSymbols(); else if (e.getSource() == undoBtn) undoDecluttering(); else if (e.getSource() == clearBtn) clear(); } catch (Exception ex) { ex.printStackTrace(); } } // createRandomPoint // creates a new point and sets its properties public IPoint createRandomPoint() { IPoint point = null; try { point = new com.esri.arcgis.geometry.Point(); point.putCoords(-180 + randomNum.nextDouble()*180, -90 + randomNum.nextDouble()*90); } catch (UnknownHostException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } return point; } // addSymbols // creates and draws the military symbols defined in the Symbol ID code array public void addSymbols() { try { // Create a class-wide MoleGroupElement object moleGroup = new com.esri.arcgis.defensesolutions.MoleGroupElement(); IGroupElement ge = (IGroupElement) moleGroup; if (ge != null) { // Populate the MoleGroupElement using the Symbol ID code array IElement el = null; for (int i = 0; i < sic.length; ++i) { IPoint pPoint = createRandomPoint(); el = buildElement( pPoint, sic[i]); ge.addElement(el); } } // Set the class-wide IElement object to the MoleGroupElement object element = moleGroup; if (element != null) draw(); } catch (Exception ex) { ex.printStackTrace(); } } // leaderSymbols // groups MOLE symbols using the leadering method private void leaderSymbols() { try { if (moleGroup != null) { // set base options moleGroup.setDeclutterOption( moleDeclutterOptionEnum.moleDeclutterLeader); moleGroup.setEnableDeclutter(true); // set leadering options ((IMoleLeaderElement) moleGroup).setLeaderQuadrant( moleQuadrantEnum.moleQuadrantUR); ((IMoleLeaderElement) moleGroup).setAnchor( createRandomPoint()); ((IMoleLeaderElement) moleGroup).setBase( createRandomPoint()); element = (IElement) moleGroup; if (element != null) draw(); } } catch (Exception ex) { ex.printStackTrace(); } } // stackSymbols // groups MOLE symbols using the stacking method private void stackSymbols() { try { if (moleGroup != null) { // set base options moleGroup.setDeclutterOption( moleDeclutterOptionEnum.moleDeclutterStack); moleGroup.setEnableDeclutter(true); // Set stacking options ((IMoleStackElement) moleGroup).setStackQuadrant( moleQuadrantEnum.moleQuadrantLR); element = (IElement) moleGroup; if (element != null) draw(); } } catch (Exception ex) { ex.printStackTrace(); } } // undoDecluttering // undoes the grouping of MOLE symbols, whethre leadered or stacked private void undoDecluttering() { try { if (moleGroup != null) { moleGroup.setDeclutterOption( moleDeclutterOptionEnum.moleDeclutterNone); moleGroup.setEnableDeclutter(false); element = (IElement) moleGroup; if (element != null) draw(); } } catch (Exception ex) { ex.printStackTrace(); } } // draw // draws all MOLE symbols stored in the class-wide MoleGroupElement object private void draw() { try { if (element != null) { mapBean.getActiveView().getGraphicsContainer().addElement( element, 0); mapBean.getActiveView().partialRefresh( esriViewDrawPhase.esriViewGraphics, null, null); } } catch (Exception ex) { ex.printStackTrace(); } } // clear // clears all MOLE symbols currently displayed private void clear() { try { moleGroup.clearElements(); mapBean.getActiveView().getGraphicsContainer().deleteAllElements(); mapBean.getActiveView().refresh(); } catch (Exception ex) { ex.printStackTrace(); } } // buildElement // creates and returns an IElement object from a IMoleSymbol object private IElement buildElement(IPoint p, String sic) { try { IMoleSymbol ms = new MoleMarkerSymbol(); ms.setSymbolID( sic); // create the MarkerElement IMarkerElement me = new MarkerElement(); me.setSymbol((IMarkerSymbol) ms); element = (IElement) me ; element.setGeometry( (IGeometry) p); } catch (Exception ex) { ex.printStackTrace(); } return element; } /** * This method initializes jSplitPane * * @return javax.swing.JSplitPane */ private JSplitPane getJSplitPane() { if (jSplitPane == null) { jSplitPane = new JSplitPane(); jSplitPane.setDividerLocation(150); jSplitPane.setDividerSize(2); jSplitPane.setRightComponent(getMapBean()); jSplitPane.setLeftComponent(getTOCBean()); } return jSplitPane; } /** * This method initializes TOCBean * * @return com.esri.arcgis.beans.TOC.TOCBean */ private TOCBean getTOCBean() { if (TOCBean == null) { TOCBean = new TOCBean(); try { TOCBean.setBuddyControl(getMapBean()); } catch (IOException e) { e.printStackTrace(); } } return TOCBean; } /** * This method initializes mapBean * * @return com.esri.arcgis.beans.map.MapBean */ private MapBean getMapBean() { try{ if (mapBean == null) { mapBean = new MapBean(); String arcgisHome = null; String dataPath = null; arcgisHome = System.getenv("AGSDEVKITJAVA"); if (arcgisHome == null) { JOptionPane.showInputDialog("AGSDEVKITJAVA environment variable is not set, exiting application"); System.exit(-1); } dataPath = arcgisHome + java.io.File.separator + "java" + java.io.File.separator + "samples" + java.io.File.separator + "data" + java.io.File.separator + "mole" + java.io.File.separator + "mole_base.mxd"; mapBean.loadMxFile( dataPath, null, null); } } catch(Exception e){} return mapBean; } /** * This is the default constructor */ public MOLELeaderStacker() { super(); initializeArcGISLicenses(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(802, 572); this.setContentPane(getJContentPane()); this.setTitle("Mole Leader Stack"); this.addWindowListener( new java.awt.event.WindowAdapter() { public void windowClosing( java.awt.event.WindowEvent e) { System.out.println("windowClosing()"); try{ new AoInitialize().shutdown(); } catch (Exception ex) { ex.printStackTrace(); } } }); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jContentPane = new JPanel(); jContentPane.setLayout( new BorderLayout()); jContentPane.add( getToolbarBean(), java.awt.BorderLayout.NORTH); jContentPane.add( getJSplitPane(), java.awt.BorderLayout.CENTER); jContentPane.add( getMolePanel(),java.awt.BorderLayout.SOUTH); } return jContentPane; } private JPanel getMolePanel() { JPanel panel = new JPanel(); panel.setLayout( new java.awt.FlowLayout()); addSymbolsBtn = new JButton( "Add MOLE Graphics"); leaderBtn = new JButton( "Apply Leadering"); stackBtn = new JButton( "Apply Stacking"); clearBtn = new JButton( "Clear"); undoBtn = new JButton( "Undo"); panel.add( addSymbolsBtn); panel.add( leaderBtn); panel.add( stackBtn); panel.add( undoBtn); panel.add( clearBtn); addSymbolsBtn.addActionListener(this); leaderBtn.addActionListener(this); stackBtn.addActionListener(this); undoBtn.addActionListener(this); clearBtn.addActionListener(this); return panel; } public static void main(String[] args) { EngineInitializer.initializeVisualBeans(); MOLELeaderStacker map = new MOLELeaderStacker(); map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); map.setVisible(true); } /** * This method initializes toolbarBean * * @return com.esri.arcgis.beans.toolbar.ToolbarBean */ private ToolbarBean getToolbarBean() { if (toolbarBean == null) { toolbarBean = new ToolbarBean(); toolbarBean.setItemsString( "11|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;11|controls/ControlsMapZoomToLastExtentBackCommand|0|-1|0|0|1;11|controls/ControlsMapZoomToLastExtentForwardCommand|0|-1|0|0|1;11|controls/ControlsMapPanTool|0|-1|0|0|1;11|controls/ControlsMapZoomInTool|0|-1|0|0|1;11|controls/ControlsMapZoomOutTool|0|-1|0|0|1"); try { toolbarBean.setBuddyControl( getMapBean()); } catch (IOException e) { e.printStackTrace(); } } return toolbarBean; } static void initializeArcGISLicenses() { try { com.esri.arcgis.system.AoInitialize ao = new com.esri.arcgis.system.AoInitialize(); if (ao.isProductCodeAvailable( com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine) == com.esri.arcgis.system.esriLicenseStatus.esriLicenseAvailable) ao.initialize( com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeEngine); else { System.err.println( "Could not initialize an ArcGIS Engine license. Exiting application."); System.exit(-1); } } catch (Exception e) { e.printStackTrace(); } } }