bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/DialogUtils.java

141 lines
4.3 KiB
Java
Raw Normal View History

package the.bytecode.club.bytecodeviewer.util;
import java.io.File;
import java.util.Arrays;
import java.util.HashSet;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
2021-07-01 23:08:56 +00:00
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.Configuration;
import the.bytecode.club.bytecodeviewer.gui.components.FileChooser;
import the.bytecode.club.bytecodeviewer.gui.components.MultipleChoiceDialog;
2021-07-18 19:11:34 +00:00
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
2021-07-21 16:52:01 +00:00
import static the.bytecode.club.bytecodeviewer.gui.components.FileChooser.EVERYTHING;
2021-07-07 05:56:29 +00:00
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* 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, either version 3 of the License, or *
* (at your option) any later version. *
* *
* 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, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* @author Konloch
* @since 7/1/2021
*/
public class DialogUtils
{
/**
* Asks if the user would like to overwrite the file
*/
public static boolean canOverwriteFile(String filePath)
{
return canOverwriteFile(new File(filePath));
}
/**
* Asks if the user would like to overwrite the file
*/
public static boolean canOverwriteFile(File file) {
if (file.exists())
{
MultipleChoiceDialog dialog = new MultipleChoiceDialog("Bytecode Viewer - Overwrite File",
"Are you sure you wish to overwrite this existing file?",
2021-07-18 19:11:34 +00:00
new String[]{TranslatedStrings.YES.toString(), TranslatedStrings.NO.toString()});
if (dialog.promptChoice() == 0) {
file.delete();
return true;
} else {
return false;
}
}
return true;
}
2021-07-01 23:08:56 +00:00
/**
* Prompts a File Chooser dilogue
*/
public static File fileChooser(String title, String description, String... extensions)
{
return fileChooser(title, description, null, extensions);
}
/**
* Prompts a File Chooser dilogue
*/
public static File fileChooser(String title, String description, FileFilter filter, String... extensions)
{
return fileChooser(title, description, Configuration.getLastOpenDirectory(), filter,
2021-07-19 14:11:14 +00:00
Configuration::setLastOpenDirectory, extensions);
}
/**
* Prompts a File Chooser dilogue
*/
public static File fileChooser(String title, String description, File directory, FileFilter filter, OnOpenEvent onOpen, String... extensions)
{
2021-07-21 16:52:01 +00:00
HashSet<String> extensionSet = new HashSet<>(Arrays.asList(extensions));
final JFileChooser fc = new FileChooser(true,
directory,
2021-07-01 23:08:56 +00:00
title,
description,
extensions);
if(filter != null)
fc.setFileFilter(filter);
2021-07-21 16:52:01 +00:00
else
fc.setFileFilter(new FileFilter()
{
@Override
public boolean accept(File f)
{
if (f.isDirectory())
return true;
if(extensions[0].equals(EVERYTHING))
return true;
return extensionSet.contains(MiscUtils.extension(f.getAbsolutePath()));
}
@Override
public String getDescription() {
return description;
}
});
2021-07-01 23:08:56 +00:00
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
if (returnVal == JFileChooser.APPROVE_OPTION)
try {
File file = fc.getSelectedFile();
onOpen.onOpen(file);
2021-07-01 23:08:56 +00:00
return file;
} catch (Exception e1) {
2021-07-07 02:51:10 +00:00
BytecodeViewer.handleException(e1);
2021-07-01 23:08:56 +00:00
}
return null;
}
public interface OnOpenEvent
{
void onOpen(File fileSelected);
}
}