QueryPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.furthurnet.furi |
![]() |
![]() |
Furthurnet |
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.
/*
* FURTHUR - A distributed peer-to-peer file sharing system.
* Copyright (C) 2001-2002 Jamie M. Addessi, Furthur Network
*
* 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 org.furthurnet.furi;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.Timer;
import javax.swing.border.BevelBorder;
import org.furthurnet.datastructures.supporting.Common;
import org.furthurnet.datastructures.supporting.Constants;
import org.furthurnet.datastructures.supporting.DownloadTracker;
import org.furthurnet.servergui.Help;
import org.furthurnet.xmlparser.EncodingException;
import org.furthurnet.xmlparser.NetworkSpec;
import org.furthurnet.xmlparser.PopupProviderResult;
import org.furthurnet.xmlparser.QueryResultItem;
import org.furthurnet.xmlparser.QueryResultList;
public class QueryPanel extends JPanel implements ActionListener, KeyListener, PopupProviderResult
{
private MainFrame mainFrame = null;
private String networkType = null;
private String searchId = null;
NetworkSpec networkSpec = null;
QueryResultList results = null;
JButton searchButton = new JButton("Search");
JButton clearButton = new JButton("Clear");
JButton downloadButton = new JButton("Download Selected");
JButton newSearchButton = new JButton("New Search");
JButton helpButton1 = new JButton("Help");
JButton helpButton2 = new JButton("Help");
private String searchResultsLabelString = new String("SEARCH RESULTS (click columns to sort or right-click a row for options)");
private JLabel searchResultsLabel = new JLabel(searchResultsLabelString, JLabel.CENTER);
Timer timer = null;
private int numResultsFound = 0;
private static int MAX_RESULT_UPDATES = 300;
private Vector remoteHostList = null;
private JLabel searchLabel = new JLabel("...Performing search...", JLabel.CENTER);
JMenuItem download = null;
JMenuItem details = null;
JMenuItem chat = null;
JMenuItem viewId = null;
JFileChooser fileChooser = null;
MouseListener listener = null;
JPanel cards = null;
JPanel resultTable = null;
private static String QUERY_PANEL = "query";
private static String RESULTS_PANEL = "results";
private static String ERROR_PANEL = "error";
public QueryPanel(MainFrame _mainFrame, String _networkType, MouseListener _listener)
{
mainFrame = _mainFrame;
networkType = _networkType;
listener = _listener;
searchButton.addActionListener(this);
clearButton.addActionListener(this);
downloadButton.addActionListener(this);
newSearchButton.addActionListener(this);
helpButton1.addActionListener(this);
helpButton2.addActionListener(this);
searchButton.setIcon(Res.getIcon("Query.Icon"));
clearButton.setIcon(Res.getIcon("Clear.Icon"));
downloadButton.setIcon(Res.getIcon("Download.Icon"));
newSearchButton.setIcon(Res.getIcon("NewSearch.Icon"));
helpButton1.setIcon(Res.getIcon("Help.Icon"));
helpButton2.setIcon(Res.getIcon("Help.Icon"));
searchButton.setToolTipText(Res.getStr("Main.QueryPanel.SearchButton.ToolTip"));
clearButton.setToolTipText(Res.getStr("Main.QueryPanel.ClearButton.ToolTip"));
downloadButton.setToolTipText(Res.getStr("Main.QueryPanel.DownloadButton.ToolTip"));
newSearchButton.setToolTipText(Res.getStr("Main.QueryPanel.NewSearchButton.ToolTip"));
timer = new Timer(500, this);
cards = new JPanel();
cards.setLayout(new CardLayout());
setLayout(new BorderLayout());
add(cards, BorderLayout.CENTER);
setupErrorPanel();
try
{
networkSpec = mainFrame.getNetworkSpec(networkType);
}
catch (Exception e)
{
e.printStackTrace();
showErrorPanel();
return;
}
setupQueryPanel();
setupResultList();
showQueryPanel();
}
public void setSearchCount(int count)
{
searchResultsLabel.setText(searchResultsLabelString + " -- " + count + " filesets found");
}
private void showErrorPanel()
{
clearListeners();
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, ERROR_PANEL);
}
private void showQueryPanel()
{
clearListeners();
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, QUERY_PANEL);
}
private void showResultsPanel()
{
try
{
clearListeners();
results = new QueryResultList(networkSpec, searchLabel, false, QueryResultList.NORMAL, this);
results.addMouseListener(listener);
resultTable.removeAll();
resultTable.add(results.getTable(), BorderLayout.CENTER);
CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, RESULTS_PANEL);
}
catch (Exception e)
{
e.printStackTrace();
showErrorPanel();
}
}
private void clearListeners()
{
if (results != null)
results.removeMouseListener(listener);
}
public void setupErrorPanel()
{
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
mainPanel.add(new JLabel("Unable to read XML resources.", JLabel.CENTER), BorderLayout.CENTER);
cards.add(mainPanel, ERROR_PANEL);
}
private void setupQueryPanel()
{
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
mainPanel.add(new JLabel("Furthur Network Search : " + networkSpec.getDisplayName(), JLabel.CENTER), BorderLayout.NORTH);
JPanel queryPanel = new JPanel();
queryPanel.setLayout(new FlowLayout());
queryPanel.add(networkSpec.createQueryPanel());
networkSpec.addQueryKeyListener(this);
JScrollPane scrollPane = new JScrollPane(queryPanel);
mainPanel.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
JPanel clearPanel = new JPanel();
clearPanel.setLayout(new FlowLayout());
JPanel helpPanel1 = new JPanel();
helpPanel1.setLayout(new FlowLayout());
buttonPanel.add(searchButton);
clearPanel.add(clearButton);
helpPanel1.add(helpButton1);
JPanel tp = new JPanel();
tp.setLayout(new BorderLayout());
tp.add(helpPanel1, BorderLayout.WEST);
tp.add(buttonPanel, BorderLayout.CENTER);
tp.add(clearPanel, BorderLayout.EAST);
tp.setBorder(BorderFactory.createRaisedBevelBorder());
JPanel card = new JPanel();
card.setLayout(new BorderLayout());
card.add(mainPanel, BorderLayout.CENTER);
card.add(tp, BorderLayout.SOUTH);
cards.add(card, QUERY_PANEL);
}
private void setupResultList()
{
try
{
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
mainPanel.add(searchResultsLabel, BorderLayout.NORTH);
resultTable = new JPanel(new BorderLayout());
mainPanel.add(resultTable, BorderLayout.CENTER); // the double click functionality is done by QueryResultList
JPanel buttons = new JPanel();
buttons.setLayout(new BorderLayout());
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout());
JPanel downloadPanel = new JPanel();
downloadPanel.setLayout(new FlowLayout());
JPanel helpPanel2 = new JPanel();
helpPanel2.setLayout(new FlowLayout());
searchPanel.add(newSearchButton);
downloadPanel.add(downloadButton);
helpPanel2.add(helpButton2);
buttons.add(helpPanel2, BorderLayout.WEST);
buttons.add(downloadPanel, BorderLayout.CENTER);
buttons.add(searchPanel, BorderLayout.EAST);
JPanel card = new JPanel();
card.setLayout(new BorderLayout());
card.add(mainPanel, BorderLayout.CENTER);
card.add(buttons, BorderLayout.SOUTH);
cards.add(card, RESULTS_PANEL);
}
catch (Exception e)
{
showErrorPanel();
}
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == timer)
{
searchLabel.setText("." + searchLabel.getText() + ".");
getNewResults();
}
else if (!mainFrame.isSearchTabSelected())
return;
else if (e.getSource() == searchButton)
{
searchLabel.setText("...Performing search...");
search();
}
else if ((e.getSource() == downloadButton) || (e.getSource() == download))
{
download();
}
else if (e.getSource() == newSearchButton)
{
timer.stop();
mainFrame.stopSearch(searchId);
showQueryPanel();
}
else if (e.getSource() == clearButton)
{
networkSpec.clearQuerySelections();
}
else if ((e.getSource() == helpButton1) || (e.getSource() == helpButton2))
{
Help.showHelp(mainFrame, Help.HELP_SEARCH, false);
}
else if (e.getSource() == details)
{
results.showDetails();
}
else if (e.getSource() == viewId)
{
showID();
}
else if (e.getSource() == chat)
{
try
{
ServiceManager.getIrcManager().createPrivateChat(ServiceManager.getCfg().mIrcNickname, results.getSelectedRow().remoteUserNick);
}
catch (Exception e2)
{}
}
}
public void getNewResults()
{
numResultsFound++;
if (numResultsFound > MAX_RESULT_UPDATES)
{
timer.stop();
return;
}
Vector res = mainFrame.getNewResults(searchId);
results.addResults(res);
setSearchCount(results.resultList.size());
}
private void search()
{
numResultsFound = 0;
if (!mainFrame.isConnected())
{
JOptionPane.showMessageDialog(mainFrame, "You are not currently connected to the Furthur network.", "Cannot Search", JOptionPane.ERROR_MESSAGE);
return;
}
String searchStr = null;
try
{
searchStr = networkSpec.encodeQueryValues(mainFrame.isFirewall());
}
catch (EncodingException ee)
{
JOptionPane.showMessageDialog(mainFrame, ee.getMessage(), "Invalid Search Parameters", JOptionPane.ERROR_MESSAGE);
return;
}
if (searchStr == null)
{
JOptionPane.showMessageDialog(mainFrame, "You must specify at least one search parameter.", "Invalid Search Parameters", JOptionPane.ERROR_MESSAGE);
return;
}
searchId = mainFrame.search(searchStr, networkSpec.getNetworkType());
timer.start();
showResultsPanel();
}
public void keyTyped(KeyEvent e)
{
}
public void keyReleased(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
searchLabel.setText("...Performing search...");
search();
}
}
public void keyPressed(KeyEvent e)
{
}
public JPopupMenu getPopup(QueryResultItem qri)
{
String nick = "Unknown";
if ((qri.remoteUserNick != null) && (qri.remoteUserNick.length() > 0))
nick = qri.remoteUserNick;
if (nick.endsWith(ServiceManager.getCfg().mIrcNickIdentifier))
nick = nick.substring(0, nick.length()-1);
download = new JMenuItem("Download");
download.setIcon(Res.getIcon("Download.Icon"));
// details = new JMenuItem("View Object Details");
chat = new JMenuItem("Private Message : " + nick);
chat.setIcon(Res.getIcon("SmallChat.Icon"));
if ((nick.equals("Unknown")) || (!mainFrame.chatPane().mAuthenticated))
chat.setEnabled(false);
if (DownloadTracker.isCurrentlyDownloading(qri.id))
download.setText("View Download Progress");
download.addActionListener(this);
// details.addActionListener(this);
chat.addActionListener(this);
JPopupMenu popup = new JPopupMenu();
popup.add(download);
// popup.add(details);
popup.add(chat);
viewId = new JMenuItem("View File Set ID");
viewId.addActionListener(this);
if (Constants.ADMIN)
popup.add(viewId);
return popup;
}
public void doubleClick()
{
download();
}
public void finish()
{
timer.stop();
timer = null;
}
public static void download(QueryResultItem selected, QueryResultList results)
{
MainFrame mainFrame = ServiceManager.getManager().getMainFrame();
if (selected == null)
return;
try
{
String saveLoc =
Common.appendDel(ServiceManager.getCfg().mDefaultSaveLocation);
String saveName = Common.setFolderName(selected.getName());
if ((mainFrame.matchShare(selected.networkType, selected.size, selected.id)) || (mainFrame.matchAmbiguousShare(selected.networkType, selected.size, selected.getName())))
{
JOptionPane.showMessageDialog(mainFrame, Common.insertCRs("You have an item already shared that conflicts with this one."), "Cannot Save", JOptionPane.ERROR_MESSAGE);
return;
}
if ((mainFrame.matchPartial(selected.networkType, selected.size, selected.id)) || (mainFrame.matchAmbiguousPartial(selected.networkType, selected.size, selected.getName())))
{
JOptionPane.showMessageDialog(mainFrame, Common.insertCRs("You have an item already in your partials list that conflicts with this one."), "Cannot Save", JOptionPane.ERROR_MESSAGE);
return;
}
boolean prompt = ! ServiceManager.getCfg().mDefaultSavingEnabled;
if ((mainFrame.matchShareName(saveName)) || (mainFrame.matchPartialName(saveName)))
{
int choice = JOptionPane.showConfirmDialog(mainFrame, Common.insertCRs("You already have an object shared or downloading with the name " + saveName + ". To proceed, you will have to specify a different name and location. Are you sure you want to proceed?"), "Warning: Object Exists", JOptionPane.WARNING_MESSAGE);
if (choice == JOptionPane.YES_OPTION)
prompt = true;
else
return;
}
if (!prompt)
{
if (!mainFrame.validateOutputFolders(saveLoc))
{
mainFrame.showUserPanel();
if (!mainFrame.validateOutputFolders(saveLoc))
return;
}
}
else
{
boolean repeat;
do
{
repeat = false;
SaveFolderChooser sfc = new SaveFolderChooser(mainFrame, saveLoc, saveName);
sfc.show();
saveLoc = sfc.getParentFolder();
saveName = sfc.getSaveName();
if (saveLoc == null)
return;
File target = new File(saveLoc + saveName);
if (target.exists())
{
if (target.isFile())
{
JOptionPane.showMessageDialog(mainFrame, Common.insertCRs("A file exists with the specified name. Please specify a folder where the file set contents should be saved."), "Cannot Save", JOptionPane.ERROR_MESSAGE);
repeat = true;
}
if ((target.isDirectory()) && (target.listFiles().length > 0))
{
int choice = JOptionPane.showConfirmDialog(mainFrame, Common.insertCRs("The specified folder already exists. If you proceed, the contents of the file set will be written to this folder, and conflicting files will be overwritten. It is recommended that you specify a new folder to be created instead. Are you sure you want to proceed?"), "Warning: Folder Exists", JOptionPane.WARNING_MESSAGE);
if (choice != JOptionPane.YES_OPTION)
repeat = true;
}
}
}
while (repeat);
}
if (saveLoc == null)
return;
selected.saveLoc = saveLoc;
selected.setSaveName(saveName);
mainFrame.download(selected);
mainFrame.addPartial(selected);
if (results != null)
results.setResultDownloading(selected.id);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void download()
{
try
{
QueryResultItem[] qriList = results.getSelectedRows();
int numRows = qriList.length;
for (int idx = 0; idx<numRows; idx++)
download(qriList[idx], results);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public JPanel getSelectedItemDetails()
{
if (results == null)
return null;
else
return results.getSelectedItemDetails();
}
private void showID()
{
try
{
String id = results.getSelectedRow().id;
new IdDialog(id).show();
}
catch (Exception e)
{
// There was no selected row. Just ignore the request
}
}
private class IdDialog extends JDialog
{
public IdDialog(String id)
{
getContentPane().setLayout(new BorderLayout());
JTextArea tf = new JTextArea(id);
tf.setEditable(false);
tf.setLineWrap(true);
getContentPane().add(tf, BorderLayout.CENTER);
setSize(350, 100);
}
}
}
The table below shows all metrics for QueryPanel.java.




