From d8785d22114d3e50c830a71c629e93815ba43158 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Fri, 4 Feb 2022 19:36:04 -0500 Subject: [PATCH] move "Add Examples" to bottom of window, more logic cleanups --- app/src/processing/app/ui/ExamplesFrame.java | 55 ++++++++----------- .../processing/app/ui/SketchbookFrame.java | 20 ++++--- todo.txt | 4 +- 3 files changed, 38 insertions(+), 41 deletions(-) diff --git a/app/src/processing/app/ui/ExamplesFrame.java b/app/src/processing/app/ui/ExamplesFrame.java index 138b173f2..7a1db2c2c 100644 --- a/app/src/processing/app/ui/ExamplesFrame.java +++ b/app/src/processing/app/ui/ExamplesFrame.java @@ -22,12 +22,7 @@ package processing.app.ui; -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.Component; -import java.awt.Cursor; -import java.awt.FlowLayout; -import java.awt.Point; +import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.MouseAdapter; @@ -36,13 +31,7 @@ import java.io.File; import java.io.IOException; import java.util.Enumeration; -import javax.swing.BorderFactory; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTree; -import javax.swing.border.Border; +import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.event.TreeExpansionEvent; import javax.swing.event.TreeExpansionListener; @@ -90,17 +79,6 @@ public class ExamplesFrame extends JFrame { examplesPanel.setLayout(new BorderLayout()); examplesPanel.setBackground(Color.WHITE); - final JPanel openExamplesManagerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); - JButton addExamplesButton = new JButton(Language.text("examples.add_examples")); - openExamplesManagerPanel.add(addExamplesButton); - openExamplesManagerPanel.setOpaque(false); - Border lineBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY); - Border paddingBorder = BorderFactory.createEmptyBorder(3, 5, 1, 4); - openExamplesManagerPanel.setBorder(BorderFactory.createCompoundBorder(lineBorder, paddingBorder)); - openExamplesManagerPanel.setAlignmentX(Component.LEFT_ALIGNMENT); - openExamplesManagerPanel.setCursor(new Cursor(Cursor.HAND_CURSOR)); - addExamplesButton.addActionListener(e -> ContributionManager.openExamples()); - final JTree tree = new JTree(buildTree()); tree.setOpaque(true); @@ -178,9 +156,21 @@ public class ExamplesFrame extends JFrame { treePane.setBackground(Color.WHITE); treePane.setAlignmentX(Component.LEFT_ALIGNMENT); - examplesPanel.add(openExamplesManagerPanel,BorderLayout.PAGE_START); + //examplesPanel.add(openExamplesManagerPanel,BorderLayout.PAGE_START); examplesPanel.add(treePane, BorderLayout.CENTER); + Container buttons = Box.createHorizontalBox(); + + JButton addButton = new JButton(Language.text("examples.add_examples")); + addButton.addActionListener(e -> ContributionManager.openExamples()); + buttons.add(Box.createHorizontalGlue()); + buttons.add(addButton); + buttons.add(Box.createHorizontalGlue()); + + JPanel buttonPanel = new JPanel(); // adds extra border + buttonPanel.add(buttons); + examplesPanel.add(buttonPanel, BorderLayout.SOUTH); + getContentPane().add(examplesPanel); pack(); restoreExpanded(tree); @@ -190,16 +180,19 @@ public class ExamplesFrame extends JFrame { public void setVisible() { // Space for the editor plus a li'l gap int roughWidth = getWidth() + 20; - Point p; // If no window open, or the editor is at the edge of the screen Editor editor = base.getActiveEditor(); - if (editor == null || - (p = editor.getLocation()).x < roughWidth) { - // Center the window on the screen + if (editor == null) { setLocationRelativeTo(null); } else { - // Open the window relative to the editor - setLocation(p.x - roughWidth, p.y); + Point p = editor.getLocation(); + if (p.x < roughWidth) { + // Center the window on the screen + setLocationRelativeTo(null); + } else { + // Open the window relative to the editor + setLocation(p.x - roughWidth, p.y); + } } setVisible(true); } diff --git a/app/src/processing/app/ui/SketchbookFrame.java b/app/src/processing/app/ui/SketchbookFrame.java index 771e5a1bb..3bcd3ca54 100644 --- a/app/src/processing/app/ui/SketchbookFrame.java +++ b/app/src/processing/app/ui/SketchbookFrame.java @@ -147,7 +147,9 @@ public class SketchbookFrame extends JFrame { // Check whether sketchbook is empty or not TreeModel treeModel = tree.getModel(); if (treeModel.getChildCount(treeModel.getRoot()) != 0) { - JScrollPane treePane = new JScrollPane(tree); + JScrollPane treePane = new JScrollPane(tree, + ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, + ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); treePane.setPreferredSize(Toolkit.zoom(250, 450)); treePane.setBorder(new EmptyBorder(0, 0, 0, 0)); @@ -188,20 +190,24 @@ public class SketchbookFrame extends JFrame { public void setVisible() { - // TODO The ExamplesFrame code doesn't do this, is it necessary? + // TODO The ExamplesFrame code doesn't invokeLater(), is it necessary? // Either one of them is wrong, or this is hiding a bug [fry 150811] EventQueue.invokeLater(() -> { // Space for the editor plus a li'l gap int roughWidth = getWidth() + 20; // If no window open, or the editor is at the edge of the screen Editor editor = base.getActiveEditor(); - Point p = editor.getLocation(); - if (editor == null || p.x < roughWidth) { - // Center the window on the screen + if (editor == null) { setLocationRelativeTo(null); } else { - // Open the window relative to the editor - setLocation(p.x - roughWidth, p.y); + Point p = editor.getLocation(); + if (p.x < roughWidth) { + // Center the window on the screen + setLocationRelativeTo(null); + } else { + // Open the window relative to the editor + setLocation(p.x - roughWidth, p.y); + } } setVisible(true); }); diff --git a/todo.txt b/todo.txt index 49007ed7f..5711a9807 100755 --- a/todo.txt +++ b/todo.txt @@ -1,8 +1,6 @@ 1281 (4.0b6) X update to Java 17.0.2+8 - - -_ move "Add Examples" to bottom of window +X move "Add Examples" to bottom of window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .