Skip to content

Commit d47a458

Browse files
cmagliefacchinm
authored andcommitted
Cache editor tools (don't create new instance for each menu item)
1 parent 373600c commit d47a458

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

Diff for: app/src/processing/app/Editor.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public boolean test(SketchController sketch) {
198198
private Runnable exportAppHandler;
199199
private Runnable timeoutUploadHandler;
200200

201+
private Map<String, Tool> internalToolCache = new HashMap<String, Tool>();
202+
201203
public Editor(Base ibase, File file, int[] storedLocation, int[] defaultLocation, Platform platform) throws Exception {
202204
super("Arduino");
203205
this.base = ibase;
@@ -963,8 +965,7 @@ public void updateKeywords(PdeKeywords keywords) {
963965

964966
JMenuItem createToolMenuItem(String className) {
965967
try {
966-
Class<?> toolClass = Class.forName(className);
967-
final Tool tool = (Tool) toolClass.newInstance();
968+
final Tool tool = getOrCreateToolInstance(className);
968969

969970
JMenuItem item = new JMenuItem(tool.getMenuTitle());
970971

@@ -983,6 +984,20 @@ public void actionPerformed(ActionEvent e) {
983984
}
984985
}
985986

987+
private Tool getOrCreateToolInstance(String className) {
988+
Tool internalTool = internalToolCache.get(className);
989+
if (internalTool == null) {
990+
try {
991+
Class<?> toolClass = Class.forName(className);
992+
internalTool = (Tool) toolClass.newInstance();
993+
} catch (Exception e) {
994+
e.printStackTrace();
995+
return null;
996+
}
997+
internalToolCache.put(className, internalTool);
998+
}
999+
return internalTool;
1000+
}
9861001

9871002
private void addInternalTools(JMenu menu) {
9881003
JMenuItem item;

0 commit comments

Comments
 (0)