arcgissamples\toolbarbean\commands\OpenCommand.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.toolbarbean.commands; import java.awt.FileDialog; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JOptionPane; import arcgissamples.toolbarbean.MapAsToolbarBuddy; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.controls.ToolbarControl; import com.esri.arcgis.systemUI.ICommand; import com.esri.arcgis.systemUI.ITool; /** * Description: This class is a custom implementaion of tool button. Instance of this class can be added to toolbar control * using toolbarBean#addItem. * @see com.esri.arcgis.systemUI.ICommand * @see com.esri.arcgis.systemUI.ITool */ public class OpenCommand implements ICommand, ITool { MapAsToolbarBuddy application; boolean fileDialogShown = false; public int getBitmap() { return 0; } public String getCaption() { return "OpenMxFile"; } public String getCategory() { return "CustomCommands"; } public int getHelpContextID() { return -1; } public String getHelpFile() { return null; } public String getMessage() { return "OpenMxFile"; } public String getName() { return "OpenMxFile"; } public String getTooltip() { return "OpenMxFile"; } public boolean isChecked() { return false; } public boolean isEnabled() { return true; } public void onClick() { //If dialog is already displayed, if(fileDialogShown) return; fileDialogShown = true; Thread t = new Thread() { public void run() { try { loadFile(); } catch (IOException ex) { } } }; t.start(); } /** * Method loadFile loads the specified mxd file. * */ public boolean loadFile() throws IOException { boolean loaded = false; while (!loaded) { FileDialog fileDialog = new FileDialog(new JFrame(), "Open MXD file", FileDialog.LOAD); fileDialog.setVisible(true); String path = fileDialog.getDirectory(); String name = fileDialog.getFile(); String fileChosen = path + File.separator + name; MapBean mapBean = application.getMapBean(); PageLayoutBean pageLayoutBean = application.getPageLayoutBean(); if (mapBean.checkMxFile(fileChosen)) { //load the document into both the mapcontrol and pagelayout beans. mapBean.loadMxFile(fileChosen, null, null); pageLayoutBean.loadMxFile(fileChosen, null); loaded = true; } else { JOptionPane.showMessageDialog(null, "The current document cannot be loaded."); loaded = false; } } return loaded; } public void onCreate(Object hook) { try { ToolbarControl toolbarControl = new ToolbarControl(hook); application = (MapAsToolbarBuddy)toolbarControl.getBuddy(); } catch (IOException ex) { } } public boolean deactivate() { return true; } public int getCursor() { return 0; } public boolean onContextMenu(int x, int y) { return false; } public void onDblClick() { } public void onKeyDown(int keyCode, int shift) { } public void onKeyUp(int keyCode, int shift) { } public void onMouseDown(int button, int shift, int x, int y) { } public void onMouseMove(int button, int shift, int x, int y) { } public void onMouseUp(int button, int shift, int x, int y) { } public void refresh(int hdc) { } }