arcgissamples\toolbarbean\ModifyToolbarAppearance.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.toolbarbean; import java.io.IOException; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JColorChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import com.esri.arcgis.beans.map.MapBean; import com.esri.arcgis.beans.toolbar.ToolbarBean; import com.esri.arcgis.controls.esriToolbarFillDirection; import com.esri.arcgis.controls.esriToolbarOrientation; 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.interop.AutomationException; /** * Description: This sample demonstrates appearance features of the Toolbar. It shows usage of Orientation, Hidden * Items, Back/Fade Colors with Blended fill direction properties of the Toolbar. Checkboxes toggle the setOrientation, * setFillDirection and setShowHiddenItems methods. A JColorChooser is displayed to select colors to satisfy the * setFadeColor and setBackColor methods. */ public class ModifyToolbarAppearance extends JFrame { private JPanel jContentPane = null; private ToolbarBean toolbarBean1 = null; private MapBean mapBean1 = null; private JCheckBox VertOrientationChkBox = null; private JCheckBox showHiddenChkBox = null; private JCheckBox VertFillChkBox = null; private JLabel jLabel = null; private JLabel jLabel1 = null; private JLabel jLabel2 = null; private JButton btnBackColor = null; private JButton btnFadeColor = null; /** * This method initializes toobarBean1 * * @return com.esri.arcgis.beans.toolbar.ToolbarBean */ private ToolbarBean getToobarBean1() { if (toolbarBean1 == null) { toolbarBean1 = new ToolbarBean(); toolbarBean1.setBounds(new java.awt.Rectangle(15, 15, 271, 25)); toolbarBean1 .setItemsString("5|controls/ControlsOpenDocCommand|0|-1|0|0|1;" + "11|controls/ControlsMapZoomPanTool|0|-1|0|0|1;" + "11|controls/ControlsMapZoomInFixedCommand|0|-1|0|0|1;" + "11|controls/ControlsMapZoomOutFixedCommand|0|-1|0|0|1;" + "11|controls/ControlsMapFullExtentCommand|0|-1|0|0|1;" + "11|controls/ControlsMapPanTool|0|-1|0|0|1;" + "11|controls/ControlsMapZoomInTool|0|-1|0|0|1;" + "11|controls/ControlsMapZoomOutTool|0|-1|0|0|1;" + "4|controls/ControlsSelectAllCommand|0|-1|0|0|1;" + "4|controls/ControlsSelectByGraphicsCommand|0|-1|0|0|1;" + "4|controls/ControlsSelectFeaturesTool|0|-1|0|0|1;" + "4|controls/ControlsSelectScreenCommand|0|-1|0|0|1;" + "4|controls/ControlsClearSelectionCommand|0|-1|0|0|1;" + "4|controls/ControlsSwitchSelectionCommand|0|-1|0|0|1;" + "4|controls/ControlsZoomToSelectedCommand|0|-1|0|0|1"); try { toolbarBean1.setBuddyControl(getMapBean1()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return toolbarBean1; } /** * This method initializes mapBean1 * * @return com.esri.arcgis.beans.map.MapBean */ private MapBean getMapBean1() { if (mapBean1 == null) { mapBean1 = new MapBean(); mapBean1.setBounds(new java.awt.Rectangle(45, 45, 469, 373)); } return mapBean1; } /** * This method initializes jCheckBox * * @return javax.swing.JCheckBox */ private JCheckBox getJCheckBox() { if (VertOrientationChkBox == null) { VertOrientationChkBox = new JCheckBox(); VertOrientationChkBox.setBounds(new java.awt.Rectangle(525, 60, 31, 28)); VertOrientationChkBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { VertOrientationChkBox_itemStateChanged(); } }); } return VertOrientationChkBox; } protected void VertOrientationChkBox_itemStateChanged() { if (VertOrientationChkBox.isSelected()) try { toolbarBean1.setOrientation(esriToolbarOrientation.esriToolbarOrientationVertical); toolbarBean1.setSize(25, 271); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } else try { toolbarBean1.setOrientation(esriToolbarOrientation.esriToolbarOrientationHorizontal); toolbarBean1.setSize(271, 25); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } toolbarBean1.revalidate(); } /** * This method initializes jCheckBox1 * * @return javax.swing.JCheckBox */ private JCheckBox getJCheckBox1() { if (showHiddenChkBox == null) { showHiddenChkBox = new JCheckBox(); showHiddenChkBox.setBounds(new java.awt.Rectangle(525, 90, 31, 31)); showHiddenChkBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { showHiddenChkBox_itemStateChanged(); } }); } return showHiddenChkBox; } protected void showHiddenChkBox_itemStateChanged() { if (showHiddenChkBox.isSelected()) try { toolbarBean1.setShowHiddenItems(true); } catch (AutomationException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } else try { toolbarBean1.setShowHiddenItems(false); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ; } /** * This method initializes jCheckBox2 * * @return javax.swing.JCheckBox */ private JCheckBox getJCheckBox2() { if (VertFillChkBox == null) { VertFillChkBox = new JCheckBox(); VertFillChkBox.setBounds(new java.awt.Rectangle(525, 225, 31, 31)); VertFillChkBox.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(java.awt.event.ItemEvent e) { VertFillChkBox_itemStateChanged(); } }); } return VertFillChkBox; } protected void VertFillChkBox_itemStateChanged() { // if (VertFillChkBox.isSelected()) try { toolbarBean1.setFillDirection(esriToolbarFillDirection.esriToolbarFillVertical); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } else try { toolbarBean1.setFillDirection(esriToolbarFillDirection.esriToolbarFillHorizontal); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * This method initializes jButton * * @return javax.swing.JButton */ private JButton getJButton() { if (btnBackColor == null) { btnBackColor = new JButton(); btnBackColor.setBounds(new java.awt.Rectangle(555, 135, 106, 31)); btnBackColor.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12)); btnBackColor.setText("Back Color"); btnBackColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { btnBackColor_actionPerformed(); } }); } return btnBackColor; } protected void btnBackColor_actionPerformed() { // Display a JColorChooser to pick a color JFrame frame = new JFrame(); java.awt.Color colorType = JColorChooser.showDialog(frame, "Choose a Color", null); // ToolbarBean setBackColor() expects an OLE_Color. The OLE_Color // is made up as follows:(Red) + (Green * 256) + (Blue * 256 * 256) if (colorType != null) { int r = colorType.getRed(); int g = colorType.getGreen(); int b = colorType.getBlue(); try { toolbarBean1.setBackColor(r + (g * 256) + (b * 256 * 256)); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * This method initializes jButton1 * * @return javax.swing.JButton */ private JButton getJButton1() { if (btnFadeColor == null) { btnFadeColor = new JButton(); btnFadeColor.setBounds(new java.awt.Rectangle(555, 180, 106, 31)); btnFadeColor.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 12)); btnFadeColor.setText("Fade Color"); btnFadeColor.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { btnFadeColor_actionPerformed(); } }); } return btnFadeColor; } protected void btnFadeColor_actionPerformed() { // Display a JColorChooser to pick a color JFrame frame = new JFrame(); java.awt.Color colorType = JColorChooser.showDialog(frame, "Choose a Color", null); // ToolbarBean setFadeColor() expects an OLE_Color. The OLE_Color // is made up as follows:(Red) + (Green * 256) + (Blue * 256 * 256) if (colorType != null) { int r = colorType.getRed(); int g = colorType.getGreen(); int b = colorType.getBlue(); try { toolbarBean1.setFadeColor(r + (g * 256) + (b * 256 * 256)); } catch (AutomationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /** * @param args */ public static void main(String[] args) { EngineInitializer.initializeVisualBeans(); ModifyToolbarAppearance map = new ModifyToolbarAppearance(); map.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); map.setVisible(true); } /** * This is the default constructor */ public ModifyToolbarAppearance() { super(); initializeArcGISLicenses(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(702, 469); this.setContentPane(getJContentPane()); this.setTitle("Toolbar Appearance"); this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent e) { try { new AoInitialize().shutdown(); } catch (Exception ex) { ex.printStackTrace(); } } }); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { jLabel2 = new JLabel(); jLabel2.setBounds(new java.awt.Rectangle(555, 225, 106, 31)); jLabel2.setText("Vertical Fill Direction"); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel2.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 10)); jLabel1 = new JLabel(); jLabel1.setBounds(new java.awt.Rectangle(555, 90, 106, 31)); jLabel1.setText("Show Hidden Items"); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 10)); jLabel = new JLabel(); jLabel.setBounds(new java.awt.Rectangle(555, 60, 106, 31)); jLabel.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 10)); jLabel.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING); jLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel.setText("Vertical Orientation"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getToobarBean1(), null); jContentPane.add(getMapBean1(), null); jContentPane.add(getJCheckBox(), null); jContentPane.add(getJCheckBox1(), null); jContentPane.add(getJCheckBox2(), null); jContentPane.add(jLabel, null); jContentPane.add(jLabel1, null); jContentPane.add(jLabel2, null); jContentPane.add(getJButton(), null); jContentPane.add(getJButton1(), null); } return jContentPane; } 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();} } }