Skip to content

Commit f3d0a55

Browse files
committed
add reckon() method to simplify the props behavior
1 parent a4cc43c commit f3d0a55

2 files changed

Lines changed: 23 additions & 25 deletions

File tree

app/src/processing/app/Settings.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,26 @@ public String remove(String key) {
261261
}
262262

263263

264-
public void deleteIfEmpty() {
265-
if (table.isEmpty() && file.exists()) {
266-
file.delete();
264+
/**
265+
* The day of reckoning: save() a file if it has entries, or delete
266+
* if the file exists but the table no longer has any entries.
267+
*/
268+
public void reckon() {
269+
if (table.isEmpty()) {
270+
if (file.exists() && !file.delete()) {
271+
System.err.println("Could not remove empty " + file);
272+
}
273+
} else {
274+
save();
267275
}
268276
}
269277

278+
279+
/*
270280
public boolean isEmpty() {
271281
return table.isEmpty();
272282
}
283+
*/
273284

274285

275286
public Map<String, String> getMap() {

app/src/processing/app/Sketch.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,37 +1229,25 @@ protected void updateModeProperties(Mode mode, Mode defaultMode) {
12291229
* Create or modify a sketch.properties file to specify the given Mode.
12301230
*/
12311231
static protected void updateModeProperties(File folder, Mode mode, Mode defaultMode) {
1232-
File propsFile = null;
12331232
try {
12341233
// Read the old sketch.properties file if it already exists
1235-
propsFile = new File(folder, "sketch.properties");
1236-
Settings settings = new Settings(propsFile);
1234+
Settings props = loadProperties(folder);
12371235

12381236
// If changing to the default Mode,
12391237
// remove those entries from sketch.properties
12401238
if (mode == defaultMode) {
1241-
Map<String, String> map = settings.getMap();
1242-
map.remove("mode");
1243-
map.remove("mode.id");
1244-
if (map.isEmpty()) {
1245-
if (propsFile.exists()) {
1246-
if (!propsFile.delete()) {
1247-
System.err.println("Could not remove unnecessary " + propsFile);
1248-
}
1249-
}
1250-
} else {
1251-
// Mode wasn't the only thing set, so write the other params
1252-
settings.save();
1253-
}
1239+
props.remove("mode");
1240+
props.remove("mode.id");
12541241
} else {
12551242
// Setting to something other than the default Mode,
12561243
// write that and any other params already in the file.
1257-
settings.set("mode", mode.getTitle());
1258-
settings.set("mode.id", mode.getIdentifier());
1259-
settings.save();
1244+
props.set("mode", mode.getTitle());
1245+
props.set("mode.id", mode.getIdentifier());
12601246
}
1247+
props.reckon();
1248+
12611249
} catch (IOException e) {
1262-
System.err.println("Error while writing " + propsFile);
1250+
System.err.println("Error while writing sketch.properties");
12631251
e.printStackTrace();
12641252
}
12651253
}
@@ -1328,11 +1316,10 @@ private void updateNameProperties() {
13281316

13291317
if (mainName.equals(defaultName)) {
13301318
props.remove("main");
1331-
props.deleteIfEmpty();
13321319
} else {
13331320
props.set("main", mainName);
1334-
props.save();
13351321
}
1322+
props.reckon();
13361323

13371324
} catch (IOException e) {
13381325
System.err.println("Error while updating sketch.properties");

0 commit comments

Comments
 (0)