/jEdit/tags/jedit-4-3-pre16/org/gjt/sp/jedit/textarea/TextAreaDropHandler.java

# · Java · 100 lines · 57 code · 8 blank · 35 comment · 5 complexity · 4c184334260ecf29c3724ee40c4878f8 MD5 · raw file

  1. /*
  2. * TextAreaTransferHandler.java - Drag and drop support
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2004 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.textarea;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.dnd.*;
  26. import java.awt.*;
  27. import org.gjt.sp.jedit.buffer.JEditBuffer;
  28. import org.gjt.sp.util.Log;
  29. //}}}
  30. /**
  31. * @author Slava Pestov
  32. * @version $Id: TextAreaDropHandler.java 13083 2008-07-22 08:05:52Z kpouer $
  33. */
  34. class TextAreaDropHandler extends DropTargetAdapter
  35. {
  36. private final TextArea textArea;
  37. private JEditBuffer savedBuffer;
  38. private int savedCaret;
  39. //{{{ TextAreaDropHandler constructor
  40. TextAreaDropHandler(TextArea textArea)
  41. {
  42. this.textArea = textArea;
  43. } //}}}
  44. //{{{ dragEnter() method
  45. @Override
  46. public void dragEnter(DropTargetDragEvent dtde)
  47. {
  48. Log.log(Log.DEBUG,this,"Drag enter");
  49. savedBuffer = textArea.getBuffer();
  50. textArea.setDragInProgress(true);
  51. //textArea.getBuffer().beginCompoundEdit();
  52. savedCaret = textArea.getCaretPosition();
  53. } //}}}
  54. //{{{ dragOver() method
  55. @Override
  56. public void dragOver(DropTargetDragEvent dtde)
  57. {
  58. Point p = dtde.getLocation();
  59. p = SwingUtilities.convertPoint(textArea,p,
  60. textArea.getPainter());
  61. int pos = textArea.xyToOffset(p.x,p.y,
  62. !(textArea.getPainter().isBlockCaretEnabled()
  63. || textArea.isOverwriteEnabled()));
  64. if(pos != -1)
  65. {
  66. textArea.moveCaretPosition(pos,
  67. TextArea.ELECTRIC_SCROLL);
  68. }
  69. } //}}}
  70. //{{{ dragExit() method
  71. @Override
  72. public void dragExit(DropTargetEvent dtde)
  73. {
  74. Log.log(Log.DEBUG,this,"Drag exit");
  75. textArea.setDragInProgress(false);
  76. //textArea.getBuffer().endCompoundEdit();
  77. if(textArea.getBuffer() == savedBuffer)
  78. {
  79. textArea.moveCaretPosition(savedCaret,
  80. TextArea.ELECTRIC_SCROLL);
  81. }
  82. savedBuffer = null;
  83. } //}}}
  84. //{{{ drop() method
  85. public void drop(DropTargetDropEvent dtde)
  86. {
  87. Log.log(Log.DEBUG,this,"Drop");
  88. textArea.setDragInProgress(false);
  89. //textArea.getBuffer().endCompoundEdit();
  90. savedBuffer = null;
  91. } //}}}
  92. }