package turgen;
import java.lang.reflect.InvocationTargetException;
import javax.swing.*;
import turgen.plugin.Plugin;


public class ParallelGUIUpdater {
    
    private static class SliderUpdater implements Runnable {
        
        private JSlider _s;
        private int _value;
        
        public SliderUpdater(JSlider s,int value) {
            _s=s;
            _value=value;
        }
        
        public void run() {
            _s.setValue(_value);
        }
    }
    
    
    private static class PluginInitializer implements Runnable {
        
        private Plugin p;
        
        public PluginInitializer(Plugin _p) {
            p=_p;
        }
        
        public void run() {
            p.init();
        }
    }
    
    private static class PluginGUISetter implements Runnable {
        
        private Plugin plugin;
        
        public PluginGUISetter(Plugin pgn) {
            plugin=pgn;
        }
        
        public void run() {
            Turgen.dlm.dlgRecord.setupPlugin(plugin);
        }
    }
    

    
    
    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();
        }
    }
    
    
    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 setupPluginGUI(Plugin pgn) {
        try {
            SwingUtilities.invokeAndWait(new PluginGUISetter(pgn));
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void initPlugin(Plugin p) {
        try {
            SwingUtilities.invokeAndWait(new PluginInitializer(p));
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }
    
    public static void updateSlider(JSlider s,int val) {
        try {
            SwingUtilities.invokeAndWait(new SliderUpdater(s,val));
        }
        catch (InterruptedException e1) {
            
        }
        catch (InvocationTargetException e2) {
            
        }
    }

}
