KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > text > FindDialog


1 package text;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5
6 import java.awt.*;
7 import java.awt.event.*;
8
9 import java.util.*;
10
11 public class FindDialog extends JDialog implements ActionListener, KeyListener
12 {
13    protected JButton next, prev;
14    protected WrappedDisplay display;
15    protected JTextField search;
16    protected ListIterator results = null;
17
18    public void showDialog()
19    {
20       rero.gui.KeyBindings.is_dialog_active = true;
21       results = null;
22       next.setEnabled(false);
23       prev.setEnabled(true);
24       pack();
25       setLocationRelativeTo(display);
26       setVisible(true);
27       this.search.selectAll();
28       search.requestFocus();
29    }
30
31    public FindDialog(Component comp, String JavaDoc title, WrappedDisplay _display, String JavaDoc text)
32    {
33        super(JOptionPane.getFrameForComponent(comp), title, false);
34
35        display = _display;
36
37        getContentPane().setLayout(new BorderLayout());
38
39        search = new JTextField(text, 20);
40        search.addKeyListener(this);
41        search.addActionListener(this);
42
43        JPanel panel = new JPanel();
44        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
45        panel.setLayout(new FlowLayout());
46        panel.add(new JLabel("Find: "));
47        panel.add(search);
48
49        getContentPane().add(panel, BorderLayout.CENTER);
50
51        JPanel buttons = new JPanel();
52        buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
53
54        next = new JButton("Next");
55        next.setMnemonic('N');
56        next.setEnabled(false);
57        next.addActionListener(this);
58
59        prev = new JButton("Previous");
60        prev.setMnemonic('P');
61        prev.setEnabled(text.length() > 0);
62        prev.addActionListener(this);
63
64
65        buttons.add(prev);
66        buttons.add(next);
67
68        getContentPane().add(buttons, BorderLayout.SOUTH);
69
70        addWindowListener(new WindowAdapter()
71        {
72           public void windowClosing(WindowEvent e)
73           {
74              rero.gui.KeyBindings.is_dialog_active = false;
75              setVisible(false);
76           }
77        });
78    }
79
80    public static void main(String JavaDoc args[])
81    {
82        FindDialog temp = new FindDialog(null, "Testing", null, "hi");
83        temp.showDialog();
84    }
85
86    public void keyTyped(KeyEvent ev)
87    {
88    }
89
90    public void keyPressed(KeyEvent ev) { }
91    public void keyReleased(KeyEvent ev)
92    {
93        if (ev.getKeyCode() == KeyEvent.VK_ESCAPE)
94        {
95           rero.gui.KeyBindings.is_dialog_active = false;
96           setVisible(false);
97           ev.consume();
98        }
99        else if (!ev.isActionKey() && ev.getKeyCode() != KeyEvent.VK_ENTER)
100        {
101           results = null;
102           next.setEnabled(search.getText().length() > 0);
103           prev.setEnabled(search.getText().length() > 0);
104        }
105    }
106
107    protected Object JavaDoc lastSource = null;
108
109    public void actionPerformed(ActionEvent ev)
110    {
111        if (results == null)
112        {
113           results = display.find(search.getText());
114           lastSource = null;
115        }
116
117        if ((ev.getSource() == next) && results.hasNext())
118        {
119           if (lastSource == prev) { results.next(); }
120
121           int gotoz = ( (Integer JavaDoc)results.next() ).intValue();
122
123           display.scrollTo(gotoz);
124
125           lastSource = next;
126        }
127        else if ((ev.getSource() == prev || ev.getSource() == search) && results.hasPrevious())
128        {
129           if (lastSource == next) { results.previous(); }
130
131           int gotoz = ( (Integer JavaDoc)results.previous() ).intValue();
132
133           display.scrollTo(gotoz);
134
135           lastSource = prev;
136        }
137
138        next.setEnabled(results.hasNext());
139        prev.setEnabled(results.hasPrevious());
140
141        search.requestFocus();
142    }
143 }
144
Popular Tags