arcgissamples\symbologybean\ui\SymbologyFrame.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.ui; import java.awt.FlowLayout; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import com.esri.arcgis.beans.pagelayout.PageLayoutBean; import com.esri.arcgis.carto.IFeatureLayer; import com.esri.arcgis.carto.IFeatureRenderer; import com.esri.arcgis.carto.IGeoFeatureLayer; import com.esri.arcgis.carto.ISimpleRenderer; import com.esri.arcgis.carto.SimpleRenderer; import com.esri.arcgis.carto.esriViewDrawPhase; import com.esri.arcgis.controls.ISymbologyControlEventsAdapter; import com.esri.arcgis.controls.ISymbologyControlEventsOnItemSelectedEvent; import com.esri.arcgis.controls.SymbologyControl; import com.esri.arcgis.controls.esriSymbologyStyleClass; import com.esri.arcgis.display.IStyleGalleryItem; import com.esri.arcgis.display.IStyleGalleryItemProxy; import com.esri.arcgis.display.ISymbol; import com.esri.arcgis.geometry.esriGeometryType; public class SymbologyFrame extends JFrame implements ActionListener { private JPanel jMainPanel; private JPanel jButtonPanel; private JButton jBtnOK; private JButton jBtnCancel; private PageLayoutBean pageLayoutBean; private SymbologyControl symbologyControl; private IStyleGalleryItem styleGalleryItem; private IFeatureLayer featureLayer; private ISimpleRenderer simpleRenderer; private IGeoFeatureLayer geoFeatureLayer; public SymbologyFrame(PageLayoutBean page,IFeatureLayer layer) { pageLayoutBean = page; featureLayer = layer; initialize(); loadStyle(); addListener(); } /* * Initialize the symbology Frame and set size and Title */ private void initialize() { this.setSize(487, 400); this.setContentPane(getJContentPanel()); this.setTitle("Layer Symbol"); } /* * Load the style file and set the style class */ private void loadStyle() { try { //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 symbologyControl.loadStyleFile(esriStylePath); // Set the style class if(featureLayer.getFeatureClass().getShapeType()== esriGeometryType.esriGeometryPoint){ symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassMarkerSymbols); } else if(featureLayer.getFeatureClass().getShapeType()== esriGeometryType.esriGeometryPolyline){ symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassLineSymbols); } else if(featureLayer.getFeatureClass().getShapeType()== esriGeometryType.esriGeometryPolygon){ symbologyControl.setStyleClass(esriSymbologyStyleClass.esriStyleClassFillSymbols); } } catch (Exception err) { err.printStackTrace(); } } /* * Initialize jContentPane */ private JPanel getJContentPanel() { if (jMainPanel == null) { jMainPanel = new JPanel(); jButtonPanel = new JPanel(); jButtonPanel.setLayout(new FlowLayout()); jMainPanel.setLayout(new BoxLayout(jMainPanel,BoxLayout.Y_AXIS)); jButtonPanel.add(getJButtonOK(), null); jButtonPanel.add(getJButtonCancel(), null); jMainPanel.add(getSymbologyControl()); jMainPanel.add(jButtonPanel); } return jMainPanel; } /* * Initialize symbologyControl */ private SymbologyControl getSymbologyControl() { if (symbologyControl == null) { symbologyControl = new SymbologyControl(); symbologyControl.setBounds(new java.awt.Rectangle(4, 3, 302,265)); } return symbologyControl; } /* * Initialize button btnOK */ private JButton getJButtonOK() { if (jBtnOK == null) { jBtnOK = new JButton(); jBtnOK.setBounds(new Rectangle(312, 243, 80, 20)); jBtnOK.setText("OK"); jBtnOK.setEnabled(false); jBtnOK.addActionListener(this); } return jBtnOK; } /* * Initialize btnCancel */ private JButton getJButtonCancel() { if (jBtnCancel == null) { jBtnCancel = new JButton(); jBtnCancel.setText("Cancel"); jBtnCancel.setBounds(new Rectangle(392, 243, 80, 20)); jBtnCancel.addActionListener(this); } return jBtnCancel; } /* * This method is called when button OK/Cancel is clicked */ public void actionPerformed(ActionEvent e) { Runnable r = null; if (e.getSource() == jBtnOK) { r = new Runnable(){ public void run() { try { if (styleGalleryItem == null) return; //Create a new renderer simpleRenderer = new SimpleRenderer(); //Set its symbol from the styleGalleryItem simpleRenderer.setSymbolByRef((ISymbol) styleGalleryItem.getItem()); geoFeatureLayer = (IGeoFeatureLayer) featureLayer; //Set the renderer into the geoFeatureLayer geoFeatureLayer.setRendererByRef((IFeatureRenderer) simpleRenderer); //Fire contents changed event that the TOCControl listens to pageLayoutBean.getActiveView().contentsChanged(); //Refresh the display pageLayoutBean.refresh(esriViewDrawPhase.esriViewGeography, null, null); dispose(); } catch (Exception err) { err.printStackTrace(); } } }; } if (e.getSource() == jBtnCancel) { styleGalleryItem = null; this.dispose(); } Thread t = new Thread(r); t.start(); } /* * Add necessary listener to symbologycontrol to handle the ItemSelecetd event */ @SuppressWarnings("serial") public void addListener() { try { symbologyControl.addISymbologyControlEventsListener(new ISymbologyControlEventsAdapter() { @SuppressWarnings("deprecation") public void onItemSelected(ISymbologyControlEventsOnItemSelectedEvent theEvent) { jBtnOK.setEnabled(true); try { // get the selected stylegalleryitem styleGalleryItem = new IStyleGalleryItemProxy(theEvent.getStyleGalleryItem()); } catch (Exception e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } } }