package Ui ;

import java.awt.*;

import java.applet.*;

import java.util.*;

import java.awt.event.* ;

import Maths.*;




public class Additional_Functionality extends Object  implements java.io.Serializable{

	/* this class regulates which the additional functionality is offered by
	   creation of a new UI window which is an instantiation of the SupportDialog class */


	int count = 0;

	//the list of generationmethods that offer an additional functionality,
	// characterisation is based on their ordering number in the curvepanel class!!
	int INTERP = 0;

	int BEZIER = 1;

	int BSPLINE = 2;

	int NURBS = 3;

	SupportDialog fr;//the new window and UI

	CurvePanel panel;


        public Additional_Functionality (CurvePanel panel){

		this.panel = panel;

	}



	public void AddFuncSupport(int methodnr){
	    /* this method is the link to the UI, and creates a new add-funct UI window

	       input: the IDnumber of the generationmethod the additional_functionality of which
	       	      we will support

	       result: a new window with the UI to the add-funct
	Last change:  TMC  20 Jan 98    2:21 pm
		      */


	    if(count < 1){//the count variable ensures a semaphore, so no multiple add-func interfaces may appear
			  //on screen, should the UI interpret repeated clicking as multiple demands


		//depending on the generationmethod active in the UI, a different kind of add-func support-UI is created
		if ( methodnr == INTERP ){

			fr = new SupportDialog(CurveWindow.getFrame(panel),panel.language.cb_add_func_label,panel);

			fr.setBounds(0,0,350,150);

			InterpSupport interpcomp = new InterpSupport(panel);

			fr.add(interpcomp);

			fr.setVisible(true);

		}

		if ( methodnr == BEZIER ){

			fr = new SupportDialog(CurveWindow.getFrame(panel),panel.language.cb_add_func_label,panel);

			fr.setBounds(0,0,350,150);

			BezierSupport beziercomp = new BezierSupport(panel);

			fr.add(beziercomp);

			fr.setVisible(true);

		}

		if ( methodnr == BSPLINE ){

			fr = new SupportDialog(CurveWindow.getFrame(panel),panel.language.cb_add_func_label,panel);

			fr.setBounds(0,0,350,150);

			BSplineSupport bsplinecomp = new BSplineSupport(panel);

			fr.add(bsplinecomp);

			fr.setVisible(true);

		}

		if ( methodnr == NURBS ){

			fr = new SupportDialog(CurveWindow.getFrame(panel),panel.language.cb_add_func_label,panel);

			fr.setBounds(0,0,350,150);

			NURBSSupport nurbscomp = new NURBSSupport(panel);

			fr.add(nurbscomp);

			fr.setVisible(true);

		}
        	count = 1;

	     }

    	}


	public void RemoveFuncSupport(){

	 try{
       			count =0;

			fr.dispose();
       	 }

	 catch (Exception e){}

    	}


//the following three methods are invoked by the UI, when in additional_functionality mode. The UI
//conveys them infomation about the mouse, which can be used by the additional functionality module
//in current supported additional functionality, there is no need for this information

	public void MousePressed( int methodnr, int x, int y){


        }



	public void MouseReleased( int methodnr, int x, int y){


        }


	public void MouseDragged( int methodnr, int x, int y){


        }



class DivisionScrollbarPanel extends Ui.ScrollbarPanel{
    /* a tailored UI component */

    public double value = 0.0;


    public DivisionScrollbarPanel(CurvePanel target, int orient, int value, int visible, int min, int max,String ttel){

	super(target, orient, value,  visible, min, max, ttel);

    }


    public void paint(Graphics g) {

    	  int temp = ((scroll)).getValue();

  	  value = ((double)temp)/100;

          titel.setText(ttel+ String.valueOf(value)+"  ");

    }



    public void adjustmentValueChanged(AdjustmentEvent e) {

    	int temp = ((Scrollbar)(e.getAdjustable())).getValue();

	value = ((double)temp)/100;

        titel.setText(ttel+ String.valueOf(value)+"  ");

    }


    public double GiveValue(){

	return value;

    }


}


class BezierSupport extends Panel implements ActionListener{
	/* the class that presents the additional functionality offered by the Bezier class
	   =>supported: Subdivision

	*/

	DivisionScrollbarPanel divpanel;

	CurvePanel panel;

	BezierSupport (CurvePanel panel){

		this.panel = panel;

		setLayout(new BorderLayout());

		Label label = new Label(panel.language.subdivision_label);

		label.setBackground(Color.white);

		add("North",label);

		divpanel = new DivisionScrollbarPanel(panel,Scrollbar.HORIZONTAL,50,1,1,99,panel.language.value_label);

		add("Center",divpanel);

		Button button = new Button(panel.language.subdivide_label);

		button.addActionListener(this);

		add("South",button);

		enableEvents(AWTEvent.KEY_EVENT_MASK);

        }


	public void actionPerformed(ActionEvent e){


		if (panel.cur_curve > -1 ){

			Curve tempcurve = (Curve)(panel.giveCurveslist()).elementAt(panel.cur_curve);

			Bezier bezier = (Bezier)tempcurve.polynom;

			double cntrlp[][] = bezier.GiveControlPoints();

			double left[][] = new double[bezier.dimension][cntrlp[0].length];

			double right[][] = new double[bezier.dimension][cntrlp[0].length];

			//Subdivide the curve at the user-inputed divpanel.value
			(bezier).Subdivision(divpanel.value,cntrlp,left,right);

			//the new curves
			Curve newcurve = tempcurve.Clone();

			Curve newcurveb = tempcurve.Clone();

			//initiating the new curves
			newcurve.VectoriseControlPoints(left);

			newcurve.CalculateDrawPoints();

			newcurve.curvesegmentpoints = (newcurve.polynom).CalculateCurvesegments();//calculating the new curvesegments


			newcurveb.VectoriseControlPoints(right);

			newcurveb.CalculateDrawPoints();

			newcurveb.curvesegmentpoints = (newcurveb.polynom).CalculateCurvesegments();//calculating the new curvesegments

			//removing the old curve from the curveslist in the UI
			(panel.curveslist).removeElementAt(panel.cur_curve);

			//adding the new curves
			(panel.curveslist).addElement(newcurve);

			(panel.curveslist).addElement(newcurveb);

			panel.cur_curve = panel.curveslist.size()-1;

			//updating the view
			(Curvegeneration.cp).repaint();

			if ( ((Curvegeneration.pp).dial).isVisible() ) ((Curvegeneration.pp).dial).repaint();
	    	}

		else{
                        WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.choose_curve_w);

			d.setBounds(150,150,300,150);

			d.show();
		}

	}


}

class InterpSupport extends Panel implements ActionListener{
	/* the class that presents the additional functionality offered by the Interpolating_Polynom class
	   =>supported: Node Insertion*/

	DivisionScrollbarPanel scrollpanel;

	CurvePanel panel;

	InterpSupport (CurvePanel panel){

		this.panel = panel;

		setLayout(new BorderLayout());

		Label label = new Label(panel.language.langr_inserting_point_label);

		label.setBackground(Color.white);

		add("North",label);

		scrollpanel = new DivisionScrollbarPanel(panel,Scrollbar.HORIZONTAL,50,1,1,99,panel.language.valueproc_label);

		add("Center",scrollpanel);

		Button button = new Button(panel.language.insert_controlpoint_label);

		button.addActionListener(this);

		add("South",button);

		enableEvents(AWTEvent.KEY_EVENT_MASK);

        }

	public void actionPerformed(ActionEvent e){

		Point cur_point = panel.cur_point;

		int cur_curve = panel.cur_curve;

		if (cur_point.x == cur_curve && cur_curve > -1 ){//the user indicated a valid point

		  if ( cur_point.y > 0  ){//we can't insert a node in before the first controlpoint

			Curve tempcurve = (Curve)(panel.giveCurveslist()).elementAt(cur_curve);

			Interpolating_Polynom poly= (Interpolating_Polynom)tempcurve.polynom;

			 //since, for defining the new node,
			 //the user entered a distance relative to a controlpoint, we first calulate
			 //the parametervalue of the new node
			 double h[] = poly.h;

			 double sum = 0.0;

			 for (int i = 0 ;i < cur_point.y-1 ;i++ ){

				sum+=h[i];

			 }


			//we insert the new node
			double newcntrlp[] = poly.InsertNode((sum + h[cur_point.y-1]*(scrollpanel.value)),cur_point.y-1);

			//we also insert the new controlpoints into the VECTOR of controlpoint
			//(the ARRAY has already been taken of by the InsertNode method)
                        ControlPoint point = new ControlPoint((int)newcntrlp[0],(int)newcntrlp[1]);

			tempcurve.controlpoints.insertElementAt(point,cur_point.y);

			//updating the view
			(Curvegeneration.cp).repaint();

			if ( ((Curvegeneration.pp).dial).isVisible() ) ((Curvegeneration.pp).dial).repaint();

		 }

		 else {
                         WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.not_first_point_w);

  			 d.setBounds(150,150,300,150);

			 d.show();
		  }

	    	}

		else{
                        WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.choose_curveandpoint_w);

			d.setBounds(150,150,300,150);

			d.show();
		}

	}


}


class BSplineSupport extends Panel implements ActionListener{
	/* the class that presents the additional functionality offered by the BSpline class
	   =>supported: knot Insertion*/

	DivisionScrollbarPanel scrollpanel;

	CurvePanel panel;

	BSplineSupport (CurvePanel panel){

		this.panel = panel;

		setLayout(new BorderLayout());

		Label label = new Label(panel.language.inserting_point_label);

		label.setBackground(Color.white);

		add("North",label);

		scrollpanel = new DivisionScrollbarPanel(panel,Scrollbar.HORIZONTAL,50,1,0,100,panel.language.valueproc_label);

		add("Center",scrollpanel);

		Button button = new Button(panel.language.insert_controlpoint_label);

		button.addActionListener(this);

		add("South",button);

		enableEvents(AWTEvent.KEY_EVENT_MASK);

        }

	public void actionPerformed(ActionEvent e){

		Point cur_point = panel.cur_point;

		int cur_curve = panel.cur_curve;

		if (cur_point.x == cur_curve && cur_curve > -1 ){

 		  Curve tempcurve = (Curve)(panel.giveCurveslist()).elementAt(cur_curve);

		  if ( cur_point.y == 0 && tempcurve.closed) cur_point.y = ((tempcurve.controlpoints).size())-2;

		  if ( cur_point.y > 0  ){


			BSpline poly= (BSpline)tempcurve.polynom;


			 //since, for defining the new knot,
			 //the user entered a distance relative to a controlpoint, we first calulate
			 //the parametervalue of the new knot
			 double h[] = poly.h;

			 double sum = 0.0;

			 int i = -1;

			 while(i < cur_point.y-2 && i < h.length-2){

				i++;

				sum+=h[i];

			 }


			double value = (sum + h[i+1]*(scrollpanel.value));

			//we insert the new knot
			double newcontrolp[][] = poly.InsertKnot(value);

			if ( tempcurve.closed ) {// we neglect the old periodic controlpoints

			   for(int dim = 0; dim < poly.dimension; dim++){

				newcontrolp[dim][0] = newcontrolp[dim][newcontrolp[0].length-1];

		           }

			}

                        // informing the curve of the new controlpoints
			tempcurve.VectoriseControlPoints(newcontrolp);

			//the periodic controlpoints of a closed curve are recalculated
			if ( tempcurve.closed )   poly.CreateNewControlPoints();

                        //calculating the new drawing points, just to check
                        tempcurve.drawingpoints = poly.GiveDrawPoints();

			//calculating the new curvesegments
			tempcurve.curvesegmentpoints = poly.CalculateCurvesegments();

			//updating the view
			(Curvegeneration.cp).repaint();

			if ( ((Curvegeneration.pp).dial).isVisible() ) ((Curvegeneration.pp).dial).repaint();

		 }

		 else {
                         WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.not_first_point_w);

  			 d.setBounds(150,150,300,150);

			 d.show();
		  }

	    	}

		else{
                        WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.choose_curveandpoint_w);

			d.setBounds(150,150,300,150);

			d.show();
		}

	}



}


class NURBSSupport extends Panel implements ActionListener{
	/* the class that presents the additional functionality offered by the NURBS class
	   =>supported: knot Insertion*/

	DivisionScrollbarPanel scrollpanel;

	CurvePanel panel;

	NURBSSupport (CurvePanel panel){

		this.panel = panel;

		setLayout(new BorderLayout());

		Label label = new Label(panel.language.inserting_point_label);

		label.setBackground(Color.white);

		add("North",label);

		scrollpanel = new DivisionScrollbarPanel(panel,Scrollbar.HORIZONTAL,50,1,0,100,panel.language.valueproc_label);

		add("Center",scrollpanel);

		Button button = new Button(panel.language.insert_controlpoint_label);

		button.addActionListener(this);

		add("South",button);

		enableEvents(AWTEvent.KEY_EVENT_MASK);

        }

	public void actionPerformed(ActionEvent e){

		Point cur_point = panel.cur_point;

		int cur_curve = panel.cur_curve;

		if (cur_point.x == cur_curve && cur_curve > -1 ){

 		  Curve tempcurve = (Curve)(panel.giveCurveslist()).elementAt(cur_curve);

		  if ( cur_point.y == 0 && tempcurve.closed) cur_point.y = ((tempcurve.controlpoints).size())-2;

		  if ( cur_point.y > 0  ){


			NURBS poly= (NURBS)tempcurve.polynom;

			 //since, for defining the new knot,
			 //the user entered a distance relative to a controlpoint, we first calculate
			 //the parametervalue of the new knot
			 double h[] = poly.h;

			 double sum = 0.0;

			 int i = -1;

			 while(i < cur_point.y-2 && i < h.length-2){

				i++;

				sum+=h[i];

			 }


			double value = (sum + h[i+1]*(scrollpanel.value));

			//inserting the new knot
			double newcontrolp[][] = poly.InsertKnot(value);

			if ( tempcurve.closed ) {// we neglect the old periodic controlpoints

			   for(int dim = 0; dim < poly.dimension; dim++){

				newcontrolp[dim][0] = newcontrolp[dim][newcontrolp[0].length-1];

		           }

			}

			// informing the curve of the new controlpoints
			tempcurve.VectoriseControlPoints(newcontrolp);

			if ( tempcurve.closed )   poly.CreateNewControlPoints();

			//calculating the new controlpoints
                        tempcurve.drawingpoints = poly.GiveDrawPoints();

                        //calculating the new curvesegments
			tempcurve.curvesegmentpoints = poly.CalculateCurvesegments();

			//updating the view
			(Curvegeneration.cp).repaint();

			if ( ((Curvegeneration.pp).dial).isVisible() ) ((Curvegeneration.pp).dial).repaint();

		 }

		 else {
                         WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.not_first_point_w);

  			 d.setBounds(150,150,300,150);

			 d.show();
		  }

	    	}

		else{
                        WarningDialog d = new WarningDialog(CurveWindow.getFrame(panel),panel.language.remark, panel.language.choose_curveandpoint_w);

			d.setBounds(150,150,300,150);

			d.show();
		}

	}


}


class SupportDialog extends Dialog {

	SupportDialog( Frame fr, String titel, CurvePanel panel){

		super(fr,titel,false);

        }


}



}




