SearchPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
xnap.gui |
![]() |
![]() |
XNap |
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.
/*
* Java Napster version x.yz (for current version number as well as for
* additional information see version.txt)
*
* Previous versions of this program were written by Florian Student
* and Michael Ransburg available at www.weblicity.de/jnapster and
* http://www.tux.org/~daneel/content/projects/10.shtml respectively.
*
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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 xnap.gui;
import xnap.*;
import xnap.net.*;
import xnap.net.nap.*;
import xnap.util.*;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.*;
import javax.accessibility.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class SearchPanel extends AbstractPanel
implements PropertyChangeListener, BrowseHandler {
//--- Constant(s) ---
public final static String HISTORY_FILENAME
= XNap.getHomeDir() + "search_history";
//--- Data field(s) ---
private Preferences prefs = Preferences.getInstance();
private DownloadCollector downloadCollector;
private ObservableVector servers = null;
JPopupMenu popup;
private EditableComboBox jcSearch;
private JComboBox jcBitrate;
private JComboBox jcCompare;
private JComboBox jcMediaType;
private ClosableTabbedPane jtpSearches;
private BrowseAction browseAction = new BrowseAction();
private CheckAction checkAction = new CheckAction();
private DownloadAction downloadAction = new DownloadAction();
private EraseAction eraseAction = new EraseAction();
private QueryAction queryAction = new QueryAction();
private CardLayout clCenter;
private Container jpCenter;
//--- Constructor(s) ---
public SearchPanel() {
super();
initialize ();
}
//--- Method(s) ---
private void initialize() {
/* top panels */
JPanel jpaTop = new JPanel();
popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem(downloadAction);
popup.add(menuItem);
/*
menuItem = new JMenuItem(checkAction);
popup.add(menuItem);
*/
menuItem = new JMenuItem(browseAction);
popup.add(menuItem);
// first panel
JPanel jpaFirst = new JPanel();
BoxLayout bx = new BoxLayout(jpaFirst, BoxLayout.X_AXIS);
jpaFirst.setLayout(bx);
JButton jbErase
= new JButton(eraseAction);
jbErase.setBorder(new EmptyBorder(0, 0, 0, 0));
jpaFirst.add(jbErase);
jpaFirst.add(new JLabel(" Search "));
jcSearch = new EditableComboBox(queryAction);
jcSearch.readHistoryFile(new File(HISTORY_FILENAME));
jpaFirst.add(jcSearch);
jcCompare = new JComboBox(AbstractSearchFilter.compares);
jcCompare.setSelectedIndex(prefs.getSearchCompare());
jpaFirst.add(jcCompare);
jcBitrate = new JComboBox(AbstractSearchFilter.bitrates);
jcBitrate.setSelectedIndex(prefs.getSearchBitrate());
jpaFirst.add(jcBitrate);
jcMediaType = new JComboBox(AbstractSearchFilter.media);
jcMediaType.setSelectedIndex(prefs.getSearchMediaType());
jpaFirst.add(jcMediaType);
JButton jbQuery = new JButton(queryAction);
jpaFirst.add(jbQuery);
/* tabbed pane */
jtpSearches = new ClosableTabbedPane(XNap.getSmallIcon("remove.png"));
jtpSearches.addContainerListener(new TabListener());
/* logo */
JLabel jlLogo = new JLabel(XNap.getImage("xnap_logo.png"));
/* content */
clCenter = new CardLayout();
jpCenter = new Container();
jpCenter.setLayout(clCenter);
jpCenter.add(jlLogo, "Logo");
jpCenter.add(jtpSearches, "Search");
setLayout(new java.awt.BorderLayout());
add(jpaFirst, "North");
add(jpCenter, "Center");
}
/**
* Handles browse request from sub panels.
*/
public void doBrowse(AbstractSearchResult sr)
{
SearchSubPanel panel = createSubPanel(sr.getUser());
panel.browseRequest(sr);
}
public AbstractAction[] getActions()
{
return (new AbstractAction[] { downloadAction/*, checkAction*/,
browseAction });
}
public void setDownloadCollector(DownloadCollector newValue)
{
downloadCollector = newValue;
}
public JPopupMenu getPopupMenu()
{
return popup;
}
public void setSearchPrefs()
{
prefs.setSearchBitrate(jcBitrate.getSelectedIndex());
prefs.setSearchCompare(jcCompare.getSelectedIndex());
prefs.setSearchMediaType(jcMediaType.getSelectedIndex());
jcSearch.writeHistoryFile(new File(HISTORY_FILENAME));
}
public void setServers(ObservableVector newValue) {
servers = newValue;
}
public void propertyChange(PropertyChangeEvent e)
{
super.propertyChange(e);
if (e.getPropertyName().equals("status"))
{
// pass on sub panel events
setStatus((String)e.getNewValue());
}
}
private SearchSubPanel createSubPanel(String title)
{
SearchSubPanel panel = new SearchSubPanel(this, servers,
downloadCollector);
panel.addPropertyChangeListener(this);
jtpSearches.addTab(title, panel);
return panel;
}
/**
*
*/
private class TabListener implements ContainerListener
{
public void componentAdded(ContainerEvent e)
{
updateLayout(e.getID());
}
public void componentRemoved(ContainerEvent e)
{
updateLayout(e.getID());
}
private void updateLayout(int id)
{
Debug.log("search tab count: " + jtpSearches.getTabCount());
// we get the events after the tab has been added
// and before it is actually removed!
if (id == ContainerEvent.COMPONENT_REMOVED
&& jtpSearches.getTabCount() == 1)
clCenter.show(jpCenter, "Logo");
else
clCenter.show(jpCenter, "Search");
}
}
/**
* Erases the current search text.
*/
private class EraseAction extends AbstractAction {
public EraseAction()
{
putValue(Action.NAME, "");
putValue(Action.SMALL_ICON,
XNap.getSmallIcon("locationbar_erase.png"));
putValue(Action.SHORT_DESCRIPTION,
"Erase the search text" );
putValue(Action.MNEMONIC_KEY, new Integer('E'));
putValue(Action.ACCELERATOR_KEY, new Integer('E'));
}
public void actionPerformed(ActionEvent event)
{
jcSearch.setText("");
jcSearch.grabFocus();
}
}
/**
* Performs a search.
*/
private class QueryAction extends AbstractAction {
public QueryAction()
{
putValue(Action.NAME, "Query");
putValue(Action.SHORT_DESCRIPTION,
"Perform search");
putValue(Action.MNEMONIC_KEY, new Integer('Q'));
putValue(Action.ACCELERATOR_KEY, new Integer('Q'));
}
public void actionPerformed(ActionEvent event)
{
String s = jcSearch.getText().trim();
if (!s.equals("")) {
int c = jcCompare.getSelectedIndex();
String b = ((String)jcBitrate.getSelectedItem()).trim();
int m =jcMediaType.getSelectedIndex();
SearchSubPanel panel = createSubPanel(s);
panel.searchRequest(s, c, b, m);
jcSearch.addDistinctItemAtTop(s);
}
else
setStatus("What are you trying to search for?");
}
}
/**
* Passes the download request to the selected sub panel.
*/
private class DownloadAction extends AbstractAction {
public DownloadAction()
{
putValue(Action.NAME, "Download");
putValue(Action.SHORT_DESCRIPTION,
"Download selected file(s)");
putValue(Action.SMALL_ICON, XNap.getIcon("bottom.png"));
putValue(Action.MNEMONIC_KEY, new Integer('D'));
putValue(Action.ACCELERATOR_KEY, new Integer('D'));
}
public void actionPerformed(ActionEvent event)
{
if (jtpSearches.getSelectedComponent() != null)
((SearchSubPanel)jtpSearches.getSelectedComponent())
.doDownload();
}
}
/**
* Passes the download request to the selected sub panel.
*/
private class CheckAction extends AbstractAction {
public CheckAction()
{
putValue(Action.NAME, "Check availabilty");
putValue(Action.SHORT_DESCRIPTION,
"Check if the selected file(s) are available");
putValue(Action.SMALL_ICON, XNap.getIcon("help.png"));
putValue(Action.MNEMONIC_KEY, new Integer('C'));
putValue(Action.ACCELERATOR_KEY, new Integer('C'));
}
public void actionPerformed(ActionEvent event)
{
if (jtpSearches.getSelectedComponent() != null)
((SearchSubPanel)jtpSearches.getSelectedComponent())
.doCheck();
}
}
/**
* Passes the download request to the selected sub panel.
*/
private class BrowseAction extends AbstractAction {
public BrowseAction()
{
putValue(Action.NAME, "Browse");
putValue(Action.SHORT_DESCRIPTION,
"Browse selected user");
putValue(Action.SMALL_ICON, XNap.getIcon("filefind.png"));
putValue(Action.MNEMONIC_KEY, new Integer('B'));
putValue(Action.ACCELERATOR_KEY, new Integer('B'));
}
public void actionPerformed(ActionEvent event)
{
if (jtpSearches.getSelectedComponent() != null)
((SearchSubPanel)jtpSearches.getSelectedComponent())
.doBrowse();
}
}
}
The table below shows all metrics for SearchPanel.java.




