arcgissamples\scenario\map\IdentifyDialog.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.scenario.map; /** * Dialog to display identify results. * */ import java.util.TreeMap; import javax.swing.JComboBox; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.JTable; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreePath; public class IdentifyDialog extends JDialog { private static final long serialVersionUID = 1L; // Only one dialog is displayed at any time // private static IdentifyDialog identifyDialog = null; // private MapInfo mapInfo; @SuppressWarnings("unused") private MapComponent _mapComponent; private JPanel layerDropdownPanel = new JPanel(); public final static String TOPMOSTLAYER = "< Top- most layer >"; public final static String VISIBLELAYER = "Visible layers"; public final static String ALLLAYERS = "All layers"; private JComboBox visibilityCombo = new JComboBox( new Object[] { TOPMOSTLAYER, VISIBLELAYER, ALLLAYERS } ); private JLabel layerName = new JLabel("Layers : "); // layers private JSplitPane splitPane; private static JTree layerTree; DefaultMutableTreeNode root = new DefaultMutableTreeNode(); private static JTable dataTable; // private Object[] dataTableColumnNames = {"Field", "Value"}; private DataTableModel dataTableModel = new DataTableModel(); // private DefaultTableModel model = new DefaultTableModel(); private TreeMap resultData = null; public IdentifyDialog(JFrame owner) { super(owner, "Identify"); jbInit(); } /** Constructs identify dialog as per the following diagram: * * * |----------------------------------------------------------------------| * | | * | Layers JCombobox | * | | * |----------------------------------------------------------------------| * | | | * | |<Layers1> | Field | Value | * | | | | | | * | | |<Field name1> | | | * | | |<Field name2> | | | * | | | ... | | | * | | | | | * | |--<layers 2> | | | * | | |---<field name1> | | | * | | ... | | | * | |--<layers 3> | | | * | ... | | | * | | | | * | | | | * | | | * | | | * |----------------------------------------------------------------------| */ public void jbInit() { layerTree = new JTree(root); layerTree.setRootVisible(false); layerTree.setShowsRootHandles(false); layerTree.getSelectionModel().setSelectionMode( javax.swing.tree.TreeSelectionModel.SINGLE_TREE_SELECTION ); DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer(); renderer.setIcon(null); renderer.setLeafIcon(null); renderer.setOpenIcon(null); renderer.setClosedIcon(null); layerTree.setCellRenderer(renderer); layerTree.addMouseListener(new LayerTreeListener()); dataTable = new JTable(dataTableModel); dataTable.setCellSelectionEnabled(false); dataTable.setColumnSelectionAllowed(false); dataTable.setShowGrid(false); JScrollPane treeScrollPane = new JScrollPane(); treeScrollPane.setBorder(null); treeScrollPane.getViewport().add(layerTree); JScrollPane tableScrollPane = new JScrollPane(); tableScrollPane.setBorder(null); tableScrollPane.getViewport().add(dataTable); splitPane = new javax.swing.JSplitPane(javax.swing.JSplitPane.HORIZONTAL_SPLIT); splitPane.setLeftComponent(treeScrollPane); splitPane.setDividerLocation(150); splitPane.setRightComponent(tableScrollPane); layerDropdownPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT)); layerDropdownPanel.add(layerName); layerDropdownPanel.add(visibilityCombo); getContentPane().add(layerDropdownPanel, java.awt.BorderLayout.NORTH); getContentPane().add(splitPane, java.awt.BorderLayout.CENTER); setSize(450, 275); } public void setMapComponentByRef(MapComponent mc) { _mapComponent = mc; } /**Method which takes java.util.TreeMap * containing data in the following form: * <Layer 1> <field1> * <keys> <values> * <field2> * <keys> <values> * .... * * <layer 2> <field1> * <keys> <values> * <field2> * <keys> <values> * .... * * @param tm */ public void popluateIdentifyResults(java.util.TreeMap tm) { try { //Remove all nodes root.removeAllChildren(); //Clear table data dataTableModel.clearData(); if (resultData != null) resultData.clear(); //table.clear(); if (tm == null || tm.isEmpty()) { ( (DefaultTreeModel) layerTree.getModel()).nodeStructureChanged(root); return; } java.util.Iterator it = tm.keySet().iterator(); if (it == null) return; resultData = tm; while (it.hasNext()) { // Add first layer node String layerName = (String) it.next(); DefaultMutableTreeNode layerNode = new DefaultMutableTreeNode(layerName, true); root.add(layerNode); //Get layer data IdentifyInfo identifyData = (IdentifyInfo) tm.get(layerName); if (identifyData == null) continue; //Get Field values java.util.TreeMap map = identifyData.getLayerData(); java.util.Iterator mapit = map.keySet().iterator(); while (mapit.hasNext()) { Object idFieldValue = (Object) mapit.next(); DefaultMutableTreeNode idNode = new DefaultMutableTreeNode( idFieldValue, true); layerNode.add(idNode); } } //Display the first layers data in the table. IdentifyInfo data = (IdentifyInfo) tm.get(tm.firstKey()); dataTableModel.setTableData(data.getFirstData().keys, data.getFirstData().values); layerTree.setShowsRootHandles(true); //Fire event to indicate the tree data is changed ( (DefaultTreeModel) layerTree.getModel()).nodeStructureChanged(root); TreePath path = layerTree.getPathForRow(0); layerTree.expandPath(path); layerTree.setSelectionInterval(1, 1); } catch(Exception ex) { ( (DefaultTreeModel) layerTree.getModel()).nodeStructureChanged(root); } } /**Gets the dropdwon value. * @return */ public String getDropDownValue() { return (String)visibilityCombo.getSelectedItem(); } public void setDropDownValues(java.util.Vector v) { javax.swing.DefaultComboBoxModel model = ((javax.swing.DefaultComboBoxModel)visibilityCombo.getModel()); model.removeAllElements(); for(int i =0 ; i < v.size(); i++) { String s = (String)v.get(i); model.addElement(s); } } public class LayerTreeListener extends java.awt.event.MouseAdapter { public void mouseClicked(java.awt.event.MouseEvent e) { TreePath path = layerTree.getPathForLocation(e.getX(), e.getY()); if(path == null) return; dataTableModel.clearData(); if(path.getPathCount() > 2) { String layerName = path.getPathComponent(1).toString(); Object idField = path.getLastPathComponent(); //Get the identify data for the layer IdentifyInfo identifyData = (IdentifyInfo) resultData.get(layerName); //Get the selected field data IdentifyInfo.LayerData layerData = identifyData.getIdFieldData(idField); //Populate field and values data in the table dataTableModel.setTableData(layerData.keys, layerData.values); } } } /** * Class which extends AbstractTableModel * @see javax.swing.table.AbstractTableModel */ public class DataTableModel extends javax.swing.table.AbstractTableModel { private static final long serialVersionUID = 1L; public DataTableModel() { } public Object getValueAt(int rowIndex, int columnIndex) { if(columnIndex == 0) return fields[rowIndex]; else return values[rowIndex]; } public void setValueAt(Object aValue, int rowIndex, int columnIndex) { } public int getColumnCount() { return 2; } public int getRowCount() { if(fields != null) return fields.length; else return 0; } public String getColumnName(int c) { return _columnNames[c].toString(); } public void clearData() { if(fields != null && values != null) { fields = null; values = null; } fireTableDataChanged(); } public void setTableData(String [] flds, Object [] vals) { fields = flds; values = vals; fireTableDataChanged(); } public boolean isCellEditable(int row, int col) { return false; } private String[] fields = null; private Object[] values = null; private Object[] _columnNames = {"Field", "Value"}; //l1n } }