2024-10-15 23:01:36 +03:00
|
|
|
package Common;
|
2025-01-14 00:47:19 +03:00
|
|
|
import Common.Utils.Utils_;
|
2025-01-16 02:26:51 +03:00
|
|
|
import Common.Visual.Controls.StableMenuItem;
|
2025-01-14 00:47:19 +03:00
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2025-01-16 02:26:51 +03:00
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
2025-01-14 00:47:19 +03:00
|
|
|
import java.io.File;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.LinkedHashMap;
|
2024-10-15 23:01:36 +03:00
|
|
|
public class Properties {
|
2025-01-16 02:26:51 +03:00
|
|
|
protected LinkedHashMap<String, JMenuItem> controls = new LinkedHashMap<>();
|
|
|
|
|
private File file = null; //файл где хранятся настройки.
|
2025-03-13 00:32:20 +03:00
|
|
|
public Properties() {
|
|
|
|
|
}
|
|
|
|
|
public Properties(File file_in) {
|
|
|
|
|
setFile(file_in);
|
|
|
|
|
}
|
2025-01-14 00:47:19 +03:00
|
|
|
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 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 "?";
|
|
|
|
|
}
|
2025-01-16 02:26:51 +03:00
|
|
|
public boolean controlAction(String fieldName, JMenuItem control
|
2025-03-13 00:32:20 +03:00
|
|
|
) {
|
2025-01-16 02:26:51 +03:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
public JMenuItem getMenuItem(String fieldName) {
|
|
|
|
|
final JMenuItem menuItem;
|
|
|
|
|
if (controls.containsKey(fieldName))
|
|
|
|
|
menuItem = controls.get(fieldName);
|
|
|
|
|
else {
|
|
|
|
|
menuItem = new StableMenuItem();
|
|
|
|
|
Mark(fieldName, menuItem);
|
|
|
|
|
menuItem.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
if (controlAction(fieldName, menuItem))
|
|
|
|
|
Mark(fieldName, menuItem);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
controls.put(fieldName, menuItem);
|
|
|
|
|
}
|
|
|
|
|
return menuItem;
|
|
|
|
|
}
|
|
|
|
|
public void Mark(String fieldName, JMenuItem control) {
|
|
|
|
|
String description = getFieldDescription(fieldName);
|
2025-01-14 00:47:19 +03:00
|
|
|
try {
|
|
|
|
|
Object value = this.getClass().getField(fieldName).get(this);
|
2025-01-16 02:26:51 +03:00
|
|
|
if (value instanceof Boolean) {
|
2025-01-14 00:47:19 +03:00
|
|
|
Boolean flag = (Boolean) value;
|
|
|
|
|
control.setText(description);
|
2025-01-16 02:26:51 +03:00
|
|
|
control.setIcon(Utils_.getIcon(flag ? "/Common/icons/Pick.png" : "/Common/icons/NotPick.png"));
|
|
|
|
|
} else {
|
|
|
|
|
control.setText(description + " : " + value);
|
2025-01-14 00:47:19 +03:00
|
|
|
}
|
2025-01-16 02:26:51 +03:00
|
|
|
} catch (Exception ex) {
|
2025-01-14 00:47:19 +03:00
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-13 00:32:20 +03:00
|
|
|
public void setControlVisible(String settingName, boolean flag) {
|
|
|
|
|
if (controls.containsKey(settingName)) {
|
2025-02-28 19:12:32 +03:00
|
|
|
controls.get(settingName).setVisible(flag);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-15 23:01:36 +03:00
|
|
|
}
|