1 19 20 package org.netbeans.modules.tasklist.usertasks.treetable; 21 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.MouseAdapter ; 24 import java.awt.event.MouseEvent ; 25 import java.beans.PropertyVetoException ; 26 import java.util.logging.Level ; 27 28 import javax.swing.Action ; 29 import javax.swing.JPopupMenu ; 30 import javax.swing.event.ListSelectionEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 33 import org.openide.awt.MouseUtils; 34 import org.openide.explorer.ExplorerManager; 35 import org.openide.nodes.AbstractNode; 36 import org.openide.nodes.Children; 37 import org.openide.nodes.Node; 38 import org.openide.util.Utilities; 39 40 43 public abstract class NodesTreeTable extends TreeTable { 44 47 private static class RootNode extends AbstractNode { 48 51 public RootNode() { 52 super(new Children.Array()); 53 } 54 } 55 56 private ExplorerManager em; 57 58 62 private RootNode rootNode; 63 64 65 public NodesTreeTable(ExplorerManager explorerManager, TreeTableModel ttm) { 66 super(ttm); 67 this.em = explorerManager; 68 this.rootNode = new RootNode(); 69 this.em.setRootContext(rootNode); 70 71 addMouseListener(new MouseUtils.PopupMouseAdapter() { 72 public void showPopup(MouseEvent e) { 73 int row = rowAtPoint(e.getPoint()); 74 int col = columnAtPoint(e.getPoint()); 75 Action [] actions; 76 if (row < 0 || col < 0) { 77 actions = getFreeSpaceActions(); 78 } else { 79 if (!getSelectionModel().isSelectedIndex(row)) { 80 setRowSelectionInterval(row, row); 81 } 82 Node n = createNode(getNodeForRow(row)); 83 if (n == null) 84 return; 85 86 actions = n.getActions(false); 87 } 88 JPopupMenu pm = Utilities.actionsToPopup(actions, 89 NodesTreeTable.this); 90 if(pm != null) 91 pm.show(NodesTreeTable.this, e.getX(), e.getY()); 92 } 93 }); 94 95 addMouseListener(new MouseAdapter () { 96 public void mouseClicked(MouseEvent e) { 97 if (!MouseUtils.isDoubleClick(e)) 98 return; 99 100 int row = rowAtPoint(e.getPoint()); 101 int col = columnAtPoint(e.getPoint()); 102 if (row < 0 || col < 0) 103 return; 104 105 setRowSelectionInterval(row, row); 106 Node n = createNode(getNodeForRow(row)); 107 if (n == null) 108 return; 109 110 Action action = n.getPreferredAction(); 111 if (action != null) { 112 action.actionPerformed( 113 new ActionEvent (this, ActionEvent.ACTION_PERFORMED, 114 null)); 115 } 116 } 117 }); 118 119 getSelectionModel().addListSelectionListener( 120 new ListSelectionListener () { 121 public void valueChanged(ListSelectionEvent e) { 122 int[] rows = getSelectedRows(); 123 Node[] nodes = new Node[rows.length]; 124 for (int i = 0; i < nodes.length; i++) { 125 nodes[i] = createNode(getNodeForRow(rows[i])); 126 } 127 128 Children.Array ch = (Children.Array) rootNode.getChildren(); 129 ch.remove(ch.getNodes()); 130 ch.add(nodes); 131 try { 132 if (nodes.length > 0) { 133 em.setExploredContext(nodes[0], nodes); 134 } else { 135 em.setSelectedNodes(nodes); 136 } 137 } catch (PropertyVetoException ex) { 138 UTUtils.LOGGER.log(Level.WARNING, "", e); } 140 } 141 } 142 ); 143 } 144 145 151 public abstract Node createNode(Object obj); 152 153 158 public Action [] getFreeSpaceActions() { 159 return new Action [0]; 160 } 161 } 162 | Popular Tags |