package gui;
  
import network.Command;
import network.NetPack;
import network.NetworkHandler;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Scale;
import org.eclipse.swt.widgets.Text;
import other.MsgBox;

/**
 * TabItemManual Klasse 
 * Dient zum Manuellen Steuern des Roboters, sowohl mit
 * Hilfe von Schiebereglern, als auch Eingabefeldern zur
 * direkten Ansteuerung von Positionen
 */
public class TabItemManual extends Composite {

	// Attribute
	private Group group = null;
	private Group group1 = null;
	private Group group2 = null;
	private Group group3 = null;
	private Label label = null;
	private Label label1 = null;
	private Button bHome2 = null;
	private Scale sPos2 = null;
	private Text tPos2 = null;
	private Button bPos2 = null;
	private Label label2 = null;
	private Label label3 = null;
	private Button bHome3 = null;
	private Scale sPos3 = null;
	private Text tPos3 = null;
	private Button bPos3 = null;
	private Label label4 = null;
	private Label label5 = null;
	private Button bHome4 = null;
	private Scale sPos4 = null;
	private Text tPos4 = null;
	private Button bPos4 = null;
	private Label label8 = null;
	private Label label9 = null;
	private Scale sVel2 = null;
	private Text tVel2 = null;
	private Button bVel2 = null;
	private Label label11 = null;
	private Label label12 = null;
	private Scale sVel3 = null;
	private Text tVel3 = null;
	private Button bVel3 = null;
	private Label label13 = null;
	private Label label14 = null;
	private Scale sVel4 = null;
	private Text tVel4 = null;
	private Button bVel4 = null;
	private Label label6 = null;
	private Label label7 = null;
	private Scale sAcc2 = null;
	private Text tAcc2 = null;
	private Button bAcc2 = null;
	private Label label10 = null;
	private Label label15 = null;
	private Scale sAcc3 = null;
	private Text tAcc3 = null;
	private Button bAcc3 = null;
	private Label label16 = null;
	private Label label17 = null;
	private Scale sAcc4 = null;
	private Text tAcc4 = null;
	private Button bAcc4 = null;
	private Label label18 = null;
	private Label label19 = null;
	private Text tPosX = null;
	private Label label20 = null;
	private Text tPosY = null;
	private Label label21 = null;
	private Text tPosZ = null;
	private Button bPos = null;
	private Label label22 = null;
	private Label label23 = null;
	private Text tPosRad = null;
	private Label label24 = null;
	private Text tPosAlpha = null;
	private Label label25 = null;
	private Text tPosZb = null;
	private Button bPosb = null;
	private Label label26 = null;
	private Label label27 = null;
	private Text tMotionSpeed = null;
	private Button bMotionSpeed = null;
	private Button bGripOpen = null;
	private Button bGripClose = null;
	private Label label28 = null;
	private Label label29 = null;
	private Button chLinear = null;
	private Button chIntell = null;
	private Label label30 = null;
	private Label label31 = null;
	private Composite composite = null;
	private Composite composite2 = null;
	private NetworkHandler netHandler = null;

	/**
	 * Konstruktor
	 * @param parent das Parent Composite
	 * @param style der SWT Style
	 * @param netH das vorherrschende NetworkHandler Objekt
	 */
	public TabItemManual(Composite parent, int style, NetworkHandler netH) {
		super(parent, style);
		netHandler = netH;
		initialize();
		initValues();
	}

	/* ##########################################################################
	 * 						VISUAL EDITOR
	 * ##########################################################################*/
	
	/**
	 * Diese Methode erwirkt eine Initialisierung aller wichtiger
	 * Komponenten und ordnet diese auf dem Composite an
	 */
	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		gridLayout.horizontalSpacing = 15;
		gridLayout.verticalSpacing = 15;
		gridLayout.makeColumnsEqualWidth = true;
		this.setLayout(gridLayout);
		createGroup();
		createGroup1();
		createGroup2();
		createGroup3();
		setSize(new Point(952, 532));
	}

	/**
	 * Erzeugt die GroupBox für Modulpositionen, alle wichtigen darin
	 * enthaltenen Komponenten und ordnet diese an	
	 */
	private void createGroup() {
		GridData gridData26 = new GridData();
		gridData26.grabExcessVerticalSpace = true;
		gridData26.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData25 = new GridData();
		gridData25.grabExcessVerticalSpace = true;
		gridData25.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData24 = new GridData();
		gridData24.grabExcessVerticalSpace = true;
		gridData24.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData15 = new GridData();
		gridData15.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData14 = new GridData();
		gridData14.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData13 = new GridData();
		gridData13.grabExcessHorizontalSpace = true;
		gridData13.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData12 = new GridData();
		gridData12.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData12.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData11 = new GridData();
		gridData11.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData11.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData10 = new GridData();
		gridData10.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData10.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData8 = new GridData();
		gridData8.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData8.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData7 = new GridData();
		gridData7.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData6.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData4.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridLayout gridLayout1 = new GridLayout();
		gridLayout1.numColumns = 3;
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData.grabExcessVerticalSpace = true;
		group = new Group(this, SWT.NONE);
		group.setLayoutData(gridData);
		group.setLayout(gridLayout1);
		group.setText("Modulpositionen");
		label = new Label(group, SWT.NONE);
		label.setText("Vertikale Linearachse");
		label.setLayoutData(gridData24);
		label.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label1 = new Label(group, SWT.NONE);
		label1.setText("-200 - 0 mm");
		label1.setLayoutData(gridData10);
		bHome2 = new Button(group, SWT.NONE);
		bHome2.setText("Referenzfahrt");
		bHome2.setLayoutData(gridData4);
		bHome2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
		     homeModule(2);
			}
		});
		sPos2 = new Scale(group, SWT.NONE);
		sPos2.setMaximum(200);
		sPos2.setPageIncrement(20);
		sPos2.setLayoutData(gridData13);
		sPos2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sPos2.getSelection() - 200;
				tPos2.setText(""+x+".0");
				moveModule(2,posToMeter(x));
			}
		});
		tPos2 = new Text(group, SWT.BORDER | SWT.RIGHT);
		bPos2 = new Button(group, SWT.NONE);
		bPos2.setText("Setzen");
		bPos2.setLayoutData(gridData5);
        bPos2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tPos2.getText(),0,-200);
        		if(x==Float.MAX_VALUE) return;
        		sPos2.setSelection((int)x+200);
        		moveModule(2,posToMeter(x));
        	}
        });
		label2 = new Label(group, SWT.NONE);
		label2.setText("Horizontale Linearachse");
		label2.setLayoutData(gridData25);
		label2.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label3 = new Label(group, SWT.NONE);
		label3.setText("0 - 400 mm");
		label3.setLayoutData(gridData11);
		bHome3 = new Button(group, SWT.NONE);
		bHome3.setText("Referenzfahrt");
		bHome3.setLayoutData(gridData6);
		bHome3.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
		     homeModule(3);
			}
		});
		sPos3 = new Scale(group, SWT.NONE);
		sPos3.setMaximum(400);
		sPos3.setPageIncrement(40);
		sPos3.setLayoutData(gridData14);
		sPos3.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sPos3.getSelection();
				tPos3.setText(""+x+".0");
				moveModule(3,posToMeter(x));
			}
		});
		tPos3 = new Text(group, SWT.BORDER | SWT.RIGHT);
		bPos3 = new Button(group, SWT.NONE);
		bPos3.setText("Setzen");
		bPos3.setLayoutData(gridData7);
        bPos3.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tPos3.getText(),400);
        		if(x==Float.MAX_VALUE) return;
        		sPos3.setSelection((int)x);
        		moveModule(3,posToMeter(x));
        	}
        });
		label4 = new Label(group, SWT.NONE);
		label4.setText("Rotationsachse");
		label4.setLayoutData(gridData26);
		label4.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label5 = new Label(group, SWT.NONE);
		label5.setText("-160 - 160°");
		label5.setLayoutData(gridData12);
		bHome4 = new Button(group, SWT.NONE);
		bHome4.setText("Referenzfahrt");
		bHome4.setLayoutData(gridData8);
		bHome4.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
		     homeModule(4);
			}
		});
		sPos4 = new Scale(group, SWT.NONE);
		sPos4.setMinimum(0);
		sPos4.setMaximum(320);
		sPos4.setSelection(160);
		sPos4.setPageIncrement(32);
		sPos4.setLayoutData(gridData15);
		sPos4.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sPos4.getSelection()-160;
				tPos4.setText(""+x+".0");
				moveModule(4,posToRad(x));
			}
		});
		tPos4 = new Text(group, SWT.BORDER | SWT.RIGHT);
		bPos4 = new Button(group, SWT.NONE);
		bPos4.setText("Setzen");
		bPos4.setLayoutData(gridData9);
        bPos4.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tPos4.getText(),160,-160);
        		if(x==Float.MAX_VALUE) return;
        		sPos4.setSelection((int)x+160);
        		moveModule(4,posToRad(x));
        	}
        });
	}

	/**
	 * Erzeugt die GroupBox für Roboter, alle wichtigen darin
	 * enthaltenen Komponenten und ordnet diese an	
	 */
	private void createGroup1() {
		
		GridLayout gridLayout5 = new GridLayout();
		gridLayout5.numColumns = 3;
		GridData gridData56 = new GridData();
		gridData56.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData55 = new GridData();
		gridData55.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData54 = new GridData();
		gridData54.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		
		
		GridData gridData53 = new GridData();
		gridData53.horizontalSpan = 2;
		GridData gridData52 = new GridData();
		gridData52.horizontalSpan = 2;
		GridData gridData51 = new GridData();
		gridData51.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData50 = new GridData();
		gridData50.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData49 = new GridData();
		gridData49.grabExcessHorizontalSpace = true;
		gridData49.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData48 = new GridData();
		gridData48.horizontalSpan = 3;
		GridData gridData47 = new GridData();
		gridData47.horizontalSpan = 3;
		GridData gridData46 = new GridData();
		gridData46.horizontalSpan = 6;
		gridData46.grabExcessVerticalSpace = true;
		GridData gridData45 = new GridData();
		gridData45.horizontalSpan = 4;
		GridData gridData44 = new GridData();
		gridData44.horizontalSpan = 5;
		gridData44.grabExcessVerticalSpace = true;
		GridData gridData43 = new GridData();
		gridData43.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData42 = new GridData();
		gridData42.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData41 = new GridData();
		gridData41.horizontalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData40 = new GridData();
		gridData40.horizontalSpan = 6;
		gridData40.grabExcessVerticalSpace = true;
		GridData gridData39 = new GridData();
		gridData39.horizontalSpan = 6;
		gridData39.grabExcessVerticalSpace = true;
		GridLayout gridLayout4 = new GridLayout();
		gridLayout4.numColumns = 7;
		GridData gridData2 = new GridData();
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData2.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		group1 = new Group(this, SWT.NONE);
		group1.setLayoutData(gridData2);
		group1.setLayout(gridLayout4);
		group1.setText("Roboter");
		label18 = new Label(group1, SWT.NONE);
		label18.setText("Position (kartesisches Koordinatensystem)");
		label18.setLayoutData(gridData39);
		label18.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		Label filler51 = new Label(group1, SWT.NONE);
		label19 = new Label(group1, SWT.NONE);
		label19.setText("X:");
		label19.setLayoutData(gridData42);
		tPosX = new Text(group1, SWT.BORDER | SWT.RIGHT);
		label20 = new Label(group1, SWT.NONE);
		label20.setText("Y:");
		label20.setLayoutData(gridData41);
		tPosY = new Text(group1, SWT.BORDER | SWT.RIGHT);
		label21 = new Label(group1, SWT.NONE);
		label21.setText("Z:");
		label21.setLayoutData(gridData43);
		tPosZ = new Text(group1, SWT.BORDER | SWT.RIGHT);
		bPos = new Button(group1, SWT.NONE);
		bPos.setText("    Setzen    ");
		bPos.setLayoutData(gridData49);
        bPos.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tPosX.getText(),500,-500);
        		if(x==Float.MAX_VALUE) return;
        		float y = checkText(tPosY.getText(),500,-500);
        		if(y==Float.MAX_VALUE) return;
        		float z = checkText(tPosZ.getText(),500);
        		if(z==Float.MAX_VALUE) return;
        		movePosition(x, y, z, "ka");
        	}
        });
		label22 = new Label(group1, SWT.NONE);
		label22.setText("Position (sphärisches Koordinatensystem)");
		label22.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label22.setLayoutData(gridData40);
		Label filler52 = new Label(group1, SWT.NONE);
		label23 = new Label(group1, SWT.NONE);
		label23.setText("Rad:");
		tPosRad = new Text(group1, SWT.BORDER | SWT.RIGHT);
		label24 = new Label(group1, SWT.NONE);
		label24.setText("Alpha:");
		tPosAlpha = new Text(group1, SWT.BORDER | SWT.RIGHT);
		label25 = new Label(group1, SWT.NONE);
		label25.setText("    Z:");
		tPosZb = new Text(group1, SWT.BORDER | SWT.RIGHT);
		bPosb = new Button(group1, SWT.NONE);
		bPosb.setText("    Setzen    ");
		bPosb.setLayoutData(gridData50);
        bPosb.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float r = checkText(tPosRad.getText(),520,100);
        		if(r==Float.MAX_VALUE) return;
        		float a = checkText(tPosAlpha.getText(),180,-180);
        		if(a==Float.MAX_VALUE) return;
        		float z = checkText(tPosZ.getText(),500);
        		if(z==Float.MAX_VALUE) return;
        		movePosition(r, posToRad(a), z, "sa");
        	}
        });

        label26 = new Label(group1, SWT.NONE);
		label26.setText("Geschwindigkeit und Beschleunigung");
		label26.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label26.setLayoutData(gridData44);

		label30 = new Label(group1, SWT.NONE);
		label30.setText("Gripper");
		label30.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label30.setLayoutData(gridData52);

		composite = new Composite(group1, SWT.NONE);
		composite.setLayout(gridLayout5);
        composite.setLayoutData(gridData45);
		label27 = new Label(composite, SWT.NONE);
		label27.setText("Prozentangabe:");
		label27.setLayoutData(gridData54);
		tMotionSpeed = new Text(composite, SWT.BORDER | SWT.RIGHT);
		tMotionSpeed.setLayoutData(gridData55);
		bMotionSpeed = new Button(composite, SWT.NONE);
		bMotionSpeed.setText("Setzen");
		bMotionSpeed.setLayoutData(gridData56);
		bMotionSpeed.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float m = checkText(tMotionSpeed.getText(),300,10);
        		if(m==Float.MAX_VALUE) return;
        		setMotionSpeed(m/100);
        	}
        });
        Label filler6 = new Label(group1, SWT.NONE);
		RowLayout rowLayout2 = new RowLayout();
		rowLayout2.marginBottom = 0;
		rowLayout2.marginLeft = 0;
		rowLayout2.marginRight = 0;
		rowLayout2.marginTop = 0;
		composite2 = new Composite(group1, SWT.NONE);
		composite2.setLayout(rowLayout2);
        composite2.setLayoutData(gridData53);
		bGripOpen = new Button(composite2, SWT.NONE);
		bGripOpen.setText("  Öffnen  ");
		bGripOpen.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		netHandler.getComQueue().enqueue(new Command(NetPack.GRIP,0,0));
        	}
        });
        
		bGripClose = new Button(composite2, SWT.NONE);
		bGripClose.setText("Schließen");
		bGripClose.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		netHandler.getComQueue().enqueue(new Command(NetPack.GRIP,1,0));
        	}
        });
		
		
		label28 = new Label(group1, SWT.NONE);
		label28.setText("Bewegungsparameter");
		label28.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label28.setLayoutData(gridData46);
		Label filler10 = new Label(group1, SWT.NONE);
		label29 = new Label(group1, SWT.NONE);
		label29.setText("");
		chLinear = new Button(group1, SWT.CHECK);
		chLinear.setText("Lineare Bewegung");
		chLinear.setLayoutData(gridData47);
		chLinear.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		updateMotionParam();
        	}
        });
		chIntell = new Button(group1, SWT.CHECK);
		chIntell.setText("Alternative Wege zulässig");
		chIntell.setLayoutData(gridData48);
		chIntell.setSelection(true);
		chIntell.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		updateMotionParam();
        	}
        });
		label30 = new Label(group1, SWT.NONE);
		label30.setText("");
	}

	/**
	 * Erzeugt die GroupBox für Modulgeschwindigkeiten, alle wichtigen darin
	 * enthaltenen Komponenten und ordnet diese an	
	 */
	private void createGroup2() {
		GridData gridData29 = new GridData();
		gridData29.grabExcessVerticalSpace = true;
		GridData gridData28 = new GridData();
		gridData28.grabExcessVerticalSpace = true;
		GridData gridData27 = new GridData();
		gridData27.grabExcessVerticalSpace = true;
		GridData gridData23 = new GridData();
		gridData23.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData22 = new GridData();
		gridData22.verticalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		GridData gridData21 = new GridData();
		gridData21.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData21.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData20 = new GridData();
		gridData20.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData20.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData19 = new GridData();
		gridData19.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData19.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData18 = new GridData();
		gridData18.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData17 = new GridData();
		gridData17.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData16 = new GridData();
		gridData16.grabExcessHorizontalSpace = true;
		gridData16.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout2 = new GridLayout();
		gridLayout2.numColumns = 3;
		GridData gridData1 = new GridData();
		gridData1.grabExcessHorizontalSpace = true;
		gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData1.grabExcessVerticalSpace = true;
		group2 = new Group(this, SWT.NONE);
		group2.setLayoutData(gridData1);
		group2.setLayout(gridLayout2);
		group2.setText("Modulgeschwindigkeiten");
		label8 = new Label(group2, SWT.NONE);
		label8.setText("Vertikale Linearachse");
		label8.setLayoutData(gridData27);
		label8.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label9 = new Label(group2, SWT.NONE);
		label9.setText("0 - 0.25 m/s");
		label9.setLayoutData(gridData19);
		Label filler4 = new Label(group2, SWT.NONE);
		sVel2 = new Scale(group2, SWT.NONE);
		sVel2.setLayoutData(gridData16);
		sVel2.setMaximum(250);
		sVel2.setPageIncrement(25);
		sVel2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sVel2.getSelection();
				float ff = (float)(x/1000.0);
				tVel2.setText(""+ff);
				setModVel(2,ff);
			}
		});
		tVel2 = new Text(group2, SWT.BORDER | SWT.RIGHT);
		tVel2.setLayoutData(gridData22);
		bVel2 = new Button(group2, SWT.NONE);
		bVel2.setText("Setzen");
		bVel2.setLayoutData(gridData23);
        bVel2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tVel2.getText(),(float)0.25);
        		if(x==Float.MAX_VALUE) return;
        		sVel2.setSelection((int)(x*1000));
        		setModVel(2,x);
        	}
        });
		label11 = new Label(group2, SWT.NONE);
		label11.setText("Horizontale Linearachse");
		label11.setLayoutData(gridData28);
		label11.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label12 = new Label(group2, SWT.NONE);
		label12.setText("0 - 0.25 m/s");
		label12.setLayoutData(gridData20);
		Label filler7 = new Label(group2, SWT.NONE);
		sVel3 = new Scale(group2, SWT.NONE);
		sVel3.setLayoutData(gridData17);
		sVel3.setMaximum(250);
		sVel3.setPageIncrement(25);
		sVel3.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sVel3.getSelection();
				float ff = (float)(x/1000.0);
				tVel3.setText(""+ff);
				setModVel(3,ff);
			}
		});
		tVel3 = new Text(group2, SWT.BORDER | SWT.RIGHT);
		bVel3 = new Button(group2, SWT.NONE);
		bVel3.setText("Setzen");
        bVel3.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tVel3.getText(),(float)0.25);
        		if(x==Float.MAX_VALUE) return;
        		sVel3.setSelection((int)(x*1000));
        		setModVel(3,x);
        	}
        });
		label13 = new Label(group2, SWT.NONE);
		label13.setText("Rotationsachse");
		label13.setLayoutData(gridData29);
		label13.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label14 = new Label(group2, SWT.NONE);
		label14.setText("0 - 3,77 rad/s");
		label14.setLayoutData(gridData21);
		Label filler8 = new Label(group2, SWT.NONE);
		sVel4 = new Scale(group2, SWT.NONE);
		sVel4.setLayoutData(gridData18);
		sVel4.setMaximum(377);
		sVel4.setPageIncrement(38);
		sVel4.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sVel4.getSelection();
				float ff = (float)(x/100.0);
				tVel4.setText(""+ff);
				setModVel(4,ff);
			}
		});
		tVel4 = new Text(group2, SWT.BORDER | SWT.RIGHT);
		bVel4 = new Button(group2, SWT.NONE);
		bVel4.setText("Setzen");
        bVel4.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tVel4.getText(),(float)3.77);
        		if(x==Float.MAX_VALUE) return;
        		sVel4.setSelection((int)(x*100));
        		setModVel(4,x);
        	}
        });
	}

	/**
	 * Erzeugt die GroupBox für Modulbeschleunigungen, alle wichtigen darin
	 * enthaltenen Komponenten und ordnet diese an	
	 */
	private void createGroup3() {
		GridData gridData38 = new GridData();
		gridData38.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData38.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData37 = new GridData();
		gridData37.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData37.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData36 = new GridData();
		gridData36.horizontalAlignment = org.eclipse.swt.layout.GridData.CENTER;
		gridData36.verticalAlignment = org.eclipse.swt.layout.GridData.END;
		GridData gridData35 = new GridData();
		gridData35.grabExcessVerticalSpace = true;
		GridData gridData34 = new GridData();
		gridData34.grabExcessVerticalSpace = true;
		GridData gridData33 = new GridData();
		gridData33.grabExcessVerticalSpace = true;
		GridData gridData32 = new GridData();
		gridData32.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData31 = new GridData();
		gridData31.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridData gridData30 = new GridData();
		gridData30.grabExcessHorizontalSpace = true;
		gridData30.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		GridLayout gridLayout3 = new GridLayout();
		gridLayout3.numColumns = 3;
		GridData gridData3 = new GridData();
		gridData3.verticalAlignment = org.eclipse.swt.layout.GridData.FILL;
		gridData3.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
		group3 = new Group(this, SWT.NONE);
		group3.setLayoutData(gridData3);
		group3.setLayout(gridLayout3);
		group3.setText("Modulbeschleunigungen");
		label6 = new Label(group3, SWT.NONE);
		label6.setText("Vertikale Linearachse");
		label6.setLayoutData(gridData33);
		label6.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label7 = new Label(group3, SWT.NONE);
		label7.setText("0 - 1.0 m/s²");
		label7.setLayoutData(gridData36);
		Label filler = new Label(group3, SWT.NONE);
		sAcc2 = new Scale(group3, SWT.NONE);
		sAcc2.setLayoutData(gridData30);
		sAcc2.setMaximum(100);
		sAcc2.setPageIncrement(10);
		sAcc2.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sAcc2.getSelection();
				float ff = (float)(x/100.0);
				tAcc2.setText(""+ff);
				setModAcc(2,ff);
			}
		});
		tAcc2 = new Text(group3, SWT.BORDER | SWT.RIGHT);
		bAcc2 = new Button(group3, SWT.NONE);
		bAcc2.setText("Setzen");
        bAcc2.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tAcc2.getText(),(float)1.0);
        		if(x==Float.MAX_VALUE) return;
        		sAcc2.setSelection((int)(x*100));
        		setModAcc(2,x);
        	}
        });
		label10 = new Label(group3, SWT.NONE);
		label10.setText("Horizontale Linearachse");
		label10.setLayoutData(gridData34);
		label10.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label15 = new Label(group3, SWT.NONE);
		label15.setText("0 - 1.0 m/s²");
		label15.setLayoutData(gridData37);
		Label filler1 = new Label(group3, SWT.NONE);
		sAcc3 = new Scale(group3, SWT.NONE);
		sAcc3.setLayoutData(gridData31);
		sAcc3.setMaximum(100);
		sAcc3.setPageIncrement(10);
		sAcc3.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sAcc3.getSelection();
				float ff = (float)(x/100.0);
				tAcc3.setText(""+ff);
				setModAcc(3,ff);
			}
		});
		tAcc3 = new Text(group3, SWT.BORDER | SWT.RIGHT);
		bAcc3 = new Button(group3, SWT.NONE);
		bAcc3.setText("Setzen");
        bAcc3.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tAcc3.getText(),(float)1.0);
        		if(x==Float.MAX_VALUE) return;
        		sAcc3.setSelection((int)(x*100));
        		setModAcc(3,x);
        	}
        });
		label16 = new Label(group3, SWT.NONE);
		label16.setText("Rotationsachse");
		label16.setLayoutData(gridData35);
		label16.setFont(new Font(Display.getDefault(), "Tahoma", 8, SWT.BOLD));
		label17 = new Label(group3, SWT.NONE);
		label17.setText("0 - 15.0 rad/s²");
		label17.setLayoutData(gridData38);
		Label filler2 = new Label(group3, SWT.NONE);
		sAcc4 = new Scale(group3, SWT.NONE);
		sAcc4.setLayoutData(gridData32);
		sAcc4.setMaximum(150);
		sAcc4.setPageIncrement(15);
		sAcc4.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				int x = sAcc4.getSelection();
				float ff = (float)(x/10.0);
				tAcc4.setText(""+ff);
				setModAcc(4,ff);
			}
		});
		tAcc4 = new Text(group3, SWT.BORDER | SWT.RIGHT);
		bAcc4 = new Button(group3, SWT.NONE);
		bAcc4.setText("Setzen");
        bAcc4.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
        	public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {  
        		float x = checkText(tAcc4.getText(),(float)15.0);
        		if(x==Float.MAX_VALUE) return;
        		sAcc4.setSelection((int)(x*10));
        		setModAcc(4,x);
        	}
        });
	}
	
	/* ##########################################################################
	 * 						EREIGNISSE
	 * ##########################################################################*/

	/**
	 * Wird vom Hauptfenster aufgerufen, wenn gerade
	 * eine Verbindung aufgebaut wurde.
	 */
	public void connectionEstablished(){
		setEnableState(true);
		//System.out.println("Cmd (Manual) Established");
	}
	
	/**
	 * Wird vom Hauptfenster aufgerufen, wenn die Verbindung
	 * gerade unterbrochen wurde.
	 */
	public void connectionDisrupted(){
		setEnableState(false);
		//System.out.println("Cmd (Manual) Disrupted");
	}

	/* ##########################################################################
	 * 						ÖFFENTLICHE METHODEN
	 * ##########################################################################*/	
	
	/**
	 * Setzt die Roboterposition, an denen er sich tatsächlich befindet
	 * @param x X-Koordinate der Position
	 * @param y Y-Koordinate der Position
	 * @param z Z-Koordinate der Position
	 */
	public void setPosition(float x, float y, float z){
		// Werte hineinschreiben
		tPosX.setText(""+x);
		tPosY.setText(""+y);
		tPosZ.setText(""+z);
		tPosZb.setText(tPosZ.getText());
		// Umrechnungen
		tPosRad.setText(""+Math.round(Math.sqrt(x*x+y*y)));
		tPosAlpha.setText(""+Math.round(posToGrad(Math.tan(y/x))));
	}
	
	/* ##########################################################################
	 * 						HILFSMETHODEN
	 * ##########################################################################*/

	/**
	 * Initialisiert die Textbox-Werte
	 */
	private void initValues(){
		String z = "0.0";
		tPos2.setText(z);		tPos3.setText(z);		tPos4.setText(z);
		tVel2.setText(z);		tVel3.setText(z);		tVel4.setText(z);
		tAcc2.setText(z);		tAcc3.setText(z);		tAcc4.setText(z);
		tPosX.setText(z);		tPosY.setText(z);		tPosZ.setText(z);
		tPosRad.setText(z);		tPosAlpha.setText(z);	tPosZb.setText(z);
		tMotionSpeed.setText("100");
	}	
	
	/**
	 * Veranlasst eine Roboterbewegung
	 * @param x 1. Koordinate
	 * @param y 2. Koordinate
	 * @param z 3. Koordinate
	 * @char s 'ka' für kartesisch oder 'sa' für spärisch
	 */
	private void movePosition(float x, float y, float z, String s){
		netHandler.getComQueue().enqueue(new Command(NetPack.MOVEPOS, 0, 0, x, y, z, s));	
	}
	
	/**
	 * Veranlasst eine Referenzfahrt
	 * @param m Nummer des Modules
	 */
	private void homeModule(int m){
		netHandler.getComQueue().enqueue(new Command(NetPack.HOME, m, 0));	
	}
	
	/**
	 * Setzt die gesamte Bewegungsgeschwindigkeit in Prozent
	 * @param m Prozentangabe der Gesamtgeschwindigkeit
	 */
	private void setMotionSpeed(float m){
		netHandler.getComQueue().enqueue(new Command(NetPack.MOTION_PERC, 0, m));			
	}
	
	/**
	 * Setzt die Bewegungsparameter laut CheckBoxen auf im Bereich Roboter
	 */
	private void updateMotionParam(){
		int a = 0;
		if(chLinear.getSelection()) a+= NetPack.PARAM_LIN;
		if(!chIntell.getSelection()) a+= NetPack.PARAM_DIRECT;
		netHandler.getComQueue().enqueue(new Command(NetPack.MOTION_PARAM, a, 0));
	}
	
	/**
	 * Veranlasst eine Modulbewegung
	 * @param mod das Modul (2, 3, 4)
	 * @param pos Position in m oder rad
	 */
	private void moveModule(int mod, float pos){
		netHandler.getComQueue().enqueue(new Command(NetPack.MOVEMOD, mod, pos));
	}
	
	/**
	 * Setzt eine Modulgeschwindigkeit
	 * @param mod das Modul (2, 3, 4)
	 * @param vel Geschwindigkeit in m/s oder rad/s
	 */
	private void setModVel(int mod, float vel){
		netHandler.getComQueue().enqueue(new Command(NetPack.RAMPVEL, mod, vel));
	}
	
	/**
	 * Setzt eine Modulbeschleunigung
	 * @param mod das Modul (2, 3, 4)
	 * @param acc Beschleunigung in m/s2 oder rad/s2
	 */
	private void setModAcc(int mod, float acc){
		netHandler.getComQueue().enqueue(new Command(NetPack.RAMPACC, mod, acc));
	}
	
	/**
	 * Versucht einen String (z.B. aus Textbox-Eingabe)
	 * in eine Float-Zahl umzuwandeln. Außerdem wird geprüft, ob
	 * die Zahl zwischen den angegebenen Grenzen liegt.
	 * Bei Erfolg wird die Zahl zurückgegeben, ansonsten wird
	 * eine Fehlermeldung angezeigt und Float.MAX_VALUE zurückgegeben.
	 * @param str Text der in Float Zahl umgewandelt werden soll
	 * @param max erlaubter Maximalwert
	 * @param min erlaubter Minimalwert
	 * @return wenn zwischen min u. max, vorher als String übergebener Wert als Floatzahl, sonst Float.MAX_VALUE
	 */
	private float checkText(String str, float max, float min){
		try{
			float x = Float.parseFloat(str);
			if(x>=min && x<=max){
				return x;
			}else{
				MsgBox.showError("Bitte Zahl zwischen " + min + " und " +
						max + " eingeben!");
				return Float.MAX_VALUE;
			} 
		}catch(Exception e){
			MsgBox.showError("Bitte gültige Zahl eingeben!");
			return Float.MAX_VALUE;
		}
	}
	
	/**
	 * Arbeitet genau gleich wie checkText(), nur mit min = 0.
	 * @param str Text der in Float Zahl umgewandelt werden soll
	 * @param max erlaubter Maximalwert
	 * @return wenn zwischen min u. max, vorher als String übergebener Wert als Floatzahl, sonst Float.MAX_VALUE
	 */
	private float checkText(String str, float max){
		return checkText(str,max,0);
	}
	
	/**
	 * Umrechnung von Millimeter in Meter
	 * @param millimeter Wert in Millimeter, der in Meter umgewandelt werden soll
	 * @return übergebenen Millimeterwert in der Einheit Meter
	 */
	private float posToMeter(float millimeter){
		return millimeter/1000;
	}

	/**
	 * Umrechung von Grad in Radiant
	 * @param grad Wert in Grad, der in Radiant umgewandelt werden soll
	 * @return übergebenen Gradwert in Radiant
	 */
	private float posToRad(float grad){
		return grad*(float)Math.PI/180;
	}
	
	/**
	 * Umrechung von Radiant in Grad
	 * @param rad Wert in Radiant, der in Grad umgewandelt werden soll
	 * @return übergebenen Radwert in Grad
	 */
	private double posToGrad(double rad){
		return rad/Math.PI*180;
	}
	
	/**
	 * Schaltet alle Widgets ein oder aus
	 * @param e gibt an ob die Komponeten enabled true od. false sind
	 */
	private void setEnableState(boolean e){
		// Modulpositionen
		bHome2.setEnabled(e); sPos2.setEnabled(e); tPos2.setEnabled(e); bPos2.setEnabled(e);
		bHome3.setEnabled(e); sPos3.setEnabled(e); tPos3.setEnabled(e); bPos3.setEnabled(e);
		bHome4.setEnabled(e); sPos4.setEnabled(e); tPos4.setEnabled(e); bPos4.setEnabled(e);
		// Geschwindigkeiten
		sVel2.setEnabled(e); tVel2.setEnabled(e); bVel2.setEnabled(e);
		sVel3.setEnabled(e); tVel3.setEnabled(e); bVel3.setEnabled(e);
		sVel4.setEnabled(e); tVel4.setEnabled(e); bVel4.setEnabled(e);
		// Beschleunigungen
		sAcc2.setEnabled(e); tAcc2.setEnabled(e); bAcc2.setEnabled(e);
		sAcc3.setEnabled(e); tAcc3.setEnabled(e); bAcc3.setEnabled(e);
		sAcc4.setEnabled(e); tAcc4.setEnabled(e); bAcc4.setEnabled(e);
		// Roboter
		tPosX.setEnabled(e); tPosY.setEnabled(e); tPosZ.setEnabled(e); bPos.setEnabled(e);
		tPosRad.setEnabled(e); tPosAlpha.setEnabled(e); tPosZb.setEnabled(e);
		bPosb.setEnabled(e);
		tMotionSpeed.setEnabled(e); bMotionSpeed.setEnabled(e);
		chLinear.setEnabled(e); chIntell.setEnabled(e);
		bGripOpen.setEnabled(e); bGripClose.setEnabled(e);
	}
	
}
