arcgissamples\editing\SnappingDialog.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.editing; import java.awt.Dimension; import java.awt.Frame; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; import com.esri.arcgis.controls.EngineEditor; import com.esri.arcgis.controls.esriEngineSnapToleranceUnits; public class SnappingDialog extends JDialog implements ActionListener, KeyListener { private static final long serialVersionUID = 1L; //A custom model for the JTable that represents the snap agents private SnappingTableModel snappingModel; private EngineEditor editor; public SnappingDialog(Frame mainWindow, String title) { super(mainWindow, title); try { this.editor = new EngineEditor(); //Set our custom model for the JTable. this.snappingModel = new SnappingTableModel(); getTblSnapAgents().setModel(this.snappingModel); } catch (Exception e) { e.printStackTrace(); } //build the user interface initializeUI(); //Only Dispose the window on close, don't exit the application this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); } //Handle user actions public void actionPerformed(ActionEvent e) { try { if (e.getSource() == chkboxSnapTips) { //Show the snap tips editor.setSnapTips(chkboxSnapTips.isSelected()); }else if(e.getSource() == cmboSnapTolerance){ //Set the Snapping tolerance units. if(cmboSnapTolerance.getSelectedItem().equals("Pixels")) editor.setSnapToleranceUnits(esriEngineSnapToleranceUnits.esriEngineSnapTolerancePixels); else editor.setSnapToleranceUnits(esriEngineSnapToleranceUnits.esriEngineSnapToleranceMapUnits); }else if(e.getSource() == btnClearAgents){ //Remove all snapping agents this.snappingModel.removeAll(); }else if(e.getSource() == btnFeatureSnap){ //Add feature snap agents this.snappingModel.addFeatureSnapAgents(); }else if(e.getSource() == btnSketchSnap){ //Add sketch snap agents this.snappingModel.addSketchSnapAgents(); }else if(e.getSource() == btnReverseAgents){ //Reverse the priority of snap agents this.snappingModel.reverseSnapAgens(); }else if(e.getSource() == btnTurnOffAgents){ //Turn off snap agents this.snappingModel.turnOffAgents(); } } catch (Exception ex) { ex.printStackTrace(); } } //Respond to changes in snapping tolerance public void keyTyped(KeyEvent e) { //Validate the tolerance is a number //e.getKeyChar() < e.VK_0 || e.getKeyChar() > e.VK_9 if(e.getKeyChar()< KeyEvent.VK_0|| e.getKeyChar() > KeyEvent.VK_9){ JOptionPane.showMessageDialog(this, "Please enter only digits","Error",JOptionPane.ERROR_MESSAGE); //Consume the event e.consume(); }else{ try { //Set the snapping tolerance editor.setSnapTolerance(Double.parseDouble(this.txtSnapTolerance.getText())); } catch (Exception e1) { e1.printStackTrace(); } } } public void keyPressed(KeyEvent e) { /*Dont care*/ } public void keyReleased(KeyEvent e) { /*Dont care*/ } private void initializeUI() { this.setSize(new Dimension(591, 225)); this.setMinimumSize(new Dimension(591, 200)); this.setContentPane(getMainPanel()); this.setTitle("Snapping Settings"); } private JPanel getMainPanel() { if (mainPanel == null) { GridBagConstraints gridBagConstraints4 = new GridBagConstraints(); gridBagConstraints4.gridy = 3; gridBagConstraints4.gridwidth = 3; gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL; gridBagConstraints4.insets = new Insets(5, 5, 5, 0); gridBagConstraints4.gridx = 0; GridBagConstraints gridBagConstraints3 = new GridBagConstraints(); gridBagConstraints3.gridx = 3; gridBagConstraints3.fill = GridBagConstraints.NONE; gridBagConstraints3.insets = new Insets(5, 5, 0, 5); gridBagConstraints3.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints3.gridy = 2; GridBagConstraints gridBagConstraints21 = new GridBagConstraints(); gridBagConstraints21.fill = GridBagConstraints.BOTH; gridBagConstraints21.gridy = 2; gridBagConstraints21.weightx = 1.0; gridBagConstraints21.weighty = 1.0; gridBagConstraints21.gridwidth = 3; gridBagConstraints21.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints21.insets = new Insets(5, 5, 0, 0); gridBagConstraints21.gridx = 0; GridBagConstraints gridBagConstraints11 = new GridBagConstraints(); gridBagConstraints11.fill = GridBagConstraints.NONE; gridBagConstraints11.gridy = 1; gridBagConstraints11.weightx = 1.0; gridBagConstraints11.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints11.insets = new Insets(5, 5, 0, 0); gridBagConstraints11.gridx = 2; GridBagConstraints gridBagConstraints2 = new GridBagConstraints(); gridBagConstraints2.fill = GridBagConstraints.NONE; gridBagConstraints2.gridy = 1; gridBagConstraints2.weightx = 0.0D; gridBagConstraints2.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints2.insets = new Insets(5, 5, 0, 0); gridBagConstraints2.gridx = 1; GridBagConstraints gridBagConstraints1 = new GridBagConstraints(); gridBagConstraints1.gridx = 0; gridBagConstraints1.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints1.insets = new Insets(5, 5, 0, 0); gridBagConstraints1.gridy = 1; lblSnapTolerance = new JLabel(); lblSnapTolerance.setText("Snap Tolerance"); GridBagConstraints gridBagConstraints = new GridBagConstraints(); gridBagConstraints.gridx = 0; gridBagConstraints.anchor = GridBagConstraints.NORTHWEST; gridBagConstraints.insets = new Insets(0, 5, 0, 0); gridBagConstraints.gridy = 0; mainPanel = new JPanel(); mainPanel.setLayout(new GridBagLayout()); mainPanel.add(getChkboxSnapTips(), gridBagConstraints); mainPanel.add(lblSnapTolerance, gridBagConstraints1); mainPanel.add(getTxtSnapTolerance(), gridBagConstraints2); mainPanel.add(getCmboSnapTolerance(), gridBagConstraints11); mainPanel.add(getTblscrollPane(), gridBagConstraints21); mainPanel.add(getSnapAgentPanel(), gridBagConstraints3); mainPanel.add(getPnlActions(), gridBagConstraints4); } return mainPanel; } private JCheckBox getChkboxSnapTips() { if (chkboxSnapTips == null) { chkboxSnapTips = new JCheckBox(); chkboxSnapTips.setText("Snap tips"); chkboxSnapTips.addActionListener(this); } return chkboxSnapTips; } private JTextField getTxtSnapTolerance() { if (txtSnapTolerance == null) { txtSnapTolerance = new JTextField(); txtSnapTolerance.setText("7"); txtSnapTolerance.setPreferredSize(new Dimension(40, 20)); txtSnapTolerance.setMinimumSize(new Dimension(40, 20)); txtSnapTolerance.addKeyListener(this); } return txtSnapTolerance; } private JComboBox getCmboSnapTolerance() { if (cmboSnapTolerance == null) { cmboSnapTolerance = new JComboBox(); cmboSnapTolerance.setPreferredSize(new Dimension(80, 20)); cmboSnapTolerance.setMinimumSize(new Dimension(120, 20)); cmboSnapTolerance.addItem("Pixels"); cmboSnapTolerance.addItem("Map Units"); try{ if(this.editor.getSnapToleranceUnits()==esriEngineSnapToleranceUnits.esriEngineSnapTolerancePixels) cmboSnapTolerance.setSelectedIndex(0); else cmboSnapTolerance.setSelectedIndex(1); }catch(Exception e){ e.printStackTrace(); } cmboSnapTolerance.addActionListener(this); } return cmboSnapTolerance; } private JScrollPane getTblscrollPane() { if (tblscrollPane == null) { tblscrollPane = new JScrollPane(); tblscrollPane.setViewportView(getTblSnapAgents()); } return tblscrollPane; } private JTable getTblSnapAgents() { if (tblSnapAgents == null) { tblSnapAgents = new JTable(); tblSnapAgents.setShowGrid(true); } return tblSnapAgents; } private JPanel getSnapAgentPanel() { if (snapAgentPanel == null) { GridLayout gridLayout = new GridLayout(); gridLayout.setRows(2); gridLayout.setVgap(5); gridLayout.setColumns(1); snapAgentPanel = new JPanel(); snapAgentPanel.setLayout(gridLayout); snapAgentPanel.add(getBtnFeatureSnap(), null); snapAgentPanel.add(getBtnSketchSnap(), null); } return snapAgentPanel; } private JButton getBtnFeatureSnap() { if (btnFeatureSnap == null) { btnFeatureSnap = new JButton(); btnFeatureSnap.setText("Add Feature Snap Agent"); btnFeatureSnap.addActionListener(this); } return btnFeatureSnap; } private JButton getBtnSketchSnap() { if (btnSketchSnap == null) { btnSketchSnap = new JButton(); btnSketchSnap.setText("Add Sketch Snap Agent"); btnSketchSnap.setEnabled(false); btnSketchSnap.addActionListener(this); } return btnSketchSnap; } private JPanel getPnlActions() { if (pnlActions == null) { GridLayout gridLayout1 = new GridLayout(); gridLayout1.setRows(1); gridLayout1.setHgap(5); gridLayout1.setColumns(3); pnlActions = new JPanel(); pnlActions.setLayout(gridLayout1); pnlActions.add(getBtnClearAgents(), null); pnlActions.add(getBtnTurnOffAgents(), null); pnlActions.add(getBtnReverseAgents(), null); } return pnlActions; } private JButton getBtnClearAgents() { if (btnClearAgents == null) { btnClearAgents = new JButton(); btnClearAgents.setText("Clear Agents"); btnClearAgents.addActionListener(this); } return btnClearAgents; } private JButton getBtnTurnOffAgents() { if (btnTurnOffAgents == null) { btnTurnOffAgents = new JButton(); btnTurnOffAgents.setText("Turn Off Agents"); btnTurnOffAgents.addActionListener(this); } return btnTurnOffAgents; } private JButton getBtnReverseAgents() { if (btnReverseAgents == null) { btnReverseAgents = new JButton(); btnReverseAgents.setText("Reverse Agents"); btnReverseAgents.addActionListener(this); } return btnReverseAgents; } public static void main(String args[]) { new SnappingDialog(null, "").setVisible(true); } private JPanel mainPanel = null; private JCheckBox chkboxSnapTips = null; private JLabel lblSnapTolerance = null; private JTextField txtSnapTolerance = null; private JComboBox cmboSnapTolerance = null; private JScrollPane tblscrollPane = null; private JTable tblSnapAgents = null; private JPanel snapAgentPanel = null; private JButton btnFeatureSnap = null; private JButton btnSketchSnap = null; private JPanel pnlActions = null; private JButton btnClearAgents = null; private JButton btnTurnOffAgents = null; private JButton btnReverseAgents = null; }