EOS 2  1.1.0
Einfache Objektbasierte Sprache
Configuration.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.robot.gui;
2 
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.util.LinkedList;
10 import java.util.Properties;
11 import de.lathanda.eos.base.ResourceLoader;
12 
13 
20 public class Configuration {
21 
22  public static final Configuration def = new Configuration();
23  private Properties configuration;
24  private boolean dirty;
25 
26  private int fallheight;
27  private int jumpheight;
28 
29  private Configuration() {
30  try {
31  load();
32  } catch (Exception e) {
33  setDefault();
34  }
35  }
36  private void load() throws IOException {
37  String home = System.getProperty("user.home");
38  File file = new File(home + "/.eos/config.ini");
39  if (file.exists()) {
40  try (InputStream in = new FileInputStream(file)) {
41  load(in);
42  }
43  } else {
44  try (InputStream in = ResourceLoader.getResourceAsStream("config.ini")) {
45  load(in);
46  }
47  }
48  }
49 
50  public void save() throws IOException {
51  String home = System.getProperty("user.home");
52  File dir = new File(home + "/.eos");
53  File file = new File(home + "/.eos/config.ini");
54  if (!(dir.exists() && dir.isDirectory())) {
55  dir.mkdir();
56  }
57  save(new FileOutputStream(file));
58  }
59 
60  private void load(InputStream in) throws IOException {
61  configuration = new Properties();
62  configuration.load(in);
63  fallheight = Integer.valueOf(configuration.getProperty("fallheight"));
64  jumpheight = Integer.valueOf(configuration.getProperty("jumpheight"));
65  dirty = false;
66  }
67 
68  private void save(OutputStream out) throws IOException {
69  configuration.setProperty("fallheight", Integer.toString(fallheight));
70  configuration.setProperty("jumpheight", Integer.toString(jumpheight));
71  configuration.store(out, "EOS Configuration");
72  dirty = false;
73  }
74  public void cleanup() throws Throwable {
75  if (dirty) {
76  save();
77  }
78  }
79  private void setDefault() {
80  fallheight = 1;
81  jumpheight = 1;
82  dirty = false;
83  }
84  public int getFallheight() {
85  return fallheight;
86  }
87  public void setFallheight(int fallheight) {
88  if (this.fallheight != fallheight) {
89  this.fallheight = fallheight;
91  }
92  }
93  public int getJumpheight() {
94  return jumpheight;
95  }
96  public void setJumpheight(int jumpheight) {
97  if (this.jumpheight != jumpheight) {
98  this.jumpheight = jumpheight;
100  }
101  }
102 
103  private LinkedList<ConfigurationListener> configurationListener = new LinkedList<>();
104  public synchronized void addConfigurationListener(ConfigurationListener cf) {
105  configurationListener.add(cf);
106  }
107  public synchronized void removeConfigurationListener(ConfigurationListener cf) {
108  configurationListener.remove(cf);
109  }
110  public synchronized void fireRobotConfigurationChanged() {
111  dirty = true;
112  configurationListener.forEach(cf -> cf.robotConfigurationChanged(fallheight, jumpheight));
113  }
114 
115  public interface ConfigurationListener {
116  public default void robotConfigurationChanged(int fallheight, int jumpheight) {}
117  }
118 }
synchronized void removeConfigurationListener(ConfigurationListener cf)
synchronized void addConfigurationListener(ConfigurationListener cf)
synchronized void fireRobotConfigurationChanged()
default void robotConfigurationChanged(int fallheight, int jumpheight)
Impressum