-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerProperties.java
executable file
·122 lines (101 loc) · 2.85 KB
/
PowerProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package peter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import G1.View;
import peter.Property.*;
public class PowerProperties {
private ArrayList<Property> props;
private View view;
public PowerProperties(View view) {
this(view, new ArrayList<Property>());
}
public PowerProperties(View view, ArrayList<Property> props) {
this.view = view;
this.props = props;
}
/**
* TODO: add methds for things besides floats...
* @param name
* @param val
* @param inc
*/
public void addPropery(String name, float val, float inc) {
props.add(new FloatProperty(name, val, inc));
}
/**
* returns the property's value if it exists, null if not found.
* @param name
* @return
*/
public Property getProperty(String name){
for(Property p : props){
if (p.getName().equals(name))
return p;
}
return null;
}
public ArrayList<Property> getProps(){
return props;
}
/**
* This loads properties from a properties file if they and it exist, allows the user to customize each
* property, optionally saves the property, and returns the updated properties list.
*
* @param props
* @param fName the properties ini that you waqnt to use
* @param save whether to save.
* @return
*/
//TODO: This really doesn't belong in view, but would otherwise need to be passed a view.
public void configAll(String fName, boolean save) {
if (props.size()> 0) {
Properties p = new Properties();
try {
p.load(new FileInputStream(new File(fName))); // this will close stream when done?
} catch (FileNotFoundException e) {
view.printErr(fName + "not found"); // uses static class View
} catch (IOException e) {
view.printErr(fName + "IO exception");
}
for (Property prop : props){
if (p.getProperty(prop.getName()) != null) { //TODO: checl for number format exception!
prop.setVal(p.getProperty(prop.getName()));
}
}
try { // TODO catch
view.configMenu(props);
} catch (InterruptedException e) {
}
for (Property prop : props){
view.println(view.makeTitle(prop.getName() + ": ", prop.getStrVal()));
p.setProperty(prop.getName(), prop.getStrVal());
}
if (save) {
File config = new File(fName);
if (config.exists()) {
config.delete();
}
try {
config.createNewFile();
} catch (IOException e1) {
view.printErr("can't make properties file");
}
FileOutputStream fout = new FileOutputStream(config);
try {
p.store(fout, "This is an NXT config file");
fout.close();
} catch (IOException e) {
view.printErr("error writing indy.ini: " + e.getMessage());
}
}
}
}
}