arcgissamples\scenario\MapViewerFrame.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package arcgissamples.scenario; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import com.esri.arcgis.beans.TOC.TOCBean; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.Map; 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.controls.ControlsPagePanTool; import com.esri.arcgis.controls.ControlsPageZoomInFixedCommand; import com.esri.arcgis.controls.ControlsPageZoomInTool; import com.esri.arcgis.controls.ControlsPageZoomOutFixedCommand; import com.esri.arcgis.controls.ControlsPageZoomOutTool; import com.esri.arcgis.controls.ControlsPageZoomPageToLastExtentBackCommand; import com.esri.arcgis.controls.ControlsPageZoomPageToLastExtentForwardCommand; import com.esri.arcgis.controls.ControlsPageZoomWholePageCommand; import com.esri.arcgis.controls.CustomizeDialog; import com.esri.arcgis.controls.ICustomizeDialogEvents; import com.esri.arcgis.controls.ICustomizeDialogEventsOnCloseDialogEvent; import com.esri.arcgis.controls.ICustomizeDialogEventsOnStartDialogEvent; import com.esri.arcgis.controls.IMapControlEvents2Adapter; import com.esri.arcgis.controls.IMapControlEvents2OnAfterDrawEvent; import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter; import com.esri.arcgis.controls.IPageLayoutControlEventsOnMouseDownEvent; import com.esri.arcgis.controls.IPageLayoutControlEventsOnPageLayoutReplacedEvent; import com.esri.arcgis.controls.ITOCControlEventsAdapter; import com.esri.arcgis.controls.ITOCControlEventsOnEndLabelEditEvent; import com.esri.arcgis.controls.ToolbarMenu; import com.esri.arcgis.controls.esriTOCControlEdit; import com.esri.arcgis.display.DisplayTransformation; import com.esri.arcgis.display.ITransformEventsAdapter; import com.esri.arcgis.display.ITransformEventsVisibleBoundsUpdatedEvent; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.SimpleFillSymbol; import com.esri.arcgis.display.SimpleLineSymbol; import com.esri.arcgis.geometry.Envelope; import com.esri.arcgis.systemUI.esriCommandStyles; public class MapViewerFrame extends JFrame { private static final long serialVersionUID = 1L; JCheckBox customizeCB; // JCheckBox to control toolbar customization CustomizeDialog customizeDialog; // The Customize dialog box used by // the ToolbarBean constructor; SimpleFillSymbol fillSymbol; // The symbol used to draw the envelope Envelope currentExtent; // The envelope drawn on the MapBean Map focusMap; // The PageLayoutBean's focus map PageLayoutBean pageLayout; MapBean map; TOCBean toc; ToolbarBean toolbar; ToolbarMenu popupMenu; JLabel statusLabel; //Constructor public MapViewerFrame() { setTitle("MapViewer"); pageLayout = new PageLayoutBean(); map = new MapBean(); toc = new TOCBean(); toolbar = new ToolbarBean(); statusLabel = new JLabel(" "); try { popupMenu = new ToolbarMenu(); } catch (IOException e) { e.printStackTrace(); } } public void buildAndShow() throws IOException { JPanel mapTOCPanel = buildMapTOCPanel(); createCustomizeDialog(); customizeCB = new JCheckBox("Customize"); JPanel topPanel = new JPanel(); topPanel.setLayout(new BorderLayout()); topPanel.add(toolbar, BorderLayout.CENTER); topPanel.add(customizeCB, BorderLayout.EAST); this.getContentPane().add(topPanel, BorderLayout.NORTH); //this.getContentPane().add(toolbar, BorderLayout.NORTH); this.getContentPane().add(pageLayout, BorderLayout.CENTER); this.getContentPane().add(mapTOCPanel, BorderLayout.WEST); this.getContentPane().add(statusLabel, BorderLayout.SOUTH); // Set label editing to manual. toc.setLabelEdit(esriTOCControlEdit.esriTOCControlManual); // Create symbol used to draw overview on the MapBean. createOverviewSymbol(); // Add popup menu items popupMenu.addItem( new ControlsPageZoomInFixedCommand(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText ); popupMenu.addItem( new ControlsPageZoomOutFixedCommand(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText ); popupMenu.addItem( new ControlsPageZoomWholePageCommand(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText ); popupMenu.addItem( new ControlsPageZoomPageToLastExtentBackCommand(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText ); popupMenu.addItem( new ControlsPageZoomPageToLastExtentForwardCommand(), 0, -1, false, esriCommandStyles.esriCommandStyleIconAndText ); // Add generic commands to the toolbar toolbar.addItem( new ControlsOpenDocCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPageZoomInTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPageZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPagePanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPageZoomWholePageCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPageZoomPageToLastExtentBackCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsPageZoomPageToLastExtentForwardCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsMapZoomInTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem( new ControlsMapFullExtentCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly ); toolbar.addItem(new AddDateCommand(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleTextOnly); addEventListeners(); // Load a preauthored map document into the PageLayout control. //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); String documentPath = devKitHome + File.separator + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "mxds" + File.separator + "gulf of st. lawrence.mxd"; if (pageLayout.checkMxFile(documentPath)) pageLayout.loadMxFile(documentPath, null); // Set buddy controls to wire up the TOC and Toolbar bean // with the PageLayout bean. toc.setBuddyControl(pageLayout); toolbar.setBuddyControl(pageLayout); popupMenu.setHook(pageLayout); this.setVisible(true); } public void addEventListeners() throws IOException { pageLayout.addIPageLayoutControlEventsListener(new IPageLayoutControlEventsAdapter() { private static final long serialVersionUID = 1L; public void onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent evt) throws IOException { map.loadMxFile(pageLayout.getDocumentFilename(), null, null); map.setExtent(map.getFullExtent()); focusMap = (Map)pageLayout.getActiveView().getFocusMap(); currentExtent = (Envelope) focusMap.getExtent(); DisplayTransformation dt = (DisplayTransformation)focusMap.getScreenDisplay().getDisplayTransformation(); dt.addITransformEventsListener(new ITransformEventsAdapter() { private static final long serialVersionUID = 1L; public void visibleBoundsUpdated(ITransformEventsVisibleBoundsUpdatedEvent evt) throws IOException { // Set currentExtent to the new visible extent. currentExtent = (Envelope) evt.getSender().getVisibleBounds(); // Refresh the map components foreground phase. map.refresh(esriViewDrawPhase.esriViewForeground, null, null); } }); } public void onMouseDown(IPageLayoutControlEventsOnMouseDownEvent evt) throws IOException { // If Right mouse button(2) is pressed, display the popup menu. if(evt.getButton() == 2) popupMenu.popupMenu(evt.getX(), evt.getY(), pageLayout.getHWnd()); } }); toc.addITOCControlEventsListener(new ITOCControlEventsAdapter() { private static final long serialVersionUID = 1L; public void onEndLabelEdit(ITOCControlEventsOnEndLabelEditEvent labelEditEvt) throws IOException { String newLabel = labelEditEvt.getNewLabel(); // If the new label is an empty string, prevent the edit. if (newLabel.equals("")) labelEditEvt.setCanEdit(false); } }); map.addIMapControlEvents2Listener(new IMapControlEvents2Adapter() { private static final long serialVersionUID = 1L; public void onAfterDraw(IMapControlEvents2OnAfterDrawEvent evt) throws IOException { if (evt.getViewDrawPhase() == esriViewDrawPhase.esriViewForeground) { try { // Draw the shape on the MapBean. map.drawShape(currentExtent, fillSymbol); } catch (Exception e) { System.err.println("Error in drawing shape on MapBean"); //e.printStackTrace(); } } } }); customizeCB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { if (customizeCB.isSelected()) { customizeDialog.startDialog(toolbar.getHWnd()); } else { customizeDialog.closeDialog(); } } catch (Exception ee) { ee.printStackTrace(); } } }); customizeDialog.addICustomizeDialogEventsListener(new ICustomizeDialogEvents() { private static final long serialVersionUID = 1L; public void onStartDialog(ICustomizeDialogEventsOnStartDialogEvent arg0) throws IOException { toolbar.setCustomize(true); } public void onCloseDialog(ICustomizeDialogEventsOnCloseDialogEvent arg0) throws IOException { toolbar.setCustomize(false); java.awt.EventQueue.invokeLater(new Runnable() { public void run() { customizeCB.setSelected(false); } }); } }); } private JPanel buildMapTOCPanel() { JPanel leftPanel = new JPanel(); JPanel bottomPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); bottomPanel.setLayout(new BorderLayout()); bottomPanel.setPreferredSize(new Dimension(200,200)); bottomPanel.add(map, BorderLayout.CENTER); leftPanel.add(toc, BorderLayout.CENTER); leftPanel.add(bottomPanel, BorderLayout.SOUTH); return leftPanel; } private void createOverviewSymbol() throws IOException { RgbColor color = new RgbColor(); color.setRed(255); color.setGreen(0); color.setBlue(0); color.setTransparency((byte)255); SimpleLineSymbol outline = new SimpleLineSymbol(); outline.setWidth(15); outline.setColor(color); fillSymbol = new SimpleFillSymbol(); color.setTransparency((byte)0); fillSymbol.setColor(color); fillSymbol.setOutline(outline); } private void createCustomizeDialog() throws IOException { customizeDialog = new CustomizeDialog(); customizeDialog.setDialogTitle("Customize Toolbar Items"); customizeDialog.setShowAddFromFile(true); // Set the toolbar that the new items will be added to. customizeDialog.setDoubleClickDestination(toolbar); } } // end MapViewerFrame class