?? copyfocusmap.java
字號:
/*
* ArcGIS Engine Developer Sample
* Application Name: CopyFocusMap.java
*/
package com.esri.arcgis.samples.beans.pagelayoutcontrol;
import java.awt.BorderLayout;
import java.awt.GridLayout;
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.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.filechooser.FileFilter;
import com.esri.arcgis.beans.map.IMapControlEvents2Adapter;
import com.esri.arcgis.beans.map.IMapControlEvents2OnAfterScreenDrawEvent;
import com.esri.arcgis.beans.map.IMapControlEvents2OnBeforeScreenDrawEvent;
import com.esri.arcgis.beans.map.IMapControlEvents2OnMouseDownEvent;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsAdapter;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsOnAfterScreenDrawEvent;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsOnBeforeScreenDrawEvent;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsOnFocusMapChangedEvent;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsOnMouseDownEvent;
import com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsOnPageLayoutReplacedEvent;
import com.esri.arcgis.beans.pagelayout.PageLayoutBean;
import com.esri.arcgis.carto.IActiveView;
import com.esri.arcgis.carto.IActiveViewProxy;
import com.esri.arcgis.carto.IElement;
import com.esri.arcgis.carto.IElementProperties;
import com.esri.arcgis.carto.IElementPropertiesProxy;
import com.esri.arcgis.carto.IGraphicsContainer;
import com.esri.arcgis.carto.Map;
import com.esri.arcgis.carto.MapFrame;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.display.IDisplayTransformation;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.ObjectCopy;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.systemUI.esriControlsMousePointer;
/**
* Description:This sample demonstrates setting the PageLayoutBean's FocusMap and using
* the IObjecyCopy object to overwrite the MapBean's Map object with a copy
* of the PageLayoutBean's FocusMap. The Map object has to copied because the
* MapBean and PageLayoutBean are unable to share the same ActiveView at
* the same time.
* The file chooser dialog allows users to search and
* select map documents, which are validated and loaded into the PageLayoutBean
* using the checkMxFile and loadMxFile methods. This triggers the OnPageLayoutReplaced
* event which loops through the Elements in the PageLayoutBean's GraphicsContainer
* finding MapFrame elements. The name of each MapFrame is set to the name of the Map
* in the MapFrame using the IElementProperties interface. A ComboBox is populated with
* all Map name's to allow users to change the FocusMap. The FindElementByName method is
* used to find the MapFrame containing the Map that is to become the FocusMap.<p/>
*
* The PageLayoutBean's OnFocusMapChanged and OnAfterScreenDraw (when a new Map
* Document has been loaded into the PageLayoutBean) events use the IObjectCopy
* Copy and Overwrite methods to overwrite the MapBean's Map with a copy of the
* PageLayoutBean's FocusMap.
*
* The PageLayoutBean and MapBean OnMouseDown event's use the TrackRectangle
* and Pan methods to zoomin and pan around either the PageLayoutBean's Page or
* the MapBean's Map. If the MapBean's display is redrawn due to a change in
* VisibleExtent, the OnAfterScreenDraw event is used set the Extent of the
* PageLayoutBean's FocusMap equal to that of the MapBean using the
* IDisplayTransformation interface.
*/
public class CopyFocusMap extends JFrame implements ActionListener {
JPanel topPanel = null;
JPanel mainPanel = null;
JPanel centerPanel = null;
JPanel pageLayoutPanel = null;
JPanel bottomPanel = null;
JButton loadDocumentButton = null;
JTextField pathField = null;
String helpString = "Right mouse button to pan page or data and Left mouse button to zoom in";
JLabel helpLabel = null;
JButton zoomToPageButton = null;
JButton zoomToFullDataButton = null;
JComboBox focusMapCombo = null;
PageLayoutBean pageLayoutBean;
MapBean mapBean;
boolean updateFocusMap;
boolean replacedPageLayout;
public CopyFocusMap() {
super("Copy Focus Map Example");
buildFrame();
setSize(700,500);
setVisible(true);
initControl();
}
/**This method builds 'this' frame as per the following diagram:
*
* /----------------------------------------------------------------\
* | JButton BorderLayout.NORTH |
* | JTextField |
* |----------------------------------------------------------------|
* | JPanel: Borderlayout.CENTER |
* | |
* | |
* |-------------------JPanel : GridLayout--------------------------|
* | | |
* | JPanel : BorderLayout | |
* | | |
* | JComboBox:BorderLayout.NORTH | |
* | | Map Control |
* | | |
* | PageLayout Control : | |
* BorderLayout.CENTER | |
* | | |
* | | |
* | | |
* | | |
* | | |
* |----------------------------------------------------------------|
* | BorderLayout.SOUTH |
* | JButton JLabel JButton |
* |----------------------------------------------------------------|
*/
public void buildFrame() {
topPanel = new JPanel();
mainPanel = new JPanel();
centerPanel = new JPanel();
pageLayoutPanel = new JPanel();
bottomPanel = new JPanel();
topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.X_AXIS));
mainPanel.setLayout(new BorderLayout());
centerPanel.setLayout(new GridLayout(1,2, 10, 0));
pageLayoutPanel.setLayout(new BorderLayout());
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));
loadDocumentButton = new JButton("Load Document");
loadDocumentButton.addActionListener(this);
pathField = new JTextField();
pathField.setEditable(false);
topPanel.add(loadDocumentButton);
topPanel.add(Box.createHorizontalStrut(5));
topPanel.add(pathField);
topPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0));
pageLayoutBean = new PageLayoutBean();
focusMapCombo = new JComboBox();
focusMapCombo.addActionListener(this);
focusMapCombo.setLightWeightPopupEnabled(false);
pageLayoutPanel.add(focusMapCombo, BorderLayout.NORTH);
pageLayoutPanel.add(pageLayoutBean, BorderLayout.CENTER);
mapBean = new MapBean();
centerPanel.add(pageLayoutPanel);
centerPanel.add(mapBean);
zoomToPageButton = new JButton("Zoom To Page");
zoomToPageButton.addActionListener(this);
helpLabel = new JLabel(helpString);
helpLabel.setHorizontalTextPosition(JLabel.CENTER);
zoomToFullDataButton = new JButton("Zoom to Full Data Extent");
zoomToFullDataButton.addActionListener(this);
bottomPanel.add(zoomToPageButton);
bottomPanel.add(Box.createHorizontalStrut(30));
bottomPanel.add(helpLabel);
bottomPanel.add(Box.createHorizontalStrut(10));
bottomPanel.add(zoomToFullDataButton);
//Create page layout control add it to the center of the main panel.
mainPanel.add(topPanel, BorderLayout.NORTH);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
getContentPane().add(mainPanel, BorderLayout.CENTER);
}
/* Intializes control
*/
public void initControl() {
try {
updateFocusMap = false;
replacedPageLayout = false;
mapBean.setShowScrollbars(false);
//Add control listener
mapBean.addIMapControlEvents2Listener(new MapControlListener());
pageLayoutBean.addIPageLayoutControlEventsListener(new PageLayoutControlListener());
}catch (IOException ex) {
System.out.println("Exception in initControl : " + ex);
ex.printStackTrace();
}
}
/**@see java.awt.event.ActionListener#actionPerformed(ActionEvent event)
* @param event
*/
public void actionPerformed(ActionEvent event) {
if(event.getSource() == focusMapCombo) {
try {
//Get IMapFrame interface
if (focusMapCombo.getSelectedItem() != null) {
IElement element = pageLayoutBean.findElementByName( (String)
focusMapCombo.getSelectedItem(), 1);
//Set the focusMap
MapFrame mapFrame = new MapFrame(element);
pageLayoutBean.getActiveView().setFocusMapByRef(mapFrame.getMap());
}
}catch (IOException ex) {
System.out.println("Exception in focusMapCombo#actionPerformed: " + ex);
ex.printStackTrace();
}
}
if(event.getSource() == zoomToPageButton) {
try {
//Zoom to the whole page
pageLayoutBean.zoomToWholePage();
}catch (IOException ex) {
System.out.println("Exception in zoomToPageButton#actionPerformed: " + ex);
ex.printStackTrace();
}
}
if(event.getSource() == zoomToFullDataButton) {
try {
//Assign map controls extent property to the full extent of all the layers
mapBean.setExtent(mapBean.getFullExtent());
}
catch (IOException ex) {
System.out.println(
"Exception in zoomToFullDataButton#actionPerformed : " + ex);
ex.printStackTrace();
}
}
if(event.getSource() == loadDocumentButton) {
try {
loadFile();
}catch (IOException ex) {
System.out.println("Exception in loadDocumentButton#actionPerformed: " + ex);
ex.printStackTrace();
}
}
}
/**
* Method loadFile loads the specified mxd file.
*/
public boolean loadFile() throws IOException {
//Open a JFileChooser for selecting map documents
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return (f.isDirectory() || f.getAbsolutePath().toUpperCase().endsWith(".MXD") || f.getAbsolutePath().toUpperCase().endsWith(".MXT") || f.getAbsolutePath().toUpperCase().endsWith(".PMF"));
}
public String getDescription() {
return "Map Documents(*.mxd, *.mxt or *.pmf)";
}
});
boolean loaded = false;
int returnVal = 0;
returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileChosen = chooser.getCurrentDirectory() + File.separator + chooser.getSelectedFile().getName();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -