?? copyfocusmap.java
字號:
System.out.println("File picked: [" + fileChosen + "]");
//Check if the selected document can be loaded into control
//Update the textfiled with the file name.
pathField.setText(fileChosen);
//Validate and load map document
if(pageLayoutBean.checkMxFile(fileChosen)) {
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass);
mapBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass);
//Reset controls
mapBean.getActiveView().clear();
mapBean.getActiveView().getGraphicsContainer().deleteAllElements();
mapBean.refresh(esriViewDrawPhase.esriViewForeground, null, null);
focusMapCombo.removeAllItems();
//Load map document
pageLayoutBean.loadMxFile(fileChosen, null);
//Set mouse pointers
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
mapBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
}else
JOptionPane.showMessageDialog(this, fileChosen + " is not a valid ArcMap Document");
loaded = true;
}else {
loaded = false;
}
return loaded;
}
/**
* Method which overwrites map controls map object with the pagelayout's focus map
*/
public void copyAndOverwriteMap() {
try {
//Get IObjectCopy interface
ObjectCopy objectCopy = new ObjectCopy();
//Get the map to copy
Object toCopyMap = pageLayoutBean.getActiveView().getFocusMap();
//Get the map to be overwritten
Object toOverwriteMap [] = { mapBean.getMap() };
//Overwrite the MapBean's map
objectCopy.overwrite(objectCopy.copy(toCopyMap), toOverwriteMap);
//Update map control's extent
setMapExtent();
}catch (IOException ex) {
System.out.println("Exception in copyAndOverwriteMap : " + ex);
ex.printStackTrace();
}
}
/**
* Method which sets the Extent of the pageLayoutBean's FocusMap equal to that of the MapBean using the
* IDisplayTransformation interface
*/
public void setMapExtent() {
try {
//Get active map
Map map = new Map(pageLayoutBean.getActiveView().getFocusMap());
//Set the control's extent
mapBean.setExtent(map.getExtent());
//Refresh the display
mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
}catch (IOException ex) {
System.out.println("Exception in setMapExtent : " + ex);
ex.printStackTrace();
}
}
/**
* Method which populates the combobox with the list of maps in the pagelayout control.
* it also sets the selected item in the combo box to that of the pagelayouts focus map.
*/
public void listMaps() {
try {
//Get IGraphicsContainer interface
IGraphicsContainer graphicsContainer;
graphicsContainer = pageLayoutBean.getGraphicsContainer();
graphicsContainer.reset();
//Query Interface for IElement interface
IElement element = graphicsContainer.next();
//Loop through the elements
while (element != null) {
//Create a MapFrame class using element.
MapFrame mapFrame;
String mapName;
try {
mapFrame = new MapFrame(element);
//Get the name of the Map in the MapFrame
mapName = mapFrame.getMap().getName();
}catch(Exception NotAnInstanceOfMapFrame) {
element = graphicsContainer.next();
continue;
}
//Get IElementProperties interface . Set the name of the MapFrame to the Map's name
IElementProperties elementProperties = new IElementPropertiesProxy(element);
elementProperties.setName(mapName);
//Add the map name to the ComboBox
focusMapCombo.addItem(mapName);
//If the Map is the FocusMap select the MapName in the ComboBox
if(mapName.equals(pageLayoutBean.getActiveView().getFocusMap().getName()))
focusMapCombo.setSelectedItem(mapName);
element = graphicsContainer.next();
}
}catch (IOException ex) {
System.out.println("Exception in listMaps : " + ex);
ex.printStackTrace();
}
}
/**
* Description: Class which extends pagelayout control event class IPageLayoutControlEvents2Adapter
* @see com.esri.arcgis.beans.pagelayout.IPageLayoutControlEventsAdapter
* */
class PageLayoutControlListener extends IPageLayoutControlEventsAdapter {
/**@see com.esri.arcgis.beans.pagelayout.onAfterScreenDraw(IPageLayoutControlEventsOnAfterScreenDrawEvent theEvent)
* @param theEvent
*/
public void onAfterScreenDraw(IPageLayoutControlEventsOnAfterScreenDrawEvent theEvent) {
try {
//Set mouse pointer
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
mapBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
if(!replacedPageLayout) return;
copyAndOverwriteMap();
replacedPageLayout = false;
}
catch (Exception ex) {
System.out.println("Exception in PageLayoutControlListener#onAfterScreenDraw : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.onBeforeScreenDraw(IPageLayoutControlEventsOnBeforeScreenDrawEvent theEvent) {
* @param theEvent
*/
public void onBeforeScreenDraw(IPageLayoutControlEventsOnBeforeScreenDrawEvent theEvent) {
try {
//Set mouse pointer
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass);
}catch (Exception ex) {
System.out.println("Exception in PageLayoutControlListener#onBeforeScreenDraw : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.pagelayout.onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent theEvent) {
* @param theEvent
*/
public void onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent theEvent) {
try {
replacedPageLayout = true;
listMaps();
}catch (Exception ex) {
System.out.println("Exception in PageLayoutControlListener#onPageLayoutReplaced : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.pagelayout.onMouseDown(IPageLayoutControlEventsOnMouseDownEvent theEvent)
* @param theEvent
*/
public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent theEvent) {
try {
//Zoom in
if(theEvent.getButton() == 1) {
pageLayoutBean.setExtent(pageLayoutBean.trackRectangle());
}else if( theEvent.getButton() == 2)
pageLayoutBean.pan();
} catch(Exception ex) {
System.out.println("Exception in PageLayoutControlListener#onMouseDown : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.pagelayout.onFocusMapChanged(IPageLayoutControlEventsOnFocusMapChangedEvent theEvent) {
* @param theEvent
*/
public void onFocusMapChanged(IPageLayoutControlEventsOnFocusMapChangedEvent theEvent) {
try {
copyAndOverwriteMap();
}catch (Exception ex) {
System.out.println(
"Exception in PageLayoutControlListener#onFocusMapChanged : " +
ex);
ex.printStackTrace();
}
}
} //End of PageLayoutListener class
/**
* Description: Class which extends map control event class IMapControlEvents2Adapter
* @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter
* */
class MapControlListener extends IMapControlEvents2Adapter {
/**
* @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent)
* @param theEvent
*/
public void onMouseDown(IMapControlEvents2OnMouseDownEvent theEvent) {
try {
updateFocusMap = true;
//If left mouse button
if(theEvent.getButton() == 1) {
//If user dragged a rectangle
mapBean.setExtent(mapBean.trackRectangle());
}else if(theEvent.getButton() == 2)
mapBean.pan();
}catch(Exception ex) {
System.out.println("Exception in MapControlListener#onMouseDown : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onAfterScreenDraw(IMapControlEvents2OnAfterScreenDrawEvent theEvent)
* @param theEvent
*/
public void onAfterScreenDraw(IMapControlEvents2OnAfterScreenDrawEvent theEvent) {
try {
//Set mouse pointers
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
mapBean.setMousePointer(esriControlsMousePointer.esriPointerDefault);
if(!updateFocusMap) return;
//Get IActiveView interface
IActiveView activeView = new IActiveViewProxy(pageLayoutBean.getActiveView().getFocusMap());
//Get IDisplayTransformation interface
IDisplayTransformation displayTransformation = activeView.getScreenDisplay().getDisplayTransformation();
//Set the visible extent of the focus map
displayTransformation.setVisibleBounds(mapBean.getExtent());
//Refresh the focus map
activeView.refresh();
updateFocusMap = false;
}catch(Exception ex) {
System.out.println("Exception in MapControlListener#onAfterScreenDraw : " + ex);
ex.printStackTrace();
}
}
/**
* @see com.esri.arcgis.beans.map.IMapControlEvents2Adapter#onBeforeScreenDraw(IMapControlEvents2OnBeforeScreenDrawEvent theEvent)
* @param theEvent
*/
public void onBeforeScreenDraw(IMapControlEvents2OnBeforeScreenDrawEvent theEvent){
try {
//Set mouse pointer
pageLayoutBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass);
mapBean.setMousePointer(esriControlsMousePointer.esriPointerHourglass);
}
catch(Exception ex) {
System.out.println("Exception in MapControlListener#onBeforeScreenDraw : " + ex);
ex.printStackTrace();
}
}
}//End of MapControlListener class
/**
* 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);
System.out.println("License is verified");
CopyFocusMap copyFocusMap = new CopyFocusMap();
copyFocusMap.setDefaultCloseOperation(CopyFocusMap.EXIT_ON_CLOSE);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -