File Source: SwingSearchBar.java
/*
P/P * Method: com.dmdirc.addons.ui_swing.components.SwingSearchBar__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 package com.dmdirc.addons.ui_swing.components;
23
24 import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
25 import com.dmdirc.addons.ui_swing.components.frames.InputTextFrame;
26 import com.dmdirc.ui.IconManager;
27 import com.dmdirc.ui.interfaces.SearchBar;
28 import com.dmdirc.ui.messages.ColourManager;
29 import com.dmdirc.addons.ui_swing.UIUtilities;
30 import com.dmdirc.addons.ui_swing.actions.SearchAction;
31 import com.dmdirc.addons.ui_swing.textpane.IRCDocument;
32 import com.dmdirc.addons.ui_swing.textpane.IRCDocumentSearcher;
33 import com.dmdirc.addons.ui_swing.textpane.LinePosition;
34 import com.dmdirc.addons.ui_swing.textpane.TextPane;
35 import com.dmdirc.util.ListenerList;
36
37 import java.awt.Window;
38 import java.awt.event.ActionEvent;
39 import java.awt.event.ActionListener;
40 import java.awt.event.KeyEvent;
41 import java.awt.event.KeyListener;
42
43 import javax.swing.JButton;
44 import javax.swing.JCheckBox;
45 import javax.swing.JComponent;
46 import javax.swing.JOptionPane;
47 import javax.swing.JPanel;
48 import javax.swing.JTextField;
49 import javax.swing.KeyStroke;
50 import javax.swing.SwingUtilities;
51 import javax.swing.event.DocumentEvent;
52 import javax.swing.event.DocumentListener;
53
54 import net.miginfocom.swing.MigLayout;
55
56 /**
57 * Status bar, shows message and info on the gui.
58 */
/*
P/P * Method: ListenerList access$200(SwingSearchBar)
*
* Preconditions:
* x0 != null
*
* Postconditions:
* return_value == x0.listeners
* init'ed(return_value)
*/
59 public final class SwingSearchBar extends JPanel implements ActionListener,
60 KeyListener, SearchBar, DocumentListener {
61
62 /**
63 * A version number for this class. It should be changed whenever the class
64 * structure is changed (or anything else that would prevent serialized
65 * objects being unserialized with the new class).
66 */
67 private static final long serialVersionUID = 6;
68 /** Frame parent. */
69 private final TextFrame parent;
70 /** Close button. */
71 private ImageButton closeButton;
72 /** Next match button. */
73 private JButton nextButton;
74 /** Previous match button. */
75 private JButton prevButton;
76 /** Case sensitive checkbox. */
77 private JCheckBox caseCheck;
78 /** Search text field. */
79 private JTextField searchBox;
80 /** Line to search from. */
81 private int line;
82 /** Listener list. */
83 private final ListenerList listeners;
84 /** Parent window. */
85 private Window parentWindow;
86
87 /**
88 * Creates a new instance of StatusBar.
89 *
90 * @param newParent parent frame for the dialog
91 * @param parentWindow Parent window
92 */
93 public SwingSearchBar(final TextFrame newParent, final Window parentWindow) {
/*
P/P * Method: void com.dmdirc.addons.ui_swing.components.SwingSearchBar(TextFrame, Window)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.components.SwingSearchBar:getActionMap(...)@104 != null
* com.dmdirc.addons.ui_swing.components.SwingSearchBar:getInputMap(...)@101 != null
*
* Postconditions:
* this.caseCheck == &new JCheckBox(initComponents#4)
* this.closeButton == &new ImageButton(initComponents#1)
* init'ed(this.line)
* this.listeners == &new ListenerList(SwingSearchBar#1)
* this.nextButton == &new JButton(initComponents#2)
* this.parent == newParent
* init'ed(this.parent)
* this.parentWindow == parentWindow
* init'ed(this.parentWindow)
* this.prevButton == &new JButton(initComponents#3)
* ...
*/
94 super();
95
96 listeners = new ListenerList();
97
98 this.parent = newParent;
99 this.parentWindow = parentWindow;
100
101 getInputMap(JComponent.WHEN_FOCUSED).
102 put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "searchAction");
103
104 getActionMap().put("searchAction", new SearchAction(this));
105
106 initComponents();
107 layoutComponents();
108 addListeners();
109 }
110
111 /** Initialises components. */
112 private void initComponents() {
/*
P/P * Method: void initComponents()
*
* Presumptions:
* com.dmdirc.ui.IconManager:getIconManager(...)@113 != null
*
* Postconditions:
* this.caseCheck == &new JCheckBox(initComponents#4)
* this.closeButton == &new ImageButton(initComponents#1)
* this.line == -1
* this.nextButton == &new JButton(initComponents#2)
* this.prevButton == &new JButton(initComponents#3)
* this.searchBox == &new JTextField(initComponents#5)
* new ImageButton(initComponents#1) num objects == 1
* new JButton(initComponents#2) num objects == 1
* new JButton(initComponents#3) num objects == 1
* new JCheckBox(initComponents#4) num objects == 1
* ...
*/
113 closeButton = new ImageButton("close",
114 IconManager.getIconManager().getIcon("close-inactive"),
115 IconManager.getIconManager().getIcon("close-active"));
116 nextButton = new JButton();
117 prevButton = new JButton();
118 caseCheck = new JCheckBox();
119 searchBox = new JTextField();
120
121 nextButton.setText("Later");
122 prevButton.setText("Earlier");
123 caseCheck.setText("Case sensitive");
124
125 line = -1;
126 }
127
128 /** Lays out components. */
129 private void layoutComponents() {
/*
P/P * Method: void layoutComponents()
*
* Preconditions:
* init'ed(this.caseCheck)
* init'ed(this.closeButton)
* init'ed(this.nextButton)
* init'ed(this.prevButton)
* init'ed(this.searchBox)
*/
130 this.setLayout(new MigLayout("ins 0, fill"));
131
132 add(closeButton);
133 add(searchBox, "growx, pushx, sgy all");
134 add(prevButton, "sgx button, sgy all");
135 add(nextButton, "sgx button, sgy all");
136 add(caseCheck, "sgy all");
137 }
138
139 /** Adds listeners to components. */
140 private void addListeners() {
/*
P/P * Method: void addListeners()
*
* Preconditions:
* this.caseCheck != null
* this.closeButton != null
* this.nextButton != null
* this.prevButton != null
* this.searchBox != null
*/
141 closeButton.addActionListener(this);
142 searchBox.addKeyListener(this);
143 nextButton.addActionListener(this);
144 prevButton.addActionListener(this);
145 caseCheck.addActionListener(this);
146 }
147
148 /**
149 * {@inheritDoc}.
150 *
151 * @param e Action event
152 */
153 @Override
154 public void actionPerformed(final ActionEvent e) {
/*
P/P * Method: void actionPerformed(ActionEvent)
*
* Preconditions:
* e != null
* init'ed(this.closeButton)
* (soft) this.caseCheck != null
* (soft) init'ed(this.nextButton)
* (soft) this.parent != null
* (soft) init'ed(this.parentWindow)
* (soft) init'ed(this.prevButton)
* (soft) this.searchBox != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.components.frames.TextFrame:getTextPane(...)@163 != null
* init'ed(com.dmdirc.ui.interfaces.SearchBar$Direction.DOWN)
* init'ed(com.dmdirc.ui.interfaces.SearchBar$Direction.UP)
*
* Postconditions:
* possibly_updated(this.line)
*/
155 if (e.getSource() == closeButton) {
156 close();
157 } else if (e.getSource() == nextButton) {
158 search(Direction.DOWN, searchBox.getText(), caseCheck.isSelected());
159 } else if (e.getSource() == prevButton) {
160 search(Direction.UP, searchBox.getText(), caseCheck.isSelected());
161 } else if (e.getSource() == caseCheck) {
162 searchBox.setBackground(ColourManager.getColour("FFFFFF"));
163 line = parent.getTextPane().getLastVisibleLine();
164 }
165 }
166
167 /** {@inheritDoc}. */
168 @Override
169 public void open() {
/*
P/P * Method: void open()
*/
170 SwingUtilities.invokeLater(new Runnable() {
171
172 /** {@inheritDoc} */
173 @Override
174 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.searchBox != null
*/
175 setVisible(true);
176 searchBox.setBackground(ColourManager.getColour("FFFFFF"));
177 getFocus();
178 }
179 });
180 }
181
182 /** {@inheritDoc}. */
183 @Override
184 public void close() {
/*
P/P * Method: void close()
*/
185 SwingUtilities.invokeLater(new Runnable() {
186
187 /** {@inheritDoc} */
188 @Override
189 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.parent != null
* (soft) this.parent.inputField != null
*/
190 setVisible(false);
191 if (parent instanceof InputTextFrame) {
192 ((InputTextFrame) parent).getInputField().requestFocusInWindow();
193 } else {
194 parent.requestFocusInWindow();
195 }
196 }
197 });
198 }
199
200 /** {@inheritDoc}. */
201 @Override
202 public void search(final String text, final boolean caseSensitive) {
/*
P/P * Method: void search(String, bool)
*
* Preconditions:
* this.searchBox != null
* (soft) init'ed(this.line)
* (soft) this.parent != null
* (soft) init'ed(this.parentWindow)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.components.frames.TextFrame:getTextPane(...)@205 != null
* init'ed(com.dmdirc.ui.interfaces.SearchBar$Direction.UP)
* javax.swing.JTextField:getText(...)@203 != null
*
* Postconditions:
* init'ed(this.line)
*
* Test Vectors:
* this.line: {-231..-2, 0..232-1}, {-1}
* java.lang.String:isEmpty(...)@203: {1}, {0}
*/
203 if (!searchBox.getText().isEmpty()) {
204 if (line == -1) {
205 line = parent.getTextPane().getLastVisibleLine();
206 }
207 search(Direction.UP, text, caseSensitive);
208 }
209 }
210
211 /** {@inheritDoc}. */
212 @Override
213 public void search(final Direction direction, final String text,
214 final boolean caseSensitive) {
/*
P/P * Method: void search(SearchBar$Direction, String, bool)
*
* Preconditions:
* this.parent != null
* this.searchBox != null
* (soft) init'ed(this.parentWindow)
*
* Presumptions:
* com.dmdirc.addons.ui_swing.components.frames.TextFrame:getTextPane(...)@219 != null
* com.dmdirc.addons.ui_swing.textpane.TextPane:getSelectedRange(...)@229 != null
* init'ed(com.dmdirc.ui.interfaces.SearchBar$Direction.UP)
*
* Test Vectors:
* com.dmdirc.addons.ui_swing.textpane.LinePosition:getEndLine(...)@229: {-231..-1, 1..232-1}, {0}
* com.dmdirc.addons.ui_swing.textpane.LinePosition:getEndPos(...)@229: {0}, {-231..-1, 1..232-1}
* javax.swing.JOptionPane:showConfirmDialog(...)@229: {0}, {-231..-1, 1..232-1}
*/
215 boolean foundText = false;
216
217 final boolean up = Direction.UP == direction;
218
219 final TextPane textPane = parent.getTextPane();
220 final IRCDocument document = textPane.getDocument();
221 final IRCDocumentSearcher searcher = new IRCDocumentSearcher(text, document,
222 caseSensitive);
223 searcher.setPosition(textPane.getSelectedRange());
224
225 final LinePosition result = up ? searcher.searchUp() : searcher.searchDown();
226
227 if (result == null) {
228 foundText = false;
229 } else if ((textPane.getSelectedRange().getEndLine() != 0 ||
230 textPane.getSelectedRange().getEndPos() != 0)
231 && ((up && result.getEndLine() > textPane.getSelectedRange().getEndLine())
232 || (!up && result.getStartLine() < textPane.getSelectedRange().getStartLine()))
233 && JOptionPane.showConfirmDialog(parentWindow,
234 "Do you want to continue searching from the " + (up ? "end" : "beginning") + "?",
235 "No more results", JOptionPane.OK_CANCEL_OPTION,
236 JOptionPane.QUESTION_MESSAGE) != JOptionPane.OK_OPTION) {
237 // It's wrapped, and they don't want to continue searching
238
239 foundText = false;
240 } else {
241 //found, select and return found
242 textPane.setScrollBarPosition(result.getEndLine());
243 textPane.setSelectedTexT(result);
244 foundText = true;
245 }
246
247 if (foundText) {
248 searchBox.setBackground(ColourManager.getColour("FFFFFF"));
249 } else {
250 searchBox.setBackground(ColourManager.getColour("FF0000"));
251 }
252 }
253
254 /**
255 * {@inheritDoc}.
256 *
257 * @param event Key event
258 */
259 @Override
260 public void keyPressed(final KeyEvent event) {
/*
P/P * Method: void keyPressed(KeyEvent)
*
* Preconditions:
* event != null
* this.listeners != null
* (soft) this.caseCheck != null
* (soft) this.parent != null
* (soft) init'ed(this.parentWindow)
* (soft) this.searchBox != null
*
* Presumptions:
* com.dmdirc.addons.ui_swing.components.frames.TextFrame:getTextPane(...)@267 != null
* init'ed(com.dmdirc.ui.interfaces.SearchBar$Direction.UP)
* com.dmdirc.util.ListenerList:get(...)@271 != null
* java.util.Iterator:next(...)@271 != null
*
* Postconditions:
* possibly_updated(this.line)
*
* Test Vectors:
* java.awt.event.KeyEvent:getKeyCode(...)@262: {-231..26, 28..232-1}, {27}
* java.awt.event.KeyEvent:getKeyCode(...)@264: {-231..9, 11..232-1}, {10}
* java.awt.event.KeyEvent:getKeyCode(...)@266: {114}, {-231..113, 115..232-1}
* java.awt.event.KeyEvent:getKeyCode(...)@266: {70}, {-231..69, 71..232-1}
* java.util.Iterator:hasNext(...)@271: {0}, {1}
*/
261 if (event.getSource() == searchBox) {
262 if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
263 close();
264 } else if (event.getKeyCode() == KeyEvent.VK_ENTER) {
265 search(Direction.UP, searchBox.getText(), caseCheck.isSelected());
266 } else if (event.getKeyCode() != KeyEvent.VK_F3 && event.getKeyCode() != KeyEvent.VK_F) {
267 line = parent.getTextPane().getLastVisibleLine();
268 }
269 }
270
271 for (KeyListener listener : listeners.get(KeyListener.class)) {
272 listener.keyPressed(event);
273 }
274 }
275
276 /**
277 * {@inheritDoc}.
278 *
279 * @param event Key event
280 */
281 @Override
282 public void keyTyped(final KeyEvent event) {
283 //Ignore
/*
P/P * Method: void keyTyped(KeyEvent)
*/
284 }
285
286 /**
287 * {@inheritDoc}.
288 *
289 * @param event Key event
290 */
291 @Override
292 public void keyReleased(final KeyEvent event) {
293 //Ignore
/*
P/P * Method: void keyReleased(KeyEvent)
*/
294 }
295
296 /** Focuses the search box in the search bar. */
297 public void getFocus() {
/*
P/P * Method: void getFocus()
*/
298 SwingUtilities.invokeLater(new Runnable() {
299
300 /** {@inheritDoc} */
301 @Override
302 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.searchBox != null
*
* Presumptions:
* javax.swing.JTextField:getText(...)@305 != null
*/
303 searchBox.requestFocusInWindow();
304 searchBox.setSelectionStart(0);
305 searchBox.setSelectionEnd(searchBox.getText().length());
306 }
307 });
308 }
309
310 /** {@inheritDoc}. */
311 @Override
312 public String getSearchPhrase() {
/*
P/P * Method: String getSearchPhrase()
*
* Preconditions:
* this.searchBox != null
*
* Postconditions:
* init'ed(return_value)
*/
313 return searchBox.getText();
314 }
315
316 /** {@inheritDoc}. */
317 @Override
318 public boolean isCaseSensitive() {
/*
P/P * Method: bool isCaseSensitive()
*
* Preconditions:
* this.caseCheck != null
*
* Postconditions:
* init'ed(return_value)
*/
319 return caseCheck.isSelected();
320 }
321
322 /** {@inheritDoc}. */
323 @Override
324 public void insertUpdate(final DocumentEvent e) {
/*
P/P * Method: void insertUpdate(DocumentEvent)
*
* Preconditions:
* this.searchBox != null
*/
325 searchBox.setBackground(ColourManager.getColour("FFFFFF"));
326 }
327
328 /** {@inheritDoc}. */
329 @Override
330 public void removeUpdate(final DocumentEvent e) {
/*
P/P * Method: void removeUpdate(DocumentEvent)
*
* Preconditions:
* this.searchBox != null
*/
331 searchBox.setBackground(ColourManager.getColour("FFFFFF"));
332 }
333
334 /** {@inheritDoc}. */
335 @Override
336 public void changedUpdate(final DocumentEvent e) {
337 //Ignore
/*
P/P * Method: void changedUpdate(DocumentEvent)
*/
338 }
339
340 /** {@inheritDoc} */
341 @Override
342 public void addKeyListener(final KeyListener l) {
/*
P/P * Method: void addKeyListener(KeyListener)
*/
343 UIUtilities.invokeLater(new Runnable() {
344
345 /** {@inheritDoc} */
346 @Override
347 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.listeners != null
*/
348 listeners.add(KeyListener.class, l);
349 }
350 });
351 }
352
353 /** {@inheritDoc} */
354 @Override
355 public void removeKeyListener(final KeyListener l) {
/*
P/P * Method: void removeKeyListener(KeyListener)
*/
356 UIUtilities.invokeLater(new Runnable() {
357
358 /** {@inheritDoc} */
359 @Override
360 public void run() {
/*
P/P * Method: void run()
*
* Preconditions:
* this.listeners != null
*/
361 listeners.remove(KeyListener.class, l);
362 }
363 });
364 }
365 }
SofCheck Inspector Build Version : 2.17854
| SwingSearchBar.java |
2009-Jun-25 01:54:24 |
| SwingSearchBar.class |
2009-Sep-02 17:04:14 |
| SwingSearchBar$1.class |
2009-Sep-02 17:04:14 |
| SwingSearchBar$2.class |
2009-Sep-02 17:04:14 |
| SwingSearchBar$3.class |
2009-Sep-02 17:04:14 |
| SwingSearchBar$4.class |
2009-Sep-02 17:04:14 |
| SwingSearchBar$5.class |
2009-Sep-02 17:04:14 |