Skip to content

Commit 18946f7

Browse files
committed
Refactor examples manager window: Fixes #3133
1 parent 8a18c69 commit 18946f7

File tree

2 files changed

+33
-110
lines changed

2 files changed

+33
-110
lines changed

app/src/processing/app/Base.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,14 +1275,14 @@ protected boolean addSketches(DefaultMutableTreeNode node, File folder) throws I
12751275
return false; // let's not go there
12761276
}
12771277

1278-
String[] list = folder.list();
1278+
String[] fileList = folder.list();
12791279
// If a bad folder or unreadable or whatever, this will come back null
1280-
if (list == null) {
1280+
if (fileList == null) {
12811281
return false;
12821282
}
12831283

12841284
// Alphabetize the list, since it's not always alpha order
1285-
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
1285+
Arrays.sort(fileList, String.CASE_INSENSITIVE_ORDER);
12861286

12871287
// ActionListener listener = new ActionListener() {
12881288
// public void actionPerformed(ActionEvent e) {
@@ -1301,8 +1301,8 @@ protected boolean addSketches(DefaultMutableTreeNode node, File folder) throws I
13011301
//menu.addActionListener(listener);
13021302

13031303
boolean found = false;
1304-
1305-
for (String name : list) {
1304+
for (String name : fileList) {
1305+
//Skip hidden files
13061306
if (name.charAt(0) == '.') {
13071307
continue;
13081308
}
@@ -1317,18 +1317,14 @@ protected boolean addSketches(DefaultMutableTreeNode node, File folder) throws I
13171317
if (subfolder.isDirectory()) {
13181318
File entry = checkSketchFolder(subfolder, name);
13191319
if (entry != null) {
1320-
// DefaultMutableTreeNode item = new DefaultMutableTreeNode(name);
13211320
DefaultMutableTreeNode item =
13221321
new DefaultMutableTreeNode(new SketchReference(name, entry));
1323-
// item.addActionListener(listener);
1324-
// item.setActionCommand(entry.getAbsolutePath());
1325-
// menu.add(item);
1322+
13261323
node.add(item);
13271324
found = true;
13281325

13291326
} else {
13301327
// not a sketch folder, but maybe a subfolder containing sketches
1331-
// JMenu submenu = new JMenu(name);
13321328
DefaultMutableTreeNode subnode = new DefaultMutableTreeNode(name);
13331329
// needs to be separate var otherwise would set ifound to false
13341330
boolean anything = addSketches(subnode, subfolder);

app/src/processing/app/Mode.java

Lines changed: 27 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -650,118 +650,51 @@ public boolean accept(File dir, String name) {
650650

651651

652652
public DefaultMutableTreeNode buildExamplesTree() {
653-
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Examples");
653+
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Examples");
654654

655-
// JTree examplesTree = new JTree(node);
656-
// rebuildExamplesTree(node);
657-
// }
658-
659-
//DefaultTreeCellRenderer renderer = tree.
660-
// TreeCellRenderer tcr = examplesTree.getCellRenderer();
661-
662-
//
663-
//
664-
// public void rebuildExamplesTree(DefaultMutableTreeNode node) {
665655
try {
666-
// break down the examples folder for examples
667-
// File[] subfolders = examplesFolder.listFiles(new FilenameFilter() {
668-
// public boolean accept(File dir, String name) {
669-
// return dir.isDirectory() && name.charAt(0) != '.';
670-
// }
671-
// });
672-
File[] subfolders = getExampleCategoryFolders();
673656

674-
DefaultMutableTreeNode modeExParent = new DefaultMutableTreeNode("Mode Examples");
675-
676-
for (File sub : subfolders) {
677-
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(sub.getName());
678-
if (base.addSketches(subNode, sub)) {
679-
// examplesParent.add(subNode);
680-
modeExParent.add(subNode);
657+
File[] examples = getExampleCategoryFolders();
658+
659+
for (File subFolder : examples) {
660+
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(subFolder.getName());
661+
if (base.addSketches(subNode, subFolder)) {
662+
root.add(subNode);
681663
}
682664
}
683-
684-
// get library examples
685-
boolean any = false;
665+
666+
DefaultMutableTreeNode foundationLibraries = new DefaultMutableTreeNode("Core Libraries");
667+
668+
// Get examples for core libraries
686669
for (Library lib : coreLibraries) {
687670
if (lib.hasExamples()) {
688671
DefaultMutableTreeNode libNode = new DefaultMutableTreeNode(lib.getName());
689672
if (base.addSketches(libNode, lib.getExamplesFolder()))
690-
modeExParent.add(libNode);
673+
foundationLibraries.add(libNode);
691674
}
692675
}
676+
if(foundationLibraries.getChildCount() > 0) {
677+
root.add(foundationLibraries);
678+
}
693679

694-
if (modeExParent.getChildCount() > 0)
695-
node.add(modeExParent);
696-
697-
// get contrib library examples
698-
any = false;
680+
// Get examples for third party libraries
681+
DefaultMutableTreeNode contributed = new DefaultMutableTreeNode("Libraries");
699682
for (Library lib : contribLibraries) {
700683
if (lib.hasExamples()) {
701-
any = true;
702-
}
703-
}
704-
if (any) {
705-
// menu.addSeparator();
706-
DefaultMutableTreeNode contribParent = new DefaultMutableTreeNode("Library Examples");
707-
// Base.addDisabledItem(menu, "Contributed");
708-
for (Library lib : contribLibraries) {
709-
if (lib.hasExamples()) {
710-
// JMenu libMenu = new JMenu(lib.getName());
711684
DefaultMutableTreeNode libNode = new DefaultMutableTreeNode(lib.getName());
712-
// base.addSketches(libMenu, lib.getExamplesFolder(), replace);
713685
base.addSketches(libNode, lib.getExamplesFolder());
714-
// menu.add(libMenu);
715-
contribParent.add(libNode);
716-
}
686+
contributed.add(libNode);
717687
}
718-
node.add(contribParent);
719-
}
720-
} catch (IOException e) {
721-
e.printStackTrace();
722-
}
723-
724-
DefaultMutableTreeNode contribExampleNode = buildContributedExamplesTrees();
725-
if (contribExampleNode.getChildCount() > 0)
726-
node.add(contribExampleNode);
727-
return node;
728-
}
729-
730-
731-
public DefaultMutableTreeNode buildContributedExamplesTrees() {
732-
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Contributed Examples");
733-
734-
try {
735-
File[] subfolders = ContributionType.EXAMPLES.listCandidates(examplesContribFolder);
736-
if (subfolders == null) {
737-
subfolders = new File[0]; //empty array
738688
}
739-
for (File sub : subfolders) {
740-
if (!ExamplesContribution.isExamplesCompatible(base, sub))
741-
continue;
742-
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(sub.getName());
743-
if (base.addSketches(subNode, sub)) {
744-
node.add(subNode);
745-
int exampleNodeNumber = -1;
746-
for (int y = 0; y < subNode.getChildCount(); y++)
747-
if (subNode.getChildAt(y).toString().equals("examples"))
748-
exampleNodeNumber = y;
749-
if (exampleNodeNumber == -1)
750-
continue;
751-
TreeNode exampleNode = subNode.getChildAt(exampleNodeNumber);
752-
subNode.remove(exampleNodeNumber);
753-
int count = exampleNode.getChildCount();
754-
for (int x = 0; x < count; x++) {
755-
subNode.add((DefaultMutableTreeNode) exampleNode.getChildAt(0));
756-
}
757-
}
689+
if(contributed.getChildCount() > 0){
690+
root.add(contributed);
758691
}
759692
} catch (IOException e) {
760693
e.printStackTrace();
761694
}
762-
return node;//examplesTree;
763-
}
764695

696+
return root;
697+
}
765698

766699
public void resetExamples() {
767700
if (examplesFrame != null) {
@@ -886,24 +819,19 @@ public void actionPerformed(ActionEvent e) {
886819
examplesPanel.setLayout(new BorderLayout());
887820
examplesPanel.setBackground(Color.WHITE);
888821

889-
final JPanel openExamplesManagerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
890-
JLabel openExamplesManagerLabel = new JLabel(Language.text("examples.add_examples"));
891-
// openExamplesManagerLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
892-
openExamplesManagerPanel.add(openExamplesManagerLabel);
822+
final JPanel openExamplesManagerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
823+
JButton addExamplesButton = new JButton(Language.text("examples.add_examples"));
824+
openExamplesManagerPanel.add(addExamplesButton);
893825
openExamplesManagerPanel.setOpaque(false);
894826
Border lineBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLACK);
895827
Border paddingBorder = BorderFactory.createEmptyBorder(3, 5, 1, 4);
896828
openExamplesManagerPanel.setBorder(BorderFactory.createCompoundBorder(lineBorder, paddingBorder));
897-
// openExamplesManagerLabel.set
898829
openExamplesManagerPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
899830
openExamplesManagerPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
900-
// openExamplesManagerLabel.setForeground(new Color(0, 0, 238));
901-
openExamplesManagerPanel.addMouseListener(new MouseAdapter() {
902-
831+
addExamplesButton.addActionListener(new ActionListener() {
903832
@Override
904-
public void mouseClicked(MouseEvent e) {
833+
public void actionPerformed(ActionEvent e) {
905834
base.handleOpenExampleManager();
906-
// openExamplesManagerLabel.setForeground(new Color(85, 26, 139));
907835
}
908836
});
909837

@@ -916,7 +844,6 @@ public void mouseClicked(MouseEvent e) {
916844

917845
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
918846
tree.setShowsRootHandles(true);
919-
// tree.setToggleClickCount(2);
920847
// expand the root
921848
tree.expandRow(0);
922849
// now hide the root

0 commit comments

Comments
 (0)