Skip to content

Commit 79efbb9

Browse files
committed
Added AndroidTool class to handle UI tools using SDK libs
Right now, used only for the SDKUpdater, but could be used to wrap other tools in the future
1 parent 2f8fba3 commit 79efbb9

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/processing/mode/android/AndroidEditor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,20 @@ public void actionPerformed(ActionEvent e) {
351351
item.addActionListener(new ActionListener() {
352352
public void actionPerformed(ActionEvent e) {
353353

354+
File toolPath = new File(androidMode.getFolder(), "tools/SDKUpdater");
355+
AndroidTool tool = null;
356+
try {
357+
tool = new AndroidTool(toolPath, androidMode.getSDK());
358+
} catch (Throwable e1) {
359+
// TODO Auto-generated catch block
360+
e1.printStackTrace();
361+
}
362+
if (tool != null) {
363+
tool.init(base);
364+
tool.run();
365+
}
366+
367+
354368
/*
355369
AndroidSDK sdk = androidMode.getSDK();
356370
File lib = new File(sdk.getToolsFolder(), "lib");
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2+
3+
/*
4+
Part of the Processing project - http://processing.org
5+
6+
Copyright (c) 2017 The Processing Foundation
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 version 2
10+
as published by the Free Software Foundation.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program; if not, write to the Free Software Foundation,
19+
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20+
*/
21+
22+
package processing.mode.android;
23+
24+
import java.io.File;
25+
import java.net.URL;
26+
import java.net.URLClassLoader;
27+
28+
import processing.app.Base;
29+
import processing.app.Messages;
30+
import processing.app.Util;
31+
import processing.app.contrib.ContributionType;
32+
import processing.app.contrib.IgnorableException;
33+
import processing.app.contrib.LocalContribution;
34+
import processing.app.contrib.ToolContribution;
35+
import processing.app.tools.Tool;
36+
37+
public class AndroidTool extends LocalContribution implements Tool, Comparable<ToolContribution> {
38+
private AndroidSDK sdk;
39+
private Tool tool;
40+
41+
AndroidTool(File toolFolder, AndroidSDK sdk) throws Throwable {
42+
super(toolFolder);
43+
this.sdk = sdk;
44+
45+
String className = initLoader(null);
46+
if (className != null) {
47+
Class<?> toolClass = loader.loadClass(className);
48+
tool = (Tool) toolClass.newInstance();
49+
}
50+
}
51+
52+
public void init(Base base) {
53+
tool.init(base);
54+
}
55+
56+
57+
public void run() {
58+
tool.run();
59+
}
60+
61+
62+
public String getMenuTitle() {
63+
return tool.getMenuTitle();
64+
}
65+
66+
public String initLoader(String className) throws Exception {
67+
File toolDir = new File(folder, "tool");
68+
if (toolDir.exists()) {
69+
Messages.log("checking mode folder regarding " + className);
70+
// If no class name specified, search the main <modename>.jar for the
71+
// full name package and mode name.
72+
if (className == null) {
73+
String shortName = folder.getName();
74+
File mainJar = new File(toolDir, shortName + ".jar");
75+
if (mainJar.exists()) {
76+
className = findClassInZipFile(shortName, mainJar);
77+
} else {
78+
throw new IgnorableException(mainJar.getAbsolutePath() + " does not exist.");
79+
}
80+
81+
if (className == null) {
82+
throw new IgnorableException("Could not find " + shortName +
83+
" class inside " + mainJar.getAbsolutePath());
84+
}
85+
}
86+
87+
// Add .jar and .zip files from the "tool" and the SDK/tools/lib
88+
// folder into the classpath
89+
File libDir = new File(sdk.getToolsFolder(), "lib");
90+
File[] toolArchives = Util.listJarFiles(toolDir);
91+
File[] libArchives = Util.listJarFiles(libDir);
92+
93+
if (toolArchives != null && toolArchives.length > 0 &&
94+
libArchives != null && libArchives.length > 0) {
95+
URL[] urlList = new URL[toolArchives.length + libArchives.length];
96+
97+
int j;
98+
for (j = 0; j < toolArchives.length; j++) {
99+
Messages.log("Found archive " + toolArchives[j] + " for " + getName());
100+
urlList[j] = toolArchives[j].toURI().toURL();
101+
}
102+
for (int k = 0; k < libArchives.length; k++, j++) {
103+
Messages.log("Found archive " + libArchives[k] + " for " + getName());
104+
urlList[j] = libArchives[k].toURI().toURL();
105+
}
106+
107+
loader = new URLClassLoader(urlList);
108+
Messages.log("loading above JARs with loader " + loader);
109+
}
110+
}
111+
112+
// If no archives were found, just use the regular ClassLoader
113+
if (loader == null) {
114+
loader = Thread.currentThread().getContextClassLoader();
115+
}
116+
return className;
117+
}
118+
119+
120+
@Override
121+
public int compareTo(ToolContribution o) {
122+
return getMenuTitle().compareTo(o.getMenuTitle());
123+
}
124+
125+
126+
@Override
127+
public ContributionType getType() {
128+
return ContributionType.TOOL;
129+
}
130+
}

0 commit comments

Comments
 (0)