package turgen;
import java.awt.Window;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import javax.swing.*;
import turgen.plugin.Plugin;


public class ParallelGUIUpdater {
    
    private static class SliderUpdater implements Runnable {
        
        private final JSlider _s;
        private final int _value;
        
        public SliderUpdater(JSlider s,int value) {
            _s=s;
            _value=value;
        }
        
        public void run() {
            _s.setValue(_value);
        }
    }
    
    
    private static class PluginsInitializer implements Runnable {
        
        private final Iterator<Plugin> pluginsIterator;
        
        public PluginsInitializer(Iterator<Plugin> _pluginsIterator) {
            pluginsIterator=_pluginsIterator;
        }
        
        public void run() {
            while (pluginsIterator.hasNext()) {
                Plugin p = pluginsIterator.next();
                p.init();
                Turgen.dlm.dlgRecord.setupPlugin(p);
            }
            Turgen.dlm.dlgRecord.selectInitialPlugin();
            
        }
    }
    
    
    

    
    
    private static class ComponentEnabler implements Runnable {
        
        private JComponent[] comps;
        private boolean b;
        
        public ComponentEnabler(JComponent[] cmps,boolean _b) {
            comps=cmps;
            b=_b;
        }

        public void run() {
            int l = comps.length;
            for (int i = 0; i < l; i++) {
                comps[i].setEnabled(b);
            }
        }
    }
    
    private static class MessageShower implements Runnable {
        private String message;
        
        public MessageShower(String s) {
            message=s;
        }
        
        public void run() {
            JOptionPane.showMessageDialog(null, message);
        }
    }
    
    private static class StatusUpdater implements Runnable {
        public void run() {
             Turgen.dlm.frmTurgen.getStatusObject().updateStatusGUI();
        }
    }
    
    private static class Packer implements Runnable {
        
        private final Window window;
        
        public Packer(Window w) {
            window=w;
        }
        
        public void run() {
            window.pack();
        }
    }
    
    
    public static void setComponentsEnabled(JComponent[] comps,boolean b) {
        try {
            SwingUtilities.invokeAndWait(new ComponentEnabler(comps,b));
            
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void showMessage(String s) {
        try {
            SwingUtilities.invokeAndWait(new MessageShower(s));
            
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void updateStatusGUI() {
        try {
            SwingUtilities.invokeAndWait(new StatusUpdater());
            
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void initPlugins(Iterator<Plugin> pluginsIterator) {
        try {
            SwingUtilities.invokeAndWait(new PluginsInitializer(pluginsIterator));
        }
        catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        catch (InvocationTargetException e2) {
            e2.printStackTrace();
        }
    }
    
    public static void updateSlider(JSlider s,int val) {
        try {
            SwingUtilities.invokeAndWait(new SliderUpdater(s,val));
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void pack(Window w) {
        try {
            SwingUtilities.invokeAndWait(new Packer(w));
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
        
    }

}
