Dialogs.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import org.xnap.XNap;
import org.xnap.gui.component.DefaultDialog;
import org.xnap.gui.list.FileListCellRenderer;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
import org.xnap.io.Library;
import org.xnap.util.FileHelper;
import org.xnap.util.Preferences;
import org.xnap.util.PreferencesProvider;
import org.xnap.util.StringHelper;
/**
* Provides a set of static methods to display common dialogs.
*/
public class Dialogs
{
//--- Constant(s) ---
//--- Data Field(s) ---
private static Preferences prefs = Preferences.getInstance();
//--- Constructor(s) ---
public static boolean copy(File[] files, File target)
{
boolean success = true;
files = Dialogs.showCopyDialog(null, files, target);
if (files != null) {
for (int i = 0; i < files.length; i++) {
try {
FileHelper.copy(files[i], new File
(target, files[i].getName()));
}
catch (IOException e) {
success = false;
JOptionPane.showMessageDialog
(null,
"Could not copy file: " + e.getLocalizedMessage(),
XNap.tr("Error"), JOptionPane.ERROR_MESSAGE);
}
}
}
else {
success = false;
}
return success;
}
public static void error(Component parent, String message)
{
JOptionPane.showMessageDialog
(parent, message, XNap.tr("Error"), JOptionPane.ERROR_MESSAGE);
}
public static boolean getShowDialog(PreferencesProvider prefs,
String dialogName)
{
return prefs.getBoolean("show" + dialogName + "Dialog");
}
public static boolean getShowDialog(String dialogName)
{
return getShowDialog(prefs, dialogName);
}
public static void setShowDialog(PreferencesProvider prefs,
String dialogName, boolean newValue)
{
prefs.set("show" + dialogName + "Dialog", newValue);
}
public static void setShowDialog(String dialogName, boolean newValue)
{
setShowDialog(prefs, dialogName, newValue);
}
public static void info(Component c, String message, String title)
{
JOptionPane.showMessageDialog
(c, message, title, JOptionPane.INFORMATION_MESSAGE);
}
public static boolean move(File[] files, File target)
{
boolean success = true;
files = Dialogs.showMoveDialog(null, files, target);
if (files != null) {
for (int i = 0; i < files.length; i++) {
try {
FileHelper.moveUnique(files[i], target.getAbsolutePath());
}
catch (IOException e) {
success = false;
JOptionPane.showMessageDialog
(null,
"Could not move file: " + e.getLocalizedMessage(),
XNap.tr("Error"), JOptionPane.ERROR_MESSAGE);
}
}
}
else {
success = false;
}
return success;
}
/**
* Returns true, if user presses okay button.
*/
public static boolean showCloseDialog(Component c)
{
JCheckBox jcb
= new JCheckBox(XNap.tr("Always quit without prompting me."));
Object[] message = new Object[] {
XNap.tr("This will abort any downloads and uploads."),
XNap.tr("Do you really want to quit XNap?"),
jcb
};
int response = JOptionPane.showConfirmDialog
(c, message, XNap.tr("Quit XNap"), JOptionPane.OK_CANCEL_OPTION );
setShowDialog("Close", !jcb.isSelected());
prefs.write();
return (response == JOptionPane.OK_OPTION);
}
/**
* Shows a confirmation dialog and saves if it should be shown again next
* time.
*
* @param c parent component used for centering the dialog
* @param dialogName the preferences key that is used to save the
* do not show again option
* @param title the title of the message border
* @param question the message to show
* @param optionType
* @param prefs PreferencesProvider used for saving "do not show again
*
* @param returns JOptionPane.YES_OPTION if user checked the "Do
* not ask me again" check box before
*/
public static int showConfirmDialog(Component c, String dialogName,
String title, String question,
int optionType,
PreferencesProvider prefs)
{
if (!getShowDialog(prefs, dialogName)) {
return JOptionPane.YES_OPTION;
}
JCheckBox jcb = new JCheckBox(XNap.tr("Do not ask me again."));
JLabel jl = new JLabel(GUIHelper.tt(question, 400));
jl.setBorder(GUIHelper.createDefaultBorder(title));
// MultiLineLabel ml = new MultiLineLabel(question);
// ml.setBorder(GUIHelper.createDefaultBorder(title));
// ml.setColumns(40);
Object[] message = new Object[] {
jl, jcb
};
int response = JOptionPane.showConfirmDialog
(c, message, title, optionType);
setShowDialog(prefs, dialogName, !jcb.isSelected());
return response;
}
public static File[] showCopyDialog(Component c, File files[], File dir)
{
return showFilesActionDialog
(c, files,
XNap.tr("Do you really want to copy these files to \"{0}\"?", dir),
XNap.tr("Copy Files"), "Copy");
}
public static File[] showDeleteFilesDialog(Component c, File files[])
{
return showFilesActionDialog
(c, files, XNap.tr("Do you really want to delete these files?"),
XNap.tr("Delete Files"), "DeleteFiles");
}
// public static Transfer[] showDeleteDownloadsDialog(Component c,
// Transfer[] transfers)
// {
// if (!prefs.getShowDeleteDownloadsDialog()) {
// return transfers;
// }
// JList jList = new JList(transfers);
// jList.setCellRenderer(new TransferListCellRenderer());
// jList.setVisibleRowCount(4);
// JScrollPane jsp = new JScrollPane();
// jsp.setViewportView(jList);
// // select all transfers
// jList.setSelectionInterval(0, transfers.length - 1);
// JCheckBox jcb = new JCheckBox(XNap.tr("Do not ask me again."));
// Object[] message = new Object[] {
// jsp, jcb, XNap.tr("Do you really want to delete these files?")
// };
// int response = JOptionPane.showConfirmDialog
// (c, message,
// MessageFormat.format(XNap.tr("{0} Files"),
// new Object[] { XNap.tr("Delete") }),
// JOptionPane.YES_NO_OPTION);
// prefs.setShowDeleteDownloadsDialog(!jcb.isSelected());
// if (response == JOptionPane.YES_OPTION) {
// Object[] rows = jList.getSelectedValues();
// ITransferContainer[] selected =
// new ITransferContainer[rows.length];
// System.arraycopy(rows, 0, selected, 0, selected.length);
// return selected;
// }
// else {
// return null;
// }
// }
protected static File[] showFilesActionDialog
(Component c, File files[], String question, String title,
String dialogPref)
{
if (!getShowDialog(dialogPref)) {
return files;
}
JList jList = new JList(files);
jList.setCellRenderer(new FileListCellRenderer());
jList.setVisibleRowCount(4);
JScrollPane jsp = new JScrollPane();
jsp.setViewportView(jList);
// select all files
jList.setSelectionInterval(0, files.length - 1);
JCheckBox jcb = new JCheckBox(XNap.tr("Do not ask me again."));
Object[] message = new Object[] {
jsp, jcb, question
};
int response = JOptionPane.showConfirmDialog
(c, message, title, JOptionPane.YES_NO_OPTION);
setShowDialog(dialogPref, !jcb.isSelected());
if (response == JOptionPane.YES_OPTION) {
Object rows[] = jList.getSelectedValues();
File selected[] = new File[rows.length];
System.arraycopy(rows, 0, selected, 0, selected.length);
return selected;
}
else {
return null;
}
}
public static File[] showMoveDialog(Component c, File files[], File dir)
{
return showFilesActionDialog
(c, files,
XNap.tr("Do you really want to move these files to \"{0}\"?", dir),
XNap.tr("Move Files"), "Move");
}
public static void showNotification(Component c, String dialogName,
String title, String message)
{
if (getShowDialog(dialogName)) {
NotificationDialog d
= new NotificationDialog(dialogName, title, message);
d.show(c);
}
}
public static void showNotification(Component c, String dialogName,
String title, String message,
PreferencesProvider prefs)
{
if (getShowDialog(prefs, dialogName)) {
NotificationDialog d
= new NotificationDialog(dialogName, title, message, prefs);
d.show(c);
}
}
public static String showRenameDialog(Component c, String filename)
{
JTextField jtf = new JTextField(filename);
Object[] message = new Object[] {
XNap.tr("Filename"),
jtf
};
int response = JOptionPane.showConfirmDialog
(c, message, XNap.tr("Rename File"), JOptionPane.OK_CANCEL_OPTION);
return (response == JOptionPane.OK_OPTION) ? jtf.getText() : null;
}
public static void showRestartNotification(Component c)
{
JOptionPane.showMessageDialog
(c, XNap.tr("You need to restart XNap to activate your changes."),
XNap.tr("Preferences"), JOptionPane.INFORMATION_MESSAGE);
}
/**
* Shows a message dialog if the library contains similiar files to file.
*
* @return true, if user want to continue with the download anyway
* or no files were found; false, if the user responded with no */
public static boolean showSearchLibraryDialog
(Component c, String filename)
{
String searchText = StringHelper.stripExtra(filename);
File[] files = Library.getInstance().search(searchText);
if (files != null && files.length > 0) {
return showFilesActionDialog
(c, files,
XNap.tr("There seem to be similiar files in your library. Do you really want to download?"),
XNap.tr("Download of {0}", filename),
"SearchLibraryOnDownload") != null;
}
return true;
}
// --- Inner Class(es) ---
/**
* This class provides a notification dialog with a do not show again
* option. There is no need to provide line breakes in the messsage.
* It is displayed in a multi line label.
*/
public static class NotificationDialog extends DefaultDialog
{
//--- Data field(s) ---
private PreferencesProvider prefs;
private JCheckBox jcbDoNotShowAgain;
private String dialogName;
//--- Constructor(s) ---
/**
* Constructs a notification dialog with an "Okay" button and
* "Notification" as the dialog title.
*
* @param dialogName the preferences key that is used to save the
* do not show again option
* @param title the title of the message border
* @param msg the message to show
* @param prefs PreferencesProvider used for saving "do not show again
* option"
*/
public NotificationDialog(String dialogName, String title,
String msg, PreferencesProvider prefs)
{
super(BUTTON_OKAY);
this.dialogName = dialogName;
this.prefs = prefs;
// text panel
JLabel jl = new JLabel(GUIHelper.tt(msg, 400));
jl.setBorder(GUIHelper.createDefaultBorder(title));
// MultiLineLabel mllText = new MultiLineLabel(msg);
// mllText.setBorder(GUIHelper.createDefaultBorder(title));
// mllText.setColumns(40);
// checkbox
jcbDoNotShowAgain
= new JCheckBox(XNap.tr("Do not show again"),
!getShowDialog(prefs, dialogName));
// content
setTitle(XNap.tr("Notification"));
getMainPanel().setBorder(new EmptyBorder(5, 5, 5, 5));
getMainPanel().setLayout(new GridBagLayout());
GridBagHelper.add(getMainPanel(), jl);
GridBagHelper.add(getMainPanel(), jcbDoNotShowAgain);
pack();
}
/**
* Used from within XNap's main code.
*
* Plugins have to provide their own {@link PreferencesProvider}.
*/
public NotificationDialog(String dialogName, String title, String msg)
{
this(dialogName, title, msg, Preferences.getInstance());
}
// --- Methods ---
public boolean apply()
{
setShowDialog(prefs, dialogName, !jcbDoNotShowAgain.isSelected());
return true;
}
}
// public static class ConfirmationDialog extends NotificationDialog
// {
// }
}
The table below shows all metrics for Dialogs.java.



