arcgissamples\pagelayoutbean\SaveMapDocument.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.pagelayoutbean; import java.awt.BorderLayout; import java.awt.Button; import java.awt.FileDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.BorderFactory; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.filechooser.FileFilter; import com.esri.arcgis.carto.MapDocument; import com.esri.arcgis.controls.PageLayoutControl; import com.esri.arcgis.controls.TOCControl; import com.esri.arcgis.controls.ToolbarControl; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.systemUI.esriCommandStyles; /** * The samples allows users load and save map documents. */ public class SaveMapDocument extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; JPanel mainPanel = null; JPanel rightPanel = null; PageLayoutControl pageLayoutControl; ToolbarControl toolbarControl; TOCControl tocControl; Button buttonLoad = null; Button buttonSaveAs = null; Button buttonSave = null; MapDocument mapDocument = null; /** * Constructor */ public SaveMapDocument() { buildFrame(); setSize(550, 400); setVisible(true); initControl(); } /** * This method builds 'this' frame as per the following diagram: * /----------------------------------------------------------\ | BorderLayout.NORTH | | Toolbar Control | * |--------------|----------------------------|--------------| | | | | | | | | | TocControl | PageLayout Control | * RightPanel | | BorderLayout| BorderLayout.CENTER | BorderLayout | | WEST | | .EAST | | | | | | | | | | | | | * |--------------|----------------------------|--------------| */ public void buildFrame() { this.setTitle("SaveMapDocument Java Sample Application"); try { mapDocument = new MapDocument(); } catch (IOException e) { e.printStackTrace(); } mainPanel = new JPanel(); pageLayoutControl = new PageLayoutControl(); toolbarControl = new ToolbarControl(); tocControl = new TOCControl(); tocControl.setSize(200, 490); toolbarControl.setSize(490, 20); rightPanel = new JPanel(); rightPanel.setLayout(new java.awt.GridLayout(20, 1)); buttonLoad = new Button(); buttonSaveAs = new Button(); buttonSave = new Button(); buttonLoad.setLabel("Open Document ..."); buttonSaveAs.setLabel("Save Document As..."); buttonSave.setLabel("Save Document"); buttonLoad.addActionListener(this); buttonSaveAs.addActionListener(this); buttonSave.addActionListener(this); rightPanel.add(buttonLoad, null); rightPanel.add(buttonSaveAs, null); rightPanel.add(buttonSave, null); mainPanel.setLayout(new BorderLayout()); mainPanel.add(toolbarControl, BorderLayout.NORTH); mainPanel.add(tocControl, BorderLayout.WEST); mainPanel.add(pageLayoutControl, BorderLayout.CENTER); mainPanel.add(rightPanel, BorderLayout.EAST); mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); getContentPane().add(mainPanel, BorderLayout.CENTER); } /** * Intializes control */ public void initControl() { try { // Set the Buddy toolbarControl.setBuddyControl(pageLayoutControl); tocControl.setBuddyControl(pageLayoutControl); // Add custom tool toolbarControl.addToolbarDef("esriControlCommands.ControlsPageLayoutToolbar", -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); toolbarControl.addToolbarDef("esriControlCommands.ControlsGraphicElementToolbar", -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); } catch (Exception ex) { System.out.println("Exception in initControl : " + ex); ex.printStackTrace(); } } /** * Main program to start the program execution. * * @param s */ public static void main(String s[]) { try { EngineInitializer.initializeVisualBeans(); // Set the system look and feel UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); SaveMapDocument document = new SaveMapDocument(); document.setDefaultCloseOperation(SaveMapDocument.EXIT_ON_CLOSE); } catch (Exception ex) { ex.printStackTrace(); } } /** * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent event) * @param event */ public void actionPerformed(ActionEvent event) { try { // Load if (event.getSource() == buttonLoad) { FileDialog fileDialog = new FileDialog(new JFrame(), "Open MXD|MXT|PMF file", FileDialog.LOAD); fileDialog.setVisible(true); String path = fileDialog.getDirectory(); String name = fileDialog.getFile(); if (!(path != null && name != null && path.length() > 0 && name.length() > 0)) { // Nothing chosen return; } String fileChosen = path + File.separator + name; mapDocument.open(fileChosen, ""); pageLayoutControl.setPageLayoutByRef(mapDocument.getPageLayout()); } // SaveAs if (event.getSource() == buttonSaveAs) { JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(new FileFilter() { public boolean accept(File f) { return (f.isDirectory() || f.getAbsolutePath().toLowerCase().endsWith(".mxd") || f.getAbsolutePath().toLowerCase().endsWith(".mxt") || f.getAbsolutePath() .toLowerCase().endsWith(".pmf")); } public String getDescription() { return "Map Documents(*.mxd, *.mxt, *.pmf)"; } }); if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) { String fileChosen = chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName(); if (!fileChosen.toLowerCase().endsWith(".mxd")) fileChosen += ".mxd"; System.out.println("Save As: [" + fileChosen + "]"); mapDocument.saveAs(fileChosen, mapDocument.isUsesRelativePaths(), false); } } // Save if (event.getSource() == buttonSave) { mapDocument.save(mapDocument.isUsesRelativePaths(), false); } } catch (IOException e) { e.printStackTrace(); } } static void initializeArcGISLicenses() { try { AoInitialize ao = new AoInitialize(); if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(com.esri.arcgis.system.esriLicenseProductCode.esriLicenseProductCodeArcInfo); } catch (Exception e) {e.printStackTrace();} } }