/** * This class is used to handle mouse click events in the JLegend control. * The mouse click event handler looks to see if the mouse click is over the * check box of a tree node and updates the visibility of the corresponding * layer as appropriate. Note that the actual drawing of the check box and * displaying of its state is handled by {@link LegendTreeCellRenderer}. */ protected class LegendMouseListener extends MouseAdapter { int _hotspot = new JCheckBox().getPreferredSize().width; /* * (non-Javadoc) * * @see * java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent) */ @Override public void mousePressed(MouseEvent e) { TreePath selPath = _tree.getPathForLocation(e.getX(), e.getY()); if (selPath != null && SwingUtilities.isRightMouseButton(e)) { _tree.setSelectionPath(selPath); Object userObject = ((DefaultMutableTreeNode) selPath .getLastPathComponent()).getUserObject(); if (userObject instanceof Layer) { Layer layer = (Layer) userObject; JOptionPane.showMessageDialog(getParent(), "You right-clicked on Layer " + layer.getName()); } else if (userObject instanceof LayerInfo) { LayerInfo info = (LayerInfo) userObject; JOptionPane.showMessageDialog(getParent(), "You right-clicked on LayerInfo " + info.getName()); } else { // What else were you expecting? } } else { // Not a right-click } super.mousePressed(e); } /* * (non-Javadoc) * * @see * java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent) */ @Override public void mouseClicked(MouseEvent e) { TreePath path = _tree.getPathForLocation(e.getX(), e.getY()); if (path != null && e.getX() < _tree.getPathBounds(path).x + _hotspot) { Object userObject = ((DefaultMutableTreeNode) path .getLastPathComponent()).getUserObject(); if (userObject instanceof Layer) { Layer layer = (Layer) userObject; if (SwingUtilities.isRightMouseButton(e)) { // handle right-click on Layer JOptionPane .showMessageDialog( getParent(), "You right-clicked on Layer " + layer.getName()); } else { layer.setVisible(!layer.isVisible()); } } else if (userObject instanceof LayerInfo) { LayerInfo info = (LayerInfo) userObject; if (SwingUtilities.isRightMouseButton(e)) { // handle right-click on LayerInfo JOptionPane.showMessageDialog( getParent(), "You right-clicked on LayerInfo " + info.getName()); } else { info.setVisible(!info.isVisible()); } } _tree.repaint(); } } }
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.