arcgissamples\symbologybean\tools\CreateScaleBarTool.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.symbologybean.tools; import javax.swing.SwingUtilities; import arcgissamples.symbologybean.ui.SymbologyFrame; import com.esri.arcgis.controls.BaseTool; import com.esri.arcgis.controls.HookHelper; import com.esri.arcgis.display.INewEnvelopeFeedback; import com.esri.arcgis.display.NewEnvelopeFeedback; import com.esri.arcgis.geometry.Envelope; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.interop.AutomationException; public class CreateScaleBarTool extends BaseTool { private HookHelper hookHelper; private IPoint point; private INewEnvelopeFeedback envFeedback; private boolean inUse = false; private SymbologyFrame symbolForm = null; private Envelope envelope; public CreateScaleBarTool() { name = "AddScaleBar"; message = "Adds a ScaleBar element to the page layout"; caption = "ScaleBar"; toolTip = "Add a ScaleBar"; category = "CustomTool"; enabled = true; } /* * On Create of ScaleBar Tool */ public void onCreate(Object obj) { try { // Create a hookhelper to hook pagelayput hookHelper = new HookHelper(); // Setting the hook to pagelayout hookHelper.setHookByRef(obj); } catch (Exception e) { e.printStackTrace(); } } /* * On Mousedown after clicking on ScaleBar Tool */ public void onMouseDown(int button, int shift, int x, int y) { // Create a point in map coordinates try { point = hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().toMapPoint(x, y); } catch (Exception e) { e.printStackTrace(); } inUse = true; } /* * On MouseMove after clicking on ScaleBar Tool */ public void onMouseMove(int button, int shift, int x, int y) { if (inUse == false) return; try { // Start an envelope feedback if (envFeedback == null) { envFeedback = new NewEnvelopeFeedback(); envFeedback.setDisplayByRef(hookHelper.getActiveView().getScreenDisplay()); envFeedback.start(point); } // Move the envelope feedback envFeedback.moveTo(hookHelper.getActiveView().getScreenDisplay().getDisplayTransformation().toMapPoint(x, y)); } catch (Exception e) { e.printStackTrace(); } } /* * On MouseUp after clicking on ScaleBar Tool */ public void onMouseUp(int button, int shift, int x, int y) { if (inUse == false) return; // If an envelope has not been tracked or its height/width is 0 if (envFeedback == null) { inUse = false; return; } try { envelope = (Envelope) envFeedback.stop(); envFeedback.refresh(hookHelper.getActiveView().getScreenDisplay().getHWnd()); if ((envelope.isEmpty()) || (envelope.getWidth() == 0) || (envelope.getHeight() == 0)) { envFeedback = null; inUse = false; return; } SwingUtilities.invokeLater(new Runnable() { public void run(){ try{ // Create the form with the SymbologyControl symbolForm = new SymbologyFrame(hookHelper.getActiveView(), envelope,"scalebar"); symbolForm.setVisible(true); symbolForm.setSize(487, 299); } catch(Exception e){ e.printStackTrace(); } } }); envFeedback = null; inUse = false; } catch (Exception e) { e.printStackTrace(); } } }