arcgissamples\cartography\CharacterIndexMain.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.cartography; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Choice; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.carto.IGraphicsContainer; import com.esri.arcgis.carto.IPage; import com.esri.arcgis.carto.MarkerElement; import com.esri.arcgis.carto.PageLayout; import com.esri.arcgis.carto.TextElement; import com.esri.arcgis.display.CharacterMarkerSymbol; import com.esri.arcgis.display.ICharacterMarkerSymbol; import com.esri.arcgis.display.IDisplayTransformation; import com.esri.arcgis.display.IFormattedTextSymbol; import com.esri.arcgis.display.IScreenDisplay; import com.esri.arcgis.display.ITextSymbol; import com.esri.arcgis.display.RgbColor; import com.esri.arcgis.display.TextSymbol; import com.esri.arcgis.display.esriScreenCache; import com.esri.arcgis.display.esriTextHorizontalAlignment; import com.esri.arcgis.display.esriTextVerticalAlignment; import com.esri.arcgis.geometry.Envelope; import com.esri.arcgis.geometry.IGeometry; import com.esri.arcgis.geometry.IPoint; import com.esri.arcgis.geometry.ITransformation; import com.esri.arcgis.geometry.Point; import com.esri.arcgis.geometry.Polygon; import com.esri.arcgis.interop.AutomationException; import com.esri.arcgis.support.ms.stdole.Font; import com.esri.arcgis.support.ms.stdole.StdFont; import com.esri.arcgis.system.AoInitialize; import com.esri.arcgis.system.EngineInitializer; import com.esri.arcgis.system.esriLicenseProductCode; import com.esri.arcgis.system.esriLicenseStatus; /** * Use this sample to find out what all the glyphs in a Font look like, * by using a CharacterMarkerSymbol and the CharacterIndex property. */ public class CharacterIndexMain extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; static AoInitialize aoInit; PageLayoutBean pageLayoutBean = new PageLayoutBean(); Button buttonShowGlyphs = new Button(); Choice comboFonts = new Choice(); /** * Constructor */ public CharacterIndexMain() { initUI(); } /** * Method to start the program execution. * @param s String[] */ public static void main(String s[]) { try { EngineInitializer.initializeVisualBeans(); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); initializeArcGISLicenses(); new CharacterIndexMain(); } catch (Exception ex) { ex.printStackTrace(); } } static void initializeArcGISLicenses() { try { aoInit = new AoInitialize(); if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine); else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable) aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView); } catch (Exception e) { e.printStackTrace(); } } /* * Lays out the User Interface, and sets up event listeners */ private void initUI() { this.setTitle("Character Index Sample Application"); this.setSize(new Dimension(600, 500)); this.getContentPane().setLayout(new BorderLayout()); this.comboFonts.add("ESRI Arrowhead"); this.comboFonts.add("ESRI Cartography"); this.comboFonts.add("ESRI Climate & Precipitation"); this.comboFonts.add("ESRI Conservation"); this.comboFonts.add("ESRI Crime Analysis"); this.comboFonts.add("ESRI Default Marker"); this.comboFonts.add("ESRI Dimensioning"); this.comboFonts.add("ESRI Environmental & Icons"); this.comboFonts.add("ESRI Geology"); this.comboFonts.add("ESRI Geometric Symbols"); this.comboFonts.add("ESRI North"); this.comboFonts.add("ESRI Oil, Gas, & Water"); this.comboFonts.add("ESRI Public1"); this.comboFonts.add("ESRI Shields"); this.comboFonts.add("ESRI Telecom"); this.comboFonts.add("ESRI Transportation & Civic"); this.comboFonts.add("ESRI Transportation & Municipal"); this.comboFonts.add("ESRI Weather"); this.comboFonts.select(5); this.buttonShowGlyphs.setLabel("Show"); JPanel toolPanel = new JPanel(new FlowLayout(FlowLayout.LEADING)); toolPanel.add(this.comboFonts); toolPanel.add(this.buttonShowGlyphs); this.getContentPane().add(this.pageLayoutBean, BorderLayout.CENTER); this.getContentPane().add(toolPanel, BorderLayout.SOUTH); this.setVisible(true); // set listeners this.buttonShowGlyphs.addActionListener(this); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { try { aoInit.shutdown(); } catch(IOException ex) { // exit anyway } System.exit(0); } }); } /** * Method implements interface ActionListener * @see ActionListener#actionPerformed * @param event */ public void actionPerformed(ActionEvent event) { try { placeElements(); // shows glyphs this.pageLayoutBean.getActiveView().refresh(); } catch( IOException ex ) { ex.printStackTrace(); // never happened } } // private methods // This method shows the glyphs private void placeElements() throws IOException, AutomationException { PageLayout pageLayout = (PageLayout) this.pageLayoutBean.getActiveView(); pageLayout.deleteAllElements(); // Set local variables for the display. IPage page = pageLayout.getPage(); Envelope printEnvelope = (Envelope) page.getPrintableBounds(); IScreenDisplay display = pageLayout.getScreenDisplay(); // Check a few variables to make sure. if(pageLayout == null || printEnvelope == null) return; // Set up the basic CharacterMarkerSymbol and the TextSymbol for the label. CharacterMarkerSymbol charMarker = setupCharacterMarkerSymbol(); TextSymbol textSym = setupLabelTextSymbol(); // First, check if we are going to have a title. Point titlePnt = new Point(); titlePnt.putCoords( printEnvelope.getXMin(), printEnvelope.getYMax() * 0.97 ); textSym.setText( "Font: " + this.comboFonts.getSelectedItem() ); addTextElement( titlePnt, textSym ); // Move top of Bounds envelope. printEnvelope.setYMax( printEnvelope.getYMax() * 0.94 ); // Calculate the row height and column width. Also calculate the horizontal // offset from the Center of the Column for the CharacterMarkerSymbol and the // Label. We use arbitrary values of a third and three-fifths. int lRows = 25; int lColumns = 3; int lStart = 33; double dRowHgt = printEnvelope.getHeight() / lRows; double dColWth = printEnvelope.getWidth() / lColumns; double dCharOffset = dColWth / 3; double dLabelOffset = dColWth * 0.6; // Iterate specified number of Columns then Rows. int lGlyphCounter = lStart; for( int j = 0; j < lColumns; j++ ) { for( int i = 0; i < lRows; i ++ ) { // Make CenterPoint for Character, and add with the appropriate symbol. Point point = new Point(); point.putCoords( (j * dColWth) + dCharOffset + printEnvelope.getXMin(), printEnvelope.getYMax() - (i * dRowHgt) - (dRowHgt / 2) ); charMarker.setCharacterIndex(lGlyphCounter); if( checkIndex(charMarker, point, display) ) { addCharElement( point, charMarker); } else { // If the character is empty, just put a line instead. textSym.setText("-"); addTextElement(point, textSym); } // Make CenterPoint for Character, and add with the appropriate symbol. Point pointCenter = new Point(); pointCenter.putCoords( (j * dColWth) + dLabelOffset + printEnvelope.getXMin(), printEnvelope.getYMax() - (i * dRowHgt) - (dRowHgt / 2) ); textSym.setText( " " + lGlyphCounter ); addTextElement( pointCenter, textSym ); // Increment the index counter. lGlyphCounter = lGlyphCounter + 1; } } } // This method checks the index. private boolean checkIndex(ICharacterMarkerSymbol iCharacterMarkerSymbol, IGeometry geom, IScreenDisplay disp) throws IOException, AutomationException { // This is a rather arbitrary test for an empty glyph - they report an unusual symbol size. if( iCharacterMarkerSymbol != null ) { CharacterMarkerSymbol characterMarkerSymbol = (CharacterMarkerSymbol)(iCharacterMarkerSymbol); Polygon polyBound = new Polygon(); disp.startDrawing( disp.getHDC(), (short)esriScreenCache.esriNoScreenCache ); IDisplayTransformation displayTransformation = disp.getDisplayTransformation(); ITransformation transformation = displayTransformation; characterMarkerSymbol.queryBoundary( disp.getHDC(), transformation, geom, polyBound ); disp.finishDrawing(); if( polyBound == null ) return false; else if( polyBound.isEmpty() ) return false; else { if( polyBound.getArea() > 20 ) return false; return true; } } return true; } // This method adds a CharacterMarkerSymbol Element to the ActiveView. private void addCharElement(IPoint point, ICharacterMarkerSymbol sym) throws IOException, AutomationException { IGraphicsContainer graphicsContainer = this.pageLayoutBean.getActiveView().getGraphicsContainer(); if( graphicsContainer != null ) { if( point != null ) { if( !point.isEmpty() ) { if( sym != null ) { MarkerElement element = new MarkerElement(); element.setGeometry(point); element.setSymbol(sym); graphicsContainer.addElement(element, 0); } } } } } // This method adds a TextSymbol Element to the ActiveView. private void addTextElement(IPoint point, ITextSymbol sym) throws IOException, AutomationException { IGraphicsContainer graphicsContainer = this.pageLayoutBean.getActiveView().getGraphicsContainer(); if( graphicsContainer != null ) { if( point != null ) { if( !point.isEmpty() ) { if( sym != null ) { TextElement textElement = new TextElement(); textElement.setGeometry(point); textElement.setSymbol(sym); textElement.setText(sym.getText()); graphicsContainer.addElement(textElement, 0); } } } } } // The method creates character symbol private CharacterMarkerSymbol setupCharacterMarkerSymbol() throws IOException, AutomationException { RgbColor color = new RgbColor(); color.setRGB( 0x0000FF ); // red Font font = new StdFont(); font.setName( this.comboFonts.getSelectedItem() ); CharacterMarkerSymbol characterMarkerSymbol = new CharacterMarkerSymbol(); characterMarkerSymbol.setFont(font); characterMarkerSymbol.setColor(color); characterMarkerSymbol.setSize( 28 ); return characterMarkerSymbol; } // The method creates label symbol private TextSymbol setupLabelTextSymbol() throws IOException, AutomationException { TextSymbol labelTextSymbol = new TextSymbol(); labelTextSymbol.setSize( 28 ); IFormattedTextSymbol formattedTextSymbol = labelTextSymbol; formattedTextSymbol.setHorizontalAlignment(esriTextHorizontalAlignment.esriTHALeft); formattedTextSymbol.setVerticalAlignment(esriTextVerticalAlignment.esriTVACenter); return labelTextSymbol; } }