81 lines
2.5 KiB
Java
81 lines
2.5 KiB
Java
package Common;
|
|
import Common.UI.Menus_2023.StableMenuItem;
|
|
import Common.Utils.Utils;
|
|
|
|
import javax.swing.*;
|
|
import java.io.File;
|
|
import java.lang.reflect.Field;
|
|
public abstract class Properties {
|
|
public void addFlagMenuItem(JMenu menu, String fieldName) {
|
|
JMenuItem menu_item = new StableMenuItem(getFieldDescription(fieldName),
|
|
getFlag(fieldName) ? "/icons/Pick.png" : "/icons/NotPick.png");
|
|
//-
|
|
menu_item.addActionListener(e -> {
|
|
switchFlag(fieldName);
|
|
Update();
|
|
menu_item.setIcon(Utils.getIcon(getFlag(fieldName) ? "/icons/Pick.png" : "/icons/NotPick.png"));
|
|
});
|
|
menu.add(menu_item);
|
|
}
|
|
public boolean getFlag(String fieldName) {
|
|
boolean field = false;
|
|
try {
|
|
field = (boolean) GlobalProperties.class.getField(fieldName).get(this);
|
|
//
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
return field;
|
|
}
|
|
public void switchFlag(String fieldName) {
|
|
boolean field = false;
|
|
try {
|
|
field = (boolean) GlobalProperties.class.getField(fieldName).get(this);
|
|
GlobalProperties.class.getField(fieldName).set(this, !field);
|
|
//
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
}
|
|
}
|
|
public void Update() {
|
|
try {
|
|
Utils.jsonToFile(this, getFile());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
//--
|
|
public abstract String getFieldDescription(String fieldName);
|
|
public abstract File getFile();
|
|
public boolean updateField(String name, Object newValue) {
|
|
try {
|
|
Field field = getClass().getField(name);
|
|
Object oldValue = field.get(Global.properties);
|
|
//---
|
|
if (newValue.equals(oldValue))
|
|
return false;
|
|
//--
|
|
field.set(this, newValue);
|
|
this.Update();
|
|
return true;
|
|
//--
|
|
} catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
public void switchAndUpdateFlag(String name){
|
|
try {
|
|
Field field = getClass().getField(name);
|
|
boolean oldValue = (boolean) field.get(Global.properties);
|
|
boolean newValue = !oldValue;
|
|
//---
|
|
field.set(this, newValue);
|
|
this.Update();
|
|
//--
|
|
} catch (Exception exception) {
|
|
exception.printStackTrace();
|
|
}
|
|
}
|
|
}
|