PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/Java14.java

#
Java | 189 lines | 130 code | 16 blank | 43 comment | 29 complexity | 2bda261d4f126bf8928ed5e97c5d9558 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * Java14.java - Java 2 version 1.4 API calls
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001, 2002 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;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.datatransfer.*;
  26. import java.awt.event.*;
  27. import java.awt.*;
  28. import org.gjt.sp.jedit.msg.*;
  29. import org.gjt.sp.jedit.textarea.JEditTextArea;
  30. import org.gjt.sp.jedit.EditBus;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. /**
  34. * This file must be compiled with a JDK 1.4 or higher javac. If you are using
  35. * an older Java version and wish to compile from source, you can safely leave
  36. * this file out.
  37. * @since jEdit 4.0pre4
  38. * @author Slava Pestov
  39. * @version $Id: Java14.java 4568 2003-03-23 18:44:35Z spestov $
  40. */
  41. class Java14
  42. {
  43. //{{{ init() method
  44. public static void init()
  45. {
  46. JFrame.setDefaultLookAndFeelDecorated(
  47. jEdit.getBooleanProperty("decorate.frames"));
  48. JDialog.setDefaultLookAndFeelDecorated(
  49. jEdit.getBooleanProperty("decorate.dialogs"));
  50. KeyboardFocusManager.setCurrentKeyboardFocusManager(
  51. new MyFocusManager());
  52. EditBus.addToBus(new EBComponent()
  53. {
  54. public void handleMessage(EBMessage msg)
  55. {
  56. if(msg instanceof ViewUpdate)
  57. {
  58. ViewUpdate vu = (ViewUpdate)msg;
  59. if(vu.getWhat() == ViewUpdate.CREATED)
  60. {
  61. vu.getView().setFocusTraversalPolicy(
  62. new MyFocusTraversalPolicy());
  63. }
  64. }
  65. else if(msg instanceof EditPaneUpdate)
  66. {
  67. EditPaneUpdate eu = (EditPaneUpdate)msg;
  68. if(eu.getWhat() == EditPaneUpdate.CREATED)
  69. {
  70. eu.getEditPane().getTextArea()
  71. .addMouseWheelListener(
  72. new MouseWheelHandler());
  73. }
  74. }
  75. }
  76. });
  77. Clipboard selection = Toolkit.getDefaultToolkit().getSystemSelection();
  78. if(selection != null)
  79. {
  80. Log.log(Log.DEBUG,Java14.class,"Setting % register"
  81. + " to system selection");
  82. Registers.setRegister('%',new Registers.ClipboardRegister(selection));
  83. }
  84. } //}}}
  85. //{{{ MyFocusManager class
  86. static class MyFocusManager extends DefaultKeyboardFocusManager
  87. {
  88. MyFocusManager()
  89. {
  90. setDefaultFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
  91. }
  92. public boolean postProcessKeyEvent(KeyEvent evt)
  93. {
  94. if(!evt.isConsumed())
  95. {
  96. Component comp = (Component)evt.getSource();
  97. if(!comp.isShowing())
  98. return true;
  99. for(;;)
  100. {
  101. if(comp instanceof View)
  102. {
  103. ((View)comp).processKeyEvent(evt);
  104. return true;
  105. }
  106. else if(comp == null || comp instanceof Window
  107. || comp instanceof JEditTextArea)
  108. {
  109. break;
  110. }
  111. else
  112. comp = comp.getParent();
  113. }
  114. }
  115. return super.postProcessKeyEvent(evt);
  116. }
  117. } //}}}
  118. //{{{ MyFocusTraversalPolicy class
  119. static class MyFocusTraversalPolicy extends LayoutFocusTraversalPolicy
  120. {
  121. public Component getDefaultComponent(Container focusCycleRoot)
  122. {
  123. return GUIUtilities.getView(focusCycleRoot).getTextArea();
  124. }
  125. } //}}}
  126. //{{{ WheelScrollListener class
  127. static class MouseWheelHandler implements MouseWheelListener
  128. {
  129. public void mouseWheelMoved(MouseWheelEvent e)
  130. {
  131. JEditTextArea textArea = (JEditTextArea)e.getSource();
  132. /****************************************************
  133. * move caret depending on pressed control-keys:
  134. * - Alt: move cursor, do not select
  135. * - Alt+(shift or control): move cursor, select
  136. * - shift: scroll page
  137. * - control: scroll single line
  138. * - <else>: scroll 3 lines
  139. ****************************************************/
  140. if(e.isAltDown())
  141. {
  142. moveCaret(textArea,e.getWheelRotation(),
  143. e.isShiftDown() || e.isControlDown());
  144. }
  145. else if(e.isShiftDown())
  146. scrollPage(textArea,e.getWheelRotation());
  147. else if(e.isControlDown())
  148. scrollLine(textArea,e.getWheelRotation());
  149. else if(e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL)
  150. scrollLine(textArea,e.getUnitsToScroll());
  151. else
  152. scrollLine(textArea,3 * e.getWheelRotation());
  153. }
  154. private void scrollLine(JEditTextArea textArea, int amt)
  155. {
  156. textArea.setFirstLine(textArea.getFirstLine() + amt);
  157. }
  158. private void scrollPage(JEditTextArea textArea, int amt)
  159. {
  160. if(amt > 0)
  161. textArea.scrollDownPage();
  162. else
  163. textArea.scrollUpPage();
  164. }
  165. private void moveCaret(JEditTextArea textArea, int amt, boolean select)
  166. {
  167. if (amt < 0)
  168. textArea.goToPrevLine(select);
  169. else
  170. textArea.goToNextLine(select);
  171. }
  172. } //}}}
  173. }