mirror of
https://github.com/deathmarine/Luyten.git
synced 2024-11-26 22:20:32 +00:00
Save Theme and FileOpen/Save dialogs last directory
This commit is contained in:
parent
aeba43309d
commit
336a076809
@ -1,5 +1,7 @@
|
||||
package com.modcrafting.luyten;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.prefs.Preferences;
|
||||
import com.strobel.decompiler.DecompilerSettings;
|
||||
import com.strobel.decompiler.languages.Language;
|
||||
@ -28,6 +30,7 @@ public class ConfigSaver {
|
||||
private DecompilerSettings decompilerSettings;
|
||||
private WindowPosition mainWindowPosition;
|
||||
private WindowPosition findWindowPosition;
|
||||
private LuytenPreferences luytenPreferences;
|
||||
|
||||
private static ConfigSaver theLoadedInstance;
|
||||
|
||||
@ -56,6 +59,9 @@ public class ConfigSaver {
|
||||
if (decompilerSettings.getFormattingOptions() == null) {
|
||||
decompilerSettings.setFormattingOptions(JavaFormattingOptions.createDefault());
|
||||
}
|
||||
luytenPreferences = new LuytenPreferences();
|
||||
mainWindowPosition = new WindowPosition();
|
||||
findWindowPosition = new WindowPosition();
|
||||
try {
|
||||
Preferences prefs = Preferences.userNodeForPackage(ConfigSaver.class);
|
||||
|
||||
@ -78,6 +84,7 @@ public class ConfigSaver {
|
||||
|
||||
mainWindowPosition = loadWindowPosition(prefs, MAIN_WINDOW_ID_PREFIX);
|
||||
findWindowPosition = loadWindowPosition(prefs, FIND_WINDOW_ID_PREFIX);
|
||||
luytenPreferences = loadLuytenPreferences(prefs);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -93,6 +100,32 @@ public class ConfigSaver {
|
||||
return windowPosition;
|
||||
}
|
||||
|
||||
// load preferences by their java variable names
|
||||
private LuytenPreferences loadLuytenPreferences(Preferences prefs) throws Exception {
|
||||
LuytenPreferences newLuytenPrefs = new LuytenPreferences();
|
||||
for (Field field : LuytenPreferences.class.getDeclaredFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers()))
|
||||
continue;
|
||||
field.setAccessible(true);
|
||||
String prefId = field.getName();
|
||||
Object defaultVal = field.get(newLuytenPrefs);
|
||||
|
||||
if (field.getType() == String.class) {
|
||||
String defaultStr = (String) (defaultVal == null ? "" : defaultVal);
|
||||
field.set(newLuytenPrefs, prefs.get(prefId, defaultStr));
|
||||
|
||||
} else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
|
||||
Boolean defaultBool = (Boolean) (defaultVal == null ? new Boolean(false) : defaultVal);
|
||||
field.setBoolean(newLuytenPrefs, prefs.getBoolean(prefId, defaultBool));
|
||||
|
||||
} else if (field.getType() == Integer.class || field.getType() == int.class) {
|
||||
Integer defaultInt = (Integer) (defaultVal == null ? new Integer(0) : defaultVal);
|
||||
field.setInt(newLuytenPrefs, prefs.getInt(prefId, defaultInt));
|
||||
}
|
||||
}
|
||||
return newLuytenPrefs;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
// Registry path on Windows Xp:
|
||||
// HKEY_CURRENT_USER\Software\JavaSoft\Prefs\com\modcrafting\luyten
|
||||
@ -110,6 +143,7 @@ public class ConfigSaver {
|
||||
|
||||
saveWindowPosition(prefs, MAIN_WINDOW_ID_PREFIX, mainWindowPosition);
|
||||
saveWindowPosition(prefs, FIND_WINDOW_ID_PREFIX, findWindowPosition);
|
||||
saveLuytenPreferences(prefs);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -123,6 +157,27 @@ public class ConfigSaver {
|
||||
prefs.putInt(windowIdPrefix + WINDOW_Y_ID, windowPosition.getWindowY());
|
||||
}
|
||||
|
||||
// save preferences by their java variable names
|
||||
private void saveLuytenPreferences(Preferences prefs) throws Exception {
|
||||
for (Field field : LuytenPreferences.class.getDeclaredFields()) {
|
||||
if (Modifier.isStatic(field.getModifiers()))
|
||||
continue;
|
||||
field.setAccessible(true);
|
||||
String prefId = field.getName();
|
||||
Object value = field.get(luytenPreferences);
|
||||
|
||||
if (field.getType() == String.class) {
|
||||
prefs.put(prefId, (String) (value == null ? "" : value));
|
||||
|
||||
} else if (field.getType() == Boolean.class || field.getType() == boolean.class) {
|
||||
prefs.putBoolean(prefId, (Boolean) (value == null ? new Boolean(false) : value));
|
||||
|
||||
} else if (field.getType() == Integer.class || field.getType() == int.class) {
|
||||
prefs.putInt(prefId, (Integer) (value == null ? new Integer(0) : value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Language findLanguageByName(String languageName) {
|
||||
if (languageName != null) {
|
||||
|
||||
@ -154,4 +209,8 @@ public class ConfigSaver {
|
||||
public WindowPosition getFindWindowPosition() {
|
||||
return findWindowPosition;
|
||||
}
|
||||
|
||||
public LuytenPreferences getLuytenPreferences() {
|
||||
return luytenPreferences;
|
||||
}
|
||||
}
|
||||
|
44
src/com/modcrafting/luyten/LuytenPreferences.java
Normal file
44
src/com/modcrafting/luyten/LuytenPreferences.java
Normal file
@ -0,0 +1,44 @@
|
||||
package com.modcrafting.luyten;
|
||||
|
||||
/**
|
||||
* Do not instantiate this class, get the instance from
|
||||
* ConfigSaver. All not-static fields will be saved
|
||||
* automatically named by the field's java variable name.
|
||||
* (Watch for collisions with existing IDs defined in
|
||||
* ConfigSaver.) Only String, boolean and int fields are
|
||||
* supported. Write default values into the field
|
||||
* declarations.
|
||||
*/
|
||||
public class LuytenPreferences {
|
||||
|
||||
public static final String THEME_XML_PATH = "/themes/";
|
||||
public static final String DEFAULT_THEME_XML = "eclipse.xml";
|
||||
|
||||
private String themeXml = DEFAULT_THEME_XML;
|
||||
private String fileOpenCurrentDirectory = "";
|
||||
private String fileSaveCurrentDirectory = "";
|
||||
|
||||
public String getThemeXml() {
|
||||
return themeXml;
|
||||
}
|
||||
|
||||
public void setThemeXml(String themeXml) {
|
||||
this.themeXml = themeXml;
|
||||
}
|
||||
|
||||
public String getFileOpenCurrentDirectory() {
|
||||
return fileOpenCurrentDirectory;
|
||||
}
|
||||
|
||||
public void setFileOpenCurrentDirectory(String fileOpenCurrentDirectory) {
|
||||
this.fileOpenCurrentDirectory = fileOpenCurrentDirectory;
|
||||
}
|
||||
|
||||
public String getFileSaveCurrentDirectory() {
|
||||
return fileSaveCurrentDirectory;
|
||||
}
|
||||
|
||||
public void setFileSaveCurrentDirectory(String fileSaveCurrentDirectory) {
|
||||
this.fileSaveCurrentDirectory = fileSaveCurrentDirectory;
|
||||
}
|
||||
}
|
@ -138,6 +138,7 @@ public class Model extends JFrame implements WindowListener {
|
||||
private FindBox findBox;
|
||||
private ConfigSaver configSaver;
|
||||
private WindowPosition windowPosition;
|
||||
private LuytenPreferences luytenPrefs;
|
||||
|
||||
public Model() {
|
||||
frame = this;
|
||||
@ -155,6 +156,7 @@ public class Model extends JFrame implements WindowListener {
|
||||
configSaver = ConfigSaver.getLoadedInstance();
|
||||
settings = configSaver.getDecompilerSettings();
|
||||
windowPosition = configSaver.getMainWindowPosition();
|
||||
luytenPrefs = configSaver.getLuytenPreferences();
|
||||
|
||||
this.adjustWindowPositionBySavedState();
|
||||
|
||||
@ -167,11 +169,21 @@ public class Model extends JFrame implements WindowListener {
|
||||
e.printStackTrace();
|
||||
}
|
||||
this.setDropTarget(dt);
|
||||
try {
|
||||
theme = Theme.load(getClass().getResourceAsStream("/themes/eclipse.xml"));
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
String themeXml = luytenPrefs.getThemeXml();
|
||||
theme = Theme.load(getClass().getResourceAsStream(LuytenPreferences.THEME_XML_PATH + themeXml));
|
||||
} catch (Exception e1) {
|
||||
try {
|
||||
e1.printStackTrace();
|
||||
String themeXml = LuytenPreferences.DEFAULT_THEME_XML;
|
||||
luytenPrefs.setThemeXml(themeXml);
|
||||
theme = Theme.load(getClass().getResourceAsStream(LuytenPreferences.THEME_XML_PATH + themeXml));
|
||||
} catch (Exception e2) {
|
||||
e2.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException e) {
|
||||
@ -338,16 +350,19 @@ public class Model extends JFrame implements WindowListener {
|
||||
fileMenu = new JMenu("Themes");
|
||||
languagesGroup = new ButtonGroup();
|
||||
JRadioButtonMenuItem a = new JRadioButtonMenuItem(new ThemeAction("Default", "default.xml"));
|
||||
a.setSelected("default.xml".equals(luytenPrefs.getThemeXml()));
|
||||
languagesGroup.add(a);
|
||||
fileMenu.add(a);
|
||||
a = new JRadioButtonMenuItem(new ThemeAction("Dark", "dark.xml"));
|
||||
a.setSelected("dark.xml".equals(luytenPrefs.getThemeXml()));
|
||||
languagesGroup.add(a);
|
||||
fileMenu.add(a);
|
||||
a = new JRadioButtonMenuItem(new ThemeAction("Eclipse", "eclipse.xml"));
|
||||
a.setSelected(true);
|
||||
a.setSelected("eclipse.xml".equals(luytenPrefs.getThemeXml()));
|
||||
languagesGroup.add(a);
|
||||
fileMenu.add(a);
|
||||
a = new JRadioButtonMenuItem(new ThemeAction("Visual Studio", "vs.xml"));
|
||||
a.setSelected("vs.xml".equals(luytenPrefs.getThemeXml()));
|
||||
languagesGroup.add(a);
|
||||
fileMenu.add(a);
|
||||
menuBar.add(fileMenu);
|
||||
@ -857,12 +872,17 @@ public class Model extends JFrame implements WindowListener {
|
||||
fc.addChoosableFileFilter(new FileChooserFileFilter("*.class"));
|
||||
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
fc.setMultiSelectionEnabled(false);
|
||||
retrieveOpenDialogDir(fc);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
|
||||
retrieveOpenDialogDir(fc);
|
||||
int returnVal = fc.showOpenDialog(Model.frame);
|
||||
saveOpenDialogDir(fc);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
file = fc.getSelectedFile();
|
||||
if (open)
|
||||
@ -1007,6 +1027,7 @@ public class Model extends JFrame implements WindowListener {
|
||||
fc.addChoosableFileFilter(new FileChooserFileFilter("*.java"));
|
||||
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
fc.setMultiSelectionEnabled(false);
|
||||
retrieveSaveDialogDir(fc);
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent event) {
|
||||
@ -1023,7 +1044,11 @@ public class Model extends JFrame implements WindowListener {
|
||||
final RSyntaxTextArea pane = (RSyntaxTextArea) co.getViewport().getView();
|
||||
String title = house.getTitleAt(index);
|
||||
fc.setSelectedFile(new File(title.replace(".class", ".java")));
|
||||
|
||||
retrieveSaveDialogDir(fc);
|
||||
int returnVal = fc.showSaveDialog(Model.frame);
|
||||
saveSaveDialogDir(fc);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
@ -1059,6 +1084,7 @@ public class Model extends JFrame implements WindowListener {
|
||||
fc.addChoosableFileFilter(new FileChooserFileFilter("*.zip"));
|
||||
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
||||
fc.setMultiSelectionEnabled(false);
|
||||
retrieveSaveDialogDir(fc);
|
||||
}
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
@ -1076,7 +1102,11 @@ public class Model extends JFrame implements WindowListener {
|
||||
if (s.toLowerCase().endsWith(".jar"))
|
||||
s = s.replaceAll("\\.[jJ][aA][rR]", ".zip");
|
||||
fc.setSelectedFile(new File("decompiled-" + s));
|
||||
|
||||
retrieveSaveDialogDir(fc);
|
||||
int returnVal = fc.showSaveDialog(Model.frame);
|
||||
saveSaveDialogDir(fc);
|
||||
|
||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
@ -1300,22 +1330,23 @@ public class Model extends JFrame implements WindowListener {
|
||||
|
||||
public ThemeAction(String name, String xml) {
|
||||
putValue(NAME, name);
|
||||
this.xml = "/themes/" + xml;
|
||||
this.xml = xml;
|
||||
}
|
||||
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
InputStream in = getClass().getResourceAsStream(xml);
|
||||
try {
|
||||
if (in != null) {
|
||||
theme = Theme.load(in);
|
||||
for (OpenFile f : hmap) {
|
||||
theme.apply(f.textArea);
|
||||
}
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
JOptionPane.showMessageDialog(null, e1.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
InputStream in = getClass().getResourceAsStream(LuytenPreferences.THEME_XML_PATH + xml);
|
||||
try {
|
||||
if (in != null) {
|
||||
theme = Theme.load(in);
|
||||
luytenPrefs.setThemeXml(xml);
|
||||
for (OpenFile f : hmap) {
|
||||
theme.apply(f.textArea);
|
||||
}
|
||||
}
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
JOptionPane.showMessageDialog(null, e1.toString(), "Error!", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1364,4 +1395,54 @@ public class Model extends JFrame implements WindowListener {
|
||||
windowPosition.getWindowWidth(), windowPosition.getWindowHeight());
|
||||
}
|
||||
}
|
||||
|
||||
private void retrieveOpenDialogDir(JFileChooser fc) {
|
||||
try {
|
||||
String currentDirStr = luytenPrefs.getFileOpenCurrentDirectory();
|
||||
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
|
||||
File currentDir = new File(currentDirStr);
|
||||
if (currentDir.exists() && currentDir.isDirectory()) {
|
||||
fc.setCurrentDirectory(currentDir);
|
||||
}
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveOpenDialogDir(JFileChooser fc) {
|
||||
try {
|
||||
File currentDir = fc.getCurrentDirectory();
|
||||
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
|
||||
luytenPrefs.setFileOpenCurrentDirectory(currentDir.getAbsolutePath());
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void retrieveSaveDialogDir(JFileChooser fc) {
|
||||
try {
|
||||
String currentDirStr = luytenPrefs.getFileSaveCurrentDirectory();
|
||||
if (currentDirStr != null && currentDirStr.trim().length() > 0) {
|
||||
File currentDir = new File(currentDirStr);
|
||||
if (currentDir.exists() && currentDir.isDirectory()) {
|
||||
fc.setCurrentDirectory(currentDir);
|
||||
}
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void saveSaveDialogDir(JFileChooser fc) {
|
||||
try {
|
||||
File currentDir = fc.getCurrentDirectory();
|
||||
if (currentDir != null && currentDir.exists() && currentDir.isDirectory()) {
|
||||
luytenPrefs.setFileSaveCurrentDirectory(currentDir.getAbsolutePath());
|
||||
}
|
||||
} catch (Exception exc) {
|
||||
exc.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user