/* Auteur: Nicolas JANEY, nico@univ-fcomte.fr */

import java.net.* ;
import java.awt.* ;
import java.io.* ;
import java.awt.event.*;

public class ServeurBrasRobot implements MouseListener,MouseMotionListener {

  private static Frame f ;
  private static Scrollbar sb1 ;
  private static Scrollbar sb2 ;
  private static Scrollbar sb3 ;
  private static EtatScrollBar esb1 ;
  private static EtatScrollBar esb2 ;
  private static EtatScrollBar esb3 ;
  public int a1 ;
  public int a2 ;
  public int a3 ;

  public static void main(String [] args) {
    ServeurBrasRobot se6 = new ServeurBrasRobot() ;
    }

  public ServeurBrasRobot() {
    ServerSocket srv ;
    int port = 5556 ;
    try {
      srv = new ServerSocket(port) ;
      System.out.println("Serveur Ok") ;
      ServeurRobot s6 = new ServeurRobot(srv,this) ;
      s6.start() ; }
    catch(IOException e) { }
    f = new Frame("Serveur BrasRobot") ;
    f.setSize(200,200) ;
    Panel c = new Panel();
    c.setLayout(new GridLayout(1,3,2,0));
    sb1 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
    c.add(sb1);
    sb2 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
    c.add(sb2);
    sb3 = new Scrollbar(Scrollbar.VERTICAL,0,1,0,255);
    c.add(sb3);
    esb1 = new EtatScrollBar(sb1,0) ;
    sb1.addMouseListener(this);
    sb1.addMouseMotionListener(this);
    esb2 = new EtatScrollBar(sb2,0) ;
    sb2.addMouseListener(this);
    sb2.addMouseMotionListener(this);
    esb3 = new EtatScrollBar(sb3,0) ;
    sb3.addMouseListener(this);
    sb3.addMouseMotionListener(this);

    f.setLayout(new BorderLayout());
    f.add("East",c);
    f.show() ;
    while ( true ) {
      try {
        Thread.sleep(100) ; }
      catch (Exception e) { } }
    }

  private void miseAJour(int a,int b,int c) {
    a1 = a ;
	a2 = b ;
	a3 = c ;
    }

  public void mouseClicked(MouseEvent e ) {
    }

  public void mouseReleased(MouseEvent e ) {
    }

  private void traitementScrollBar(EtatScrollBar esb,MouseEvent e) {
    Scrollbar sb = esb.getScrollbar() ;
    if ( e.getSource() == sb ) {
      int y ;
      int h ;
      if ( sb.getOrientation() == Scrollbar.VERTICAL) {
         y = e.getY() ;
         h =(int) sb.getSize().getHeight() ; }
         else {
         y = e.getX() ;
         h =(int) sb.getSize().getWidth() ; }
      int pos = -1 ;
      if ( y < 16 ) {
        pos = ( sb.getValue() - 1 < sb.getMinimum() ) ? sb.getMinimum() : sb.getValue()-1 ; }
        else
        if ( y > h-16 ) {
          pos = ( sb.getValue() + 1 > sb.getMaximum()-1 ) ? sb.getMaximum()-1 : sb.getValue()+1 ;}
          else {
          int ymin = 16-3+(h-32)*sb.getValue()/255 ; 
          int ymax = ymin+6 ;
          if ( y < ymin )
            pos = ( sb.getValue() - sb.getBlockIncrement() < sb.getMinimum() ) ? sb.getMinimum() : (sb.getValue()-sb.getBlockIncrement()) ;
            else
            if ( y > ymax ) {
              pos = ( sb.getValue() + sb.getBlockIncrement() > sb.getMaximum()-1 ) ? sb.getMaximum()-1 : (sb.getValue()+sb.getBlockIncrement()) ; } }
      if ( pos != -1 ) {
        if ( sb == sb1 )
          miseAJour(pos,sb2.getValue(),sb3.getValue()) ;
        if ( sb == sb2 )
          miseAJour(sb1.getValue(),pos,sb3.getValue()) ;
        if ( sb == sb3 )
          miseAJour(sb1.getValue(),sb2.getValue(),pos) ; }
        else
        esb.setEtat(1) ; }
    }

  public void mousePressed(MouseEvent e ) {
    traitementScrollBar(esb1,e) ;
    traitementScrollBar(esb2,e) ;
    traitementScrollBar(esb3,e) ;
    }

  public void mouseDragged(MouseEvent e ) {
    }

  private void traitement2ScrollBar(EtatScrollBar esb,MouseEvent e) {
    if ( ( e.getSource() == sb1 ) || ( e.getSource() == sb2 ) || ( e.getSource() == sb3 ) )
      if ( esb.getEtat() == 1 ) {
        esb.setEtat(0) ;
        miseAJour(sb1.getValue(),sb2.getValue(),sb3.getValue()) ; }
    }

  public void mouseMoved(MouseEvent e ) {
    traitement2ScrollBar(esb1,e) ;
    traitement2ScrollBar(esb2,e) ;
    traitement2ScrollBar(esb3,e) ;
    }

  public void mouseEntered(MouseEvent e ) {
    }

  public void mouseExited(MouseEvent e ) {
    }
  }
