home *** CD-ROM | disk | FTP | other *** search
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Panel;
- import java.awt.Scrollbar;
-
- class SchematicPanel extends Panel {
- protected Scrollbar myHorizontal;
- protected Scrollbar myVertical;
- DigSim applet;
- static final int GridStep = 8;
- static final int hgs = 4;
- Wire NewWire;
- ElectronicComponent ComponentPressed;
- ElectronicComponent PrevSelectedComponent;
- int OldMouseX;
- int OldMouseY;
- boolean WireDrawing = false;
- boolean JunctionDrawing = false;
- protected Font SchematicFont;
- protected FontMetrics SchematicFontMetrics;
- static final Color BorderColor;
- int GridXOffset;
- int GridYOffset;
- boolean SelectBox = false;
- int SelectBoxX1;
- int SelectBoxY1;
- int SelectBoxX2;
- int SelectBoxY2;
- Schematic SelectSchematic;
- Schematic CopySchematic;
- static final Color JunctionColor;
- boolean AutoJunction = true;
-
- public SchematicPanel(DigSim var1) {
- this.applet = var1;
- this.SelectSchematic = new Schematic();
- ((Container)this).setLayout(new BorderLayout());
- this.SchematicFont = new Font("TimesRoman", 0, 20);
- this.SchematicFontMetrics = ((Component)this).getFontMetrics(this.SchematicFont);
- ((Component)this).setFont(this.SchematicFont);
- super.setLayout(new BorderLayout());
- this.myVertical = new Scrollbar(1);
- ((Container)this).add("East", this.myVertical);
- this.myHorizontal = new Scrollbar(0);
- ((Container)this).add("South", this.myHorizontal);
- ((Component)this).repaint();
- }
-
- public boolean handleEvent(Event var1) {
- if (var1.target instanceof Scrollbar) {
- ((Component)this).repaint();
- return true;
- } else {
- super.handleEvent(var1);
- return false;
- }
- }
-
- public void Cut() {
- this.applet.MySchematic.RemoveSameElements(this.applet.PinGrid, this.SelectSchematic);
- this.CopySchematic = new Schematic(this.SelectSchematic);
- this.SelectSchematic.RemoveAllComponents();
- this.applet.updateAnalyzer();
- ((Component)this).repaint();
- }
-
- public void Copy() {
- this.CopySchematic = new Schematic(this.SelectSchematic);
- }
-
- public void Paste() {
- if (this.CopySchematic != null) {
- this.SelectSchematic.RemoveAllComponents();
- this.SelectSchematic = this.applet.MySchematic.PasteSchematic(this.applet.PinGrid, this.CopySchematic, this.GridXOffset, this.GridYOffset);
- this.applet.frame.CutMenuItem.enable();
- this.applet.frame.CopyMenuItem.enable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.enable();
- }
-
- this.applet.MyControlPanel.EnableButton("Cut");
- this.applet.MyControlPanel.EnableButton("Copy");
- this.applet.updateAnalyzer();
- ((Component)this).repaint();
- }
-
- }
-
- public void CopyTo() {
- this.applet.frame.DoFileSaveDialog(this.SelectSchematic, true, "Copy to");
- }
-
- public void PasteFrom() {
- Schematic var1 = this.applet.frame.DoFileOpenDialog((Pin[][])null, "Paste from");
- if (var1 != null) {
- this.SelectSchematic.RemoveAllComponents();
- this.SelectSchematic = this.applet.MySchematic.PasteSchematic(this.applet.PinGrid, var1, this.GridXOffset, this.GridYOffset);
- this.applet.frame.CutMenuItem.enable();
- this.applet.frame.CopyMenuItem.enable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.enable();
- }
-
- this.applet.MyControlPanel.EnableButton("Cut");
- this.applet.MyControlPanel.EnableButton("Copy");
- this.applet.updateAnalyzer();
- ((Component)this).repaint();
- }
-
- }
-
- public void SelectAll() {
- this.SelectSchematic = new Schematic(this.applet.MySchematic);
- if (this.SelectSchematic.size() > 0) {
- this.SelectSchematic.SetAllComponentsSelected();
- this.applet.frame.CutMenuItem.enable();
- this.applet.frame.CopyMenuItem.enable();
- this.applet.frame.PasteMenuItem.enable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.enable();
- }
-
- this.applet.MyControlPanel.EnableButton("Cut");
- this.applet.MyControlPanel.EnableButton("Copy");
- this.applet.MyControlPanel.EnableButton("Paste");
- } else {
- this.applet.frame.CutMenuItem.disable();
- this.applet.frame.CopyMenuItem.disable();
- this.applet.frame.PasteMenuItem.disable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.disable();
- }
-
- this.applet.MyControlPanel.DisableButton("Cut");
- this.applet.MyControlPanel.DisableButton("Copy");
- this.applet.MyControlPanel.DisableButton("Paste");
- }
-
- ((Component)this).repaint();
- }
-
- public void CheckDimensions() {
- int var1 = ((Component)this).size().width;
- int var2 = ((Component)this).size().height;
- int var3 = var1 / 8 - 1;
- int var4 = var2 / 8 - 1;
- int var5 = var3 + this.GridXOffset - 100 - 1;
- int var6 = var4 + this.GridYOffset - 100 - 1;
- if (var5 > 0) {
- this.GridXOffset -= var5;
- if (this.GridXOffset < 0) {
- this.GridXOffset = 0;
- }
-
- this.myHorizontal.setValue(this.GridXOffset);
- }
-
- if (var6 > 0) {
- this.GridYOffset -= var6;
- if (this.GridYOffset < 0) {
- this.GridYOffset = 0;
- }
-
- this.myVertical.setValue(this.GridYOffset);
- }
-
- }
-
- public void AdjustScrollbars() {
- this.GridXOffset = this.myHorizontal.getValue();
- this.GridYOffset = this.myVertical.getValue();
- int var2 = ((Component)this).size().width / 8 - 1;
- int var3 = ((Component)this).size().height / 8 - 1;
- String var1 = System.getProperty("os.name");
- if (!var1.equals("Windows 95") && !var1.equals("Windows NT")) {
- this.myVertical.setValues(this.GridYOffset, var3, 0, 101 - var3);
- this.myHorizontal.setValues(this.GridXOffset, var2, 0, 101 - var2);
- } else {
- this.myVertical.setValues(this.GridYOffset, var3, 0, 101);
- this.myHorizontal.setValues(this.GridXOffset, var2, 0, 101);
- }
- }
-
- public void DrawBorder(Graphics var1) {
- var1.setColor(BorderColor);
- var1.drawRect(-this.GridXOffset * 8 + 4, -this.GridYOffset * 8 + 4, 792, 792);
- var1.setColor(DigSim.BackGroundColor);
- var1.drawLine((100 - this.GridXOffset) * 8, 0, (100 - this.GridXOffset) * 8, (100 - this.GridYOffset) * 8);
- var1.drawLine(0, (100 - this.GridYOffset) * 8, (100 - this.GridXOffset) * 8, (100 - this.GridYOffset) * 8);
- }
-
- public void TellNotReady(Graphics var1) {
- String var2 = "Please wait, I'm preparing the image";
- var1.setColor(Color.green);
- var1.fillRect(0, 0, ((Component)this).size().width, ((Component)this).size().height);
- var1.setColor(Color.black);
- var1.drawString(var2, ((Component)this).size().width / 2 - this.SchematicFontMetrics.stringWidth(var2) / 2, ((Component)this).size().height / 2);
- }
-
- public synchronized void paint(Graphics var1) {
- this.CheckDimensions();
- if (this.applet.OffScreenWidth < ((Component)this).size().width || this.applet.OffScreenHeight < ((Component)this).size().height) {
- this.update(var1);
- }
-
- if (this.applet.ImagesReady()) {
- var1.drawImage(this.applet.ImageBuffer, 0, 0, this);
- } else {
- this.TellNotReady(var1);
- }
- }
-
- public synchronized void update(Graphics var1) {
- this.CheckDimensions();
- if (this.applet.OffScreenWidth < ((Component)this).size().width || this.applet.OffScreenHeight < ((Component)this).size().height) {
- this.applet.OffScreenHeight = ((Component)this).size().height;
- this.applet.OffScreenWidth = ((Component)this).size().width;
- this.applet.SetUpImages();
- }
-
- this.AdjustScrollbars();
- if (this.applet.ImagesReady()) {
- this.applet.ibg.drawImage(this.applet.GridImage, 0, 0, this);
- this.DrawBorder(this.applet.ibg);
- this.applet.MySchematic.draw(this.applet.ibg, this.GridXOffset, this.GridYOffset, 8);
- if (this.NewWire != null) {
- this.NewWire.draw(this.applet.ibg, this.GridXOffset, this.GridYOffset, 8);
- }
-
- if (this.SelectBox) {
- this.DrawSelectBox();
- }
-
- this.DrawJunctions(this.applet.PinGrid, this.applet.ibg, this.GridXOffset, this.GridYOffset, 8);
- var1.drawImage(this.applet.ImageBuffer, 0, 0, this);
- } else {
- this.TellNotReady(var1);
- }
- }
-
- public void DrawJunctions(Pin[][] var1, Graphics var2, int var3, int var4, int var5) {
- int var9 = ((Component)this).size().width / 8 + var3 - 1;
- int var10 = ((Component)this).size().height / 8 + var4 - 1;
- if (var9 >= 100) {
- var9 = 99;
- }
-
- if (var10 >= 100) {
- var10 = 99;
- }
-
- var2.setColor(JunctionColor);
-
- for(int var6 = var3; var6 < var9; ++var6) {
- for(int var7 = var4; var7 < var10; ++var7) {
- int var8 = var1[var6][var7].Components.size();
- if (var8 == 2 && (ElectronicComponent)var1[var6][var7].Components.elementAt(0) instanceof Wire && (ElectronicComponent)var1[var6][var7].Components.elementAt(1) instanceof Wire || var8 > 2) {
- var2.drawLine((var6 - var3) * var5 - 1, (var7 - var4) * var5, (var6 - var3) * var5 + 1, (var7 - var4) * var5);
- var2.drawLine((var6 - var3) * var5, (var7 - var4) * var5 - 1, (var6 - var3) * var5, (var7 - var4) * var5 + 1);
- }
- }
- }
-
- }
-
- public void DrawSelectBox() {
- int var1 = Math.abs(this.SelectBoxX2 - this.SelectBoxX1);
- int var2 = Math.abs(this.SelectBoxY2 - this.SelectBoxY1);
- int var3 = this.SelectBoxX1 < this.SelectBoxX2 ? this.SelectBoxX1 : this.SelectBoxX2;
- int var4 = this.SelectBoxY1 < this.SelectBoxY2 ? this.SelectBoxY1 : this.SelectBoxY2;
- this.applet.ibg.setColor(Color.white);
- this.applet.ibg.drawRect(var3, var4, var1, var2);
- }
-
- public int GetXCoord(int var1) {
- int var2 = (var1 + 4) / 8 + this.GridXOffset;
- if (var2 < 1) {
- var2 = 1;
- }
-
- if (var2 >= 100) {
- var2 = 99;
- }
-
- return var2;
- }
-
- public int GetYCoord(int var1) {
- int var2 = (var1 + 4) / 8 + this.GridYOffset;
- if (var2 < 1) {
- var2 = 1;
- }
-
- if (var2 >= 100) {
- var2 = 99;
- }
-
- return var2;
- }
-
- public void WireMouseDown(int var1, int var2) {
- this.StatusMessage("Move to the desired end position and release the mouse button.");
- this.NewWire = new Wire(this.applet.PinGrid, this.GetXCoord(var1), this.GetYCoord(var2));
- this.NewWire.Set2ndCoord(this.applet.PinGrid, this.GetXCoord(var1), this.GetYCoord(var2));
- }
-
- public void JunctionMouseDown(int var1, int var2) {
- if (!this.applet.MySchematic.PlaceJunction(this.applet.PinGrid, this.GetXCoord(var1), this.GetYCoord(var2), 8)) {
- this.StatusMessage("Sorry, can't place a junction here. Try again.");
- } else {
- this.StatusMessage("Click again to add another junction.");
- ((Component)this).repaint();
- }
- }
-
- public void WireMouseDrag(int var1, int var2) {
- this.StatusMessage("Release mouse button to add the wire here.");
- if (this.NewWire != null) {
- this.NewWire.Set2ndCoord(this.applet.PinGrid, this.GetXCoord(var1), this.GetYCoord(var2));
- ((Component)this).repaint();
- }
-
- }
-
- public void WireMouseUp(int var1, int var2) {
- if (this.NewWire != null) {
- if (this.NewWire.x1 == this.GetXCoord(var1) && this.NewWire.y1 == this.GetYCoord(var2)) {
- this.StatusMessage("Sorry - can't place wire. Length must be minimal one gridpoint.");
- this.NewWire = null;
- return;
- }
-
- this.StatusMessage("Choose a position for the next wire and press the mouse button.");
- this.NewWire.Set2ndCoord(this.applet.PinGrid, this.GetXCoord(var1), this.GetYCoord(var2));
- this.applet.addComponent(this.NewWire);
- this.NewWire.CheckPosition();
- this.NewWire.PlacePinsHere(this.applet.PinGrid);
- if (this.AutoJunction) {
- this.applet.MySchematic.PlaceJunction(this.applet.PinGrid, this.NewWire.x1, this.NewWire.y1, 8);
- this.applet.MySchematic.PlaceJunction(this.applet.PinGrid, this.NewWire.x2, this.NewWire.y2, 8);
- }
-
- this.NewWire = null;
- ((Component)this).repaint();
- }
-
- }
-
- public void MouseDownSimulateRunning(int var1, int var2) {
- this.ComponentPressed = this.applet.MySchematic.CheckIfComponentClicked(this.GetXCoord(var1), this.GetYCoord(var2));
- if (this.ComponentPressed != null) {
- if (!this.ComponentPressed.SimMouseDown()) {
- this.StatusMessage("Sorry, cannot move component while simulating. Press 'simulate' again to stop.");
- return;
- }
- } else {
- this.StatusMessage("Can't find a component here. Try again please.");
- }
-
- }
-
- public void MouseUpSimulateRunning(int var1, int var2) {
- if (this.ComponentPressed != null) {
- this.ComponentPressed.SimMouseUp();
- }
-
- }
-
- public void MouseDragSimulateRunning(int var1, int var2) {
- }
-
- public synchronized boolean mouseDown(Event var1, int var2, int var3) {
- this.SelectBox = false;
- if (this.applet.SimulateRunning()) {
- this.MouseDownSimulateRunning(var2, var3);
- return true;
- } else if (this.WireDrawing) {
- this.WireMouseDown(var2, var3);
- return true;
- } else if (this.JunctionDrawing) {
- this.JunctionMouseDown(var2, var3);
- return true;
- } else {
- ElectronicComponent var4 = this.applet.MySchematic.CheckIfComponentClicked(this.GetXCoord(var2), this.GetYCoord(var3));
- if (var4 != null) {
- if (var1.clickCount == 2 && var4 instanceof Caption && var4 == this.PrevSelectedComponent) {
- Caption var6 = (Caption)var4;
- if (this.applet.frame.MyTextChangeDialog == null) {
- this.applet.frame.MyTextChangeDialog = new TextDialog(this.applet.frame, var6, 6);
- }
- }
-
- if (var1.clickCount == 2 && var4 instanceof Probe && var4 == this.PrevSelectedComponent) {
- Probe var7 = (Probe)var4;
- if (this.applet.frame.MyTextProbeChangeDialog == null) {
- this.applet.frame.MyTextProbeChangeDialog = new TextDialog(this.applet.frame, var7, 8);
- }
- }
-
- this.PrevSelectedComponent = var4;
- if (this.SelectSchematic.size() > 0 && !this.SelectSchematic.InSchematic(var4)) {
- if (var1.shiftDown()) {
- var4.Selected = true;
- this.SelectSchematic.addComponent(var4);
- } else {
- this.SelectSchematic.RemoveAllComponents();
- }
-
- ((Component)this).repaint();
- }
-
- if (this.SelectSchematic.size() > 0) {
- this.SelectSchematic.RemovePinsGrid(this.applet.PinGrid);
- if (this.SelectSchematic.size() > 1) {
- this.StatusMessage("Move all components to the desired position and release the mouse button.");
- } else {
- ElectronicComponent var5 = (ElectronicComponent)this.SelectSchematic.Components.elementAt(0);
- this.StatusMessage("Move this " + var5.getName() + " to the desired position and release the mouse button.");
- }
- } else {
- var4.Selected = true;
- this.SelectSchematic.addComponent(var4);
- this.StatusMessage("Move this " + var4.getName() + " to the desired position and release the mouse button.");
- var4.RemovePinsGrid(this.applet.PinGrid);
- ((Component)this).repaint();
- }
-
- this.OldMouseX = var2;
- this.OldMouseY = var3;
- } else {
- this.SelectSchematic.RemoveAllComponents();
- this.SelectBox = true;
- this.SelectBoxX1 = this.SelectBoxX2 = var2;
- this.SelectBoxY1 = this.SelectBoxY2 = var3;
- ((Component)this).repaint();
- }
-
- return true;
- }
- }
-
- public synchronized boolean mouseUp(Event var1, int var2, int var3) {
- if (this.applet.HelpWanted) {
- ElectronicComponent var4;
- if ((var4 = this.applet.MySchematic.CheckIfComponentClicked(this.GetXCoord(var2), this.GetYCoord(var3))) != null) {
- new HelpDialog(this.applet.frame, var4.ComponentName);
- } else {
- new HelpDialog(this.applet.frame, "Schematic");
- }
-
- return true;
- } else if (this.applet.SimulateRunning()) {
- this.MouseUpSimulateRunning(var2, var3);
- return true;
- } else if (this.WireDrawing) {
- this.WireMouseUp(var2, var3);
- return true;
- } else if (this.JunctionDrawing) {
- return true;
- } else {
- if (this.SelectSchematic.size() > 0) {
- this.SelectSchematic.CheckPosition();
- this.SelectSchematic.PlacePinsHere(this.applet.PinGrid);
- this.StatusMessage("Ready. Select another component or make a choice.");
- ((Component)this).repaint();
- } else if (this.SelectBox) {
- this.SelectBox = false;
- this.applet.MySchematic.CheckIfComponentsInSelectBox(this.SelectSchematic, this.GetXCoord(this.SelectBoxX1), this.GetYCoord(this.SelectBoxY1), this.GetXCoord(this.SelectBoxX2), this.GetYCoord(this.SelectBoxY2));
- if (this.SelectSchematic.size() > 0) {
- ((Component)this).repaint();
- }
- }
-
- if (this.SelectSchematic.size() > 0) {
- this.applet.frame.CutMenuItem.enable();
- this.applet.frame.CopyMenuItem.enable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.enable();
- }
-
- this.applet.MyControlPanel.EnableButton("Cut");
- this.applet.MyControlPanel.EnableButton("Copy");
- } else {
- this.applet.frame.CutMenuItem.disable();
- this.applet.frame.CopyMenuItem.disable();
- if (this.applet.EnableFileOperations) {
- this.applet.frame.CopyDiskMenuItem.disable();
- }
-
- this.applet.MyControlPanel.DisableButton("Cut");
- this.applet.MyControlPanel.DisableButton("Copy");
- }
-
- return true;
- }
- }
-
- public synchronized boolean mouseDrag(Event var1, int var2, int var3) {
- if (var2 != -1 && var3 != -1) {
- if (this.applet.SimulateRunning()) {
- this.MouseDragSimulateRunning(var2, var3);
- return true;
- } else if (this.WireDrawing) {
- this.WireMouseDrag(var2, var3);
- return true;
- } else if (this.JunctionDrawing) {
- return true;
- } else {
- if (this.SelectSchematic.size() > 0) {
- int var4 = (var2 - this.OldMouseX) / 8;
- int var5 = (var3 - this.OldMouseY) / 8;
- if (var4 != 0 || var5 != 0) {
- this.OldMouseX = var2 - (var2 - this.OldMouseX) % 8;
- this.OldMouseY = var3 - (var3 - this.OldMouseY) % 8;
- this.SelectSchematic.AdjustPosition(this.applet.PinGrid, var4, var5);
- this.applet.MySchematic.Modified = true;
- ((Component)this).repaint();
- }
- } else if (this.SelectBox) {
- this.SelectBoxX2 = var2;
- this.SelectBoxY2 = var3;
- ((Component)this).repaint();
- }
-
- return true;
- }
- } else {
- return true;
- }
- }
-
- public void StatusMessage(String var1) {
- this.applet.StatusMessage(var1);
- }
-
- static {
- BorderColor = Color.red;
- JunctionColor = Color.white;
- }
- }
-