1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.gui;
26
27
28 import java.awt.*;
29 import java.awt.event.*;
30 import java.util.*;
31 import javax.swing.*;
32
33
34 /***
35 * A dialog that allows the user to select an item from the list of recently
36 * used search patterns.
37 *
38 * @author Murali Krishnan
39 *
40 */
41 public class RecentPatternSelector {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 private App _app = null;
57 private JDialog _dialog = null;
58 private JList _list = new JList();
59 private JButton _buttonOk = new JButton("Select");
60 private JButton _buttonCancel = new JButton("Cancel");
61
62
63
64
65
66 RecentPatternSelector(App pApp) {
67 _app = pApp;
68
69 initialize();
70 }
71
72
73
74
75
76 protected App getApp() {return _app;}
77 protected JDialog getDialog() {return _dialog;}
78 protected JList getList() {return _list;}
79 protected JButton getButtonOk() {return _buttonOk;}
80 protected JButton getButtonCancel() {return _buttonCancel;}
81
82
83
84
85
86 protected void setDialog(JDialog val) {_dialog=val;}
87
88
89
90
91
92 public void initialize() {
93 createComponents();
94 defineActions();
95 makeFrame();
96 }
97
98
99 public void createComponents() {
100 getList().setListData(new Vector(getApp().getRecentSearches()));
101 getList().setBorder(BorderFactory.createTitledBorder("Recent searches:"));
102
103 String title = "Recent Search Patterns";
104 _dialog = new JDialog(getApp().getFrame(), title, true);
105 }
106
107
108 public void defineActions() {
109 getButtonOk().addActionListener(new ActionListener() {
110 public void actionPerformed (ActionEvent e) {
111 handleItemSelection();
112 }
113 });
114
115 getButtonCancel().addActionListener(new ActionListener() {
116 public void actionPerformed (ActionEvent e) {
117 getDialog().dispose();
118 }
119 });
120
121
122 getList().addMouseListener(new MouseAdapter() {
123 public void mouseClicked(MouseEvent e) {
124 if (e.getClickCount() == 2) {
125 handleItemSelection();
126 }
127 }
128 });
129 }
130
131
132 public void makeFrame() {
133 getDialog().getContentPane().setLayout(new BorderLayout());
134
135 getDialog().getContentPane().add(makeMainPanel(), BorderLayout.CENTER);
136 getDialog().getContentPane().add(makeButtonPanel(), BorderLayout.SOUTH);
137
138 getDialog().pack();
139 Point location = getApp().getFrame().getLocation();
140 Point buttonLocation = getApp().findButtonSelectPattern().getLocation();
141 location.translate(buttonLocation.x, 50);
142 getDialog().setLocation(location);
143 getDialog().setVisible(true);
144 }
145
146
147 protected JPanel makeMainPanel() {
148 JPanel result = new JPanel(new BorderLayout());
149
150 result.add(getList(), BorderLayout.CENTER);
151
152 return result;
153 }
154
155
156 protected JPanel makeButtonPanel() {
157 JPanel result = new JPanel(new BorderLayout());
158
159 ArrayList<JButton> buttons = new ArrayList<JButton>();
160 {
161 buttons.add(getButtonOk());
162 buttons.add(getButtonCancel());
163 }
164
165 result.add(App.wrapButtons(buttons), BorderLayout.EAST);
166
167 return result;
168 }
169
170
171
172
173
174 protected void handleItemSelection() {
175 String selection = (String) getList().getSelectedValue();
176 if (selection == null) {
177 System.err.println("Nothing selected.");
178 } else {
179 getApp().setSearchText(selection);
180 JTextField field = getApp().findSearchField();
181 field.grabFocus();
182 getDialog().dispose();
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 }