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

📄 mydownloader.java

📁 MyDownloader 是一款使用 http 协议(RFC 1867)用于下载一个或多个文件到本地的简单易用的收费 Java 程序.使用托拽操作,你可以在一个页面内下载多个文件.在下载文件的过程当中
💻 JAVA
字号:
/*
 * Copyright 2007 JavaAtWork All rights reserved.
 * Use is subject to license terms.
 */
package com.javaatwork.mydownloader;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;

import com.javaatwork.mydownloader.dialog.ImagePreview;
import com.javaatwork.mydownloader.dialog.ConfirmationDialog;
import com.javaatwork.mydownloader.listeners.DestroyListener;
import com.javaatwork.mydownloader.listeners.DownloadActionListener;
import com.javaatwork.mydownloader.listeners.FileDragGestureListener;
import com.javaatwork.mydownloader.utils.IconManager;
import com.javaatwork.mydownloader.utils.LocaleManager;
import com.javaatwork.mydownloader.utils.Logger;
import com.javaatwork.mydownloader.utils.Parameters;
import com.javaatwork.mydownloader.utils.TempDirectoryManager;
import com.javaatwork.mydownloader.utils.Version;

/**
 * The main applet.
 * 
 * @author Johannes Postma
 */
public class MyDownloader extends JApplet {

	private static final long serialVersionUID = -210559793375998923L;
	
	private JPanel mainPanel = null;
	private JScrollPane pane = null;
	private FileTable fileTable = null;
	private JButton button = null;
	private ConfirmationDialog overwriteDialog = null;
	private Vector destroyListeners = null;
		
	/* (non-Javadoc)
	 * @see java.applet.Applet#init()
	 */
	public void init() {
		
		System.out.println(Version.COPYRIGHT);
		
		try {
			SwingUtilities.invokeAndWait(new Runnable() {

				public void run() {
					
					try {
						TempDirectoryManager.createTempDirectory();
					} catch (Exception e) {
						
						Logger.log("MyDownloader", "run", e.toString());						
					}
					
					setDefaultFont();
					createGUI();
					
				}});
		} catch (InterruptedException e) {
			
			Logger.log("MyDownloader", "init", e.toString());
			
			e.printStackTrace();
			
		} catch (InvocationTargetException e) {
			
			Logger.log("MyDownloader", "init", e.toString());
			
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see java.applet.Applet#destroy()
	 */
	public void destroy() {
		
		// removes all the temp files
		TempDirectoryManager.deleteTempDirectory();
		
		// closes all connections
		for (int i = 0; i < destroyListeners.size(); i++) {
			((DestroyListener)destroyListeners.elementAt(i)).destroy();
		}
		
		// kill all threads
		Thread currentThread = Thread.currentThread();
		ThreadGroup threadGroup = currentThread.getThreadGroup();
		threadGroup.interrupt();
	}
	
	/**
	 * Sets the default font.
	 */
	private void setDefaultFont() {
		UIDefaults ud = UIManager.getDefaults();
		Enumeration enumeration = ud.keys();
		
		while (enumeration.hasMoreElements()) {
			Object o = enumeration.nextElement();

			if (o.toString().endsWith(".font")) {
				UIManager.put(o, new Font("SanSerif", Font.PLAIN, 12));
			}
		}
	}
	
	/**
	 * Creates the GUI.
	 */
	private void createGUI() {
		
		// retrieve the parameters
		Parameters.loadParameters(this);
		
		LocaleManager localeManager = LocaleManager.getInstance();
		
		destroyListeners = new Vector();
		
		// initialise the scrollpane
		pane = new JScrollPane();
		overwriteDialog = new ConfirmationDialog(getFrame());
		
		URL codeBase = null;
		
		try {
			codeBase = this.getCodeBase();
		} catch (NullPointerException npe) {
			// in case of a stand alone application
		}
		
		DownloadFile [] files = FileManager.getFilesToDownload(codeBase);
		DownloadTableModel model = new DownloadTableModel(Arrays.asList(files)); 
		pane.getViewport().add(fileTable = new FileTable(this, model, files));
		pane.getViewport().setBackground(Color.WHITE);
				
		// initialise the mainpanel
		mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(pane, BorderLayout.CENTER);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.setBackground(Color.WHITE);
		
		button = new JButton(LocaleManager.getInstance().getString("download"));
		button.addActionListener(new DownloadActionListener(this));
		
		ImageIcon iconDownload = IconManager.getImageIcon(Parameters.getParameter(Parameters.ICON_DOWNLOAD, null), codeBase);
		
		if (iconDownload != null) {
			 button.setIcon(iconDownload);
		}
		
		JPanel rightPanel = new JPanel();
		rightPanel.setBorder(BorderFactory.createEmptyBorder(5,0,5,5));
		rightPanel.setLayout(new BorderLayout());
		rightPanel.setBackground(Color.WHITE);
		
		if (Parameters.getParameter(Parameters.SHOW_DOWNLOAD_BUTTON, true)) {
			rightPanel.add(button, BorderLayout.NORTH);
			
			if (Parameters.getParameter(Parameters.SHOW_THUMBNAILS_IN_APPLET, false)) {
				
				// the imagepreview can be 90 width
				// buttons without images can be smaller
				// therefore set the prefferd width
				if (!(button.getPreferredSize().getWidth() > 100)) {
					button.setPreferredSize(new Dimension(100,25));	
				}				
			} 
		}
		
		DragSource ds = DragSource.getDefaultDragSource();
		DragGestureRecognizer dgr = ds.createDefaultDragGestureRecognizer(fileTable, DnDConstants.ACTION_COPY_OR_MOVE, new FileDragGestureListener(this));
		
		// add thumbnails
		if (Parameters.getParameter(Parameters.SHOW_THUMBNAILS_IN_APPLET, false)) {
			JPanel thumbNailPanel = new JPanel();
			thumbNailPanel.setBackground(Color.WHITE);
			thumbNailPanel.setLayout(new BorderLayout());
			ImagePreview imagePreview = new ImagePreview(fileTable);
			thumbNailPanel.add(imagePreview);
			rightPanel.add(thumbNailPanel, BorderLayout.CENTER);
			
		}
		
		fileTable.setSelectedRow(0);
		
		this.getContentPane().add(mainPanel, BorderLayout.CENTER);
		this.getContentPane().add(rightPanel, BorderLayout.EAST);
	}
	
	/**
	 * Returns the files to download.
	 * 
	 * @return The files to download.
	 */
	public DownloadFile [] getFilesToDownload() {
		return fileTable.getFilesToDownload();
	}
		
	/**
	 * The frame of the applet.
	 * 
	 * @return The frame of the applet.
	 */
	public Frame getFrame() {
		return JOptionPane.getFrameForComponent(this);
	}
	
	/**
	 * Show a message.
	 * 
	 * @param message The message
	 * @param messageType The messageType see JOptionPane.
	 */
	public void showMessage(Object message, int messageType) {
		JOptionPane optionPane = new JOptionPane(message, messageType);

		JDialog dial = optionPane.createDialog(this, LocaleManager.getInstance().getString("message"));
		dial.setModal(true);
		dial.setVisible(true);
	}
	
	/**
	 * Adds a destroylistener. This listener is called when the applet will be destroyed.
	 * 
	 * @param destroyListener The DestroyListener.
	 */
	public void addDestroyListener(DestroyListener destroyListener) {
		destroyListeners.add(destroyListener);
	}
	
	/**
	 * Adds a destroylistener. This listener is called when the applet will be destroyed.
	 * 
	 * @param destroyListener The DestroyListener.
	 */
	public void removeDestroyListener(DestroyListener destroyListener) {
		destroyListeners.remove(destroyListener);
	}
	
	/**
	 * Shows the dialog. Possible return values
	 * ConfirmationDialog.YES_OPTION
	 * ConfirmationDialog.YES_TO_ALL_OPTION
	 * ConfirmationDialog.NO_OPTION
	 * ConfirmationDialog.CANCEL_OPTION
	 * 
	 * In case of multipleFiles = false only ConfirmationDialog.YES_OPTION and ConfirmationDialog.NO_OPTION are possible.
	 * 
	 * @param file The file that can't be uploaded.
	 * @param existingFile The existing file in a specific directory
	 * @param multipleFiles True if multilple files are downloaded in one request.
	 * @return ConfirmationDialog.YES_OPTION, ConfirmationDialog.YES_TO_ALL_OPTION, onfirmationDialog.NO_OPTION or ConfirmationDialog.CANCEL_OPTION.
	 */
	public int handleExistingFile(DownloadFile file, File existingFile, boolean multipleFiles) {
		
		return overwriteDialog.showDialog(file, existingFile, multipleFiles);
	}
}

⌨️ 快捷键说明

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