From 86728758c74ec7d963089d8042dd597fe967e33e Mon Sep 17 00:00:00 2001 From: GraxCode Date: Fri, 1 May 2020 15:02:08 +0200 Subject: [PATCH] improve loading animation --- .../threadtear/swing/tree/ClassTreePanel.java | 29 ++++++++++++------- .../threadtear/swing/tree/JTreeWithHint.java | 21 ++------------ 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/src/me/nov/threadtear/swing/tree/ClassTreePanel.java b/src/me/nov/threadtear/swing/tree/ClassTreePanel.java index 221fcf5..42719f9 100644 --- a/src/me/nov/threadtear/swing/tree/ClassTreePanel.java +++ b/src/me/nov/threadtear/swing/tree/ClassTreePanel.java @@ -3,6 +3,8 @@ import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.GridLayout; +import java.awt.Image; +import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.File; @@ -11,13 +13,14 @@ import java.util.Enumeration; import javax.swing.BorderFactory; +import javax.swing.ImageIcon; import javax.swing.JButton; +import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreeNode; import javax.swing.tree.TreePath; import javax.swing.tree.TreeSelectionModel; @@ -169,16 +172,19 @@ public void mouseClicked(MouseEvent e) { } } + private ImageIcon loading = new ImageIcon(Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/res/spin.gif")).getScaledInstance(32, 32, Image.SCALE_DEFAULT)); + @Override public void onJarLoad(File input) { - tree.isLoading = true; - tree.setModel(new DefaultTreeModel((TreeNode) tree.getModel().getRoot())); - tree.invalidate(); - tree.validate(); - tree.repaint(); + this.remove(outerPanel); + JLabel loadingLabel = new JLabel("Loading class files... ", loading, JLabel.CENTER); + this.add(loadingLabel, BorderLayout.CENTER); + this.invalidate(); + this.validate(); + this.repaint(); try { SwingUtilities.invokeLater(() -> { - Thread loadingThread = new Thread(() -> { + new Thread(() -> { this.inputFile = input; try { this.classes = JarIO.loadClasses(input); @@ -194,9 +200,12 @@ public void onJarLoad(File input) { refreshIgnored(); model.reload(); analysis.setEnabled(true); - tree.isLoading = false; - }); - loadingThread.start(); + this.remove(loadingLabel); + this.add(outerPanel, BorderLayout.CENTER); + this.invalidate(); + this.validate(); + this.repaint(); + }).start(); }); } catch (Exception e) { e.printStackTrace(); diff --git a/src/me/nov/threadtear/swing/tree/JTreeWithHint.java b/src/me/nov/threadtear/swing/tree/JTreeWithHint.java index 1d33167..cd99981 100644 --- a/src/me/nov/threadtear/swing/tree/JTreeWithHint.java +++ b/src/me/nov/threadtear/swing/tree/JTreeWithHint.java @@ -3,10 +3,7 @@ import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Image; import java.awt.RenderingHints; -import java.awt.Toolkit; -import java.awt.image.ImageObserver; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; @@ -14,7 +11,6 @@ public class JTreeWithHint extends JTree { private static final long serialVersionUID = 1L; protected String hint; - protected boolean isLoading; public JTreeWithHint(String hint) { this.hint = hint; @@ -22,28 +18,15 @@ public JTreeWithHint(String hint) { // this.putClientProperty("JTree.lineStyle", "none"); } - private Image loading = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/res/spin.gif")).getScaledInstance(32, 32, Image.SCALE_DEFAULT); - @Override public void paint(Graphics g) { super.paint(g); DefaultMutableTreeNode tn = (DefaultMutableTreeNode) getModel().getRoot(); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); - if (isLoading) { - g.drawImage(loading, getWidth() / 2 - 16, getHeight() / 2 - 16, new NodeImageObserver()); - } else if (tn.getChildCount() == 0) { + ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + if (tn.getChildCount() == 0) { g.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 12)); g.drawString(hint, getWidth() / 2 - g.getFontMetrics().stringWidth(hint) / 2, getHeight() / 2); } } - - public class NodeImageObserver implements ImageObserver { - - public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h) { - if ((flags & (FRAMEBITS | ALLBITS)) != 0) { - repaint(); - } - return (flags & (ALLBITS | ABORT)) == 0; - } - } }