⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userinputmanager.java

📁 peeranha42是jxta的 p2p程序核心
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
		chooserdir = chooser.getCurrentDirectory().getPath();
		chooser.setCurrentDirectory(new File(chooserdir));

		new DocumentClassifier().start();
	}

	/**
	 * The method adds files from a directory/directoies to the sharelist.
	 * Therefore a filechooserdialog is used.
	 */
	public static void addSharedDirectory() {
		//JFileChooser chooser =
		//  new JFileChooser(chooserdir);
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_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 = "";

				// collect all files from chosen directory
				ArrayList list = new ArrayList();
				for (int i = 0; i < files.length; i++) {
					list.addAll(FileUtilities.getFiles(files[i]));
				}

				boolean b;
				Share share = null;
				Object val = null;

				if (!list.isEmpty()) {

					for (int j = 0; j < list.size(); j++) {
						File file = (File) list.get(j);
						b = true;

						val = shareTable.get(file.getPath());
						if (val != null
								&& (((Share) val).file.getPath()).equals(file
										.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(file.getPath().equals(share.file.getPath())) { b =
						 * false; break; } }
						 */

						if (b) {
							share = new Share(file);
							FilesharingState.sharedfiles.add(share);
							shareTable.put(file.getPath(), share);
							shares += file.getPath() + ";";
							Runnable update = new Runnable() {
								public void run() {
									FilesharingState.sharedpanel.getTable()
											.updateUI();
								}
							};
							SwingUtilities.invokeLater(update);

						}

						try {
							Thread.sleep(20);
						} catch (InterruptedException e1) {
							log.fatal(e1.toString());
						}

					}

					Runnable update = new Runnable() {
						public void run() {
							FilesharingState.sharedpanel.getTable().updateUI();
							FilesharingState.info
									.append("\nAdd new shared directory");
						}
					};
					SwingUtilities.invokeLater(update);

				}

				FilesharingState.config.setValue("shares", shares);
				FilesharingState.config.applyChanges();

			}
		}

		chooserdir = chooser.getCurrentDirectory().getPath();
		chooser.setCurrentDirectory(new File(chooserdir));

		new DocumentClassifier().start();
	}

	/**
	 * The method removes a share of the sharelist.
	 */
	public static void removeShare() {
		int row[] = FilesharingState.sharedpanel.getTable().getSelectedRows();

		String[] shares = FilesharingState.config.getValue("shares").split(";");

		Share temp = null;
		for (int i = row.length - 1; i >= 0; i--) {
			temp = (Share) FilesharingState.sharedfiles.remove(row[i]);
			shareTable.remove(temp.file.getPath());

			if (shares != null) {
				for (int j = 0; j < shares.length; j++) {
					if (temp.file.toString().equals(shares[j])) {
						shares[j] = "";
						break;
					}
				}
			}
		}

		String configshares = "";
		for (int k = 0; k < shares.length; k++) {
			if (shares[k] != "")
				configshares += shares[k] + ";";
		}
		FilesharingState.config.setValue("shares", configshares);
		FilesharingState.config.applyChanges();

		Runnable update = new Runnable() {
			public void run() {
				FilesharingState.sharedpanel.getTable().updateUI();
				FilesharingState.info.append("\nShare(s) removed");
				FilesharingState.sharedpanel.getTable().clearSelection();
			}
		};
		SwingUtilities.invokeLater(update);

		new DocumentClassifier().start();
	}

	/**
	 * Clears highlighted downloadjob(s) in list.
	 */
	public static void clearDownloadList() {
		int row[] = FilesharingState.downloadpanel.getTable().getSelectedRows();

		DownloadJob job = null;

		for (int i = row.length - 1; i >= 0; i--) {
			job = (DownloadJob) FilesharingState.downloads.get(row[i]);

			if ((job.getStatus() == DownloadJob.CANCELT)
					|| (job.getStatus() == DownloadJob.FINISHED)) {
				FilesharingState.downloads.remove(row[i]);
			}
		}

		Runnable update = new Runnable() {
			public void run() {
				FilesharingState.downloadpanel.getTable().updateUI();
				FilesharingState.downloadpanel.getTable().clearSelection();

			}
		};
		javax.swing.SwingUtilities.invokeLater(update);

	}

	/**
	 * Clears downloadjoblist.
	 */
	public static void clearAllDownloadList() {
		clearAllDownloads(0);

		Runnable update = new Runnable() {
			public void run() {
				FilesharingState.downloadpanel.getTable().updateUI();

				FilesharingState.info.append("\nDownloadlist cleared");
				FilesharingState.downloadpanel.getTable().clearSelection();
			}
		};
		SwingUtilities.invokeLater(update);

		new DocumentClassifier().start();
	}

	/**
	 * Clears all cancelt and finished downloads starting at line give by number
	 * i.
	 * 
	 * @param i
	 *            number of line where to start the cleaning
	 */
	private static void clearAllDownloads(int i) {
		if (i >= FilesharingState.downloads.size()) {
			return;
		}

		DownloadJob job = null;

		for (; i < FilesharingState.downloads.size(); i++) {
			job = (DownloadJob) FilesharingState.downloads.get(i);

			if ((job.getStatus() == DownloadJob.CANCELT)
					|| (job.getStatus() == DownloadJob.FINISHED)) {
				FilesharingState.downloads.remove(i);

				break;
			}
		}

		clearAllDownloads(i);
	}

	/**
	 * Opens dialog for choosing the download directory.
	 */
	public static void chooseDownloadDirectory() {
		chooser.setCurrentDirectory(dir);
		chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		chooser.setMultiSelectionEnabled(false);

		if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();

			FilesharingState.config.setValue("download_directory", file
					.getPath());
			FilesharingState.config.applyChanges();

			Runnable update = new Runnable() {
				public void run() {
					FilesharingState.info.append("\nNew Download directory");
				}
			};

			SwingUtilities.invokeLater(update);
		}

		dir = new File(chooserdir);
	}

	/**
	 * Clears the list of shared files.
	 */
	public static void removeAllShares() {
		Runnable update = new Runnable() {
			public void run() {
				if (JOptionPane.showOptionDialog(null,
						"Do you really want to remove all shares from list?\n",
						"Warning", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE, null, null, null) == JOptionPane.YES_OPTION) {
					// clear file list
					FilesharingState.sharedfiles.clear();
					shareTable.clear();

					// write down in config file
					FilesharingState.config.setValue("shares", "");
					FilesharingState.config.applyChanges();

					// update gui
					FilesharingState.sharedpanel.getTable().updateUI();
					FilesharingState.info.append("\nAll shares removed");
					FilesharingState.sharedpanel.getTable().clearSelection();

				}
			}
		};

		SwingUtilities.invokeLater(update);

		new DocumentClassifier().start();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -