⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addfeaturelayer.java

📁 ArcObjects 添加要素图层到地图对象
💻 JAVA
字号:
/*
 * 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.controlcommands.ControlsMapFullExtentCommand;
import com.esri.arcgis.controlcommands.ControlsMapPanTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomInTool;
import com.esri.arcgis.controlcommands.ControlsMapZoomOutTool;
import com.esri.arcgis.controlcommands.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();
      tocBean = new TOCBean();
      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 {
        //Set the system look and feel
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        EngineInitializer.initializeVisualBeans();

        AoInitialize aoInit = new AoInitialize();
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);

        AddFeatureLayer addFeatureLayer = new AddFeatureLayer();
        addFeatureLayer.setDefaultCloseOperation(AddFeatureLayer.EXIT_ON_CLOSE);
     }
     catch (Exception ex) {
        ex.printStackTrace();
     }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -