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

134 lines
3.6 KiB
Java
Raw Normal View History

package the.bytecode.club.bytecodeviewer;
2021-06-29 09:41:29 +00:00
import com.google.gson.reflect.TypeToken;
import me.konloch.kontainer.io.DiskReader;
import me.konloch.kontainer.io.DiskWriter;
2021-06-29 09:41:29 +00:00
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
2021-06-29 09:41:29 +00:00
import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
2021-06-29 09:41:29 +00:00
import static the.bytecode.club.bytecodeviewer.BytecodeViewer.gson;
import static the.bytecode.club.bytecodeviewer.Constants.*;
/**
2018-01-31 15:41:24 +00:00
* @author Konloch
2021-06-29 09:41:29 +00:00
* @since 6/29/2021
*/
2021-06-29 09:41:29 +00:00
public class Settings
{
public static boolean firstBoot = true; //stays true after settings load on first boot
public static boolean hasSetLanguageAsSystemLanguage = false;
private static List<String> recentPlugins;
private static List<String> recentFiles;
static
{
try
{
if (new File(filesName).exists())
recentFiles = gson.fromJson(DiskReader.loadAsString(filesName), new TypeToken<ArrayList<String>>() {}.getType());
else
recentFiles = DiskReader.loadArrayList(getBCVDirectory() + fs + "recentfiles.bcv", false);
if (new File(pluginsName).exists())
recentPlugins = gson.fromJson(DiskReader.loadAsString(pluginsName), new TypeToken<ArrayList<String>>() {}.getType());
else
recentPlugins = DiskReader.loadArrayList(getBCVDirectory() + fs + "recentplugins.bcv", false);
MiscUtils.deduplicateAndTrim(recentFiles, 25);
MiscUtils.deduplicateAndTrim(recentPlugins, 25);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Add the recent file
*
* @param f the recent file
*/
2021-07-05 04:24:19 +00:00
public static synchronized void addRecentFile(File f)
2021-06-29 09:41:29 +00:00
{
recentFiles.remove(f.getAbsolutePath()); // already added on the list
recentFiles.add(0, f.getAbsolutePath());
MiscUtils.deduplicateAndTrim(recentFiles, 25);
DiskWriter.replaceFile(filesName, MiscUtils.listToString(recentFiles), false);
resetRecentFilesMenu();
}
2021-07-05 04:24:19 +00:00
public static synchronized void removeRecentFile(File f)
{
if(recentFiles.remove(f.getAbsolutePath()))
{
DiskWriter.replaceFile(filesName, MiscUtils.listToString(recentFiles), false);
resetRecentFilesMenu();
}
}
2021-06-29 09:41:29 +00:00
/**
* Add to the recent plugin list
*
* @param f the plugin file
*/
2021-07-05 04:24:19 +00:00
public static synchronized void addRecentPlugin(File f)
2021-06-29 09:41:29 +00:00
{
recentPlugins.remove(f.getAbsolutePath()); // already added on the list
recentPlugins.add(0, f.getAbsolutePath());
MiscUtils.deduplicateAndTrim(recentPlugins, 25);
DiskWriter.replaceFile(pluginsName, MiscUtils.listToString(recentPlugins), false);
resetRecentFilesMenu();
}
2021-07-05 04:24:19 +00:00
public static synchronized void removeRecentPlugin(File f)
{
if(recentPlugins.remove(f.getAbsolutePath()))
{
DiskWriter.replaceFile(pluginsName, MiscUtils.listToString(recentPlugins), false);
resetRecentFilesMenu();
}
}
2021-06-29 09:41:29 +00:00
/**
* resets the recent files menu
*/
protected static void resetRecentFilesMenu()
{
//build recent files
BytecodeViewer.viewer.recentFilesSecondaryMenu.removeAll();
for (String s : recentFiles)
{
if (!s.isEmpty())
{
JMenuItem m = new JMenuItem(s);
m.addActionListener(e ->
{
JMenuItem m12 = (JMenuItem) e.getSource();
BytecodeViewer.openFiles(new File[]{new File(m12.getText())}, true);
});
BytecodeViewer.viewer.recentFilesSecondaryMenu.add(m);
}
}
//build recent plugins
BytecodeViewer.viewer.recentPluginsSecondaryMenu.removeAll();
for (String s : recentPlugins)
{
if (!s.isEmpty())
{
JMenuItem m = new JMenuItem(s);
m.addActionListener(e ->
{
JMenuItem m1 = (JMenuItem) e.getSource();
BytecodeViewer.startPlugin(new File(m1.getText()));
});
BytecodeViewer.viewer.recentPluginsSecondaryMenu.add(m);
}
}
}
}