arcgissamples\symbologybean\ModifyFrameProperties.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.symbologybean; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.UIManager; 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.symbology.SymbologyBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.IActiveView; import com.esri.arcgis.carto.IBackgroundProxy; import com.esri.arcgis.carto.IBorder; import com.esri.arcgis.carto.IFrameProperties; import com.esri.arcgis.carto.IGraphicsContainer; import com.esri.arcgis.carto.IMapFrame; import com.esri.arcgis.carto.IShadow; 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.ControlsPageZoomInTool; import com.esri.arcgis.controls.ControlsPageZoomOutTool; import com.esri.arcgis.controls.ControlsPageZoomWholePageCommand; import com.esri.arcgis.controls.ControlsSelectTool; import com.esri.arcgis.controls.IPageLayoutControlEventsAdapter; import com.esri.arcgis.controls.IPageLayoutControlEventsOnPageLayoutReplacedEvent; import com.esri.arcgis.controls.ISymbologyControlEventsAdapter; import com.esri.arcgis.controls.ISymbologyControlEventsOnItemSelectedEvent; import com.esri.arcgis.controls.ISymbologyStyleClass; import com.esri.arcgis.controls.esriSymbologyStyleClass; import com.esri.arcgis.display.IStyleGalleryItem; import com.esri.arcgis.display.IStyleGalleryItemProxy; import com.esri.arcgis.display.ServerStyleGalleryItem; import com.esri.arcgis.geometry.IEnvelope; import com.esri.arcgis.geometry.esriGeometryType; import com.esri.arcgis.interop.NativeLoader; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; import com.esri.arcgis.systemUI.esriCommandStyles; /** * This sample demonstrates using the SymbologyControl for changing background,border and shadows of the page frame */ @SuppressWarnings("serial") public class ModifyFrameProperties extends JFrame { private JPanel jContentPanel; private JPanel jMainPanel; private JPanel jLabelPanel; private JComboBox jCmbBox; private JLabel jLabel1; private JLabel jLabel2; private SymbologyBean symbologyBean; private PageLayoutBean pageLayoutBean; private TOCBean tocBean; private ToolbarBean toolbarBean; /* * Main program to start the execution. */ public static void main(String s[]) { try { initVisualbeans(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); new ModifyFrameProperties(); } catch (Exception e) { e.printStackTrace(); System.out.println("Exiting ..."); } } /* * Initialize engine in visualbeans mode */ public static void initVisualbeans() { EngineInitializer.initializeVisualBeans(); } /* * Check the availability and then initialize proper license */ public static void initializeArcGISLicenses() { try { AoInitialize ao = new AoInitialize(); if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); }catch (Exception e){e.printStackTrace();} } public ModifyFrameProperties() throws Exception { buildFrame(); initControl(); addListener(); shutdownArcObjects(); } /* * Build the frame with ArcGIS and Swing Components */ private void buildFrame() throws Exception { setSize(900, 600); setContentPane(getJContentPanel()); setTitle("FrameProperties"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); setVisible(true); setDefaultCloseOperation(ModifyFrameProperties.DISPOSE_ON_CLOSE); } /* * Initialize jContentPane */ private JPanel getJContentPanel() { if (jContentPanel == null) { jContentPanel = new JPanel(); jContentPanel.setLayout(new BorderLayout()); jContentPanel.add(getJMainPanel(), BorderLayout.EAST); jContentPanel.add(getPageLayoutBean(), BorderLayout.CENTER); jContentPanel.add(getTocBean(), BorderLayout.WEST); jContentPanel.add(getToolbarBean(), BorderLayout.NORTH); jLabelPanel = new JPanel(); jLabelPanel.setLayout(new GridLayout(2, 1)); jLabelPanel.add(getJLabel1()); jLabelPanel.add(getJLabel2()); jContentPanel.add(jLabelPanel, BorderLayout.SOUTH); } return jContentPanel; } /* * Initialize mainpanel */ private JPanel getJMainPanel() { if (jMainPanel == null) { try { GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.gridx = 0; gridBagConstraints1.anchor = GridBagConstraints.NORTH; gridBagConstraints1.fill = GridBagConstraints.BOTH; gridBagConstraints1.weighty = 1.0D; gridBagConstraints1.gridy = 1; GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints.anchor = GridBagConstraints.NORTH; gridBagConstraints.weightx = 1.0; jMainPanel = new JPanel(); jMainPanel.setLayout(new GridBagLayout()); jMainPanel.add(getcmbBox(), gridBagConstraints); jMainPanel.add(getSymbologyBean(), gridBagConstraints1); } catch (Exception e) { e.printStackTrace(); } } return jMainPanel; } /* * Initialize ComboBox cmbType */ private JComboBox getcmbBox() { JPopupMenu.setDefaultLightWeightPopupEnabled(false); if (jCmbBox == null) { try { jCmbBox = new JComboBox(); jCmbBox.setPreferredSize(new Dimension(100, 25)); // Add style classes to the combo box jCmbBox.addItem("Backgrounds"); jCmbBox.addItem("Borders"); jCmbBox.addItem("Shadows"); jCmbBox.setSelectedIndex(0); jCmbBox.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { try { if (jCmbBox.getSelectedItem().toString().equals("Backgrounds")) symbologyBean.setStyleClass(esriSymbologyStyleClass.esriStyleClassBackgrounds); else if (jCmbBox.getSelectedItem().toString().equals("Borders")) symbologyBean.setStyleClass(esriSymbologyStyleClass.esriStyleClassBorders); else if (jCmbBox.getSelectedItem().toString().equals("Shadows")) symbologyBean.setStyleClass(esriSymbologyStyleClass.esriStyleClassShadows); } catch (Exception ex) { ex.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } } return jCmbBox; } /* * Initialize label1 */ private JLabel getJLabel1() { if (jLabel1 == null) { jLabel1 = new JLabel(); jLabel1.setText("1) Select a background,border or shadow from the combobox and accordingly " + "the different types of symbols will be shown in the right panel"); } return jLabel1; } /* * Initialize label2 */ private JLabel getJLabel2() { if (jLabel2 == null) { jLabel2 = new JLabel(); jLabel2.setText("2) Select a background/border or shadow symbol and it will be applied to the frame"); } return jLabel2; } /* * Initialize SymbologyBean */ private SymbologyBean getSymbologyBean() { if (symbologyBean == null) { try { symbologyBean = new SymbologyBean(); symbologyBean.setPreferredSize(new Dimension(200, 50)); //Get the ArcGIS Engine runtime, if it is available String runtimeHome = System.getenv("AGSENGINEJAVA"); //If the ArcGIS Engine runtime is not available, then we can try ArcGIS Desktop runtime if(runtimeHome == null){ runtimeHome = System.getenv("AGSDESKTOPJAVA"); } //If no runtime is available, exit application gracefully if(runtimeHome == null){ if(System.getProperty("os.name").toLowerCase().indexOf("win") > -1){ System.err.println("You must have ArcGIS Engine Runtime or ArcGIS Desktop " + "installed in order to execute this sample."); System.err.println("Install one of the products above, then re-run this sample."); System.err.println("Exiting execution of this sample..."); System.exit(0); }else{ System.err.println("You must have ArcGIS Engine Runtime installed " + "in order to execute this sample."); System.err.println("Install the product above, then re-run this sample."); System.err.println("Exiting execution of this sample..."); System.exit(0); } } //Obtain relative path to the ESRI.ServerStyle file String esriStylePath = runtimeHome + "styles" + File.separator + "ESRI.ServerStyle"; File styleFile = new File(esriStylePath); //Test to make sure style file is presente if(!styleFile.exists()){ System.err.println("The ESRI.ServerStyle was not found in the following location: " + styleFile.getParent()); System.err.println("Verify that ESRI.ServerStyle can be located in the specified folder."); System.err.println("If not present, try uninstalling your ArcGIS software and reinstalling it."); System.err.println("Exiting execution of this sample..."); System.exit(0); } // Load the ESRI.ServerStyle file into the SymbologyControl symbologyBean.loadStyleFile(esriStylePath); symbologyBean.setStyleClass(esriSymbologyStyleClass.esriStyleClassBackgrounds); } catch (Exception e) { e.printStackTrace(); } } return symbologyBean; } /* * Initialize pagelayoutBean */ private PageLayoutBean getPageLayoutBean() { if (pageLayoutBean == null) { try { pageLayoutBean = new PageLayoutBean(); //Get DEVKITHOME Home String devKitHome = System.getenv("AGSDEVKITJAVA"); if (devKitHome == null) { System.out.println("ArcObjects Java SDK is not installed on your system"); System.out.println("Exiting ..."); System.exit(0); } try { pageLayoutBean.loadMxFile(devKitHome + "java" + File.separator + "samples" + File.separator + "data" + File.separator + "mxds" + File.separator + "gulf of st. lawrence.mxd", null); IEnvelope env = ((Map) pageLayoutBean.getActiveView().getFocusMap()).getFullExtent(); ((Map) pageLayoutBean.getActiveView().getFocusMap()).setExtent(env); pageLayoutBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } } return pageLayoutBean; } /* * Initialize tocBean */ private TOCBean getTocBean() { if (tocBean == null) { try { tocBean = new TOCBean(); } catch (Exception e) { e.printStackTrace(); } } return tocBean; } /* * Initialize toolbarBean */ private ToolbarBean getToolbarBean() { if (toolbarBean == null) { try { toolbarBean = new ToolbarBean(); } catch (Exception e) { e.printStackTrace(); } } return toolbarBean; } /* * Initialize control and add tools and commands to toolbar */ public void initControl() throws Exception { try { // Set the Buddy toolbarBean.setBuddyControl(pageLayoutBean); tocBean.setBuddyControl(pageLayoutBean); // Add tool bar items.. toolbarBean.addItem(new ControlsOpenDocCommand(), 0, 0, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // open toolbarBean.addItem(new ControlsPageZoomInTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); // PageZoomIn toolbarBean.addItem(new ControlsPageZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // PageZoomOut toolbarBean.addItem(new ControlsPageZoomWholePageCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // PagePan toolbarBean.addItem(new ControlsMapZoomInTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // MapZoomIn toolbarBean.addItem(new ControlsMapZoomOutTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // MapZoomout toolbarBean.addItem(new ControlsMapPanTool(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // Map // Pan toolbarBean.addItem(new ControlsMapFullExtentCommand(), 0, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly); // MapFullExtent toolbarBean.addItem(new ControlsSelectTool(), 0, -1, true, 0, esriCommandStyles.esriCommandStyleIconOnly); // Select } catch (Exception e) { System.out.println("Couldn't add commands or tools to toolbar bean."); e.printStackTrace(); } } /* * Add necessary listener to PageLayout and symbologycontrol to handle the PageLayoutReplaced and onItemSelected * events respectively */ public void addListener() { try { pageLayoutBean.getPageLayoutControl().addIPageLayoutControlEventsListener( new IPageLayoutControlEventsAdapter() { public void onPageLayoutReplaced(IPageLayoutControlEventsOnPageLayoutReplacedEvent theEvent) { try { IEnvelope env = ((Map) pageLayoutBean.getActiveView().getFocusMap()).getFullExtent(); ((Map) pageLayoutBean.getActiveView().getFocusMap()).setExtent(env); pageLayoutBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); // Get the frame containing the focus map IFrameProperties frameProperties = (IFrameProperties) pageLayoutBean .getGraphicsContainer().findFrame(pageLayoutBean.getActiveView().getFocusMap()); // Get the MapFrame IMapFrame mapFrame = (IMapFrame) frameProperties; // Create a new ServerStyleGalleryItem with its name set IStyleGalleryItem styleGalleryItem = new ServerStyleGalleryItem(); ISymbologyStyleClass styleClass; // Get the background style class styleClass = symbologyBean .getStyleClass2(esriSymbologyStyleClass.esriStyleClassBackgrounds); if (mapFrame.getBackground() != null) { // Set the background into the style gallery item styleGalleryItem.setItem(mapFrame.getBackground()); // Add the item to the style class styleClass.addItem(styleGalleryItem, 0); } // Get the border style class styleClass = symbologyBean .getStyleClass2(esriSymbologyStyleClass.esriStyleClassBorders); if (frameProperties.getBorder() != null) { // Set the border into the style gallery item styleGalleryItem.setItem(frameProperties.getBorder()); // Add the item to the style class styleClass.addItem(styleGalleryItem, 0); } // Get the shadow style class styleClass = symbologyBean .getStyleClass2(esriSymbologyStyleClass.esriStyleClassShadows); if (frameProperties.getShadow() != null) { // Set the shadow into the style gallery item styleGalleryItem.setItem(frameProperties.getShadow()); // Add the item to the style class styleClass.addItem(styleGalleryItem, 0); } } catch (Exception e) { e.printStackTrace(); } } }); symbologyBean.addISymbologyControlEventsListener(new ISymbologyControlEventsAdapter() { @SuppressWarnings("deprecation") public void onItemSelected(ISymbologyControlEventsOnItemSelectedEvent theEvent) { try { IStyleGalleryItem styleGalleryItem = new IStyleGalleryItemProxy(theEvent.getStyleGalleryItem()); IFrameProperties frameProperties = (IFrameProperties) pageLayoutBean.getGraphicsContainer() .findFrame(pageLayoutBean.getActiveView().getFocusMap()); if (jCmbBox.getSelectedItem().toString().equals("Backgrounds")) { // Set the frame's background frameProperties.setBackground(new IBackgroundProxy(styleGalleryItem.getItem())); pageLayoutBean.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGeography, null, null); } else if (jCmbBox.getSelectedItem().toString().equals("Borders")) { // Set the frame's border frameProperties.setBorder((IBorder) styleGalleryItem.getItem()); pageLayoutBean.getActiveView().partialRefresh(esriViewDrawPhase.esriViewBackground, null, null); } else if (jCmbBox.getSelectedItem().toString().equals("Shadows")) { // Set the frame's shadow frameProperties.setShadow((IShadow) styleGalleryItem.getItem()); pageLayoutBean.getActiveView().partialRefresh(esriViewDrawPhase.esriViewBackground, null, null); } // Refresh the PageLayoutControl pageLayoutBean.refresh(esriViewDrawPhase.esriViewBackground, null, null); } catch (Exception e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } } /* * Shutdown ArcObjects when the window closes */ public void shutdownArcObjects() throws Exception { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { new AoInitialize().shutdown(); System.exit(0); } catch (Exception err) { err.printStackTrace(); System.exit(1); } } }); } }