2021-07-01 21:54:10 +00:00
|
|
|
package the.bytecode.club.bytecodeviewer.util;
|
|
|
|
|
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;
|
2021-07-01 21:54:10 +00:00
|
|
|
import the.bytecode.club.bytecodeviewer.gui.components.MultipleChoiceDialogue;
|
2021-07-18 19:11:34 +00:00
|
|
|
import the.bytecode.club.bytecodeviewer.translation.TranslatedStrings;
|
2021-07-01 21:54:10 +00:00
|
|
|
|
2021-07-01 23:08:56 +00:00
|
|
|
import javax.swing.*;
|
|
|
|
import javax.swing.filechooser.FileFilter;
|
2021-07-01 21:54:10 +00:00
|
|
|
import java.io.File;
|
|
|
|
|
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/>. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2021-07-01 21:54:10 +00:00
|
|
|
/**
|
|
|
|
* @author Konloch
|
|
|
|
* @since 7/1/2021
|
|
|
|
*/
|
|
|
|
public class DialogueUtils
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 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())
|
|
|
|
{
|
|
|
|
MultipleChoiceDialogue dialogue = new MultipleChoiceDialogue("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()});
|
2021-07-01 21:54:10 +00:00
|
|
|
|
|
|
|
if (dialogue.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)
|
|
|
|
{
|
2021-07-18 19:53:30 +00:00
|
|
|
return fileChooser(title, description, Configuration.getLastOpenDirectory(), filter,
|
2021-07-19 14:11:14 +00:00
|
|
|
Configuration::setLastOpenDirectory, extensions);
|
2021-07-18 19:53:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prompts a File Chooser dilogue
|
|
|
|
*/
|
|
|
|
public static File fileChooser(String title, String description, File directory, FileFilter filter, OnOpenEvent onOpen, String... extensions)
|
|
|
|
{
|
|
|
|
final JFileChooser fc = new FileChooser(directory,
|
2021-07-01 23:08:56 +00:00
|
|
|
title,
|
|
|
|
description,
|
|
|
|
extensions);
|
|
|
|
|
|
|
|
if(filter != null)
|
|
|
|
fc.setFileFilter(filter);
|
|
|
|
|
|
|
|
int returnVal = fc.showOpenDialog(BytecodeViewer.viewer);
|
|
|
|
if (returnVal == JFileChooser.APPROVE_OPTION)
|
|
|
|
try {
|
|
|
|
File file = fc.getSelectedFile();
|
2021-07-18 19:53:30 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-07-18 19:53:30 +00:00
|
|
|
|
|
|
|
public interface OnOpenEvent
|
|
|
|
{
|
|
|
|
void onOpen(File fileSelected);
|
|
|
|
}
|
2021-07-01 21:54:10 +00:00
|
|
|
}
|