View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  package org.xmlhammer.gui;
22  
23  import java.awt.BorderLayout;
24  import java.awt.FlowLayout;
25  import java.awt.Font;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  import java.awt.event.KeyAdapter;
29  import java.awt.event.KeyEvent;
30  
31  import javax.swing.ImageIcon;
32  import javax.swing.JButton;
33  import javax.swing.JCheckBox;
34  import javax.swing.JLabel;
35  import javax.swing.JPanel;
36  import javax.swing.border.EmptyBorder;
37  import javax.swing.event.AncestorEvent;
38  import javax.swing.event.AncestorListener;
39  
40  import org.bounce.FormLayout;
41  import org.bounce.QDialog;
42  import org.bounce.image.ImageLoader;
43  import org.xmlhammer.gui.history.HistoryComboBox;
44  import org.xmlhammer.gui.history.HistoryComboBoxModel;
45  import org.xmlhammer.gui.history.HistoryUtilities;
46  import org.xmlhammer.gui.output.ConsolePanel;
47  import org.xmlhammer.gui.output.JavaPanel;
48  import org.xmlhammer.gui.output.LogPanel;
49  import org.xmlhammer.gui.output.ResultNode;
50  import org.xmlhammer.gui.output.ResultPanel;
51  import org.xmlhammer.gui.output.SearchablePanel;
52  
53  /***
54   * The find dialog.
55   *
56   * @version	$Revision$, $Date$
57   * @author Edwin Dankert <edankert@gmail.com>
58   */
59  public class FindDialog extends QDialog {
60      private static final long serialVersionUID = 6475728656462272652L;
61  
62      private XMLHammer parent        = null;
63  
64      private static final ImageIcon NEXT_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/search_next.gif");
65      private static final ImageIcon PREVIOUS_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/search_previous.gif");
66      private static final ImageIcon WARNING_ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/search_warning.gif");
67  
68      private boolean foundSelectedNode = false;
69  
70      private JButton closeButton	    = null;
71  	private JButton nextButton		= null;
72      private JButton previousButton  = null;
73      private JLabel warningLabel     = null;
74  	
75  	// The components that contain the values
76  	private HistoryComboBox searchField		= null;
77  
78  	private JCheckBox matchCaseButton	= null;
79  
80  	/***
81  	 * The dialog that displays the properties for the document.
82  	 *
83  	 * @param frame the parent frame.
84  	 */
85  	public FindDialog(XMLHammer parent) {
86  		super(parent, false);
87  		
88  		this.parent = parent;
89  		
90  		setResizable(false);
91  		setTitle("Find");
92  		
93  		JPanel main = new JPanel(new BorderLayout());
94  
95  		JPanel searchPanel = new JPanel(new FormLayout(20, 7));
96          searchPanel.setBorder(new EmptyBorder( 10, 10, 10, 10));
97  	
98  		searchField = new HistoryComboBox(new HistoryComboBoxModel(HistoryUtilities.getInstance().getSearches()));
99  		searchField.getEditor().getEditorComponent().addKeyListener( new KeyAdapter() {
100 			public void keyPressed( KeyEvent e) {
101 				if ( e.getKeyCode() == KeyEvent.VK_ENTER) {
102 					nextButtonPressed();
103 				}
104 			}
105 		});
106 
107         searchField.addAncestorListener( new AncestorListener() {
108             public void ancestorAdded( AncestorEvent e) {
109                 searchField.getEditor().selectAll();
110                 searchField.requestFocusInWindow();
111             }
112 
113             public void ancestorMoved( AncestorEvent e) {}
114             public void ancestorRemoved( AncestorEvent e) {}
115         });
116 
117 		searchField.setFont(searchField.getFont().deriveFont( Font.PLAIN));
118 		searchField.setEditable(true);
119 
120 		searchPanel.add(new JLabel("Find:"), FormLayout.LEFT);
121 		searchPanel.add(searchField, FormLayout.RIGHT_FILL);
122 
123 		matchCaseButton = new JCheckBox("Match case");
124 		matchCaseButton.setMnemonic('M');
125         
126         JPanel matchCasePanel = new JPanel(new BorderLayout());
127         matchCasePanel.add(matchCaseButton, BorderLayout.WEST);
128         
129         warningLabel = new JLabel("Not found");
130         warningLabel.setIcon(WARNING_ICON);
131         warningLabel.setVisible(false);
132         matchCasePanel.add(warningLabel, BorderLayout.EAST);
133 
134         searchPanel.add(new JLabel(""), FormLayout.LEFT);
135         searchPanel.add(matchCasePanel, FormLayout.RIGHT_FILL);
136 
137 		closeButton = new JButton("Close");
138 		closeButton.setMnemonic('C');
139 		closeButton.addActionListener(new ActionListener() {
140 			public void actionPerformed(ActionEvent e) {
141 				close();
142 			}
143 		});
144 
145 		nextButton = new JButton("Find Next", NEXT_ICON);
146 		nextButton.addActionListener(new ActionListener() {
147 			public void actionPerformed(ActionEvent e) {
148 				nextButtonPressed();
149 			}
150 		});
151 		getRootPane().setDefaultButton(nextButton);
152 
153         previousButton = new JButton("Find Previous", PREVIOUS_ICON);
154         previousButton.setMnemonic('P');
155         previousButton.addActionListener(new ActionListener() {
156             public void actionPerformed(ActionEvent e) {
157                 previousButtonPressed();
158             }
159         });
160 
161         JPanel closeButtonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
162         closeButtonPanel.add(closeButton);
163 
164         JPanel findButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
165         findButtonPanel.setBorder(new EmptyBorder(0, 0, 0, 20));
166         findButtonPanel.add(nextButton);
167         findButtonPanel.add(previousButton);
168 
169         JPanel buttonPanel = new JPanel(new BorderLayout());
170         buttonPanel.setBorder(new EmptyBorder( 5, 0, 3, 0));
171         buttonPanel.add(closeButtonPanel, BorderLayout.EAST);
172         buttonPanel.add(findButtonPanel, BorderLayout.WEST);
173 
174         main.add(searchPanel, BorderLayout.NORTH);
175 		main.add(buttonPanel, BorderLayout.SOUTH);
176 
177 		setContentPane(main);
178         
179         pack();
180 		setLocationRelativeTo(parent);
181 	}
182 	
183 	private void nextButtonPressed() {
184         search(true);
185 	}
186 
187     private void previousButtonPressed() {
188         search(false);
189     }
190 
191     private void search(boolean forward) {
192         Object item = searchField.getEditor().getItem();
193         foundSelectedNode = false;
194 
195         if (item != null) {
196             SearchablePanel searchablePanel = getSelectedSearchablePanel();
197             
198             if (searchablePanel != null) {
199                 if (searchablePanel.search(item.toString(), matchCaseButton.isSelected(), forward)) {
200                     warningLabel.setVisible(false);
201                     return;
202                 }
203             }
204 
205             warningLabel.setVisible(true);
206         }
207     }
208     
209     private SearchablePanel getSelectedSearchablePanel() {
210         ProjectView view = parent.getProjectsView().getSelectedView();
211         if (view != null) {
212             ResultPanel panel = view.getResultPanel();
213             if (panel.isShowing()) {
214                 return panel;
215             }
216 
217             LogPanel logPanel = view.getLogPanel();
218             if (logPanel.isShowing()) {
219                 return logPanel;
220             }
221 
222             ConsolePanel consolePanel = view.getConsolePanel();
223             if (consolePanel.isShowing()) {
224                 return consolePanel;
225             }
226 
227             JavaPanel javaPanel = view.getJavaPanel();
228             if (javaPanel.isShowing()) {
229                 return javaPanel;
230             }
231         }
232         
233         return null;
234     }
235     
236     public ResultNode find(ResultNode start, ResultNode node, String search, boolean forwards, boolean matchCase) {
237         if (foundSelectedNode && matches(node.getValue(), search, matchCase) && forwards) {
238             return node;
239         } 
240 
241         if (node == start && forwards) {
242             foundSelectedNode = true;
243         }
244         
245         if (forwards) {
246             for (int i = 0; i < node.getChildCount(); i++) {
247                 ResultNode result = find(start, (ResultNode)node.getChildAt(i), search, forwards, matchCase);
248                 if (result != null) {
249                     return result;
250                 }
251             }
252         } else {
253             for (int i = node.getChildCount()-1; i >= 0; i--) {
254                 ResultNode result = find(start, (ResultNode)node.getChildAt(i), search, forwards, matchCase);
255                 if (result != null) {
256                     return result;
257                 }
258             }
259         }
260         
261         if (foundSelectedNode && matches(node.getValue(), search, matchCase) && !forwards) {
262             return node;
263         }
264         
265         if (node == start && !forwards) {
266             foundSelectedNode = true;
267         }
268 
269         return null;
270     }
271     
272     public boolean matches(String value, String search, boolean matchCase) {
273         if (matchCase) {
274             return value.indexOf(search) != -1;
275         }
276 
277         return value.toLowerCase().indexOf(search.toLowerCase()) != -1;
278     }
279 
280     public void cancel() {
281         warningLabel.setVisible(false);
282         super.cancel();
283     }
284 
285     public void close() {
286         warningLabel.setVisible(false);
287         super.close();
288 	}
289 
290 	/***
291 	 * Initialises the values in the dialog.
292 	 */
293 	public void init() {
294 		searchField.requestFocus();
295 		
296 		if (searchField.getItemCount() > 0) {
297 			searchField.removeAllItems();
298 		}
299 		
300 //		for ( int i = 0; i < searches.size(); i++) {
301 //			searchField.addItem(searches.elementAt(i));
302 //		}
303 //
304 //		if ( searchField.getItemCount() > 0) {
305 //			searchField.setSelectedIndex( 0);
306 //		}
307 //
308 //		matchCaseButton.setSelected( editor.getProperties().isMatchCase());
309 //		
310 //		if ( editor.getProperties().isDirectionDown()) {
311 //			downButton.setSelected( true);
312 //		} else {
313 //			upButton.setSelected( true);
314 //		}
315 
316 		searchField.getEditor().selectAll();
317 	}
318 }