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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
package org.reprap;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;
import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;
import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.vecmath.Color3f;
/**
* A single centralised repository of the current preference settings. This also
* implements (almost) a singleton for easy global access. If there are no current
* preferences fallback distribution defaults are used.
*/
public class Preferences {
private static final String propsFile = "reprap.properties";
private static final String propsFolder = ".reprap";
private static final String propsFileDist = "reprap.properties.dist";
private static Preferences globalPrefs = null;
Properties fallbackPreferences;
Properties mainPreferences;
/*
* This section deals with internal (i.e. not RepRap machine, but this code or
* physics) precisions and accuracies - it should probably
* get its data from the properties file...
*/
private static final int grid = 100; // Click outline polygons to a...
private static final double gridRes = 1.0/grid; // ...10 micron grid
private static final double lessGridSquare = gridRes*gridRes*0.01; // Small squared size of a gridsquare
private static final double tiny = 1.0e-12; // A small number
private static final double swell = 1.01; // Quad tree swell factor
private static final double machineResolution = 0.1; // RepRap step size in mm - should
// derive this from Axis1Scale and Axis2Scale
private static final double absoluteZero = -273;
private static final double inToMM = 25.4;
private static final Color3f black = new Color3f(0, 0, 0);
public static int grid() { return grid; }
public static double gridRes() { return gridRes; }
public static double lessGridSquare() { return lessGridSquare; }
public static double tiny() { return tiny; }
public static double swell() { return swell; }
public static double inchesToMillimetres() { return inToMM; }
public static double machineResolution() { return machineResolution; }
public static double absoluteZero() { return absoluteZero; }
public static Appearance unselectedApp()
{
Color3f unselectedColour = null;
try
{
unselectedColour = new Color3f((float)Preferences.loadGlobalDouble("UnselectedColourR(0..1)"),
(float)Preferences.loadGlobalDouble("UnselectedColourG(0..1)"),
(float)Preferences.loadGlobalDouble("UnselectedColourB(0..1)"));
} catch (Exception ex)
{
ex.printStackTrace();
}
Appearance unselectedApp = new Appearance();
unselectedApp.setMaterial(new
Material(unselectedColour, black, unselectedColour, black, 0f));
return unselectedApp;
}
// Main preferences constructor
public Preferences() throws IOException {
fallbackPreferences = new Properties();
mainPreferences = new Properties();
URL fallbackUrl = ClassLoader.getSystemResource(propsFileDist);
// Construct URL of user properties file
String path = new String(System.getProperty("user.home") + File.separatorChar +
propsFolder + File.separatorChar + propsFile);
File mainFile = new File(path);
URL mainUrl = mainFile.toURL();
if (fallbackUrl == null && !mainFile.exists())
throw new IOException("Cannot load RepRap properties file or default "+propsFileDist);
if (fallbackUrl != null)
fallbackPreferences.load(fallbackUrl.openStream());
if (mainFile.exists())
mainPreferences.load(mainUrl.openStream());
else
{
// If we don't have a local preferences file copy the default
// file into it.
mainPreferences.load(fallbackUrl.openStream());
save();
}
}
public void save() throws FileNotFoundException, IOException {
String savePath = new String(System.getProperty("user.home") + File.separatorChar +
propsFolder + File.separatorChar);
File f = new File(savePath + File.separatorChar + propsFile);
if (!f.exists()) {
// No properties file exists, so we will create one and try again
// We'll put the properties file in the .reprap folder,
// under the user's home folder.
File p = new File(savePath);
if (!p.isDirectory()) // Create .reprap folder if necessary
p.mkdirs();
}
OutputStream output = new FileOutputStream(f);
mainPreferences.store(output, "Reprap properties http://reprap.org/");
}
public String loadString(String name) {
if (mainPreferences.containsKey(name))
return mainPreferences.getProperty(name);
if (fallbackPreferences.containsKey(name))
return fallbackPreferences.getProperty(name);
System.err.println("RepRap preference: " + name + " not found in either preference file.");
return null;
}
public int loadInt(String name) {
String strVal = loadString(name);
return Integer.parseInt(strVal);
}
public double loadDouble(String name) {
String strVal = loadString(name);
return Double.parseDouble(strVal);
}
public boolean loadBool(String name) {
String strVal = loadString(name);
if (strVal == null) return false;
if (strVal.length() == 0) return false;
if (strVal.compareToIgnoreCase("true") == 0) return true;
return false;
}
synchronized private static void initIfNeeded() throws IOException {
if (globalPrefs == null)
globalPrefs = new Preferences();
}
public static String loadGlobalString(String name) throws IOException {
initIfNeeded();
return globalPrefs.loadString(name);
}
public static int loadGlobalInt(String name) throws IOException {
initIfNeeded();
return globalPrefs.loadInt(name);
}
public static double loadGlobalDouble(String name) throws IOException {
initIfNeeded();
return globalPrefs.loadDouble(name);
}
public static boolean loadGlobalBool(String name) throws IOException {
initIfNeeded();
return globalPrefs.loadBool(name);
}
public static void saveGlobal() throws IOException {
initIfNeeded();
globalPrefs.save();
}
public static Preferences getGlobalPreferences() throws IOException {
initIfNeeded();
return globalPrefs;
}
/**
* Set a new value
* @param name
* @param value
* @throws IOException
*/
public static void setGlobalString(String name, String value) throws IOException {
initIfNeeded();
globalPrefs.setString(name, value);
}
public static void setGlobalBool(String name, boolean value) throws IOException {
initIfNeeded();
globalPrefs.setString(name, value ? "true" : "false");
}
/**
* @param name
* @param value
*/
private void setString(String name, String value) {
mainPreferences.setProperty(name, value);
}
/**
* @return an array of all the names of all the materials in extruders
* @throws IOException
*/
public static String[] allMaterials() throws IOException
{
int extruderCount = globalPrefs.loadInt("NumberOfExtruders");
String[] result = new String[extruderCount];
for(int i = 0; i < extruderCount; i++)
{
String prefix = "Extruder" + i + "_";
result[i] = globalPrefs.loadString(prefix + "MaterialType(name)");
}
return result;
}
public static String[] startsWith(String prefix) throws IOException
{
initIfNeeded();
Enumeration allOfThem = globalPrefs.mainPreferences.propertyNames();
List r = new ArrayList();
while(allOfThem.hasMoreElements())
{
String next = (String)allOfThem.nextElement();
if(next.startsWith(prefix))
r.add(next);
}
String[] result = new String[r.size()];
for(int i = 0; i < r.size(); i++)
result[i] = (String)r.get(i);
return result;
}
public static String[] notStartsWith(String prefix) throws IOException
{
initIfNeeded();
Enumeration allOfThem = globalPrefs.mainPreferences.propertyNames();
List r = new ArrayList();
while(allOfThem.hasMoreElements())
{
String next = (String)allOfThem.nextElement();
if(!next.startsWith(prefix))
r.add(next);
}
String[] result = new String[r.size()];
for(int i = 0; i < r.size(); i++)
result[i] = (String)r.get(i);
return result;
}
}
|