magiclamp

----博学、审问、慎思、明辨、笃行
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一天一例之一:Add FeatureLayer

Posted on 2008-10-07 20:14  magiclamp  阅读(1397)  评论(2)    收藏  举报

目前设置一天一例,督促自己学习,其中代码为esri里arcengine samples中例子,本文并未对其修改。文中中文是本人画蛇添足上去的,本例子比较简单,只是简单贴了下来,感觉bean挺方便了

 

/*
 * ArcGIS Engine Developer Sample
 * Application Name: AddFeatureLayer.java
 */

package com.esri.arcgis.samples.beans.mapcontrol;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
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.JDialog;
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.beans.TOC.TOCBean;
import com.esri.arcgis.beans.map.MapBean;
import com.esri.arcgis.beans.toolbar.ToolbarBean;
import com.esri.arcgis.carto.ILayer;
import com.esri.arcgis.carto.esriViewDrawPhase;
import com.esri.arcgis.controls.ControlsMapFullExtentCommand;
import com.esri.arcgis.controls.ControlsMapPanTool;
import com.esri.arcgis.controls.ControlsMapZoomInTool;
import com.esri.arcgis.controls.ControlsMapZoomOutTool;
import com.esri.arcgis.controls.ControlsOpenDocCommand;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.systemUI.esriCommandStyles;


/**
 * This sample shows how to programmatically add and delete layers
 * from a MapBean
 **/



public class AddFeatureLayer extends JFrame implements ActionListener {
JPanel mainPanel = null;
JPanel rightPanel = null;
JButton addLayerButton = null;
JButton removeLayerButton = null;
MapBean mapBean;
TOCBean tocBean;
ToolbarBean toolbarBean;
public AddFeatureLayer() {
super("Add FeatureLayer");
buildFrame();
setSize(650, 500);
setVisible(true);
initControl();
}
/**This method builds 'this' frame as per the following diagram:
*
*   /------------------------------------------------------------------------------|
*   |            BorderLayout.NORTH                                                |
*   |            ToolBarControl                                                    |
*   |------------------------------------------------------------------------------|
*   |                                                |   BorderLayout.East         |
*   |                                                |                             |
*   |                                                |  |--------JPanel:BoxLayout--|
*   |         BorderLayout.CENTER                    |  |                          |
*   |         MapBean                             |  |    JCheckBox             |
*   |                                                |  |                          |
*   |                                                |  |    JLabel                |
*   |                                                |  |                          |
*   |                                                |  |                          |
*   |                                                |  |                          |
*   |                                                |  |                          |
*   |                                                |  |                          |
*   |                                                |  |                          |
*   "------------------------------------------------|-----------------------------|
*/
public void buildFrame() {
rightPanel = new JPanel();
mainPanel = new JPanel();
rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
addLayerButton = new JButton("Add Layer");
addLayerButton.addActionListener(this);
removeLayerButton = new JButton("Remove Layer");
removeLayerButton.addActionListener(this);
rightPanel.add(addLayerButton);
rightPanel.add(Box.createVerticalStrut(10));
rightPanel.add(removeLayerButton);
rightPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
mapBean = new MapBean();//AO中地图显示的bean,呵呵都写好了,省事
tocBean = new TOCBean();//AO中TOC控制的bean
toolbarBean = new ToolbarBean();
tocBean.setSize(new Dimension(200, 100));
toolbarBean.setSize(490, 20);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(tocBean, BorderLayout.WEST);
mainPanel.add(rightPanel, BorderLayout.EAST);
mainPanel.add(toolbarBean, BorderLayout.NORTH);
mainPanel.add(mapBean, BorderLayout.CENTER);
mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
getContentPane().add(mainPanel, BorderLayout.CENTER);
}
/**Initializes control
*
*/
public void initControl() {
try {
//Set the Buddy
toolbarBean.setBuddyControl(mapBean);
tocBean.setBuddyControl(mapBean);
//Add tool bar items..
toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); //open
toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomIn
toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //ZoomOut
toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconAndText); //Pan
toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, true, 20, esriCommandStyles.esriCommandStyleTextOnly); //FullExtent
}
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() == addLayerButton) {
try {
if (!loadFile()) return;
}
catch (IOException ex) {
System.out.println(
"Exception in addLayerButton#actionPerformed" + ex);
ex.printStackTrace();
}
}
if(event.getSource() == removeLayerButton) {
try {
//Check if control has any layer
if (mapBean.getLayerCount() > 0) {
RemoveLayerDialog dialog = new RemoveLayerDialog();
dialog.show();
}
}
catch (IOException ex1) {
System.out.println(
"Exception in removeLayerButton#actionPerformed" + ex1);
ex1.printStackTrace();
}
}
}
/**
* Method loadFile loads the specified mxd file
*
*/
public boolean loadFile() throws IOException {
//Open a JFileChooser for selecting PMF documents
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return (f.isDirectory() ||
f.getAbsolutePath().toUpperCase().endsWith(".LYR"));
}
public String getDescription() {
return "Layer Documents(*.lyr)";
}
});
boolean loaded = false;
int returnVal = 0;
returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String fileChosen =
chooser.getCurrentDirectory()
+ File.separator
+ chooser.getSelectedFile().getName();
System.out.println("File picked: [" + fileChosen + "]");
//check if the selected layer document can be loaded into MapBean
try {
mapBean.addLayerFromFile(fileChosen, 0);
} catch(Exception ex) {
}
loaded = true;
System.out.println("Layer Added");
}
return loaded;
}
/**
* This class is used to create a dialog to remove a layer
* It displays a drop down with list of all the layers that are currently being added to mapcontrol
* and a removeLayer button
*/
class RemoveLayerDialog extends JDialog implements ActionListener {
JComboBox layerCombo = new JComboBox();
JButton removeButton = new JButton("RemoveLayer");
JButton cancel = new JButton("Cancel");
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel(new FlowLayout());
public RemoveLayerDialog() {
super(AddFeatureLayer.this, "Remove Layer");
setSize(300, 150);
removeButton.addActionListener(this);
updateLayerDropDown(layerCombo);
buttonPanel.add(removeButton);
buttonPanel.add(cancel);
mainPanel.add(layerCombo, BorderLayout.NORTH);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
getContentPane().add(mainPanel, BorderLayout.CENTER);
}
public void updateLayerDropDown(JComboBox layerCombo) {
//Get layer count
int layerCount = 0;
try {
layerCount = mapBean.getLayerCount();
}
catch (IOException ex1) {
}
// Add the map's layer names to a list
for (int i = 0; i < layerCount; i++) {
try {
ILayer layer = mapBean.getLayer(i);
String name = layer.getName();
layerCombo.addItem(name);
}
catch (IOException ex) {
}
}
}
/**@see java.awt.event.ActionListener#actionPerformed(ActionEvent event)
* @param event
*/
public void actionPerformed(ActionEvent event) {
if (event.getSource() == removeButton) {
try {
int layerIndex = layerCombo.getSelectedIndex();
mapBean.deleteLayer(layerIndex);
mapBean.refresh(esriViewDrawPhase.esriViewBackground, null, null);
//dispose the dialog
dispose();
}
catch (IOException ex) {
System.out.println(" Exception in PasswordDialog#okButton#actionPerformed " + ex);
ex.printStackTrace();
}
}
if (event.getSource() == cancel) {
//Dispose the password dialog
dispose();
}
}
}
/**
* 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());
AoInitialize aoInit = new AoInitialize();
aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
AddFeatureLayer addFeatureLayer = new AddFeatureLayer();
addFeatureLayer.setDefaultCloseOperation(AddFeatureLayer.EXIT_ON_CLOSE);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}