/uClinux-dist/lib/classpath/examples/gnu/classpath/examples/swing/NavigationFilterDemo.java

https://bitbucket.org/__wp__/mb-linux-msli · Java · 205 lines · 121 code · 27 blank · 57 comment · 10 complexity · f8bb08df19a7e57f6f19f9b7252cfb60 MD5 · raw file

  1. /* NavigationFilterDemo.java -- An example for the NavigationFilter class.
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath examples.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. */
  17. package gnu.classpath.examples.swing;
  18. import java.awt.BorderLayout;
  19. import java.awt.Point;
  20. import java.awt.event.ActionEvent;
  21. import java.awt.event.ActionListener;
  22. import javax.swing.JButton;
  23. import javax.swing.JComponent;
  24. import javax.swing.JFrame;
  25. import javax.swing.JPanel;
  26. import javax.swing.JTextArea;
  27. import javax.swing.SwingConstants;
  28. import javax.swing.SwingUtilities;
  29. import javax.swing.text.BadLocationException;
  30. import javax.swing.text.JTextComponent;
  31. import javax.swing.text.NavigationFilter;
  32. import javax.swing.text.Position;
  33. import javax.swing.text.Utilities;
  34. /**
  35. * A demonstration of the <code>javax.swing.text.NavigationFilter</code> class.
  36. *
  37. * <p>It shows a NavigationFilter which lets you walk word-wise
  38. * through a text.</p>
  39. *
  40. * @author Robert Schuster
  41. */
  42. public class NavigationFilterDemo
  43. extends JPanel
  44. implements ActionListener
  45. {
  46. JTextArea textArea;
  47. /**
  48. * Creates a new demo instance.
  49. */
  50. public NavigationFilterDemo()
  51. {
  52. createContent();
  53. // initFrameContent() is only called (from main) when running this app
  54. // standalone
  55. }
  56. /**
  57. * When the demo is run independently, the frame is displayed, so we should
  58. * initialise the content panel (including the demo content and a close
  59. * button). But when the demo is run as part of the Swing activity board,
  60. * only the demo content panel is used, the frame itself is never displayed,
  61. * so we can avoid this step.
  62. */
  63. void initFrameContent()
  64. {
  65. JPanel closePanel = new JPanel();
  66. JButton closeButton = new JButton("Close");
  67. closeButton.setActionCommand("CLOSE");
  68. closeButton.addActionListener(this);
  69. closePanel.add(closeButton);
  70. add(closePanel, BorderLayout.SOUTH);
  71. }
  72. private void createContent()
  73. {
  74. setLayout(new BorderLayout());
  75. add(textArea = new JTextArea(10, 20));
  76. textArea.setWrapStyleWord(true);
  77. textArea.setLineWrap(true);
  78. textArea.setNavigationFilter(new WordFilter());
  79. textArea.setText("GNU Classpath, Essential Libraries for Java, " +
  80. "is a GNU project to create free core class " +
  81. "libraries for use with virtual machines and " +
  82. "compilers for the java programming language.");
  83. }
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. if (e.getActionCommand().equals("CLOSE"))
  87. System.exit(0);
  88. }
  89. public static void main(String[] args)
  90. {
  91. SwingUtilities.invokeLater
  92. (new Runnable()
  93. {
  94. public void run()
  95. {
  96. NavigationFilterDemo app = new NavigationFilterDemo();
  97. app.initFrameContent();
  98. JFrame frame = new JFrame("NavigationFilterDemo");
  99. frame.getContentPane().add(app);
  100. frame.pack();
  101. frame.setVisible(true);
  102. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  103. }
  104. });
  105. }
  106. /**
  107. * Returns a DemoFactory that creates a NavigationFilterDemo.
  108. *
  109. * @return a DemoFactory that creates a NavigationFilterDemo
  110. */
  111. public static DemoFactory createDemoFactory()
  112. {
  113. return new DemoFactory()
  114. {
  115. public JComponent createDemo()
  116. {
  117. return new NavigationFilterDemo();
  118. }
  119. };
  120. }
  121. class WordFilter extends NavigationFilter
  122. {
  123. public int getNextVisualPositionFrom(JTextComponent text,
  124. int pos,
  125. Position.Bias bias,
  126. int direction,
  127. Position.Bias[] biasRet)
  128. throws BadLocationException
  129. {
  130. Point pt;
  131. int newpos = pos;
  132. switch (direction)
  133. {
  134. case SwingConstants.NORTH:
  135. // Find out where the caret want to be positioned ideally.
  136. pt = text.getCaret().getMagicCaretPosition();
  137. // Calculate its position above.
  138. newpos = Utilities.getPositionAbove(text, pos, (pt != null) ? pt.x : 0);
  139. // If we have a valid position, then calculate the next word start
  140. // from there.
  141. if (newpos != -1)
  142. return Utilities.getWordStart(text, newpos);
  143. else
  144. return pos;
  145. case SwingConstants.SOUTH:
  146. // Find out where the caret want to be positioned ideally.
  147. pt = text.getCaret().getMagicCaretPosition();
  148. // Calculate its position below.
  149. newpos = Utilities.getPositionBelow(text, pos, (pt != null) ? pt.x : 0);
  150. // If we have a valid position, then calculate the next word start
  151. // from there.
  152. if (newpos != -1)
  153. return Utilities.getWordStart(text, newpos);
  154. else
  155. return pos;
  156. case SwingConstants.WEST:
  157. // Calculate the next word start.
  158. newpos = Utilities.getWordStart(text, newpos);
  159. // If that means that the caret will not move, return
  160. // the start of the previous word.
  161. if (newpos != pos)
  162. return newpos;
  163. else
  164. return Utilities.getPreviousWord(text, newpos);
  165. case SwingConstants.EAST:
  166. return Utilities.getNextWord(text, newpos);
  167. default:
  168. // Do whatever the super implementation did.
  169. return super.getNextVisualPositionFrom(text, pos, bias,
  170. direction, biasRet);
  171. }
  172. }
  173. }
  174. }