📄 confirmationdialog.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 + -