Files
VisualSapfor/src/Common/Properties.java

79 lines
2.5 KiB
Java
Raw Normal View History

2023-09-17 22:13:42 +03:00
package Common;
import Common.UI.Menus_2023.StableMenuItem;
import Common.Utils.Utils;
import javax.swing.*;
import java.io.File;
import java.lang.reflect.Field;
2023-09-17 22:13:42 +03:00
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());
2023-09-17 22:13:42 +03:00
} 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);
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);
//--
} catch (Exception exception) {
exception.printStackTrace();
}
}
2023-09-17 22:13:42 +03:00
}