package ckelling.baukasten;

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.*;
import java.util.*;
import java.net.*;
/**
 *  Mein Aufbau
 *  aus Komponenten des Rechnerbaukastens
 *  von Carsten Kelling
 *
 *  0.1.0  erste Testversion
 *
 *  @author     
 *  @version    0.1.0
 */
public class MeinAufbau extends Rechner
{
    ///// Konstanten Anfang /////

    final static String VERSIONSTRING = "Mein Aufbau 0.1.0";

    ///// Konstanten Ende /////


private   Register16         Register16_1;

private   ALU                ALU_1;

//noPip private   PipelineRegister   PipelineRegister_1;

//noPip private   PipelineStageLabel PipelineStageLabel_1;

private ScrollControl scrollControl;


    public String getAppletInfo()
    {
        return "Mein Aufbau aus Komponenten des Rechner-Baukastens von Carsten Kelling";
    }

    public String getVersionString()
    {
        return VERSIONSTRING;
    }


    public void init()
    {
        System.out.println(VERSIONSTRING);

        initialize(new Dimension(400, 300), new Dimension(400, 300)); // definiert in Klasse Rechner



        String[] arrayLabel = null;
        int coordX, coordY, dimWidth, dimHeight;
        coordX = coordY = dimWidth = dimHeight = 0;
        int c1, c2, c3, c4, c5;
        c1 = c2 = c3 = c4 = c5 = 0;



// Register16: Register16_1
Register16_1 = new Register16("My Register16", 20, 40, 50, 28, "left", (Rechner) this);
add(Register16_1); Register16_1.show();
Register16_1.setEditable(true);

// ALU: ALU_1
ALU_1 = new ALU("My ALU", 20, 80, 144, 80, "left", "v", (Rechner) this);

// PipelineRegister: PipelineRegister_1
//noPip PipelineRegister_1 = new PipelineRegister("My PipelineRegister", 150, 40, 30, 200, "leftTop", (Rechner) this);

// PipelineStageLabel: PipelineStageLabel_1
//noPip PipelineStageLabel_1 = new PipelineStageLabel("My PipelineStageLabel", "", 20, 270, 200, 40, "left", (Rechner) this);


        scrollControl = new ScrollControl((Rechner) this);
        scrollControl.setTitle("Rechner steuern");

        show();
        scrollControl.show();

        versionStringX = WIDTH - stringWidth(DIALOGFONT, VERSIONSTRING) - 5;
        versionStringY = DIALOGFONTHEIGHT;

        paint(onScreenGC);
    } /* end init */

    public synchronized void paint (Graphics onScreenGC)
    {
        Rectangle size = bounds();

        // Flaeche des Applets loeschen
        offScreenGC.setColor (BACKGROUND);
        offScreenGC.fillRect(0, 0, size.width, size.height);

        // Komponenten neu zeichnen
        Register16_1 .paint(offScreenGC);

        ALU_1        .paint(offScreenGC);

        //noPip PipelineRegister_1 .paint(offScreenGC);

        //noPip PipelineStageLabel_1 .paint(offScreenGC);


        offScreenGC.setColor(Color.black);
        offScreenGC.setFont(DIALOGFONT);

        offScreenGC.drawString(VERSIONSTRING, versionStringX, versionStringY);

        onScreenGC.drawImage(offScreenImage, 0, 0, this);
        
    } /* end paint */


    public synchronized void paintActivated (Graphics onScreenGC)
    {
        Register16_1 .paintActivated(offScreenGC);

        ALU_1        .paintActivated(offScreenGC);

        //noPip PipelineRegister_1 .paintActivated(offScreenGC);

        //noPip PipelineStageLabel_1 .paintActivated(offScreenGC);


        onScreenGC.drawImage(offScreenImage, 0, 0, this);
    } /* end paintActivated */


    public void stop()
    {
        super.stop();  // Alle Threads anhalten und beseitigen

        if (scrollControl != null)
            scrollControl.hide();
    }

    public void start()
    {
        super.start();  // Alle Threads erzeugen und starten

        Rectangle bounds = bounds();
        if (scrollControl != null)
        {
            Rectangle bbounds = scrollControl.bounds();
            int x = Math.min(bounds.x + bounds.width, screenSize.width - bbounds.width);
            scrollControl.move (x, bounds.y);
            scrollControl.show();
        }
    }


    public void deactivateAll()
    {
        if (! activationLocked)
        {
            Register16_1 .deactivate();

            ALU_1        .deactivate();

            //noPip PipelineRegister_1 .deactivate();

            //noPip PipelineStageLabel_1 .deactivate();


        }
    } /* end deactivateAll */


    public synchronized void scrollAll()
    {
        if (! activationLocked)
        {


            onScreenGC.drawImage(offScreenImage, 0, 0, this);
        }
    } /* end scrollAll */


    public void blinkAll()
    {
        blinker = !blinker;

        Register16_1 .blink(blinker);

        ALU_1        .blink(blinker);

        //noPip PipelineRegister_1 .blink(blinker);

        //noPip PipelineStageLabel_1 .blink(blinker);


    } /* end blinkAll */


	/**
	 *  Alle Komponenten, die zwischen ihrem eigentlichem Wert (value)
	 *  und dem gerade sichtbaren Wert (displayValue) unterscheiden,
	 *  führen ein "displayValue = value" durch.
	 */
    public void updateAll()
    {
        Register16_1 .update();

        ALU_1        .update();

    } /* end updateAll */


    public void lockAll()
    // Keine Komponente reagiert mehr auf activate(),
    // activateCompared() oder deactivate().
    {
        activationLocked = true;

        Register16_1 .lock();

        ALU_1        .lock();

        //noPip PipelineRegister_1 .lock();

        //noPip PipelineStageLabel_1 .lock();

    } /* end lockAll */


    public void unlockAll()
    // Alle Komponenten reagieren wieder auf activate(),
    // activateCompared() und deactivate().
    {
        activationLocked = false;
        Register16_1 .unlock();

        ALU_1        .unlock();

        //noPip PipelineRegister_1 .unlock();

        //noPip PipelineStageLabel_1 .unlock();

    } /* end unlockAll */


    public synchronized void invalidateAllImageCaches()
    {
    } /* end invalidateAllImageCaches */


    public void showAllOpcodes(boolean b)
    {
        showOpcodes = b;

        paint(onScreenGC);
    }


    public synchronized void timerWokeUp(String title)
    {
        if (title.equalsIgnoreCase("scrolltimer"))
        {
            scrollAll();
        }
        else if (title.equalsIgnoreCase("blinktimer"))
        {
            blinkAll();
            onScreenGC.drawImage(offScreenImage, 0, 0, this);
        }

    } /* end timerWokeUp */


	public void showInfoTip(Point p) {}
    public void stopSimulation() {}
    public void showWindow(String name) {}
    public void hideWindow(String name) {}
    public void initCacheComponents(int cSize, int tSize, int assoc) {}
    public void removeCacheRessources() {}

    public void demonstrate()
    {
        demonstrate(true);
    }
    public void demonstrate(boolean doRepaint) {}
    public void demonstrateBack() {}
    public void bufferComputer() {}
    public void setComputer() {}

    public void singleInstruction(boolean doRepaint) {}
    public void singleInstructionBack(boolean doRepaint) {}
    public void goUntilBreakpoint() {}

} /* end MeinAufbau */

