Skip to content

Commit cf770e4

Browse files
cmagliefacchinm
authored andcommitted
Cache editor tools (don't create new instance for each menu item)
1 parent 725a190 commit cf770e4

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;
@@ -964,8 +966,7 @@ public void updateKeywords(PdeKeywords keywords) {
964966

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

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

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

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

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

0 commit comments

Comments
 (0)