📄 savedocument.java
字号:
/*
Copyright 1995-2004 ESRI
All rights reserved under the copyright laws of the United States.
You may freely redistribute and use this sample code, with or without modification.
Disclaimer: THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ESRI OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ARISING IN ANY
WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
For additional information contact: Environmental Systems Research Institute, Inc.
Attn: Contracts Dept.
380 New York Street
Redlands, California, U.S.A. 92373
Email: contracts@esri.com
*/
/**
* ArcGIS Engine Developer Sample
* Application Name: SaveDocument.java
*/
package com.esri.arcgis.samples.beans.pagelayoutcontrol;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.File;
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 javax.swing.filechooser.FileFilter;
import com.esri.arcgis.beans.TOC.TOCControl;
import com.esri.arcgis.beans.pagelayout.PageLayoutControl;
import com.esri.arcgis.beans.toolbar.ToolbarControl;
import com.esri.arcgis.carto.MapDocument;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.systemUI.esriCommandStyles;
/**
* The samples allows users load and save map documents.
*/
public class SaveDocument extends JFrame implements ActionListener {
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 SaveDocument() {
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("SaveDocument 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 {
//Set the system look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
EngineInitializer.initializeVisualBeans();
AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
SaveDocument document = new SaveDocument();
document.setDefaultCloseOperation(SaveDocument.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) {
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.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
String fileChosen = chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName();
System.out.println("File picked: [" + fileChosen + "]");
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -