Skip to content

Commit 65bed4f

Browse files
committed
Merge pull request processing#2804 from joelmoniz/addReferences
Added References for installed tools and libraries, whenever available, to the Help menu.
2 parents 10c340d + cc68c83 commit 65bed4f

5 files changed

Lines changed: 240 additions & 0 deletions

File tree

app/src/processing/app/Editor.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,11 @@ public void windowGainedFocus(WindowEvent e) {
286286
}
287287

288288

289+
protected ArrayList<ToolContribution> getCoreTools() {
290+
return coreTools;
291+
}
292+
293+
289294
/**
290295
* Broken out to get modes working for GSOC, but this needs a longer-term
291296
* solution where the listeners are handled properly.
@@ -1258,11 +1263,22 @@ public void actionPerformed(ActionEvent e) {
12581263

12591264
public void showReference(String filename) {
12601265
File file = new File(mode.getReferenceFolder(), filename);
1266+
showReferenceFile(file);
1267+
}
1268+
1269+
1270+
/**
1271+
* Given the .html file, displays it in the default browser.
1272+
*
1273+
* @param file
1274+
*/
1275+
public void showReferenceFile(File file) {
12611276
try {
12621277
file = file.getCanonicalFile();
12631278
} catch (IOException e) {
12641279
e.printStackTrace();
12651280
}
1281+
12661282
// Prepend with file:// and also encode spaces & other characters
12671283
Base.openURL(file.toURI().toString());
12681284
}

app/src/processing/app/Library.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,4 +514,26 @@ static protected void list(File folder, ArrayList<Library> libraries) {
514514
public ContributionType getType() {
515515
return ContributionType.LIBRARY;
516516
}
517+
518+
519+
/**
520+
* Returns the object stored in the referenceFile field, which contains an
521+
* instance of the file object representing the index file of the reference
522+
*
523+
* @return referenceFile
524+
*/
525+
public File getReferenceIndexFile() {
526+
return referenceFile;
527+
}
528+
529+
530+
/**
531+
* Tests whether the reference's index file indicated by referenceFile exists.
532+
*
533+
* @return true if and only if the file denoted by referenceFile exists; false
534+
* otherwise.
535+
*/
536+
public boolean hasReference() {
537+
return referenceFile.exists();
538+
}
517539
}

app/src/processing/app/contrib/ToolContribution.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
public class ToolContribution extends LocalContribution implements Tool {
3636
private Tool tool;
3737

38+
private File referenceFile; // shortname/reference/index.html
3839

3940
static public ToolContribution load(File folder) {
4041
try {
@@ -59,6 +60,8 @@ private ToolContribution(File folder) throws Exception {
5960
Class<?> toolClass = loader.loadClass(className);
6061
tool = (Tool) toolClass.newInstance();
6162
}
63+
64+
referenceFile = new File(folder, "reference/index.html");
6265
}
6366

6467

@@ -153,4 +156,26 @@ public String getMenuTitle() {
153156
public ContributionType getType() {
154157
return ContributionType.TOOL;
155158
}
159+
160+
161+
/**
162+
* Returns the object stored in the referenceFile field, which contains an
163+
* instance of the file object representing the index file of the reference
164+
*
165+
* @return referenceFile
166+
*/
167+
public File getReferenceIndexFile() {
168+
return referenceFile;
169+
}
170+
171+
172+
/**
173+
* Tests whether the reference's index file indicated by referenceFile exists.
174+
*
175+
* @return true if and only if the file denoted by referenceFile exists; false
176+
* otherwise.
177+
*/
178+
public boolean hasReference() {
179+
return referenceFile.exists();
180+
}
156181
}

app/src/processing/mode/java/JavaEditor.java

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@
44
import java.awt.event.*;
55
import java.beans.*;
66
import java.io.*;
7+
import java.util.ArrayList;
8+
import java.util.Iterator;
79

810
import javax.swing.*;
911
import javax.swing.border.*;
12+
import javax.swing.event.MenuEvent;
13+
import javax.swing.event.MenuListener;
1014

1115
import processing.app.*;
1216
import processing.app.Toolkit;
17+
import processing.app.contrib.ToolContribution;
1318
import processing.mode.java.runner.Runner;
1419

1520

@@ -134,6 +139,100 @@ public void actionPerformed(ActionEvent e) {
134139
}
135140
});
136141
menu.add(item);
142+
143+
menu.addSeparator();
144+
145+
final JMenu libRefSubmenu = new JMenu(Language.text("menu.help.libraries_reference"));
146+
// Populate only when sub-menu is opened, to avoid having spurious menu
147+
// options if a library is deleted, or a missing menu option if a library is added
148+
libRefSubmenu.addMenuListener(new MenuListener() {
149+
150+
@Override
151+
public void menuSelected(MenuEvent e) {
152+
boolean isCoreLibMenuItemAdded = false;
153+
boolean isContribLibMenuItemAdded = false;
154+
155+
// Adding this in case references are included in a core library,
156+
// or other core libraries are incuded in future
157+
isCoreLibMenuItemAdded = addLibReferencesToSubMenu(mode.coreLibraries, libRefSubmenu);
158+
159+
if (isCoreLibMenuItemAdded && !mode.contribLibraries.isEmpty())
160+
libRefSubmenu.addSeparator();
161+
162+
isContribLibMenuItemAdded = addLibReferencesToSubMenu(mode.contribLibraries, libRefSubmenu);
163+
164+
if (!isContribLibMenuItemAdded && !isCoreLibMenuItemAdded) {
165+
JMenuItem emptyMenuItem = new JMenuItem(Language.text("menu.help.empty"));
166+
emptyMenuItem.setEnabled(false);
167+
emptyMenuItem.setFocusable(false);
168+
emptyMenuItem.setFocusPainted(false);
169+
libRefSubmenu.add(emptyMenuItem);
170+
}
171+
else if (!isContribLibMenuItemAdded && !mode.coreLibraries.isEmpty()) {
172+
//re-populate the menu to get rid of terminal separator
173+
libRefSubmenu.removeAll();
174+
addLibReferencesToSubMenu(mode.coreLibraries, libRefSubmenu);
175+
}
176+
}
177+
178+
@Override
179+
public void menuDeselected(MenuEvent e) {
180+
libRefSubmenu.removeAll();
181+
}
182+
183+
@Override
184+
public void menuCanceled(MenuEvent e) {
185+
menuDeselected(e);
186+
}
187+
});
188+
menu.add(libRefSubmenu);
189+
190+
final JMenu toolRefSubmenu = new JMenu(Language.text("menu.help.tools_reference"));
191+
// Populate only when sub-menu is opened, to avoid having spurious menu
192+
// options if a tool is deleted, or a missing menu option if a library is added
193+
toolRefSubmenu.addMenuListener(new MenuListener() {
194+
195+
@Override
196+
public void menuSelected(MenuEvent e) {
197+
boolean isCoreToolMenuItemAdded = false;
198+
boolean isContribToolMenuItemAdded = false;
199+
200+
// Adding this in in case a reference folder is added for MovieMaker, or in case
201+
// other core tools are introduced later
202+
isCoreToolMenuItemAdded = addToolReferencesToSubMenu(getCoreTools(), toolRefSubmenu);
203+
204+
if (isCoreToolMenuItemAdded && !contribTools.isEmpty())
205+
toolRefSubmenu.addSeparator();
206+
207+
isContribToolMenuItemAdded = addToolReferencesToSubMenu(contribTools, toolRefSubmenu);
208+
209+
if (!isContribToolMenuItemAdded && !isCoreToolMenuItemAdded) {
210+
toolRefSubmenu.removeAll(); // in case a separator was added
211+
final JMenuItem emptyMenuItem = new JMenuItem(Language.text("menu.help.empty"));
212+
emptyMenuItem.setEnabled(false);
213+
emptyMenuItem.setBorderPainted(false);
214+
emptyMenuItem.setFocusable(false);
215+
emptyMenuItem.setFocusPainted(false);
216+
toolRefSubmenu.add(emptyMenuItem);
217+
}
218+
else if (!isContribToolMenuItemAdded && !contribTools.isEmpty()) {
219+
// re-populate the menu to get rid of terminal separator
220+
toolRefSubmenu.removeAll();
221+
addToolReferencesToSubMenu(getCoreTools(), toolRefSubmenu);
222+
}
223+
}
224+
225+
@Override
226+
public void menuDeselected(MenuEvent e) {
227+
toolRefSubmenu.removeAll();
228+
}
229+
230+
@Override
231+
public void menuCanceled(MenuEvent e) {
232+
menuDeselected(e);
233+
}
234+
});
235+
menu.add(toolRefSubmenu);
137236

138237
menu.addSeparator();
139238
item = new JMenuItem(Language.text("menu.help.online"));
@@ -184,6 +283,81 @@ public void actionPerformed(ActionEvent e) {
184283
}
185284

186285

286+
//. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
287+
288+
289+
/**
290+
* Populates the JMenu with JMenuItems, one for each Library that has a
291+
* reference accompanying it. The JMenuItems open the index.htm/index.html
292+
* file of the reference in the user's default browser, or the readme.txt in
293+
* the user's default text editor.
294+
*
295+
* @param libsList
296+
* A list of the Libraries to be added
297+
* @param subMenu
298+
* The JMenu to which the JMenuItems corresponding to the Libraries
299+
* are to be added
300+
* @return true if and only if any JMenuItems were added; false otherwise
301+
*/
302+
private boolean addLibReferencesToSubMenu(ArrayList<Library> libsList, JMenu subMenu) {
303+
boolean isItemAdded = false;
304+
Iterator<Library> iter = libsList.iterator();
305+
while (iter.hasNext()) {
306+
final Library libContrib = iter.next();
307+
if (libContrib.hasReference()) {
308+
JMenuItem libRefItem = new JMenuItem(libContrib.getName());
309+
libRefItem.addActionListener(new ActionListener() {
310+
311+
@Override
312+
public void actionPerformed(ActionEvent arg0) {
313+
showReferenceFile(libContrib.getReferenceIndexFile());
314+
}
315+
});
316+
subMenu.add(libRefItem);
317+
isItemAdded = true;
318+
}
319+
}
320+
return isItemAdded;
321+
}
322+
323+
324+
/**
325+
*
326+
* Populates the JMenu with JMenuItems, one for each Tool that has a reference
327+
* accompanying it. The JMenuItems open the index.htm/index.html file of the
328+
* reference in the user's default browser, or the readme.txt in the user's
329+
* default text editor.
330+
*
331+
* @param toolsList
332+
* A list of Tools to be added
333+
* @param subMenu
334+
* The JMenu to which the JMenuItems corresponding to the Tools are
335+
* to be added
336+
* @return true if and only if any JMenuItems were added; false otherwise
337+
*/
338+
private boolean addToolReferencesToSubMenu(ArrayList<ToolContribution> toolsList, JMenu subMenu) {
339+
boolean isItemAdded = false;
340+
Iterator<ToolContribution> iter = toolsList.iterator();
341+
while (iter.hasNext()) {
342+
final ToolContribution toolContrib = iter.next();
343+
final File toolRef = new File(toolContrib.getFolder(), "reference/index.html");
344+
if (toolRef.exists()) {
345+
JMenuItem libRefItem = new JMenuItem(toolContrib.getName());
346+
libRefItem.addActionListener(new ActionListener() {
347+
348+
@Override
349+
public void actionPerformed(ActionEvent arg0) {
350+
showReferenceFile(toolRef);
351+
}
352+
});
353+
subMenu.add(libRefItem);
354+
isItemAdded = true;
355+
}
356+
}
357+
return isItemAdded;
358+
}
359+
360+
187361
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
188362

189363

build/shared/lib/languages/PDE.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ menu.help.about = About Processing
105105
menu.help.environment = Environment
106106
menu.help.reference = Reference
107107
menu.help.find_in_reference = Find in Reference
108+
menu.help.libraries_reference = Libraries Reference
109+
menu.help.tools_reference = Tools Reference
110+
menu.help.empty = (empty)
108111
menu.help.online = Online
109112
menu.help.getting_started = Getting Started
110113
menu.help.getting_started.url = http://processing.org/learning/gettingstarted/

0 commit comments

Comments
 (0)