arcgissamples\scenario\AddDateCommand.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Build an ArcGIS controls map viewer application
arcgissamples\scenario\AddDateCommand.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.scenario;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.TextElement;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.HookHelper;
import com.esri.arcgis.display.TextSymbol;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.support.ms.stdole.StdFont;
import com.esri.arcgis.systemUI.ICommand;
import com.esri.arcgis.systemUI.ITool;
import com.esri.arcgis.interop.AutomationException;

public class AddDateCommand implements ICommand, ITool {
  private static final long serialVersionUID = 1L;
  
  HookHelper hookHelper;

  /*
   * see com.esri.arcgis.systemUI.ICommand#isEnabled()
   */
  public boolean isEnabled() throws IOException, AutomationException {
    return true;
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#isChecked()
   */
  public boolean isChecked() throws IOException, AutomationException {
    return false;
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getName()
   */
  public String getName() throws IOException, AutomationException {
    return "CustomCommands_Add Date";
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getCaption()
   */
  public String getCaption() throws IOException, AutomationException {
    return "Add date";
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getTooltip()
   */
  public String getTooltip() throws IOException, AutomationException {
    return "Add date";
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getMessage()
   */
  public String getMessage() throws IOException, AutomationException {
    return "Adds a date element to the page layout";
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getHelpFile()
   */
  public String getHelpFile() throws IOException, AutomationException {
    return null;
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getHelpContextID()
   */
  public int getHelpContextID() throws IOException, AutomationException {
    return 0;
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getBitmap()
   */
  public int getBitmap() throws IOException, AutomationException {
    return 0; // We rely on this being displayed as text
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#getCategory()
   */
  public String getCategory() throws IOException, AutomationException {
    return "CustomCommands";
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#onCreate(java.lang.Object)
   */
  public void onCreate(Object obj) throws IOException, AutomationException {
    hookHelper = new HookHelper();
    hookHelper.setHookByRef( obj );
  }

  /*
   * see com.esri.arcgis.systemUI.ICommand#onClick()
   */
  public void onClick() throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#getCursor()
   */
  public int getCursor() throws IOException, AutomationException {
    return 0;
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onMouseDown(int, int, int, int)
   */
  public void onMouseDown(int button, int shift, int x, int y) throws IOException, AutomationException {

    Date today = new Date();

    // Format the date in the form "Wed 25 Aug, 2004".
    SimpleDateFormat formatter;
    formatter = new SimpleDateFormat("EEE d MMM, yyyy");
    String dateString = formatter.format(today);

    // Create a font.
    StdFont font = new StdFont();
    font.setName("Arial");
    font.setBold(true);

    // Create a text symbol.
    TextSymbol textSymbol = new TextSymbol();
    textSymbol.setFont(font);
    textSymbol.setSize(15);

    // Create a text element to be added to the graphics container.
    TextElement textElement = new TextElement();
    textElement.setSymbol(textSymbol);
    textElement.setScaleText(false);
    textElement.setText(dateString);

    // Add the text element to the graphics container.
    IActiveView activeView = hookHelper.getActiveView();
    IPoint pt = activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(x,y);
    textElement.setGeometry(pt);
    activeView.getGraphicsContainer().addElement(textElement, 0);

    // Refresh the view.
    activeView.partialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onMouseMove(int, int, int, int)
   */
  public void onMouseMove(int arg0, int arg1, int arg2, int arg3) throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onMouseUp(int, int, int, int)
   */
  public void onMouseUp(int arg0, int arg1, int arg2, int arg3) throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onDblClick()
   */
  public void onDblClick() throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onKeyDown(int, int)
   */
  public void onKeyDown(int arg0, int arg1) throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onKeyUp(int, int)
   */
  public void onKeyUp(int arg0, int arg1) throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#onContextMenu(int, int)
   */
  public boolean onContextMenu(int arg0, int arg1) throws IOException, AutomationException {
    return false;
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#refresh(int)
   */
  public void refresh(int arg0) throws IOException, AutomationException {
    // Ignore
  }

  /*
   * see com.esri.arcgis.systemUI.ITool#deactivate()
   */
  public boolean deactivate() throws IOException, AutomationException {
    return true;
  }

} // end AddDateCommand class