package Common_old; import Common.Utils.CommonUtils; import Common_old.UI.Menus_2023.StableMenuItem; import Common_old.Utils.Utils; import Visual_DVM_2021.Passes.PassCode_2021; import Visual_DVM_2021.Passes.Pass_2021; 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) ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"); //- menu_item.addActionListener(e -> { if (Pass_2021.passes.get(PassCode_2021.UpdateProperty).Do(fieldName, !getFlag(fieldName))) menu_item.setIcon(Utils.getIcon(getFlag(fieldName) ? "/Common/icons/Pick.png" : "/Common/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 { CommonUtils.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(this); //--- 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(this); boolean newValue = !oldValue; //--- field.set(this, newValue); this.Update(); //-- } catch (Exception exception) { exception.printStackTrace(); } } }