/******************************************************************************
    Mergesort observer
----------------------------------------------------
Copyright (c) Gunnar Gotshalks. All Rights Reserved.

Permission to use, copy, modify, and distribute this software
and its documentation for NON-COMMERCIAL purposes and
without fee is hereby granted. 
*******************************************************************************/

 package FlexOr.searchAndSort;

import java.awt.*;
import java.util.*;
import java.awt.Color;

/** A merge observer implements a canvas to show the viewer what it is
observing. */

@SuppressWarnings("serial")
public class MergeObserver extends SortObserver implements Observer {

public Integer[] mergeSpace;
  boolean transfer = false;
  MergeObsData mod;
  int xshift = MaxWidth + 5;
  
  public MergeObserver(Object[] array, Integer[] mergeSpace,
                      Observable observable, String sortName) {
    super(array, observable, sortName);
    this.mergeSpace = mergeSpace;
  }
  
  public void change(int yshift, int height, int scale, int MaxItems,
                     int sleepTime) {
    super.change(yshift, height, scale, MaxItems, sleepTime);
    xshift = MaxWidth + 5;
  }

  public void update(Graphics g) {
    int i;    
        
    if (firstTime) clearCanvas(g);
    drawLegend(g);
    
    for (i = 0 ; i < array.length ; i++) {
      g.setColor(Color.white);
      g.fillRect(x, y+i*yshift, MaxWidth, height);
      g.fillRect(x+xshift, y+i*yshift, MaxWidth, height);

      if (mod != null) g.setColor(mod.tag[i]);
      else g.setColor(Color.gray);
      if (array[i] != null) {
        g.fillRect(x, y+i*yshift, array[i].intValue()*scale, height);
      }
      
      if (mod != null) g.setColor(mod.msTag[i]);
      else g.setColor(Color.gray);
      g.fillRect(x+xshift, y+i*yshift, mergeSpace[i].intValue()*scale, height);
    }

    g.setColor(Color.white);
    for ( ; i < MaxItems ; i++) {
      g.fillRect(x, y+i*yshift, MaxWidth, height);
      g. fillRect(x+xshift, y+i*yshift, MaxWidth, height);
    }
  }
    
  public void update(Observable observed, Object paramSod) {
    if ( paramSod instanceof MergeObsData) {  // Watch for end of observation.
        mod = (MergeObsData) paramSod;
    }
    super.update(observed, paramSod);
  }
  
  public void newArray() { mod = null; firstTime = true; repaint(); }
}

