/plugins/ProjectViewer/tags/pv_2_1_0_1/projectviewer/vpt/VPTSelectionListener.java

# · Java · 187 lines · 95 code · 30 blank · 62 comment · 31 complexity · 52c5d0f21257541aca36c1065fb2dcce MD5 · raw file

  1. /*
  2. * :tabSize=4:indentSize=4:noTabs=false:
  3. * :folding=explicit:collapseFolds=1:
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package projectviewer.vpt;
  20. //{{{ Imports
  21. import java.awt.event.MouseEvent;
  22. import java.awt.event.MouseListener;
  23. import javax.swing.tree.TreePath;
  24. import javax.swing.SwingUtilities;
  25. import javax.swing.event.TreeSelectionEvent;
  26. import javax.swing.event.TreeSelectionListener;
  27. import org.gjt.sp.jedit.jEdit;
  28. import org.gjt.sp.jedit.Buffer;
  29. import projectviewer.ProjectViewer;
  30. //}}}
  31. /**
  32. * Listens to the project JTree and responds to file selections.
  33. *
  34. * @author <A HREF="mailto:burton@relativity.yi.org">Kevin A. Burton</A>
  35. * @author Marcelo Vanzin
  36. * @version $Id: VPTSelectionListener.java 6334 2005-02-10 06:33:28Z vanza $
  37. */
  38. public final class VPTSelectionListener implements TreeSelectionListener, MouseListener {
  39. //{{{ Instance Variables
  40. private ProjectViewer viewer;
  41. private int lastClickButton;
  42. private long lastClickTime;
  43. private Object lastClickTarget;
  44. private boolean reportsClickCountCorrectly;
  45. //}}}
  46. //{{{ +VPTSelectionListener(ProjectViewer) : <init>
  47. /**
  48. * Create a new <code>ProjectTreeSelectionListener
  49. */
  50. public VPTSelectionListener(ProjectViewer aViewer) {
  51. viewer = aViewer;
  52. lastClickTime = 0L;
  53. reportsClickCountCorrectly = false;
  54. } //}}}
  55. //{{{ MouseListener interfaces
  56. //{{{ +mouseClicked(MouseEvent) : void
  57. /**
  58. * Determines when the user clicks on the JTree.
  59. *
  60. * @param evt Description of Parameter
  61. */
  62. public void mouseClicked(MouseEvent evt) {
  63. if (SwingUtilities.isMiddleMouseButton(evt)) {
  64. TreePath path = viewer.getCurrentTree()
  65. .getPathForLocation(evt.getX(), evt.getY());
  66. viewer.getCurrentTree().setSelectionPath(path);
  67. }
  68. VPTNode node = viewer.getSelectedNode();
  69. if (node == null) {
  70. return;
  71. }
  72. boolean doubleClick = isDoubleClick(evt);
  73. boolean middleClick = SwingUtilities.isMiddleMouseButton(evt);
  74. if (middleClick || doubleClick) {
  75. if(node.canOpen()) {
  76. boolean nodeOpen = node.isOpened();
  77. if(nodeOpen
  78. && ((doubleClick && jEdit.getBooleanProperty("vfs.browser.doubleClickClose"))
  79. || middleClick)) {
  80. node.close();
  81. ProjectViewer.nodeChanged(node);
  82. } else if (!nodeOpen) {
  83. node.open();
  84. ProjectViewer.nodeChanged(node);
  85. }
  86. }
  87. return;
  88. }
  89. TreePath path = viewer.getCurrentTree()
  90. .getPathForLocation(evt.getX(), evt.getY());
  91. if (path == null || (path.getLastPathComponent() != node)) {
  92. return;
  93. }
  94. if (SwingUtilities.isLeftMouseButton(evt) && node.isOpened()) {
  95. Buffer b = jEdit.getBuffer(node.getNodePath());
  96. if (b != null) {
  97. viewer.getView().setBuffer(b);
  98. }
  99. }
  100. } //}}}
  101. public void mousePressed(MouseEvent evt) { }
  102. public void mouseReleased(MouseEvent evt) { }
  103. public void mouseEntered(MouseEvent evt) { }
  104. public void mouseExited(MouseEvent evt) { }
  105. //}}}
  106. //{{{ TreeSelectionListener implementation
  107. /**
  108. * Receive notification that the tree selection has changed. Shows node
  109. * information on the status bar and, if the selected node is opened
  110. * but is not the current buffer, change the current buffer.
  111. *
  112. * <p>Also, if added node is a project, and it's not loaded, load it.
  113. * This allows easier keyboard navigation when using groups as the
  114. & tree root.</p>
  115. *
  116. * @param e The selection event.
  117. */
  118. public void valueChanged(TreeSelectionEvent e) {
  119. lastClickTarget = null;
  120. if (!e.isAddedPath()) return;
  121. viewer.setStatus(e.getPath().getLastPathComponent().toString());
  122. }
  123. //}}}
  124. //{{{ -isDoubleClick(MouseEvent) : boolean
  125. /**
  126. * Because IBM's JDK doesn't support <code>getClickCount()</code> for <code>JTree</code>
  127. * properly, we have to do this.
  128. *
  129. * @param evt Description of Parameter
  130. * @return The doubleClick value
  131. */
  132. private boolean isDoubleClick(MouseEvent evt) {
  133. if (reportsClickCountCorrectly)
  134. return evt.getClickCount() == 2;
  135. if (evt.getClickCount() == 2) {
  136. reportsClickCountCorrectly = true;
  137. return true;
  138. }
  139. TreePath path = viewer.getCurrentTree().getPathForLocation(evt.getX(), evt.getY());
  140. if(path == null) {
  141. lastClickTarget = null;
  142. return false;
  143. }
  144. Object target = path.getLastPathComponent();
  145. if(target == lastClickTarget &&
  146. target == viewer.getSelectedNode() &&
  147. lastClickButton == evt.getModifiers() &&
  148. (System.currentTimeMillis() - lastClickTime < 500L)) {
  149. lastClickTarget = null;
  150. return true;
  151. }
  152. lastClickButton = evt.getModifiers();
  153. lastClickTarget = target;
  154. lastClickTime = System.currentTimeMillis();
  155. return false;
  156. } //}}}
  157. }