arcgissamples\symbologybean\AddTextElementToMap.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.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; import arcgissamples.symbologybean.ui.SymbologyFrame; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.carto.IElement; import com.esri.arcgis.carto.ITextElement; import com.esri.arcgis.carto.TextElement; 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.IPageLayoutControlEventsOnMouseDownEvent; import com.esri.arcgis.display.IStyleGalleryItem; import com.esri.arcgis.display.ITextSymbol; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.Point; 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 to add a customized text in pagelayout */ public class AddTextElementToMap extends JFrame implements ActionListener { private PageLayoutBean pageLayoutBean; private ToolbarBean toolbarBean; private JButton jBtnTextSymbol; private JTextField jTxtSymbolName; private JComboBox jCmbBox; private JLabel jLabel1; private JLabel jLabel2; private JLabel jLabel3; private JPanel jMainPanel; private JPanel jTopPanel; private JPanel jLabelPanel; private SymbologyFrame symbolForm; /* * Main program to start the execution. */ public static void main(String s[]) { try { initVisualbeans(); initializeArcGISLicenses(); new AddTextElementToMap(); } 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); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcEditor) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor); else if (ao.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcInfo) == esriLicenseStatus.esriLicenseAvailable) ao.initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); } catch (Exception e) {e.printStackTrace();} } public AddTextElementToMap() throws Exception { buildFrame(); initControl(); addListener(); setSize(900,600); setTitle("TextSymbols"); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); setVisible(true); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); shutdownArcObjects(); } /* * Build the frame with ArcGIS and Swing Components */ public void buildFrame() { jMainPanel = new JPanel(); jMainPanel.setLayout(new BorderLayout()); jTopPanel = new JPanel(); jTopPanel.setLayout(new GridLayout(1, 1)); jLabelPanel = new JPanel(); jLabelPanel.setLayout(new GridLayout(3, 1)); jTopPanel.add(getToolbatBean()); jTopPanel.add(getJButtonTextSymbol()); jTopPanel.add(getJTextSymbolName()); jTopPanel.add(getJComboTextSize()); jLabelPanel.add(getJLabel1()); jLabelPanel.add(getJLabel2()); jLabelPanel.add(getJLabel3()); jMainPanel.add(jTopPanel, BorderLayout.NORTH); jMainPanel.add(getPagelayoutBean(), BorderLayout.CENTER); jMainPanel.add(jLabelPanel, BorderLayout.SOUTH); jMainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); getContentPane().add(jMainPanel, BorderLayout.CENTER); } /* * Initialize toolbarBean */ private ToolbarBean getToolbatBean() { if (toolbarBean == null) { toolbarBean = new ToolbarBean(); } return toolbarBean; } /* * Initialize the button btnTextSymbol */ private JButton getJButtonTextSymbol() { if (jBtnTextSymbol == null) { jBtnTextSymbol = new JButton(); jBtnTextSymbol.setSize(new Dimension(10, 10)); jBtnTextSymbol.setText("Select Text Symbol"); jBtnTextSymbol.addActionListener(this); } return jBtnTextSymbol; } /* * This method is called when button Select Text Symbol is clicked */ public void actionPerformed(ActionEvent e) { try{ symbolForm = new SymbologyFrame(); symbolForm.setVisible(true); } catch(Exception err){ err.printStackTrace(); } } /* * Initialize the textfield txtSymbolName */ private JTextField getJTextSymbolName() { if (jTxtSymbolName == null) { jTxtSymbolName = new JTextField(); jTxtSymbolName.setPreferredSize(new Dimension(50, 25)); jTxtSymbolName.setText("TextElement with selected TextSymbol"); } return jTxtSymbolName; } /* * Initialize combo cmbTextSize */ private JComboBox getJComboTextSize() { JPopupMenu.setDefaultLightWeightPopupEnabled(false); if (jCmbBox == null) { jCmbBox = new JComboBox(); jCmbBox.addItem("8pt"); jCmbBox.addItem("10pt"); jCmbBox.addItem("12pt"); jCmbBox.addItem("14pt"); jCmbBox.setSelectedIndex(0); } return jCmbBox; } /* * Initialize label1 */ private JLabel getJLabel1() { if (jLabel1 == null) { jLabel1 = new JLabel(); jLabel1.setText("1) Load a mapdocument into the pagelayout"); } return jLabel1; } /* * Initialize label2 */ private JLabel getJLabel2() { if (jLabel2 == null) { jLabel2 = new JLabel(); jLabel2.setText("2) Select a text symbol by clicking on the Select TextSymbol button"); } return jLabel2; } /* * Initialize label3 */ private JLabel getJLabel3() { if (jLabel3 == null) { jLabel3 = new JLabel(); jLabel3.setText("3) Customize the text and size then right click on the display to add a text element"); } return jLabel3; } /* * Initialize pagelayoutBean */ private PageLayoutBean getPagelayoutBean() { if (pageLayoutBean == null) { pageLayoutBean = new PageLayoutBean(); } return pageLayoutBean; } /* * Initialize control and add tools and commands to toolbar */ public void initControl() throws Exception { try { // Set the Buddy to pagelayoutbean toolbarBean.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 pagelayoutcontrol to handle the Mousedown event */ @SuppressWarnings("serial") public void addListener() { try { pageLayoutBean.getPageLayoutControl().addIPageLayoutControlEventsListener(new IPageLayoutControlEventsAdapter() { public void onMouseDown(final IPageLayoutControlEventsOnMouseDownEvent theEvent) { SwingUtilities.invokeLater(new Runnable() { public void run(){ try { if(symbolForm != null){ // Get the selected styleGalleryItem from symbolframe IStyleGalleryItem styleGalleryItem = symbolForm.getSelectedStyleItem();; if (styleGalleryItem == null) return; //Set the TextSymbol from styleGalleryItem ITextSymbol textSymbol = (ITextSymbol) styleGalleryItem.getItem(); //Check if the right button of the mouse was clicked if (theEvent.getButton() != 2) return; //Ensure a text symbol has been selected if (textSymbol == null) return; //Create a point and set its coordinates IPoint point = new Point(); point.setX(theEvent.getPageX()); point.setY(theEvent.getPageY()); //Create a text element ITextElement textElement = new TextElement(); // set Text for the text element textElement.setText(jTxtSymbolName.getText());; //Set the size of the text based on the selection from the text size combo if (jCmbBox.getSelectedItem().toString().equalsIgnoreCase("8pt")) textSymbol.setSize(8.0); else if (jCmbBox.getSelectedItem().toString().equalsIgnoreCase("10pt")) textSymbol.setSize(10.0); else if (jCmbBox.getSelectedItem().toString().equalsIgnoreCase("12pt")) textSymbol.setSize(12.0); else if (jCmbBox.getSelectedItem().toString().equalsIgnoreCase("14pt")) textSymbol.setSize(14.0); //Set the TextElement symbol to that of the selected text symbol textElement.setSymbol(textSymbol); textElement.setScaleText(true); //Cast to IElment IElement element = (IElement) textElement; //Set the TextElement's geometry element.setGeometry(point); //Add the element to the GraphicsContainer pageLayoutBean.getActiveView().getGraphicsContainer().addElement(element, 0); //Refresh the PageLayout pageLayoutBean.getActiveView().partialRefresh(esriViewDrawPhase.esriViewGraphics, element, 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); } } } ); } }