package xal.tools.scan;

import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.awt.Font;
import java.awt.Color;
import java.awt.BorderLayout;

import xal.ca.*;
import xal.tools.swing.*;
import xal.smf.TimingCenter;


public class BeamTrigger{

    //name of the beam trigger PV
    private String triggerNamePV = "ICS_Tim:Gate_BeamOn:SSTrigger";
    private Channel ch   = null;
   
    private JRadioButton useTriggerButton  = new JRadioButton(" Use Beam Trigger, Delay [sec]:");

    private DoubleInputTextField tDelayText = new DoubleInputTextField(5);

    private DecimalFormat tDelayFormat = new DecimalFormat("0.0#");

    public BeamTrigger(){
         
	tDelayText.setHorizontalAlignment(JTextField.CENTER);
        tDelayText.setNormalBackground(Color.white);
        tDelayText.setDecimalFormat(tDelayFormat);
	tDelayText.setValue(0.2);
        
	//get channel from timing center
	TimingCenter tmCenter = TimingCenter.getDefaultTimingCenter();
        ch = tmCenter.getChannel(TimingCenter.TRIGGER_HANDLE);

        useTriggerButton.setSelected(true);
  
 	useTriggerButton.addItemListener(new ItemListener(){
		public void itemStateChanged(ItemEvent e) {
		    if (e.getStateChange() == ItemEvent.SELECTED) {
			setOnOff(true);
		    }
		    else{
			setOnOff(false);
		    }
		}
	    });
    }

    public void setOnOff(boolean onOff){
	tDelayText.setEditable(onOff);
        useTriggerButton.setSelected(onOff);
    }

    public void setFontForAll(Font fnt){
	tDelayText.setFont(fnt);
	useTriggerButton.setFont(fnt);
    }

    public JPanel getJPanel(){
	JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(useTriggerButton,BorderLayout.CENTER);
        panel.add(tDelayText,BorderLayout.EAST);
	return panel;
    }

    public boolean isOn(){
	return useTriggerButton.isSelected();
    }

    public void makePulse(){
	if(useTriggerButton.isSelected() && ch != null){
	    try {
		ch.putVal(1.0);
	    }
	    catch (ConnectionException e){
		setOnOff(false);
		return;
	    }
	    catch (PutException e){ 
		setOnOff(false);
		return;
	    }
	    try{Thread.sleep((long) (tDelayText.getValue()*1000.0));}
	    catch(InterruptedException e){}
	}
    }

    public String getChannelName(){
	return triggerNamePV ;
    }

    public Channel getChannel(){
	return ch;
    }

    public void setChannelName(String chanName){
	triggerNamePV = chanName;
	ch = ChannelFactory.defaultFactory().getChannel(triggerNamePV);
    }

    public void setChannel(Channel ch_In){
	ch = ch_In;
        triggerNamePV = ch.channelName();
    }


    public void setDelay(double timeDelay){
	tDelayText.setValue(timeDelay);
    }

    public double getDelay(){
	return tDelayText.getValue();
    }
}

@@ -943,16 +943,22 @@
 center%0A%09
+final 
 TimingCe
@@ -1012,24 +1012,129 @@
 nter();%0A
-        
+%09%09// some sites may not have a default accelerator defined or the trigger channel%0A%09%09if ( tmCenter != null ) %7B%0A%09%09%09
 ch = tmC
@@ -1139,27 +1139,28 @@
 mCenter.
-get
+find
 Channel(
 TimingCe
@@ -1151,16 +1151,17 @@
 Channel(
+ 
 TimingCe
@@ -1183,20 +1183,39 @@
 NDLE
+ 
 );%0A
-%0A        
+%09%09%09if ( ch != null ) %7B%0A%09%09%09%09
 useT
@@ -1246,20 +1246,20 @@
 (true);%0A
-  %0A 
+%0A%09%09%09
 %09useTrig
@@ -1304,16 +1304,19 @@
 ener()%7B%0A
+%09%09%09
 %09%09public
@@ -1355,20 +1355,20 @@
  e) %7B%0A%09%09
-    
+%09%09%09%09
 if (e.ge
@@ -1406,24 +1406,28 @@
 ECTED) %7B%0A%09%09%09
+%09%09
+%09%09
 setOnOff(tru
@@ -1436,26 +1436,30 @@
 ;%0A%09%09
-    %7D%0A%09%09    
+%09%09%09%09%7D%0A%09%09%09%09%09%09
 else%7B%0A
+%09%09%09%09
 %09%09%09s
@@ -1480,26 +1480,236 @@
 ;%0A%09%09
-    %7D%0A%09%09%7D%0A%09    %7D);
+%09%09%09%09%7D%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D);%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09useTriggerButton.setSelected( false );%0A%09%09%09%09useTriggerButton.setEnabled( false );%0A%09%09%09%7D%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09useTriggerButton.setSelected( false );%0A%09%09%09useTriggerButton.setEnabled( false );%0A%09%09%7D%0A  
 %0A   

Test whether the user has defined a default accelerator. If not, the scan will exclude the beam trigger.