промежуточный. в процессе рефакторинга объектов с настройками
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package Common.Utils;
|
||||
import Common.CommonConstants;
|
||||
import Common.Passes.PassException;
|
||||
import Common.Properties;
|
||||
import _VisualDVM.GlobalProperties;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonObject;
|
||||
@@ -518,4 +520,22 @@ public class Utils_ {
|
||||
public static int fromBoolean(boolean flag) {
|
||||
return flag ? 1 : 0;
|
||||
}
|
||||
//--
|
||||
public static <T extends Properties> T SynschronizeProperties(File propertiesFile, Class<T> properties_class) {
|
||||
T res= null;
|
||||
try {
|
||||
res= properties_class.newInstance();
|
||||
if (propertiesFile.exists()){
|
||||
//файл существует. нужно его ссчитать.
|
||||
res = (T) Utils_.jsonFromFile(propertiesFile, properties_class);
|
||||
}
|
||||
res.setFile(propertiesFile);
|
||||
//перезаписываем файл в любом случае, так как может измениться формат.
|
||||
Utils_.jsonToFile(res, propertiesFile);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user