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

📄 confirmationdialog.java

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

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import com.javaatwork.mydownloader.DownloadFile;
import com.javaatwork.mydownloader.utils.ByteFormatter;
import com.javaatwork.mydownloader.utils.LocaleManager;

/**
 * A dialog that will be appear when a file to download
 * already exists in a specific directory.
 * 
 * @author Johannes Postma
 */ 
public class ConfirmationDialog {

	private JDialog dialog = null;
	private JOptionPane paneSingleFile = null;
	private JOptionPane paneMultipleFiles = null;
	private Frame frame = null;
	private String yesOption = null;
	private String yesToAllOption = null;
	private String noOption = null;
	private String cancelOption = null;
	private LocaleManager localeManager = null;
	
	public static final int YES_OPTION = 1;
	public static final int YES_TO_ALL_OPTION = 2;
	public static final int NO_OPTION = 3;
	public static final int CANCEL_OPTION = 4;
	
	/**
	 * Creates a new ConfirmationDialog.
	 *  
	 * @param frame The parent frame
	 */
	public ConfirmationDialog(Frame frame) {

		this.frame = frame;
		localeManager = LocaleManager.getInstance();
		
		yesOption = localeManager.getString("yes");
		yesToAllOption = localeManager.getString("yes_to_all");
		noOption = localeManager.getString("no");
		cancelOption = localeManager.getString("cancel");
		
		paneMultipleFiles = new JOptionPane("", JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new String[] { yesOption, yesToAllOption, noOption, cancelOption}, yesOption);
		paneSingleFile = new JOptionPane("", JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, new String[] { yesOption, noOption}, yesOption);
	}

	/**
	 * 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 showDialog(DownloadFile file, File existingFile, boolean multipleFiles) {
		
		
		DateFormat df = DateFormat.getDateTimeInstance();
		String lastModified = df.format(new Date(existingFile.lastModified()));
		
		String firstLine = localeManager.getString("file_already_exists");
		firstLine = firstLine.replaceFirst("<filename>", existingFile.getName());
		firstLine += "\n";
		
		String secondLine = localeManager.getString("replace_existing_file");
		secondLine += "\n\n";
		
		String thirdLine = localeManager.getString("size");
		thirdLine += " \t\t\t" + ByteFormatter.format(existingFile.length()) + "\n";
		
		String fourthLine = localeManager.getString("last_modified");
		fourthLine += " " + lastModified + "\n\n";
		
		String message = firstLine + secondLine + thirdLine + fourthLine;
		
		if (multipleFiles) {
		
			paneMultipleFiles.setMessage(message);
			dialog = paneMultipleFiles.createDialog(frame, localeManager.getString("file_exists"));
			
		} else {
			
			paneSingleFile.setMessage(message);
			dialog = paneSingleFile.createDialog(frame, localeManager.getString("file_exists"));
		}
	
		//  sets the frame to the center
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = dialog.getSize();

		if (frameSize.height > screenSize.height) {
			frameSize.height = screenSize.height;
		}

		if (frameSize.width > screenSize.width) {
			frameSize.width = screenSize.width;
		}

		dialog.setLocation(
			(screenSize.width - frameSize.width) / 2,
			(screenSize.height - frameSize.height) / 2);
		
		dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		dialog.setVisible(true);
		
		String value = null;
		
		if (multipleFiles) {
			value = paneMultipleFiles.getValue().toString();	
		} else {
			value = paneSingleFile.getValue().toString();
		}
 
		if (value.equals(yesOption)) {
			return YES_OPTION; 
		} else if (value.equals(yesToAllOption)) {
			return YES_TO_ALL_OPTION;
		} else if (value.equals(noOption)) {
			return NO_OPTION;
		} else {
			return CANCEL_OPTION;
		}
	}
	
	/**
	 * Ensures that the dialog is always visible.
	 */
	public void toFront() {
		if (dialog != null) {
			dialog.toFront();
		}
	}
}

⌨️ 快捷键说明

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