package Common; import Common.Utils.Utils_; import javax.swing.*; import java.io.File; import java.lang.reflect.Field; import java.util.LinkedHashMap; public class Properties { protected LinkedHashMap controls= new LinkedHashMap<>(); private File file=null; //файл где хранятся настройки. public File getFile() { return file; } public void setFile(File file) { this.file = file; } public void Update() { try { Utils_.jsonToFile(this, getFile()); } catch (Exception e) { e.printStackTrace(); } } public Properties(){} public Properties(File file_in){ setFile(file_in); } 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); Update(); return true; //-- } catch (Exception exception) { exception.printStackTrace(); } return false; } public String getFieldDescription(String fieldName) { return "?"; } public JMenuItem getMenuItem(String fieldName){return null;} //отобразить на контроле значение поля. public void Mark(String fieldName, JMenuItem control){ String description= getFieldDescription(fieldName); try { Object value = this.getClass().getField(fieldName).get(this); if (value instanceof Boolean){ Boolean flag = (Boolean) value; control.setText(description); control.setIcon(Utils_.getIcon(flag? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png")); }else { control.setText(description+" : "+value); } } catch (Exception ex){ ex.printStackTrace(); } } }