📄 userinputmanager.java
字号:
package de.uni_bremen.informatik.p2p.plugins.filesharing.control;
import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.protocol.PipeAdvertisement;
import org.apache.log4j.Logger;
import de.uni_bremen.informatik.p2p.peeranha42.core.network.NetworkException;
import de.uni_bremen.informatik.p2p.plugins.filesharing.control.PeerGroupSearch.DocumentClassifier;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.DownloadJob;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.FilesharingState;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.Protocol;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.SearchResult;
import de.uni_bremen.informatik.p2p.plugins.filesharing.data.Share;
import de.uni_bremen.informatik.p2p.plugins.filesharing.network.download.client.DownloadClient;
/**
* The class provides functions which will be called from userside.
*
* @author Lars Kordes, Philipp Hoheisel
*/
public class UserInputManager {
/** Logger */
private static Logger log = Logger.getLogger(UserInputManager.class);
/** Directory of the file chooser. */
private static String chooserdir = "";
/** File chooser reference */
private static JFileChooser chooser = null;
/** Hashtable for searching purposes */
private static Hashtable shareTable = new Hashtable();
private static File dir = new File(System.getProperty("user.home"));
/** Init */
static {
chooserdir = System.getProperty("user.home");
chooser = new JFileChooser(chooserdir);
}
/**
* The method reads the searchterm from the searchresult-userinterface and
* starts searching in the p2p-network.
*
* It will only search in the peergroup according to the searchtype.
*
*/
public static void searchFor() {
// Clear searchList
clearAllSearchList();
// Search
String term = FilesharingState.searchpanel.getSearchString();
int searchType = FilesharingState.searchpanel.getSearchType();
// Construct search message
if (term != "") {
try {
Message msg = new Message();
StringMessageElement sme =
new StringMessageElement("REQ", term, null);
msg.addMessageElement(sme);
StringMessageElement searchTypeElement =
new StringMessageElement(Protocol.SEARCHTYPE,Integer.toString(searchType),null);
msg.addMessageElement(searchTypeElement);
// Actually send search message
FilesharingState.networkHandler.sendSearchMsg(searchType, msg);
// Network.sendMsg(FilesharingState.pluginid,FilesharingState.peergroup,msg); // OLD SENDING
} catch (Exception e) {
log.fatal(e.toString());
}
FilesharingState.info.append("\nSearching for \"" + term + "\"...");
}
}
/**
* The method starts downloading the file of the highlighted searchresult in
* the searchresultlist.
*/
public static void downloadFile() {
int download = FilesharingState.searchpanel.getTable().getSelectedRow();
if (download == -1)
return;
SearchResult sr = (SearchResult) FilesharingState.searchresults
.get(download);
File filedes = FileUtilities.createDownloadFile(sr.filename, sr.size);
if (filedes == null) {
log.error("Couldn't create file. Can't start download!");
return;
} else
log.debug("File created, start download...");
DownloadJob dj = new DownloadJob(sr);
dj.setStatus(DownloadJob.DOWNLOAD);
dj.filedes = filedes;
PipeAdvertisement pipeadv = null;
for (int i = 0; i < sr.sources.size(); i++) {
Object x = sr.sources.get(i);
if (x instanceof PipeAdvertisement) {
pipeadv = (PipeAdvertisement) sr.sources.get(i);
try {
// start client and add to list
DownloadClient dc = new DownloadClient(FilesharingState.peergroup, pipeadv, dj);
dj.con.add(dc);
} catch (NetworkException e) {
log.error("Cannot download file from " + pipeadv
+ "\nException: " + e);
} catch (IndexOutOfBoundsException e) {
log.error("No more sources: " + e);
break;
}
}
dj.startObserver();
}
FilesharingState.downloads.add(dj);
FilesharingState.downloadpanel.getTable().updateUI();
}
/**
* The method clears the searchresultlist and updates the searchresulttable.
*/
public static void clearAllSearchList() {
FilesharingState.searchresults.clear();
FilesharingState.searchpanel.getTable().updateUI();
}
/**
* The method starts a download of the downloadjoblist which has been paused
* before.
*/
public static void startDownload() {
int download = FilesharingState.downloadpanel.getTable()
.getSelectedRow();
if (download == -1)
return;
DownloadJob job = (DownloadJob) FilesharingState.downloads
.get(download);
if (job == null)
return;
if (job.getStatus() != DownloadJob.CANCELT
&& job.getStatus() != DownloadJob.FINISHED)
job.setStatus(DownloadJob.DOWNLOAD);
FilesharingState.downloadpanel.getTable().updateUI();
FilesharingState.info.append("\nStart");
}
/**
* The method stops a download of the downloadjoblist which has been
* processing before.
*/
public static void stopDownload() {
int download = FilesharingState.downloadpanel.getTable()
.getSelectedRow();
if (download == -1)
return;
DownloadJob job = (DownloadJob) FilesharingState.downloads
.get(download);
if (job == null)
return;
if (job.getStatus() == DownloadJob.CANCELT
|| job.getStatus() == DownloadJob.DOWNLOAD)
job.setStatus(DownloadJob.STOPPED);
FilesharingState.downloadpanel.getTable().updateUI();
FilesharingState.info.append("\nStop");
}
/**
* The method cancels a download of the downloadjoblist.
*/
public static void cancelDownload() {
int download =
FilesharingState.downloadpanel.getTable().getSelectedRow();
if(download == -1) return;
DownloadJob job = (DownloadJob)
FilesharingState.downloads.get(download);
if(job == null) return;
if(job.getStatus() != DownloadJob.FINISHED) {
if(JOptionPane.showOptionDialog(null,
"Do you really want to cancel download?\n",
"Warning",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null, null, null )
== JOptionPane.YES_OPTION) {
job.setStatus(DownloadJob.CANCELT);
FilesharingState.downloadpanel.getTable().updateUI();
FilesharingState.info.append("\nCancel");
}
}
}
/**
* The method adds a file/files to the sharelist. Therefor it opens a
* filechooserdialog.
*/
public static void addSharedFile() {
// JFileChooser chooser =
// new JFileChooser(chooserdir);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(true);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File files[] = chooser.getSelectedFiles();
if (files != null) {
String shares = FilesharingState.config.getValue("shares");
if (shares == null)
shares = "";
boolean b;
Share share = null;
Object val = null;
for (int i = 0; i < files.length; i++) {
b = true;
val = shareTable.get(files[i].getPath());
if (val != null
&& (((Share) val).file.getPath()).equals(files[i]
.getPath()))
b = false;
/*
* causes java.lang.OutOfMemoryExceptions if large dirs are
* loaded
*
* for(int k=0; k <FilesharingState.sharedfiles.size(); k++) {
* share = (Share) FilesharingState.sharedfiles.get(k);
* if(files[i].getPath().equals(share.file.getPath())) { b =
* false; break; } }
*/
if (b) {
share = new Share(files[i]);
FilesharingState.sharedfiles.add(share);
shareTable.put(files[i].getPath(), share);
shares += files[i] + ";";
}
}
Runnable update = new Runnable() {
public void run() {
FilesharingState.sharedpanel.getTable().updateUI();
FilesharingState.info.append("\nAdd new shared file");
}
};
SwingUtilities.invokeLater(update);
FilesharingState.config.setValue("shares", shares);
FilesharingState.config.applyChanges();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -