промежуточный. в процессе рефакторинга объектов с настройками

This commit is contained in:
2025-01-14 00:47:19 +03:00
parent 63112eed7b
commit 483089e954
5 changed files with 103 additions and 81 deletions

View File

@@ -1,3 +1,66 @@
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<String,JMenuItem> 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();
}
}
}