2024-10-07 00:58:29 +03:00
|
|
|
package Common_old;
|
|
|
|
|
import Common.Utils.CommonUtils;
|
|
|
|
|
import Common_old.UI.Menus_2023.StableMenuItem;
|
2024-03-20 23:32:24 +03:00
|
|
|
import Visual_DVM_2021.Passes.PassCode_2021;
|
|
|
|
|
import Visual_DVM_2021.Passes.Pass_2021;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.io.File;
|
2023-11-13 21:07:44 +03:00
|
|
|
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),
|
2024-10-07 00:58:29 +03:00
|
|
|
getFlag(fieldName) ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png");
|
2023-09-17 22:13:42 +03:00
|
|
|
//-
|
|
|
|
|
menu_item.addActionListener(e -> {
|
2024-03-20 23:32:24 +03:00
|
|
|
if (Pass_2021.passes.get(PassCode_2021.UpdateProperty).Do(fieldName, !getFlag(fieldName)))
|
2024-10-07 20:04:11 +03:00
|
|
|
menu_item.setIcon(CommonUtils.getIcon(getFlag(fieldName) ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));
|
2023-09-17 22:13:42 +03:00
|
|
|
});
|
|
|
|
|
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 {
|
2024-10-07 00:58:29 +03:00
|
|
|
CommonUtils.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();
|
2023-11-13 21:07:44 +03:00
|
|
|
public boolean updateField(String name, Object newValue) {
|
|
|
|
|
try {
|
|
|
|
|
Field field = getClass().getField(name);
|
2024-10-07 00:58:29 +03:00
|
|
|
Object oldValue = field.get(this);
|
2023-11-13 21:07:44 +03:00
|
|
|
//---
|
|
|
|
|
if (newValue.equals(oldValue))
|
|
|
|
|
return false;
|
|
|
|
|
//--
|
|
|
|
|
field.set(this, newValue);
|
2023-11-13 21:08:44 +03:00
|
|
|
this.Update();
|
2023-11-13 21:07:44 +03:00
|
|
|
return true;
|
|
|
|
|
//--
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2024-03-20 23:32:24 +03:00
|
|
|
public void switchAndUpdateFlag(String name) {
|
2023-11-13 21:07:44 +03:00
|
|
|
try {
|
|
|
|
|
Field field = getClass().getField(name);
|
2024-10-07 00:58:29 +03:00
|
|
|
boolean oldValue = (boolean) field.get(this);
|
2023-11-13 21:07:44 +03:00
|
|
|
boolean newValue = !oldValue;
|
|
|
|
|
//---
|
|
|
|
|
field.set(this, newValue);
|
2023-11-13 21:08:44 +03:00
|
|
|
this.Update();
|
2023-11-13 21:07:44 +03:00
|
|
|
//--
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
exception.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-17 22:13:42 +03:00
|
|
|
}
|