Skip to content

Commit

Permalink
improve loading animation
Browse files Browse the repository at this point in the history
  • Loading branch information
GraxCode committed May 1, 2020
1 parent 866c2d4 commit 8672875
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
29 changes: 19 additions & 10 deletions src/me/nov/threadtear/swing/tree/ClassTreePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
21 changes: 2 additions & 19 deletions src/me/nov/threadtear/swing/tree/JTreeWithHint.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,30 @@
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;

public class JTreeWithHint extends JTree {
private static final long serialVersionUID = 1L;
protected String hint;
protected boolean isLoading;

public JTreeWithHint(String hint) {
this.hint = hint;
// this.putClientProperty("JTree.alternateRowColor", true);
// 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;
}
}
}

0 comments on commit 8672875

Please sign in to comment.