KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snow > texteditor > SearchPanel


1 package snow.texteditor;
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(Icons.CrossIcon.shared10);
23   final private JCheckBox searchFromLine = new JCheckBox("from actual position", false);
24   final private JCheckBox ignoreCase = new JCheckBox("ignore case", true);
25   final private JCheckBox backward = new JCheckBox("backward", false);
26   final private JTextField infoField = new JTextField(30);
27
28   // Todo...
29
final private JCheckBox viewAll = new JCheckBox("all", false);
30   // TODO
31
//final private JCheckBox replace = new JCheckBox("replace", false);
32

33   final private SimpleDocument doc;
34   final private JTextPane textPane;
35
36
37   private int actualPosition = -1; // no actual pos
38
private String JavaDoc actualSearch = "";
39
40   // used to avoid having a lot of highlights => always remove this before setting a new region as highlighted.
41
private Object JavaDoc actualHighlightTag = null;
42
43
44   public SearchPanel(final JTextPane textPane, SimpleDocument doc)
45   {
46     //super(new FlowLayout(FlowLayout.LEFT, 5, 1));
47
super();
48     this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
49     this.setBorder(null);
50
51     this.doc = doc;
52     this.textPane = textPane;
53
54     JPanel sPan = new JPanel(new FlowLayout(FlowLayout.LEFT,5,0));
55     add(sPan);
56     sPan.add(close);
57
58     sPan.add(new JLabel("Search: "));
59     sPan.add(searchTF);
60     searchNext.setMargin(new Insets(0,0,0,0));
61     sPan.add(searchNext);
62     searchNext.setToolTipText("Search next");
63     searchPrevious.setMargin(new Insets(0,0,0,0));
64     sPan.add(searchPrevious);
65     searchPrevious.setToolTipText("Search previous");
66     sPan.add(ignoreCase);
67     sPan.add(searchFromLine);
68     sPan.add(backward);
69     //add(viewAll); // => highlight
70

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

144   /** @param pos -1 to set from beginning
145   */

146   public void setSearchPositionStart(int pos)
147   {
148      actualPosition = pos;
149   }
150
151   public void setInfoText(final String JavaDoc info)
152   {
153      if(EventQueue.isDispatchThread())
154      {
155        infoField.setText(info);
156      }
157      else
158      {
159        EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
160          infoField.setText(info);
161        }});
162      }
163   }
164
165   public void CTRL_F_pressed()
166   {
167     if(!this.isVisible())
168     {
169        this.setVisible(true);
170     }
171     searchTF.selectAll();
172     searchTF.requestFocus();
173   }
174
175
176   public void searchNext()
177   {
178     searchNext( !this.backward.isSelected() );
179   }
180
181   /** called either when enter typed OR when F3 has been pressed in the editor
182   */

183   public void searchNext(boolean forward)
184   {
185     // if not visible, show the panel and return
186
if(!this.isVisible())
187     {
188        this.setVisible(true);
189        return;
190     }
191     searchTF.requestFocus();
192
193
194     // remove highlight
195
if(actualHighlightTag!=null)
196     {
197       textPane.getHighlighter().removeHighlight(actualHighlightTag);
198     }
199
200
201     this.actualSearch = searchTF.getText();
202     if(ignoreCase.isSelected()) actualSearch = actualSearch.toUpperCase();
203     //System.out.println("Search "+this.actualSearch+" from "+actualPosition);
204
if(actualSearch.length()==0) return;
205
206     int incr = 1;
207     if(!forward) incr = -1;
208     if(this.searchFromLine.isSelected())
209     {
210       actualPosition = search(actualSearch, textPane.getCaretPosition() + incr, !forward);
211     }
212     else
213     {
214       actualPosition = search(actualSearch, this.actualPosition + incr, !forward);
215     }
216
217     if(actualPosition>=0)
218     {
219        searchTF.setBackground(UIManager.getColor("TextField.background"));
220        // highlight result
221
//editorPanel.doc.setCharacterAttributes( actualPosition, actualSearch.length(), editorPanel.doc.getStyle("highlight"), true );
222

223        try
224        {
225          actualHighlightTag = textPane.getHighlighter().addHighlight(
226             actualPosition, actualPosition+actualSearch.length(), doc.searchHighlighter);
227          // place the caret (important if the option search from actual position)
228
textPane.setCaretPosition(actualPosition);
229          // make that portion visible
230
//DocumentUtils.scrollToMiddle( textPane, actualPosition );
231
textPane.scrollRectToVisible( textPane.modelToView(actualPosition) );
232        }
233        catch(Exception JavaDoc e) { e.printStackTrace(); }
234     }
235     else
236     {
237        // not found => set as red
238
searchTF.setBackground(Color.red);
239     }
240   }
241
242
243   private int search(String JavaDoc str, int from, boolean backward)
244   {
245      if(backward)
246      {
247        return doc.searchBackward(str, from, ignoreCase.isSelected());
248      }
249      else
250      {
251        return doc.search(str, from, ignoreCase.isSelected());
252      }
253   }
254
255   /** Special called from the editor popup to directly search the next.
256   */

257   public void searchNext_calledFromPopup(String JavaDoc str, int from)
258   {
259     // ??? some may not want to see this ??
260
this.setVisible(true);
261
262     this.actualSearch = str;
263     this.searchTF.setText(str);
264     this.backward.setSelected(false);
265
266     if(this.ignoreCase.isSelected()) str = str.toUpperCase();
267     // remove old highlighted region
268
if(actualHighlightTag!=null)
269     {
270        textPane.getHighlighter().removeHighlight(actualHighlightTag);
271     }
272
273     int pos = doc.search(str, from, ignoreCase.isSelected());
274     if(pos>=0)
275     {
276        try
277        {
278          actualHighlightTag = textPane.getHighlighter().addHighlight(
279             pos, pos+str.length(), doc.searchHighlighter);
280          // place the caret (important if the option search from actual position is active)
281
textPane.setCaretPosition(pos);
282          // make that portion visible
283
textPane.scrollRectToVisible( textPane.modelToView(pos) );
284        }
285        catch(Exception JavaDoc e) { e.printStackTrace(); }
286     }
287     else
288     {
289        System.out.println(str+" not found from "+from);
290     }
291   }
292
293   /** special called from the editor popup to directly search the next
294   */

295   public void searchPrevious_calledFromPopup(String JavaDoc str, int from)
296   {
297     this.searchTF.setText(str);
298     this.backward.setSelected(true);
299
300     if(this.ignoreCase.isSelected()) str = str.toUpperCase();
301     // remove old highlighted region
302
if(actualHighlightTag!=null)
303     {
304       textPane.getHighlighter().removeHighlight(actualHighlightTag);
305     }
306
307     int pos = doc.searchBackward(str, from, ignoreCase.isSelected());
308     if(pos>=0)
309     {
310        try
311        {
312          actualHighlightTag = textPane.getHighlighter().addHighlight(
313             pos, pos+str.length(), doc.searchHighlighter);
314          // place the caret (important if the option search from actual position)
315
textPane.setCaretPosition(pos);
316          // make that portion visible
317
textPane.scrollRectToVisible( textPane.modelToView(pos) );
318        }
319        catch(Exception JavaDoc e) { e.printStackTrace(); }
320
321     }
322     else
323     {
324       System.out.println(str+" not found backward from "+from);
325     }
326   }
327
328 }
Popular Tags