Merge branch 'subtree'
[fanfix.git] / src / be / nikiroo / utils / ui / ProgressBar.java
index 6e1e44a9f7057748389047c5d7def8c0aabf7982..11e1e6c05356638266a105762a1cf569f8797f36 100644 (file)
@@ -28,17 +28,24 @@ public class ProgressBar extends JPanel {
        private List<ActionListener> actionListeners;
        private List<ActionListener> updateListeners;
        private Progress pg;
+       private Object lock = new Object();
 
        public ProgressBar() {
                bars = new HashMap<Progress, JProgressBar>();
                actionListeners = new ArrayList<ActionListener>();
                updateListeners = new ArrayList<ActionListener>();
        }
+       
+       public ProgressBar(Progress pg) {
+               this();
+               setProgress(pg);
+       }
 
        public void setProgress(final Progress pg) {
                this.pg = pg;
 
                SwingUtilities.invokeLater(new Runnable() {
+                       @Override
                        public void run() {
                                if (pg != null) {
                                        final JProgressBar bar = new JProgressBar();
@@ -53,9 +60,11 @@ public class ProgressBar extends JPanel {
                                        bar.setString(pg.getName());
 
                                        pg.addProgressListener(new Progress.ProgressListener() {
+                                               @Override
                                                public void progress(Progress progress, String name) {
                                                        final Progress.ProgressListener l = this;
                                                        SwingUtilities.invokeLater(new Runnable() {
+                                                               @Override
                                                                public void run() {
                                                                        Map<Progress, JProgressBar> newBars = new HashMap<Progress, JProgressBar>();
                                                                        newBars.put(pg, bar);
@@ -65,26 +74,31 @@ public class ProgressBar extends JPanel {
                                                                        bar.setValue(pg.getProgress());
                                                                        bar.setString(pg.getName());
 
-                                                                       for (Progress pg : getChildrenAsOrderedList(pg)) {
-                                                                               JProgressBar bar = bars.get(pg);
-                                                                               if (bar == null) {
-                                                                                       bar = new JProgressBar();
-                                                                                       bar.setStringPainted(true);
+                                                                       synchronized (lock) {
+                                                                       for (Progress pgChild : getChildrenAsOrderedList(pg)) {
+                                                                               JProgressBar barChild = bars
+                                                                                               .get(pgChild);
+                                                                               if (barChild == null) {
+                                                                                       barChild = new JProgressBar();
+                                                                                       barChild.setStringPainted(true);
                                                                                }
 
-                                                                               newBars.put(pg, bar);
+                                                                               newBars.put(pgChild, barChild);
 
-                                                                               bar.setMinimum(pg.getMin());
-                                                                               bar.setMaximum(pg.getMax());
-                                                                               bar.setValue(pg.getProgress());
-                                                                               bar.setString(pg.getName());
+                                                                               barChild.setMinimum(pgChild.getMin());
+                                                                               barChild.setMaximum(pgChild.getMax());
+                                                                               barChild.setValue(pgChild.getProgress());
+                                                                               barChild.setString(pgChild.getName());
                                                                        }
-
+                                                                       
                                                                        if (ProgressBar.this.pg == null) {
                                                                                bars.clear();
                                                                        } else {
                                                                                bars = newBars;
-
+                                                                       }
+                                                                       }
+                                                                       
+                                                                       if (ProgressBar.this.pg != null) {
                                                                                if (pg.isDone()) {
                                                                                        pg.removeProgressListener(l);
                                                                                        for (ActionListener listener : actionListeners) {
@@ -134,33 +148,38 @@ public class ProgressBar extends JPanel {
        // only named ones
        private List<Progress> getChildrenAsOrderedList(Progress pg) {
                List<Progress> children = new ArrayList<Progress>();
-               for (Progress child : pg.getChildren()) {
+               
+               synchronized (lock) {
+                       for (Progress child : pg.getChildren()) {
                        if (child.getName() != null && !child.getName().isEmpty()) {
                                children.add(child);
                        }
                        children.addAll(getChildrenAsOrderedList(child));
                }
-
+               }
+               
                return children;
        }
 
        private void update() {
-               invalidate();
-               removeAll();
-
-               if (pg != null) {
-                       setLayout(new GridLayout(bars.size(), 1));
-                       add(bars.get(pg), 0);
-                       for (Progress child : getChildrenAsOrderedList(pg)) {
-                               JProgressBar jbar = bars.get(child);
-                               if (jbar != null) {
-                                       add(jbar);
+               synchronized (lock) {
+                       invalidate();
+                       removeAll();
+
+                       if (pg != null) {
+                               setLayout(new GridLayout(bars.size(), 1));
+                               add(bars.get(pg), 0);
+                               for (Progress child : getChildrenAsOrderedList(pg)) {
+                                       JProgressBar jbar = bars.get(child);
+                                       if (jbar != null) {
+                                               add(jbar);
+                                       }
                                }
                        }
-               }
 
-               validate();
-               repaint();
+                       validate();
+                       repaint();
+               }
 
                for (ActionListener listener : updateListeners) {
                        listener.actionPerformed(new ActionEvent(this, 0, "update"));