KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > text > SearchPanel


1 package snow.text;
2
3 import snow.utils.gui.*;
4 import javax.swing.*;
5 import javax.swing.event.*;
6 import javax.swing.text.*;
7 import java.awt.event.*;
8 import java.util.*;
9 import java.awt.*;
10
11 /** Small panel destined to be placed on the bottom (or top) of an editor pane.
12 * Pops up when CTRL+F is pressed. F4 searches the next occurence.
13 * Todo: implement replace.
14 */

15 public class SearchPanel extends JPanel
16 {
17   final private JTextField searchTF = new JTextField(12);
18
19   final private JButton searchPrevious = new JButton(new Icons.UpIcon(10,10, true));
20   final private JButton searchNext = new JButton(new Icons.DownIcon(10,10, true));
21
22   final private JButton close = new JButton(new Icons.CrossIcon(10,10, true));
23   final private JCheckBox searchFromLine = new JCheckBox("search from actual position", false);
24   final private JCheckBox ignoreCase = new JCheckBox("ignore case", true);
25   final private JCheckBox backward = new JCheckBox("backward", false);
26
27   // Todo...
28
final private JCheckBox viewAll = new JCheckBox("all", false);
29   // TODO
30
final private JCheckBox replace = new JCheckBox("replace", false);
31
32   final private SimpleDocument doc;
33   final private JTextPane textPane;
34
35
36   private int actualPosition = -1; // no actual pos
37
private String JavaDoc actualSearch = "";
38
39   // used to avoid having a lot of highlights => always remove this before setting a new region as highlighted.
40
private Object JavaDoc actualHighlightTag = null;
41
42
43   public SearchPanel(final JTextPane textPane, SimpleDocument doc)
44   {
45     super(new FlowLayout(FlowLayout.LEFT, 5, 1));
46     this.setBorder(null);
47
48     this.doc = doc;
49     this.textPane = textPane;
50
51     add(close);
52
53     add(new JLabel("Search: "));
54     add(searchTF);
55     searchNext.setMargin(new Insets(0,0,0,0));
56     add(searchNext);
57     searchNext.setToolTipText("Search next");
58     searchPrevious.setMargin(new Insets(0,0,0,0));
59     add(searchPrevious);
60     searchPrevious.setToolTipText("Search previous");
61     add(ignoreCase);
62     add(searchFromLine);
63     add(backward);
64     //add(viewAll);
65

66     searchTF.addKeyListener(new KeyAdapter()
67     {
68        @Override JavaDoc
69        public void keyReleased(KeyEvent ke)
70        {
71           if(ke.getKeyCode() == KeyEvent.VK_ENTER)
72           {
73              // search next
74
searchNext();
75           }
76           else
77           {
78              // reset the search
79
actualPosition = -1;
80              searchNext();
81           }
82        }
83     });
84
85     textPane.registerKeyboardAction(new ActionListener()
86      {
87        public void actionPerformed(ActionEvent ae)
88        {
89           CTRL_F_pressed();
90        }
91      }, "Search", KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK, true), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
92
93     textPane.registerKeyboardAction(new ActionListener()
94      {
95        public void actionPerformed(ActionEvent ae)
96        {
97           searchNext();
98        }
99      }, "SearchNext", KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0, true), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
100
101
102     close.setMargin(new Insets(0,0,0,0));
103     close.setFocusPainted(false);
104
105     close.addActionListener(new ActionListener()
106     {
107       public void actionPerformed(ActionEvent ae)
108       {
109         setVisible(false);
110         textPane.requestFocus();
111       }
112     });
113
114     searchNext.addActionListener(new ActionListener()
115     {
116       public void actionPerformed(ActionEvent ae)
117       {
118         searchNext(true);
119       }
120     });
121
122     searchPrevious.addActionListener(new ActionListener()
123     {
124       public void actionPerformed(ActionEvent ae)
125       {
126         searchNext(false);
127       }
128     });
129
130     // Turned on with CTRL+F
131
setVisible(false);
132   } // Constructor
133

134   /** @param pos -1 to set from beginning
135   */

136   public void setSearchPositionStart(int pos)
137   {
138      actualPosition = pos;
139   }
140
141
142   public void CTRL_F_pressed()
143   {
144     //System.out.println("Search");
145
if(!this.isVisible())
146     {
147        this.setVisible(true);
148     }
149     searchTF.selectAll();
150     searchTF.requestFocus();
151   }
152
153
154   public void searchNext()
155   {
156     searchNext( !this.backward.isSelected() );
157   }
158
159   /** called either when enter typed OR when F3 has been pressed in the editor
160   */

161   public void searchNext(boolean forward)
162   {
163     // if not visible, show the panel and return
164
if(!this.isVisible())
165     {
166        this.setVisible(true);
167        return;
168     }
169     searchTF.requestFocus();
170
171
172     // remove highlight
173
if(actualHighlightTag!=null)
174     {
175       textPane.getHighlighter().removeHighlight(actualHighlightTag);
176     }
177
178
179     this.actualSearch = searchTF.getText();
180     if(ignoreCase.isSelected()) actualSearch = actualSearch.toUpperCase();
181     //System.out.println("Search "+this.actualSearch+" from "+actualPosition);
182
if(actualSearch.length()==0) return;
183
184     int incr = 1;
185     if(!forward) incr = -1;
186     if(this.searchFromLine.isSelected())
187     {
188       actualPosition = search(actualSearch, textPane.getCaretPosition() + incr, !forward);
189     }
190     else
191     {
192       actualPosition = search(actualSearch, this.actualPosition + incr, !forward);
193     }
194
195     if(actualPosition>=0)
196     {
197        searchTF.setBackground(UIManager.getColor("TextField.background"));
198        // highlight result
199
//editorPanel.doc.setCharacterAttributes( actualPosition, actualSearch.length(), editorPanel.doc.getStyle("highlight"), true );
200

201        try
202        {
203          actualHighlightTag = textPane.getHighlighter().addHighlight(
204             actualPosition, actualPosition+actualSearch.length(), doc.searchHighlighter);
205          // place the caret (important if the option search from actual position)
206
textPane.setCaretPosition(actualPosition);
207          // make that portion visible
208
//DocumentUtils.scrollToMiddle( textPane, actualPosition );
209
textPane.scrollRectToVisible( textPane.modelToView(actualPosition) );
210        } catch(Exception JavaDoc e) { e.printStackTrace(); }
211     }
212     else
213     {
214        // not found => set as red
215
searchTF.setBackground(Color.red);
216     }
217   }
218
219
220   private int search(String JavaDoc str, int from, boolean backward)
221   {
222      if(backward)
223      {
224        return doc.searchBackward(str, from, ignoreCase.isSelected());
225      }
226      else
227      {
228        return doc.search(str, from, ignoreCase.isSelected());
229      }
230   }
231
232   /** special called from the editor popup to directly search the next
233   */

234   public void searchNext_calledFromPopup(String JavaDoc str, int from)
235   {
236     this.actualSearch = str;
237     this.searchTF.setText(str);
238     this.backward.setSelected(false);
239
240     if(this.ignoreCase.isSelected()) str = str.toUpperCase();
241     // remove old highlighted region
242
if(actualHighlightTag!=null)
243     {
244       textPane.getHighlighter().removeHighlight(actualHighlightTag);
245     }
246
247     int pos = doc.search(str, from, ignoreCase.isSelected());
248     if(pos>=0)
249     {
250        try
251        {
252          actualHighlightTag = textPane.getHighlighter().addHighlight(
253             pos, pos+str.length(), doc.searchHighlighter);
254          // place the caret (important if the option search from actual position is active)
255
textPane.setCaretPosition(pos);
256          // make that portion visible
257
textPane.scrollRectToVisible( textPane.modelToView(pos) );
258        } catch(Exception JavaDoc e) { e.printStackTrace(); }
259
260     }
261     else
262     {
263        System.out.println(str+" not found from "+from);
264     }
265   }
266
267   /** special called from the editor popup to directly search the next
268   */

269   public void searchPrevious_calledFromPopup(String JavaDoc str, int from)
270   {
271     this.searchTF.setText(str);
272     this.backward.setSelected(true);
273
274     if(this.ignoreCase.isSelected()) str = str.toUpperCase();
275     // remove old highlighted region
276
if(actualHighlightTag!=null)
277     {
278       textPane.getHighlighter().removeHighlight(actualHighlightTag);
279     }
280
281     int pos = doc.searchBackward(str, from, ignoreCase.isSelected());
282     if(pos>=0)
283     {
284        try
285        {
286          actualHighlightTag = textPane.getHighlighter().addHighlight(
287             pos, pos+str.length(), doc.searchHighlighter);
288          // place the caret (important if the option search from actual position)
289
textPane.setCaretPosition(pos);
290          // make that portion visible
291
textPane.scrollRectToVisible( textPane.modelToView(pos) );
292        } catch(Exception JavaDoc e) { e.printStackTrace(); }
293
294     }
295     else
296     {
297       System.out.println(str+" not found backward from "+from);
298     }
299   }
300
301
302
303
304 } // SearchPanel
Popular Tags