OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / swing / NavigationFilterDemo.java
1 /* NavigationFilterDemo.java -- An example for the NavigationFilter class.
2    Copyright (C) 2006  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
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)
9 any later version.
10
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.
15
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
19 02110-1301 USA.
20 */
21
22
23 package gnu.classpath.examples.swing;
24
25 import java.awt.BorderLayout;
26 import java.awt.Point;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.ActionListener;
29
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;
42
43 /**
44  * A demonstration of the <code>javax.swing.text.NavigationFilter</code> class.
45  * 
46  * <p>It shows a NavigationFilter which lets you walk word-wise
47  * through a text.</p>
48  * 
49  * @author Robert Schuster
50  */
51 public class NavigationFilterDemo 
52   extends JPanel 
53   implements ActionListener 
54 {
55   
56   JTextArea textArea;
57
58   /**
59    * Creates a new demo instance.
60    */
61   public NavigationFilterDemo() 
62   {
63     createContent();
64     // initFrameContent() is only called (from main) when running this app 
65     // standalone
66   }
67   
68   /**
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.
74    */
75   void initFrameContent()
76   {
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);
83   }
84
85   private void createContent()
86   {
87     setLayout(new BorderLayout());
88     
89     add(textArea = new JTextArea(10, 20));
90     
91     textArea.setWrapStyleWord(true);
92     
93     textArea.setLineWrap(true);
94         
95     textArea.setNavigationFilter(new WordFilter());
96     
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.");
101   }
102   
103   public void actionPerformed(ActionEvent e) 
104   {
105     if (e.getActionCommand().equals("CLOSE"))
106       System.exit(0);
107     
108   } 
109
110   public static void main(String[] args) 
111   {
112     SwingUtilities.invokeLater
113     (new Runnable()
114      {
115        public void run()
116        {
117          NavigationFilterDemo app = new NavigationFilterDemo();
118          app.initFrameContent();
119          JFrame frame = new JFrame("NavigationFilterDemo");
120          frame.getContentPane().add(app);
121          frame.pack();
122          frame.setVisible(true);
123          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
124        }
125      });
126   }
127
128   /**
129    * Returns a DemoFactory that creates a NavigationFilterDemo.
130    *
131    * @return a DemoFactory that creates a NavigationFilterDemo
132    */
133   public static DemoFactory createDemoFactory()
134   {
135     return new DemoFactory()
136     {
137       public JComponent createDemo()
138       {
139         return new NavigationFilterDemo();
140       }
141     };
142   }
143   
144   class WordFilter extends NavigationFilter
145   {
146     public int getNextVisualPositionFrom(JTextComponent text,
147                                          int pos,
148                                          Position.Bias bias,
149                                          int direction,
150                                          Position.Bias[] biasRet)
151                                   throws BadLocationException
152     {
153       Point pt;
154       
155       int newpos = pos;
156       switch (direction)
157       {
158         case SwingConstants.NORTH:
159           // Find out where the caret want to be positioned ideally.
160           pt = text.getCaret().getMagicCaretPosition();
161           
162           // Calculate its position above.
163           newpos = Utilities.getPositionAbove(text, pos, (pt != null) ? pt.x : 0);
164
165           // If we have a valid position, then calculate the next word start
166           // from there.
167           if (newpos != -1)
168             return Utilities.getWordStart(text, newpos);
169           else
170             return pos;
171         case SwingConstants.SOUTH:
172           // Find out where the caret want to be positioned ideally.
173           pt = text.getCaret().getMagicCaretPosition();
174
175           // Calculate its position below.
176           newpos = Utilities.getPositionBelow(text, pos, (pt != null) ? pt.x : 0);
177
178           // If we have a valid position, then calculate the next word start
179           // from there.
180           if (newpos != -1)
181             return Utilities.getWordStart(text, newpos);
182           else
183             return pos;
184         case SwingConstants.WEST:
185           // Calculate the next word start.
186           newpos = Utilities.getWordStart(text, newpos);
187           
188           // If that means that the caret will not move, return
189           // the start of the previous word.
190           if (newpos != pos)
191             return newpos;
192           else
193             return Utilities.getPreviousWord(text, newpos);
194         case SwingConstants.EAST:
195           return Utilities.getNextWord(text, newpos);
196         default:
197           // Do whatever the super implementation did.
198           return super.getNextVisualPositionFrom(text, pos, bias,
199                                                  direction, biasRet);
200       }
201     }
202     
203   }
204   
205 }