FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/Preferences.java at master · processing/processing · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
processing
/
processing
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
1.5k
Star
6.5k
Code
Issues
0
Pull requests
0
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
Preferences.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
439 lines (350 loc) · 14.4 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
Preferences.java
Copy path
File metadata and controls
439 lines (350 loc) · 14.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2014-19 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
processing
.
app
;
import
java
.
awt
.
Color
;
import
java
.
awt
.
Font
;
import
java
.
awt
.
SystemColor
;
import
java
.
io
.*;
import
java
.
util
.*;
import
processing
.
app
.
ui
.
Toolkit
;
import
processing
.
core
.*;
/**
* Storage class for user preferences and environment settings.
* <P>
* This class does not use the Properties class because .properties files use
* ISO 8859-1 encoding, which is highly likely to be a problem when trying to
* save sketch folders and locations. Like the rest of Processing, we use UTF8.
* <p>
* We don't use the Java Preferences API because it would entail writing to
* the registry (on Windows), or an obscure file location (on Mac OS X) and
* make it far more difficult (impossible) to remove the preferences.txt to
* reset them (when they become corrupt), or to find the the file to make
* edits for numerous obscure preferences that are not part of the preferences
* window. If we added a generic editor (e.g. about:config in Mozilla) for
* such things, we could start using the Java Preferences API. But wow, that
* sounds like a lot of work. Not unlike writing this paragraph.
*/
public
class
Preferences
{
// had to rename the defaults file because people were editing it
static
final
String
DEFAULTS_FILE
=
"defaults.txt"
;
//$NON-NLS-1$
static
final
String
PREFS_FILE
=
"preferences.txt"
;
//$NON-NLS-1$
static
Map
<
String
,
String
>
defaults
;
static
Map
<
String
,
String
>
table
=
new
HashMap
<>();
static
File
preferencesFile
;
// /** @return true if the sketchbook file did not exist */
// static public boolean init() {
static
public
void
init
() {
// start by loading the defaults, in case something
// important was deleted from the user prefs
try
{
// Name changed for 2.1b2 to avoid problems with users modifying or
// replacing the file after doing a search for "preferences.txt".
load
(
Base
.
getLibStream
(
DEFAULTS_FILE
));
}
catch
(
Exception
e
) {
Messages
.
showError
(
null
,
"Could not read default settings.
\n
"
+
"You'll need to reinstall Processing."
,
e
);
}
// Clone the defaults, then override any them with the user's preferences.
// This ensures that any new/added preference will be present.
defaults
=
new
HashMap
<>(
table
);
// other things that have to be set explicitly for the defaults
setColor
(
"run.window.bgcolor"
,
SystemColor
.
control
);
//$NON-NLS-1$
// For CJK users, enable IM support by default
if
(
Language
.
useInputMethod
()) {
setBoolean
(
"editor.input_method_support"
,
true
);
}
// next load user preferences file
preferencesFile
=
Base
.
getSettingsFile
(
PREFS_FILE
);
boolean
firstRun
= !
preferencesFile
.
exists
();
if
(!
firstRun
) {
try
{
load
(
new
FileInputStream
(
preferencesFile
));
}
catch
(
Exception
ex
) {
Messages
.
showError
(
"Error reading preferences"
,
"Error reading the preferences file. "
+
"Please delete (or move)
\n
"
+
preferencesFile
.
getAbsolutePath
() +
" and restart Processing."
,
ex
);
}
}
if
(
checkSketchbookPref
() ||
firstRun
) {
// if (firstRun) {
// create a new preferences file if none exists
// saves the defaults out to the file
save
();
}
PApplet
.
useNativeSelect
=
Preferences
.
getBoolean
(
"chooser.files.native"
);
//$NON-NLS-1$
// Adding option to disable this in case it's getting in the way
if
(
get
(
"proxy.system"
).
equals
(
"true"
)) {
// Use the system proxy settings by default
// https://github.com/processing/processing/issues/2643
System
.
setProperty
(
"java.net.useSystemProxies"
,
"true"
);
}
// Set HTTP, HTTPS, and SOCKS proxies for individuals
// who want/need to override the system setting
// http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
// Less readable version with the Oracle style sheet:
// http://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
handleProxy
(
"http"
,
"http.proxyHost"
,
"http.proxyPort"
);
handleProxy
(
"https"
,
"https.proxyHost"
,
"https.proxyPort"
);
handleProxy
(
"socks"
,
"socksProxyHost"
,
"socksProxyPort"
);
}
static
void
handleProxy
(
String
protocol
,
String
hostProp
,
String
portProp
) {
String
proxyHost
=
get
(
"proxy."
+
protocol
+
".host"
);
String
proxyPort
=
get
(
"proxy."
+
protocol
+
".port"
);
if
(
proxyHost
!=
null
&&
proxyHost
.
length
() !=
0
&&
proxyPort
!=
null
&&
proxyPort
.
length
() !=
0
) {
System
.
setProperty
(
hostProp
,
proxyHost
);
System
.
setProperty
(
portProp
,
proxyPort
);
}
}
static
public
String
getPreferencesPath
() {
return
preferencesFile
.
getAbsolutePath
();
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Load a set of key/value pairs from a UTF-8 encoded file into 'table'.
* For 3.0a6, this removes any platform-specific extensions from keys, so
* that we don't have platform-specific entries in a user's preferences.txt
* file, which would require all prefs to be changed twice, or risk being
* overwritten by the unchanged platform-specific version on reload.
*/
static
public
void
load
(
InputStream
input
)
throws
IOException
{
HashMap
<
String
,
String
>
platformSpecific
=
new
HashMap
<>();
String
[]
lines
=
PApplet
.
loadStrings
(
input
);
// Reads as UTF-8
for
(
String
line
:
lines
) {
if
((
line
.
length
() ==
0
) ||
(
line
.
charAt
(
0
) ==
'#'
))
continue
;
// this won't properly handle = signs being in the text
int
equals
=
line
.
indexOf
(
'='
);
if
(
equals
!= -
1
) {
String
key
=
line
.
substring
(
0
,
equals
).
trim
();
String
value
=
line
.
substring
(
equals
+
1
).
trim
();
if
(!
isPlatformSpecific
(
key
,
value
,
platformSpecific
)) {
table
.
put
(
key
,
value
);
}
}
}
// Now override the keys with any platform-specific defaults we've found.
for
(
String
key
:
platformSpecific
.
keySet
()) {
table
.
put
(
key
,
platformSpecific
.
get
(
key
));
}
}
/**
* @param key original key (may include platform extension)
* @param value
* @param specific where to put the key/value pairs for *this* platform
* @return true if a platform-specific key
*/
static
protected
boolean
isPlatformSpecific
(
String
key
,
String
value
,
Map
<
String
,
String
>
specific
) {
for
(
String
platform
:
PConstants
.
platformNames
) {
String
ext
=
"."
+
platform
;
if
(
key
.
endsWith
(
ext
)) {
String
thisPlatform
=
PConstants
.
platformNames
[
PApplet
.
platform
];
if
(
platform
.
equals
(
thisPlatform
)) {
key
=
key
.
substring
(
0
,
key
.
lastIndexOf
(
ext
));
// store this for later overrides
specific
.
put
(
key
,
value
);
}
else
{
// ignore platform-specific defaults for other platforms,
// but return 'true' because it needn't be added to the big list
}
return
true
;
}
}
return
false
;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
static
public
void
save
() {
// On startup it'll be null, don't worry about it. It's trying to update
// the prefs for the open sketch before Preferences.init() has been called.
if
(
preferencesFile
!=
null
) {
try
{
File
dir
=
preferencesFile
.
getParentFile
();
File
preferencesTemp
=
File
.
createTempFile
(
"preferences"
,
".txt"
,
dir
);
preferencesTemp
.
setWritable
(
true
,
false
);
// Fix for 0163 to properly use Unicode when writing preferences.txt
PrintWriter
writer
=
PApplet
.
createWriter
(
preferencesTemp
);
String
[]
keyList
=
table
.
keySet
().
toArray
(
new
String
[
table
.
size
()]);
// Sorting is really helpful for debugging, diffing, and finding keys
keyList
=
PApplet
.
sort
(
keyList
);
for
(
String
key
:
keyList
) {
writer
.
println
(
key
+
"="
+
table
.
get
(
key
));
//$NON-NLS-1$
}
writer
.
flush
();
writer
.
close
();
// Rename preferences.txt to preferences.old
File
oldPreferences
=
new
File
(
dir
,
"preferences.old"
);
if
(
oldPreferences
.
exists
()) {
if
(!
oldPreferences
.
delete
()) {
throw
new
IOException
(
"Could not delete preferences.old"
);
}
}
if
(
preferencesFile
.
exists
() &&
!
preferencesFile
.
renameTo
(
oldPreferences
)) {
throw
new
IOException
(
"Could not replace preferences.old"
);
}
// Make the temporary file into the real preferences
if
(!
preferencesTemp
.
renameTo
(
preferencesFile
)) {
throw
new
IOException
(
"Could not move preferences file into place"
);
}
}
catch
(
IOException
e
) {
Messages
.
showWarning
(
"Preferences"
,
"Could not save the Preferences file."
,
e
);
}
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// all the information from preferences.txt
static
public
String
get
(
String
attribute
/*, String defaultValue */
) {
return
table
.
get
(
attribute
);
}
static
public
String
getDefault
(
String
attribute
) {
return
defaults
.
get
(
attribute
);
}
static
public
void
set
(
String
attribute
,
String
value
) {
table
.
put
(
attribute
,
value
);
}
static
public
void
unset
(
String
attribute
) {
table
.
remove
(
attribute
);
}
static
public
boolean
getBoolean
(
String
attribute
) {
String
value
=
get
(
attribute
);
//, null);
return
Boolean
.
parseBoolean
(
value
);
/*
supposedly not needed, because anything besides 'true'
(ignoring case) will just be false.. so if malformed -> false
if (value == null) return defaultValue;
try {
return (new Boolean(value)).booleanValue();
} catch (NumberFormatException e) {
System.err.println("expecting an integer: " + attribute + " = " + value);
}
return defaultValue;
*/
}
static
public
void
setBoolean
(
String
attribute
,
boolean
value
) {
set
(
attribute
,
value
?
"true"
:
"false"
);
//$NON-NLS-1$ //$NON-NLS-2$
}
static
public
int
getInteger
(
String
attribute
/*, int defaultValue*/
) {
return
Integer
.
parseInt
(
get
(
attribute
));
/*
String value = get(attribute, null);
if (value == null) return defaultValue;
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
// ignored will just fall through to returning the default
System.err.println("expecting an integer: " + attribute + " = " + value);
}
return defaultValue;
//if (value == null) return defaultValue;
//return (value == null) ? defaultValue :
//Integer.parseInt(value);
*/
}
static
public
void
setInteger
(
String
key
,
int
value
) {
set
(
key
,
String
.
valueOf
(
value
));
}
static
public
Color
getColor
(
String
name
) {
Color
parsed
=
Color
.
GRAY
;
// set a default
String
s
=
get
(
name
);
if
((
s
!=
null
) && (
s
.
indexOf
(
"#"
) ==
0
)) {
//$NON-NLS-1$
try
{
parsed
=
new
Color
(
Integer
.
parseInt
(
s
.
substring
(
1
),
16
));
}
catch
(
Exception
e
) { }
}
return
parsed
;
}
static
public
void
setColor
(
String
attr
,
Color
what
) {
set
(
attr
,
"#"
+
PApplet
.
hex
(
what
.
getRGB
() &
0xffffff
,
6
));
//$NON-NLS-1$
}
// Identical version found in Settings.java
static
public
Font
getFont
(
String
attr
) {
try
{
boolean
replace
=
false
;
String
value
=
get
(
attr
);
if
(
value
==
null
) {
// use the default font instead
value
=
getDefault
(
attr
);
replace
=
true
;
}
String
[]
pieces
=
PApplet
.
split
(
value
,
','
);
if
(
pieces
.
length
!=
3
) {
value
=
getDefault
(
attr
);
pieces
=
PApplet
.
split
(
value
,
','
);
replace
=
true
;
}
String
name
=
pieces
[
0
];
int
style
=
Font
.
PLAIN
;
// equals zero
if
(
pieces
[
1
].
indexOf
(
"bold"
) != -
1
) {
//$NON-NLS-1$
style
|=
Font
.
BOLD
;
}
if
(
pieces
[
1
].
indexOf
(
"italic"
) != -
1
) {
//$NON-NLS-1$
style
|=
Font
.
ITALIC
;
}
int
size
=
PApplet
.
parseInt
(
pieces
[
2
],
12
);
// replace bad font with the default from lib/preferences.txt
if
(
replace
) {
set
(
attr
,
value
);
}
if
(!
name
.
startsWith
(
"processing."
)) {
return
new
Font
(
name
,
style
,
size
);
}
else
{
if
(
pieces
[
0
].
equals
(
"processing.sans"
)) {
return
Toolkit
.
getSansFont
(
size
,
style
);
}
else
if
(
pieces
[
0
].
equals
(
"processing.mono"
)) {
return
Toolkit
.
getMonoFont
(
size
,
style
);
}
}
}
catch
(
Exception
e
) {
// Adding try/catch block because this may be where
// a lot of startup crashes are happening.
Messages
.
log
(
"Error with font "
+
get
(
attr
) +
" for attribute "
+
attr
);
}
return
new
Font
(
"Dialog"
,
Font
.
PLAIN
,
12
);
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Check for a 3.0 sketchbook location, and if none exists,
* try to grab it from the 2.0 sketchbook location.
* @return true if a location was found and the pref didn't exist
*/
static
protected
boolean
checkSketchbookPref
() {
// If a 3.0 sketchbook location has never been inited
if
(
getSketchbookPath
() ==
null
) {
String
twoPath
=
get
(
"sketchbook.path"
);
// If they've run the 2.0 version, start with that location
if
(
twoPath
!=
null
) {
setSketchbookPath
(
twoPath
);
return
true
;
// save the sketchbook right away
}
// Otherwise it'll be null, and reset properly by Base
}
return
false
;
}
static
public
String
getOldSketchbookPath
() {
return
get
(
"sketchbook.path"
);
}
static
public
String
getSketchbookPath
() {
return
get
(
"sketchbook.path.three"
);
//$NON-NLS-1$
}
static
protected
void
setSketchbookPath
(
String
path
) {
set
(
"sketchbook.path.three"
,
path
);
//$NON-NLS-1$
}
}
Back
|
FazBrowse Home
|
New Git URL