SimpleSearchQueryPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.xnap.XNap;
import org.xnap.gui.action.EraseAction;
import org.xnap.gui.component.HistoryComboBox;
import org.xnap.gui.component.XNapButton;
import org.xnap.gui.shortcut.ShortcutManager;
import org.xnap.gui.util.FocusHandler;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.gui.util.IconHelper;
import org.xnap.search.DefaultSearchFilter;
import org.xnap.search.MediaType;
import org.xnap.search.Search;
import org.xnap.search.SearchFilter;
import org.xnap.search.SearchManager;
import org.xnap.search.SearchProvider;
import org.xnap.search.SearchProviderContainer;
import org.xnap.util.Preferences;
/**
* This class provides a panel with widgets to enter search queries
* and to set a few search options. The queries are passed to the
* {@link xnap.search.SearchManager SearchManager}.
*
* @see AdvancedSearchQueryPanel
*/
public class SimpleSearchQueryPanel extends JPanel {
//--- Constant(s) ---
//--- Data field(s) ---
protected static Preferences prefs = Preferences.getInstance();
private HistoryComboBox jcbSearch;
private JComboBox jcbMediaType;
private SearchProviderContainer spcAll = new SearchProviderContainer("");
private int initialNetworkCount;
private QueryAction acQuery = new QueryAction();
//--- Constructor(s) ---
/**
* Constructs the search query panel.
*
* @param model model used by the HistoryComboBox
*/
public SimpleSearchQueryPanel(ComboBoxModel model)
{
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(5, 5, 5, 5));
// erase button
EraseAction acErase = new EraseAction();
JButton jbErase = new JButton(acErase);
jbErase.setMargin(new Insets(1, 1, 1, 1));
GridBagHelper.addComponent(this, jbErase, GridBagConstraints.WEST);
ShortcutManager.getInstance().add(acErase, this);
JLabel l = new JLabel(XNap.tr("Text", 1));
this.add(l);
// search field
jcbSearch = new HistoryComboBox(model, 15);
l.setLabelFor(jcbSearch.getTextField());
jcbSearch.setPreferences("search");
jcbSearch.setMinimumSize(jcbSearch.getPreferredSize());
GUIHelper.bindEnterKeyLocally (jcbSearch.getTextField(), acQuery);
acErase.setTextField(jcbSearch.getTextField());
GridBagHelper.addComponent(this, jcbSearch, GridBagConstraints.WEST);
// media type
jcbMediaType = new JComboBox();
GridBagHelper.addComponent
(this, jcbMediaType, GridBagConstraints.WEST);
// buttons
GridBagHelper.addComponent
(this, new XNapButton(acQuery), GridBagConstraints.WEST);
GridBagHelper.addComponent
(this, new XNapButton(new ShowAdvancedAction()),
GridBagConstraints.WEST);
acQuery.update();
addComponentListener(new FocusHandler(jcbSearch.getTextField()));
GUIHelper.setMnemonics(this);
}
public void addToHistory(SearchFilter filter)
{
jcbSearch.addDistinctItemAtTop(filter);
}
public DefaultSearchFilter getSearchFilter()
{
DefaultSearchFilter f = new DefaultSearchFilter();
f.put(SearchFilter.TEXT, jcbSearch.getText());
f.put(SearchFilter.MEDIA_TYPE, jcbMediaType.getSelectedItem());
return f;
}
/**
* Invoked by the {@link SearchPanel} when a {@link
* SearchProvider} has been added.
*/
public void providerAdded(SearchProvider provider)
{
spcAll.add(provider);
updateMediaTypes();
}
/**
* Invoked by the {@link SearchPanel} when a {@link
* SearchProvider} has been removed.
*/
public void providerRemoved(SearchProvider provider)
{
spcAll.remove(provider);
updateMediaTypes();
}
public void setSearchFilter(SearchFilter f)
{
jcbSearch.setText((String)f.get(SearchFilter.TEXT));
jcbMediaType.setSelectedItem(f.get(SearchFilter.MEDIA_TYPE));
}
private void updateMediaTypes()
{
jcbMediaType.removeAllItems();
MediaType[] types = spcAll.getSupportedMediaTypes();
if (types != null) {
for (int i = 0; i < types.length; i++) {
jcbMediaType.addItem(types[i]);
}
}
acQuery.update();
}
/**
* Performs a search.
*/
private class QueryAction extends AbstractAction {
public QueryAction()
{
putValue(Action.NAME, XNap.tr("Query"));
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Perform search"));
putValue(IconHelper.XNAP_ICON, "filefind.png");
}
public void actionPerformed(ActionEvent event)
{
DefaultSearchFilter filter = getSearchFilter();
try {
filter.validate();
}
catch (Exception e) {
StatusBar.setText(e.getMessage());
return;
}
Search search = SearchManager.getInstance().searchAll(filter);
SearchManager.getInstance().handle(search);
}
public void update()
{
setEnabled(spcAll.size() > 0);
}
}
/**
* Switches between simple and advanced view.
*/
private class ShowAdvancedAction extends AbstractAction {
public ShowAdvancedAction()
{
putValue(Action.NAME, XNap.tr("More Options"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Displays advanced search options panel."));
//putValue(IconHelper.XNAP_ICON, "2downarrow.png");
}
public void actionPerformed(ActionEvent event)
{
prefs.set("showAdvancedSearchOptions", "true");
}
}
}
The table below shows all metrics for SimpleSearchQueryPanel.java.




