Skip to content

Commit dc61660

Browse files
committed
Various cleanups. Introduced class PreferencesMap to replace/simplify Map<String, String>.
1 parent e63c2d1 commit dc61660

File tree

5 files changed

+324
-393
lines changed

5 files changed

+324
-393
lines changed

app/src/processing/app/Base.java

Lines changed: 23 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.awt.event.*;
2727
import java.io.*;
2828
import java.util.*;
29+
import java.util.List;
2930

3031
import javax.swing.*;
3132

@@ -48,7 +49,7 @@ public class Base {
4849
/** Set true if this a proper release rather than a numbered revision. */
4950
static public boolean RELEASE = false;
5051

51-
static HashMap<Integer, String> platformNames = new HashMap<Integer, String>();
52+
static Map<Integer, String> platformNames = new HashMap<Integer, String>();
5253
static {
5354
platformNames.put(PConstants.WINDOWS, "windows");
5455
platformNames.put(PConstants.MACOSX, "macosx");
@@ -78,19 +79,18 @@ public class Base {
7879
static private File examplesFolder;
7980
static private File librariesFolder;
8081
static private File toolsFolder;
81-
static private File hardwareFolder;
8282

83-
static HashSet<File> libraries;
83+
static Set<File> libraries;
8484

8585
// maps imported packages to their library folder
86-
static HashMap<String, File> importToLibraryTable;
86+
static Map<String, File> importToLibraryTable;
8787

8888
// classpath for all known libraries for p5
8989
// (both those in the p5/libs folder and those with lib subfolders
9090
// found in the sketchbook)
9191
static public String librariesClassPath;
9292

93-
static public HashMap<String, Target> targetsTable;
93+
static public Map<String, Target> targetsTable;
9494

9595
// Location for untitled items
9696
static File untitledFolder;
@@ -99,10 +99,7 @@ public class Base {
9999
// static Image icon;
100100

101101
// int editorCount;
102-
// Editor[] editors;
103-
java.util.List<Editor> editors =
104-
Collections.synchronizedList(new ArrayList<Editor>());
105-
// ArrayList editors = Collections.synchronizedList(new ArrayList<Editor>());
102+
List<Editor> editors = Collections.synchronizedList(new ArrayList<Editor>());
106103
Editor activeEditor;
107104

108105

@@ -956,17 +953,19 @@ public void rebuildImportMenu(JMenu importMenu) {
956953
//Choose which library to add by chip platform
957954

958955
try {
959-
//Find the current target. Get the platform, and then select the correct name and core path.
960-
String platformname = this.getBoardPreferences().get("platform");
961-
String targetname = this.getPlatformPreferences(platformname).get("name");
962-
String libraryPath = this.getPlatformPreferences(platformname).get("library.core.path");
956+
// Find the current target. Get the platform, and then select the
957+
// correct name and core path.
958+
String platformname = getBoardPreferences().get("platform");
959+
String targetname = getPlatformPreferences(platformname)
960+
.get("name");
961+
String libraryPath = getPlatformPreferences(platformname).get(
962+
"library.core.path");
963963

964964
JMenuItem platformItem = new JMenuItem(targetname);
965965
platformItem.setEnabled(false);
966966
importMenu.add(platformItem);
967967
importMenu.addSeparator();
968968
addLibraries(importMenu, getCoreLibraries(libraryPath));
969-
970969
} catch (IOException e) {
971970
e.printStackTrace();
972971
}
@@ -1574,91 +1573,28 @@ static public Target getTarget() {
15741573
}
15751574

15761575

1577-
static public Map<String, String> getPlatformPreferences() {
1578-
System.out.println("getPlatformPreferences() no arguments: start");
1576+
static public PreferencesMap getPlatformPreferences() {
15791577
Target target = getTarget();
1580-
//if (target == null) return new LinkedHashMap();
1581-
Map map = target.getPlatforms();
1582-
/*
1583-
if (map == null)
1584-
{
1585-
System.err.println("Error loading platforms preference from Target");
1586-
System.exit(0);
1587-
}
1588-
*/
1589-
//if (map == null) return new LinkedHashMap();
1590-
map = (Map) map.get(Preferences.get("platform"));
1591-
//if (map == null) return new LinkedHashMap();
1592-
return map;
1578+
Map<String, PreferencesMap> platforms = target.getPlatforms();
1579+
return platforms.get(Preferences.get("platform"));
15931580
}
15941581

15951582
//Get a specific platform
1596-
static public Map<String, String> getPlatformPreferences(String platformname) {
1597-
if (platformname == null) {
1598-
platformname = Preferences.get("platform");
1599-
1600-
}
1601-
System.out.println("getlatformPreferences(String platformname)): start: platformname = " + platformname );
1583+
static public PreferencesMap getPlatformPreferences(String platformName) {
1584+
if (platformName == null)
1585+
platformName = Preferences.get("platform");
16021586
Target target = getTarget();
1603-
if (target == null ) {
1604-
System.out.println("get target is null. trouble! ");
1605-
}
1606-
Map map = target.getPlatforms();
1607-
map = (Map) map.get(platformname);
1608-
1609-
//What if null or defaults to nonexisent platform
1610-
System.out.println("PlatformName: " + platformname);
1611-
if (map == null)
1612-
{
1613-
System.err.println("Error loading platforms preference from Target");
1614-
System.exit(0);
1615-
}
1616-
1617-
return map;
1618-
}
1619-
1620-
static public Map<String, String> bogusgetBoardPreferences() {
1621-
System.out.println("getBoardPrefences method: start");
1622-
Target target = getTarget();
1623-
if (target == null) {
1624-
System.out.println("getBoardPrefereces method: target == null");
1625-
return new LinkedHashMap();
1626-
}
1627-
Map map = target.getBoards();
1628-
if (map == null) {
1629-
System.out.println("getBoardPrefereces method: target.getBoards() == null");
1630-
return new LinkedHashMap();
1631-
}
1632-
map = (Map) map.get(Preferences.get("board"));
1633-
if (map == null) {
1634-
System.out.println("getBoardPrefereces method: Preferences.get(board) == null");
1635-
return new LinkedHashMap();
1636-
}
1637-
//Debug iterate the map
1638-
Iterator iterator = map.entrySet().iterator();
1639-
while(iterator.hasNext())
1640-
{
1641-
Map.Entry pair = (Map.Entry)iterator.next();
1642-
if (pair.getValue() == null)
1643-
{
1644-
System.out.println("KeyName: " + pair.getKey() + " val: null");
1645-
}
1646-
else
1647-
{
1648-
System.out.println("KeyName: " + pair.getKey() + " val" + pair.getValue());
1649-
}
1650-
}
1651-
1652-
return map;
1587+
Map<String, PreferencesMap> platforms = target.getPlatforms();
1588+
return platforms.get(platformName);
16531589
}
16541590

1655-
static public Map<String, String> getBoardPreferences() {
1591+
static public PreferencesMap getBoardPreferences() {
16561592
Target target = getTarget();
16571593
if (target != null) {
16581594
String board = Preferences.get("board");
16591595
return target.getBoards().get(board);
16601596
}
1661-
return new HashMap<String, String>();
1597+
return new PreferencesMap();
16621598
}
16631599

16641600
static public File getSketchbookFolder() {

app/src/processing/app/Preferences.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -782,17 +782,16 @@ static public SyntaxStyle getStyle(String what /*, String dflt*/) {
782782
}
783783

784784
//get a Map of the Preferences
785-
static public Map<String, String> getMap()
785+
static public PreferencesMap getMap()
786786
{
787-
Map globalpreferences = new LinkedHashMap();
788-
Enumeration e = table.keys();
787+
PreferencesMap globalpreferences = new PreferencesMap();
788+
Enumeration<String> e = table.keys();
789789

790790
while (e.hasMoreElements())
791791
{
792792
String key = (String) e.nextElement();
793-
//System.out.println("Key: " + key + "Val: " + table.get(key));
794793
String value = (String) table.get(key);
795-
globalpreferences.put(key, value );
794+
globalpreferences.put(key, value);
796795
}
797796

798797
return globalpreferences;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
PreferencesMap - A Map<String, String> with some useful features
3+
to handle preferences.
4+
Part of the Arduino project - http://www.arduino.cc/
5+
6+
Copyright (c) 2011 Cristian Maglie
7+
8+
This program is free software; you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation; either version 2 of the License, or
11+
(at your option) any later version.
12+
13+
This program is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with this program; if not, write to the Free Software Foundation,
20+
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
22+
$Id$
23+
*/
24+
package processing.app;
25+
26+
import java.io.File;
27+
import java.io.FileInputStream;
28+
import java.io.FileNotFoundException;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.util.HashMap;
32+
import java.util.Map;
33+
34+
import processing.core.PApplet;
35+
36+
public class PreferencesMap extends HashMap<String, String> {
37+
38+
/**
39+
* Parse a property list file and put kev/value pairs into the Map
40+
*
41+
* @param file
42+
* @throws FileNotFoundException
43+
* @throws IOException
44+
*/
45+
public void load(File file) throws FileNotFoundException, IOException {
46+
load(new FileInputStream(file));
47+
}
48+
49+
/**
50+
* Parse a property list stream and put key/value pairs into the Map
51+
*
52+
* @param input
53+
* @throws IOException
54+
*/
55+
public void load(InputStream input) throws IOException {
56+
String[] lines = PApplet.loadStrings(input);
57+
for (String line : lines) {
58+
if (line.length() == 0 || line.charAt(0) == '#')
59+
continue;
60+
61+
int equals = line.indexOf('=');
62+
if (equals != -1) {
63+
String key = line.substring(0, equals);
64+
String value = line.substring(equals + 1);
65+
put(key.trim(), value.trim());
66+
}
67+
}
68+
}
69+
70+
public Map<String, PreferencesMap> createFirstLevelMap() {
71+
Map<String, PreferencesMap> res = new HashMap<String, PreferencesMap>();
72+
for (String key : keySet()) {
73+
int dot = key.indexOf('.');
74+
if (dot == -1)
75+
continue;
76+
77+
String parent = key.substring(0, dot);
78+
String child = key.substring(dot + 1);
79+
80+
if (!res.containsKey(parent))
81+
res.put(parent, new PreferencesMap());
82+
res.get(parent).put(child, get(key));
83+
}
84+
return res;
85+
}
86+
87+
private static final long serialVersionUID = 2330591567444282843L;
88+
}

0 commit comments

Comments
 (0)