001/* 002 * Copyright (c) 2005 Einar Pehrson, Malin Johansson and Sofia Nilsson 003 * 004 * This file is part of 005 * CleanSheets Extension for Dependency Trees 006 * 007 * CleanSheets Extension for Dependency Trees is free software; you can 008 * redistribute it and/or modify it under the terms of the GNU General Public 009 * License as published by the Free Software Foundation; either version 2 of 010 * the License, or (at your option) any later version. 011 * 012 * CleanSheets Extension for Dependency Trees is distributed in the hope that 013 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied 014 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 015 * See the GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets Extension for Dependency Trees; if not, write to the 019 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307 USA 021 */ 022package csheets.ext.deptree; 023 024import java.awt.event.MouseAdapter; 025import java.awt.event.MouseEvent; 026 027import javax.swing.JTree; 028import javax.swing.SwingUtilities; 029import javax.swing.tree.TreeNode; 030import javax.swing.tree.TreePath; 031 032import csheets.ui.ctrl.UIController; 033 034/** 035 * A mouse listener that updates the active cell of a spreadsheet when a 036 * cell node is double-clicked. 037 * @author Einar Pehrson 038 */ 039public class DoubleClickNavigator extends MouseAdapter { 040 041 /** The tree to use when determining the node at a given location */ 042 private JTree tree; 043 044 /** The user interface controller */ 045 private UIController uiController; 046 047 /** 048 * Creates a new double click navigator. 049 * @param tree the tree to use when determining the node at a given location 050 * @param uiController the user interface controller 051 */ 052 public DoubleClickNavigator(JTree tree, UIController uiController) { 053 // Stores members 054 this.tree = tree; 055 this.uiController = uiController; 056 } 057 058 /** 059 * Sets the cell of a double-clicked node as the application's active cell. 060 * @param event the event that was fired 061 */ 062 public void mouseClicked(MouseEvent event) { 063 if (SwingUtilities.isLeftMouseButton(event) 064 && event.getClickCount() > 1) { 065 // Fetches the path that was double-clicked 066 int row = tree.getRowForLocation(event.getX(), event.getY()); 067 TreePath path = tree.getPathForRow(row); 068 if (path != null) { 069 TreeNode lastNode = (TreeNode)path.getLastPathComponent(); 070 if (lastNode instanceof CellNode) { 071 // Sets the last node of the path as the active cell 072 CellNode node = (CellNode)lastNode; 073 uiController.setActiveCell(node.getCell()); 074 } 075 } 076 } 077 } 078}