1 /* NavigationFilterDemo.java -- An example for the NavigationFilter class.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath examples.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 package gnu.classpath.examples.swing;
25 import java.awt.BorderLayout;
26 import java.awt.Point;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
30 import javax.swing.JButton;
31 import javax.swing.JComponent;
32 import javax.swing.JFrame;
33 import javax.swing.JPanel;
34 import javax.swing.JTextArea;
35 import javax.swing.SwingConstants;
36 import javax.swing.SwingUtilities;
37 import javax.swing.text.BadLocationException;
38 import javax.swing.text.JTextComponent;
39 import javax.swing.text.NavigationFilter;
40 import javax.swing.text.Position;
41 import javax.swing.text.Utilities;
44 * A demonstration of the <code>javax.swing.text.NavigationFilter</code> class.
46 * <p>It shows a NavigationFilter which lets you walk word-wise
49 * @author Robert Schuster
51 public class NavigationFilterDemo
53 implements ActionListener
59 * Creates a new demo instance.
61 public NavigationFilterDemo()
64 // initFrameContent() is only called (from main) when running this app
69 * When the demo is run independently, the frame is displayed, so we should
70 * initialise the content panel (including the demo content and a close
71 * button). But when the demo is run as part of the Swing activity board,
72 * only the demo content panel is used, the frame itself is never displayed,
73 * so we can avoid this step.
75 void initFrameContent()
77 JPanel closePanel = new JPanel();
78 JButton closeButton = new JButton("Close");
79 closeButton.setActionCommand("CLOSE");
80 closeButton.addActionListener(this);
81 closePanel.add(closeButton);
82 add(closePanel, BorderLayout.SOUTH);
85 private void createContent()
87 setLayout(new BorderLayout());
89 add(textArea = new JTextArea(10, 20));
91 textArea.setWrapStyleWord(true);
93 textArea.setLineWrap(true);
95 textArea.setNavigationFilter(new WordFilter());
97 textArea.setText("GNU Classpath, Essential Libraries for Java, " +
98 "is a GNU project to create free core class " +
99 "libraries for use with virtual machines and " +
100 "compilers for the java programming language.");
103 public void actionPerformed(ActionEvent e)
105 if (e.getActionCommand().equals("CLOSE"))
110 public static void main(String[] args)
112 SwingUtilities.invokeLater
117 NavigationFilterDemo app = new NavigationFilterDemo();
118 app.initFrameContent();
119 JFrame frame = new JFrame("NavigationFilterDemo");
120 frame.getContentPane().add(app);
122 frame.setVisible(true);
123 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
129 * Returns a DemoFactory that creates a NavigationFilterDemo.
131 * @return a DemoFactory that creates a NavigationFilterDemo
133 public static DemoFactory createDemoFactory()
135 return new DemoFactory()
137 public JComponent createDemo()
139 return new NavigationFilterDemo();
144 class WordFilter extends NavigationFilter
146 public int getNextVisualPositionFrom(JTextComponent text,
150 Position.Bias[] biasRet)
151 throws BadLocationException
158 case SwingConstants.NORTH:
159 // Find out where the caret want to be positioned ideally.
160 pt = text.getCaret().getMagicCaretPosition();
162 // Calculate its position above.
163 newpos = Utilities.getPositionAbove(text, pos, (pt != null) ? pt.x : 0);
165 // If we have a valid position, then calculate the next word start
168 return Utilities.getWordStart(text, newpos);
171 case SwingConstants.SOUTH:
172 // Find out where the caret want to be positioned ideally.
173 pt = text.getCaret().getMagicCaretPosition();
175 // Calculate its position below.
176 newpos = Utilities.getPositionBelow(text, pos, (pt != null) ? pt.x : 0);
178 // If we have a valid position, then calculate the next word start
181 return Utilities.getWordStart(text, newpos);
184 case SwingConstants.WEST:
185 // Calculate the next word start.
186 newpos = Utilities.getWordStart(text, newpos);
188 // If that means that the caret will not move, return
189 // the start of the previous word.
193 return Utilities.getPreviousWord(text, newpos);
194 case SwingConstants.EAST:
195 return Utilities.getNextWord(text, newpos);
197 // Do whatever the super implementation did.
198 return super.getNextVisualPositionFrom(text, pos, bias,