View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui;
21  
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.awt.event.ActionEvent;
26  import javax.swing.AbstractAction;
27  import javax.swing.Action;
28  import javax.swing.ComboBoxModel;
29  import javax.swing.JButton;
30  import javax.swing.JComboBox;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.swing.border.EmptyBorder;
34  import org.xnap.XNap;
35  import org.xnap.gui.action.EraseAction;
36  import org.xnap.gui.component.HistoryComboBox;
37  import org.xnap.gui.component.XNapButton;
38  import org.xnap.gui.shortcut.ShortcutManager;
39  import org.xnap.gui.util.FocusHandler;
40  import org.xnap.gui.util.GUIHelper;
41  import org.xnap.gui.util.GridBagHelper;
42  import org.xnap.gui.util.IconHelper;
43  import org.xnap.search.DefaultSearchFilter;
44  import org.xnap.search.MediaType;
45  import org.xnap.search.Search;
46  import org.xnap.search.SearchFilter;
47  import org.xnap.search.SearchManager;
48  import org.xnap.search.SearchProvider;
49  import org.xnap.search.SearchProviderContainer;
50  import org.xnap.util.Preferences;
51  
52  /***
53   * This class provides a panel with widgets to enter search queries
54   * and to set a few search options. The queries are passed to the 
55   * {@link xnap.search.SearchManager SearchManager}.
56   *
57   * @see AdvancedSearchQueryPanel
58   */
59  public class SimpleSearchQueryPanel extends JPanel {
60  
61      //--- Constant(s) ---
62  
63      //--- Data field(s) ---
64  
65      protected static Preferences prefs = Preferences.getInstance();
66  
67      private HistoryComboBox jcbSearch;
68      private JComboBox jcbMediaType;
69  
70      private SearchProviderContainer spcAll = new SearchProviderContainer("");
71  	private int initialNetworkCount;
72  	private QueryAction acQuery = new QueryAction();
73  
74      //--- Constructor(s) ---
75      
76      /***
77       * Constructs the search query panel.
78  	 *
79  	 * @param model model used by the HistoryComboBox
80       */
81      public SimpleSearchQueryPanel(ComboBoxModel model) 
82      {
83  		setLayout(new GridBagLayout());
84  		setBorder(new EmptyBorder(5, 5, 5, 5));
85  
86  		// erase button
87  		EraseAction acErase = new EraseAction();
88  		JButton jbErase = new JButton(acErase);
89  		jbErase.setMargin(new Insets(1, 1, 1, 1));
90  		GridBagHelper.addComponent(this, jbErase, GridBagConstraints.WEST);
91  		ShortcutManager.getInstance().add(acErase, this);
92  
93  		JLabel l = new JLabel(XNap.tr("Text", 1));
94  		this.add(l);
95  	    
96  		// search field
97  		jcbSearch = new HistoryComboBox(model, 15);
98  		l.setLabelFor(jcbSearch.getTextField());
99  		jcbSearch.setPreferences("search");
100 		jcbSearch.setMinimumSize(jcbSearch.getPreferredSize());
101 		GUIHelper.bindEnterKeyLocally (jcbSearch.getTextField(), acQuery);
102 		acErase.setTextField(jcbSearch.getTextField());
103 		GridBagHelper.addComponent(this, jcbSearch, GridBagConstraints.WEST);
104 
105 		// media type
106 		jcbMediaType = new JComboBox();
107 		GridBagHelper.addComponent
108 			(this, jcbMediaType, GridBagConstraints.WEST);
109 
110 		// buttons
111 		GridBagHelper.addComponent
112 			(this, new XNapButton(acQuery), GridBagConstraints.WEST);
113 		GridBagHelper.addComponent
114   			(this, new XNapButton(new ShowAdvancedAction()), 
115 			 GridBagConstraints.WEST);
116 
117 		acQuery.update();
118 		
119 		addComponentListener(new FocusHandler(jcbSearch.getTextField()));
120 		GUIHelper.setMnemonics(this);
121     }
122 
123     public void addToHistory(SearchFilter filter)
124     {
125 		jcbSearch.addDistinctItemAtTop(filter);
126     }
127 
128     public DefaultSearchFilter getSearchFilter()
129     {
130 		DefaultSearchFilter f = new DefaultSearchFilter();
131 
132 		f.put(SearchFilter.TEXT, jcbSearch.getText());
133 		f.put(SearchFilter.MEDIA_TYPE, jcbMediaType.getSelectedItem());
134 
135 		return f;
136     }
137 
138     /***
139      * Invoked by the {@link SearchPanel} when a {@link
140      * SearchProvider} has been added.  
141      */
142     public void providerAdded(SearchProvider provider)
143     {
144 		spcAll.add(provider);
145 		updateMediaTypes();
146     }
147 
148     /***
149      * Invoked by the {@link SearchPanel} when a {@link
150      * SearchProvider} has been removed.  
151      */
152     public void providerRemoved(SearchProvider provider)
153     {
154 		spcAll.remove(provider);
155 		updateMediaTypes();
156     }
157 
158     public void setSearchFilter(SearchFilter f)
159     {
160 		jcbSearch.setText((String)f.get(SearchFilter.TEXT));
161 		jcbMediaType.setSelectedItem(f.get(SearchFilter.MEDIA_TYPE));
162     }
163 
164     private void updateMediaTypes()
165     {
166 		jcbMediaType.removeAllItems();
167 		MediaType[] types = spcAll.getSupportedMediaTypes();
168 		if (types != null) {
169 			for (int i = 0; i < types.length; i++) {
170 				jcbMediaType.addItem(types[i]);
171 			}
172 		}
173 		acQuery.update();
174     }
175 
176     /***
177      * Performs a search.
178      */
179     private class QueryAction extends AbstractAction {
180 
181         public QueryAction() 
182 		{
183             putValue(Action.NAME, XNap.tr("Query"));
184             putValue(Action.SHORT_DESCRIPTION, XNap.tr("Perform search"));
185 			putValue(IconHelper.XNAP_ICON, "filefind.png");
186         }
187 
188         public void actionPerformed(ActionEvent event) 
189 		{
190 			DefaultSearchFilter filter = getSearchFilter();
191 			try {
192 				filter.validate();
193 			}
194 			catch (Exception e) {
195 				StatusBar.setText(e.getMessage());
196 				return;
197 			}
198 
199 			Search search = SearchManager.getInstance().searchAll(filter);
200 			SearchManager.getInstance().handle(search);
201         }
202 
203 		public void update()
204 		{
205 			setEnabled(spcAll.size() > 0);
206 		}
207 
208     }
209 
210     /***
211      * Switches between simple and advanced view.
212      */
213     private class ShowAdvancedAction extends AbstractAction {
214 	
215         public ShowAdvancedAction() 
216 		{
217             putValue(Action.NAME, XNap.tr("More Options"));
218             putValue(Action.SHORT_DESCRIPTION, 
219 					 XNap.tr("Displays advanced search options panel."));
220 			//putValue(IconHelper.XNAP_ICON, "2downarrow.png");
221         }
222 	
223         public void actionPerformed(ActionEvent event) 
224 		{
225 			prefs.set("showAdvancedSearchOptions", "true");
226         }
227 
228     }
229 }