diff --git a/app/src/cc/arduino/view/JMenuUtils.java b/app/src/cc/arduino/view/JMenuUtils.java new file mode 100644 index 000000000..de64e41a1 --- /dev/null +++ b/app/src/cc/arduino/view/JMenuUtils.java @@ -0,0 +1,46 @@ +/* + * This file is part of Arduino. + * + * Arduino is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As a special exception, you may use this file as part of a free software + * library without restriction. Specifically, if other files instantiate + * templates or use macros or inline functions from this file, or you compile + * this file and link it with other files to produce an executable, this + * file does not by itself cause the resulting executable to be covered by + * the GNU General Public License. This exception does not however + * invalidate any other reasons why the executable file might be covered by + * the GNU General Public License. + * + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) + */ + +package cc.arduino.view; + +import javax.swing.*; +import java.awt.*; + +public class JMenuUtils { + + public static JMenu findSubMenuWithLabel(JMenu menu, String text) { + for (Component submenu : menu.getMenuComponents()) { + if (submenu instanceof JMenu && text.equals(((JMenu) submenu).getText())) { + return (JMenu) submenu; + } + } + return null; + } + +} diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 3d74140b7..5e55277fb 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1122,32 +1122,26 @@ public class Base { menu.addSeparator(); // Add a list of all sketches and subfolders - try { - boolean sketches = addSketches(menu, BaseNoGui.getSketchbookFolder(), true); - if (sketches) menu.addSeparator(); - } catch (IOException e) { - e.printStackTrace(); - } + boolean sketches = addSketches(menu, BaseNoGui.getSketchbookFolder(), true); + if (sketches) menu.addSeparator(); // Add each of the subfolders of examples directly to the menu - try { - boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), true); - if (found) menu.addSeparator(); - } catch (IOException e) { - e.printStackTrace(); - } + boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), true); + if (found) menu.addSeparator(); } protected void rebuildSketchbookMenu(JMenu menu) { - //System.out.println("rebuilding sketchbook menu"); - //new Exception().printStackTrace(); - try { - menu.removeAll(); - addSketches(menu, BaseNoGui.getSketchbookFolder(), false); - //addSketches(menu, getSketchbookFolder()); - } catch (IOException e) { - e.printStackTrace(); + menu.removeAll(); + addSketches(menu, BaseNoGui.getSketchbookFolder(), false); + + JMenu librariesMenu = JMenuUtils.findSubMenuWithLabel(menu, "libraries"); + if (librariesMenu != null) { + menu.remove(librariesMenu); + } + JMenu hardwareMenu = JMenuUtils.findSubMenuWithLabel(menu, "hardware"); + if (hardwareMenu != null) { + menu.remove(hardwareMenu); } } @@ -1234,30 +1228,28 @@ public class Base { } public void rebuildExamplesMenu(JMenu menu) { - if (menu == null) + if (menu == null) { return; - try { - menu.removeAll(); + } - // Add examples from distribution "example" folder - boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), false); - if (found) menu.addSeparator(); + menu.removeAll(); - // Add examples from libraries - LibraryList ideLibs = getIDELibs(); - ideLibs.sort(); - for (UserLibrary lib : ideLibs) + // Add examples from distribution "example" folder + boolean found = addSketches(menu, BaseNoGui.getExamplesFolder(), false); + if (found) menu.addSeparator(); + + // Add examples from libraries + LibraryList ideLibs = getIDELibs(); + ideLibs.sort(); + for (UserLibrary lib : ideLibs) + addSketchesSubmenu(menu, lib, false); + + LibraryList userLibs = getUserLibs(); + if (userLibs.size() > 0) { + menu.addSeparator(); + userLibs.sort(); + for (UserLibrary lib : userLibs) addSketchesSubmenu(menu, lib, false); - - LibraryList userLibs = getUserLibs(); - if (userLibs.size() > 0) { - menu.addSeparator(); - userLibs.sort(); - for (UserLibrary lib : userLibs) - addSketchesSubmenu(menu, lib, false); - } - } catch (IOException e) { - e.printStackTrace(); } } @@ -1588,7 +1580,7 @@ public class Base { * should replace the sketch in the current window, or false when the * sketch should open in a new window. */ - protected boolean addSketches(JMenu menu, File folder, final boolean replaceExisting) throws IOException { + protected boolean addSketches(JMenu menu, File folder, final boolean replaceExisting) { if (folder == null) return false; @@ -1624,14 +1616,13 @@ public class Base { } private boolean addSketchesSubmenu(JMenu menu, UserLibrary lib, - boolean replaceExisting) - throws IOException { + boolean replaceExisting) { return addSketchesSubmenu(menu, lib.getName(), lib.getInstalledFolder(), replaceExisting); } private boolean addSketchesSubmenu(JMenu menu, String name, File folder, - final boolean replaceExisting) throws IOException { + final boolean replaceExisting) { ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) {