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) 2013 The Processing Foundation
7- Copyright (c) 2011-12 Ben Fry and Casey Reas
8-
9- This program is free software; you can redistribute it and/or modify
10- it under the terms of the GNU General Public License version 2
11- as published by the Free Software Foundation.
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 along
19- with this program; if not, write to the Free Software Foundation, Inc.
20- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21- */
22- package processing .app .contrib ;
23-
24- import java .io .*;
25- import java .net .URLClassLoader ;
26- //import java.net.*;
27- import java .util .*;
28-
29- import processing .app .Base ;
30- //import processing.app.Base;
31- import processing .app .Editor ;
32- import processing .app .tools .Tool ;
33-
34-
35- public class ToolContribution extends LocalContribution implements Tool {
36- private Tool tool ;
37-
38-
39- static public ToolContribution load (File folder ) {
40- try {
41- return new ToolContribution (folder );
42- } catch (IgnorableException ig ) {
43- Base .log (ig .getMessage ());
44- } catch (Error err ) {
45- // Handles UnsupportedClassVersionError and others
46- err .printStackTrace ();
47- } catch (Exception ex ) {
48- ex .printStackTrace ();
49- }
50- return null ;
51- }
52-
53-
54- private ToolContribution (File folder ) throws Exception {
55- super (folder );
56-
57- String className = initLoader (null );
58- if (className != null ) {
59- Class <?> toolClass = loader .loadClass (className );
60- tool = (Tool ) toolClass .newInstance ();
61- }
62- }
63-
64-
65- /**
66- * Method to close the ClassLoader so that the archives are no longer "locked" and
67- * a tool can be removed without restart.
68- */
69- public void clearClassLoader (Base base ) {
70- try {
71- ((URLClassLoader ) this .loader ).close ();
72- } catch (IOException e1 ) {
73- e1 .printStackTrace ();
74- }
75- Iterator <Editor > editorIter = base .getEditors ().iterator ();
76- while (editorIter .hasNext ()) {
77- Editor editor = editorIter .next ();
78- ArrayList <ToolContribution > contribTools = editor .contribTools ;
79- for (ToolContribution toolContrib : contribTools )
80- if (toolContrib .getName ().equals (this .name )) {
81- try {
82- ((URLClassLoader ) toolContrib .loader ).close ();
83- editor .contribTools .remove (toolContrib );
84- break ;
85- } catch (IOException e ) {
86- e .printStackTrace ();
87- }
88- // base.getActiveEditor().rebuildToolMenu();
89- }
90- }
91- }
92-
93-
94- // static protected List<File> discover(File folder) {
95- // File[] folders = listCandidates(folder, "tool");
96- // if (folders == null) {
97- // return new ArrayList<File>();
98- // } else {
99- // return Arrays.asList(folders);
100- // }
101- // }
102-
103-
104- static public ArrayList <ToolContribution > loadAll (File toolsFolder ) {
105- File [] list = ContributionType .TOOL .listCandidates (toolsFolder );
106- ArrayList <ToolContribution > outgoing = new ArrayList <ToolContribution >();
107- // If toolsFolder does not exist or is inaccessible (stranger things have
108- // happened, and are reported as bugs) list will come back null.
109- if (list != null ) {
110- for (File folder : list ) {
111- try {
112- ToolContribution tc = load (folder );
113- if (tc != null ) {
114- outgoing .add (tc );
115- }
116- } catch (Exception e ) {
117- e .printStackTrace ();
118- }
119- }
120- }
121- return outgoing ;
122- }
123-
124-
125- // Editor editor; // used to send error messages
126-
127- public void init (Editor editor ) {
128- // try {
129- // this.editor = editor;
130- tool .init (editor );
131- // } catch (NoSuchMethodError nsme) {
132- // editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing");
133- // nsme.printStackTrace();
134- // }
135- }
136-
137-
138- public void run () {
139- // try {
140- tool .run ();
141- // } catch (NoSuchMethodError nsme) {
142- // editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing");
143- // nsme.printStackTrace();
144- // }
145- }
146-
147-
148- public String getMenuTitle () {
149- return tool .getMenuTitle ();
150- }
151-
152-
153- public ContributionType getType () {
154- return ContributionType .TOOL ;
155- }
156- }
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) 2013 The Processing Foundation
7+ Copyright (c) 2011-12 Ben Fry and Casey Reas
8+
9+ This program is free software; you can redistribute it and/or modify
10+ it under the terms of the GNU General Public License version 2
11+ as published by the Free Software Foundation.
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 along
19+ with this program; if not, write to the Free Software Foundation, Inc.
20+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+ */
22+ package processing .app .contrib ;
23+
24+ import java .io .*;
25+ import java .net .URLClassLoader ;
26+ //import java.net.*;
27+ import java .util .*;
28+
29+ import processing .app .Base ;
30+ //import processing.app.Base;
31+ import processing .app .Editor ;
32+ import processing .app .tools .Tool ;
33+
34+
35+ public class ToolContribution extends LocalContribution implements Tool {
36+ private Tool tool ;
37+
38+
39+ static public ToolContribution load (File folder ) {
40+ try {
41+ return new ToolContribution (folder );
42+
43+ } catch (IgnorableException ig ) {
44+ Base .log (ig .getMessage ());
45+
46+ } catch (VerifyError ve ) { // incompatible
47+ // avoid the excessive error spew that happens here
48+
49+ } catch (Throwable e ) { // unknown error
50+ e .printStackTrace ();
51+ }
52+ return null ;
53+ }
54+
55+
56+ private ToolContribution (File folder ) throws Throwable {
57+ super (folder );
58+
59+ String className = initLoader (null );
60+ if (className != null ) {
61+ Class <?> toolClass = loader .loadClass (className );
62+ tool = (Tool ) toolClass .newInstance ();
63+ }
64+ }
65+
66+
67+ /**
68+ * Method to close the ClassLoader so that the archives are no longer "locked" and
69+ * a tool can be removed without restart.
70+ */
71+ public void clearClassLoader (Base base ) {
72+ try {
73+ ((URLClassLoader ) this .loader ).close ();
74+ } catch (IOException e1 ) {
75+ e1 .printStackTrace ();
76+ }
77+ Iterator <Editor > editorIter = base .getEditors ().iterator ();
78+ while (editorIter .hasNext ()) {
79+ Editor editor = editorIter .next ();
80+ ArrayList <ToolContribution > contribTools = editor .contribTools ;
81+ for (ToolContribution toolContrib : contribTools )
82+ if (toolContrib .getName ().equals (this .name )) {
83+ try {
84+ ((URLClassLoader ) toolContrib .loader ).close ();
85+ editor .contribTools .remove (toolContrib );
86+ break ;
87+ } catch (IOException e ) {
88+ e .printStackTrace ();
89+ }
90+ // base.getActiveEditor().rebuildToolMenu();
91+ }
92+ }
93+ }
94+
95+
96+ // static protected List<File> discover(File folder) {
97+ // File[] folders = listCandidates(folder, "tool");
98+ // if (folders == null) {
99+ // return new ArrayList<File>();
100+ // } else {
101+ // return Arrays.asList(folders);
102+ // }
103+ // }
104+
105+
106+ static public ArrayList <ToolContribution > loadAll (File toolsFolder ) {
107+ File [] list = ContributionType .TOOL .listCandidates (toolsFolder );
108+ ArrayList <ToolContribution > outgoing = new ArrayList <ToolContribution >();
109+ // If toolsFolder does not exist or is inaccessible (stranger things have
110+ // happened, and are reported as bugs) list will come back null.
111+ if (list != null ) {
112+ for (File folder : list ) {
113+ try {
114+ ToolContribution tc = load (folder );
115+ if (tc != null ) {
116+ outgoing .add (tc );
117+ }
118+ } catch (Exception e ) {
119+ e .printStackTrace ();
120+ }
121+ }
122+ }
123+ return outgoing ;
124+ }
125+
126+
127+ // Editor editor; // used to send error messages
128+
129+ public void init (Editor editor ) {
130+ // try {
131+ // this.editor = editor;
132+ tool .init (editor );
133+ // } catch (NoSuchMethodError nsme) {
134+ // editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing");
135+ // nsme.printStackTrace();
136+ // }
137+ }
138+
139+
140+ public void run () {
141+ // try {
142+ tool .run ();
143+ // } catch (NoSuchMethodError nsme) {
144+ // editor.statusError(tool.getMenuTitle() + " is not compatible with this version of Processing");
145+ // nsme.printStackTrace();
146+ // }
147+ }
148+
149+
150+ public String getMenuTitle () {
151+ return tool .getMenuTitle ();
152+ }
153+
154+
155+ public ContributionType getType () {
156+ return ContributionType .TOOL ;
157+ }
158+ }
0 commit comments