home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / ed8n1t2i / schematic.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  31.5 KB  |  702 lines

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // Schematic.java        v 1.00 b1
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:
  8. // Released in public domain:
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for creating and handling a schematic
  12. //
  13. // This program and the Java source is in the public domain.
  14. // Permission to use, copy, modify, and distribute this software
  15. // and its documentation for NON-COMMERCIAL purposes and
  16. // without fee is hereby granted.
  17. //
  18. //    Copyright 1996
  19. //
  20. //    Iwan van Rienen
  21. //    Joan Maetsuyckerstr. 145
  22. //    2593 ZG  The Hague
  23. //    The Netherlands
  24. //
  25. // I am not responsible for any bugs in this program and
  26. // possible damage to hard- or software when using this program.
  27. //****************************************************************************
  28. import java.applet.Applet;
  29. import java.awt.*;
  30. import java.util.Vector;
  31. import java.io.StreamTokenizer;
  32. import java.io.InputStream;
  33. import java.io.FileOutputStream;
  34. import java.io.PrintStream;
  35. import java.io.IOException;
  36. import java.net.URL;
  37. import java.lang.Math;
  38.  
  39. class Schematic {
  40.     protected Vector Components;
  41.     boolean Modified = false;
  42.     String FileDir = null;
  43.     String FileName = null;
  44.     int SimulateCycleID = 0;
  45.  
  46. //----------------------------------------------------------------------------
  47. // The Schematic constructor
  48. //----------------------------------------------------------------------------
  49.     public Schematic() {
  50.         Components = new Vector();
  51.     }
  52.  
  53. //----------------------------------------------------------------------------
  54. // The Schematic constructor, called to copy an existing schematic
  55. //----------------------------------------------------------------------------
  56.     public Schematic(Schematic SchematicToCopy) {
  57.         ElectronicComponent TempComponent;
  58.  
  59.         Components = new Vector();
  60.  
  61.         for (int ix = 0; ix < SchematicToCopy.size(); ix++) {
  62.             TempComponent = (ElectronicComponent)SchematicToCopy.Components.elementAt(ix);
  63.             Components.addElement (TempComponent);
  64.         }
  65.     }
  66.  
  67. //----------------------------------------------------------------------------
  68. // The Schematic constructor, used to Paste
  69. //----------------------------------------------------------------------------
  70.     public Schematic PasteSchematic(Pin PinGrid[][], Schematic CopySchematic, int Xoff, int Yoff) {
  71.         ElectronicComponent TempComponent, NewComponent;
  72.         Schematic NewSelectSchematic = new Schematic();
  73.         int ix;
  74.         int MinX = -1, MinY = -1;
  75.         int WireXMin, WireYMin;
  76.         Wire TempWire;
  77.         Modified = true;
  78.  
  79.         // System.out.println ("PasteSchematic");
  80.         // First search for left-top component in CopySchematic
  81.         for (ix = 0; ix < CopySchematic.size(); ix++) {
  82.             TempComponent = (ElectronicComponent)CopySchematic.Components.elementAt(ix);
  83.             if ((TempComponent instanceof Wire)) {
  84.                 TempWire = (Wire)TempComponent;
  85.                 WireXMin = Math.min(TempWire.x1, TempWire.x2);
  86.                 WireYMin = Math.min(TempWire.y1, TempWire.y2);
  87.                 if (MinX == -1 || WireXMin < MinX) {
  88.                     MinX = WireXMin - 1;
  89.                 }
  90.                 if (MinY == -1 || WireYMin < MinY) {
  91.                     MinY = WireYMin - 1;
  92.                 }
  93.             } else {
  94.                 if (MinX == -1 || TempComponent.Pos.x < MinX) {
  95.                     MinX = TempComponent.Pos.x;
  96.                 }
  97.                 if (MinY == -1 || TempComponent.Pos.y < MinY) {
  98.                     MinY = TempComponent.Pos.y;
  99.                 }
  100.             }
  101.         }
  102.         // System.out.println ("MinXY= " + MinX + ", " + MinY);
  103.         // Now copy all components with Negative offset MinXY
  104.         for (ix = 0; ix < CopySchematic.size(); ix++) {
  105.             // System.out.println ("Copy component");
  106.             TempComponent = (ElectronicComponent)CopySchematic.Components.elementAt(ix);
  107.             NewComponent = TempComponent.Copy(MinX - Xoff , MinY - Yoff);
  108.             NewComponent.PlacePinsHere(PinGrid); // Register all new IO pins
  109.             TempComponent.Selected = false;
  110.             NewComponent.Selected = true;
  111.             if ((TempComponent instanceof Wire)) {
  112.                 TempWire = (Wire)NewComponent;
  113.                 TempWire.ChangingWire = 1 | 2;
  114.             }
  115.             addComponent (NewComponent);
  116.             NewSelectSchematic.addComponent (NewComponent);
  117.         }
  118.         return NewSelectSchematic;
  119.     }
  120.  
  121. //----------------------------------------------------------------------------
  122. // Make all components selected
  123. //----------------------------------------------------------------------------
  124.     public void SetAllComponentsSelected() {
  125.         ElectronicComponent TempComponent;
  126.         Wire TempWire;
  127.  
  128.         for (int ix = 0; ix < Components.size(); ix++) {
  129.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  130.             TempComponent.Selected = true;
  131.             if ((TempComponent instanceof Wire)) {
  132.                 TempWire = (Wire)TempComponent;
  133.                 TempWire.ChangingWire = 1 | 2;
  134.             }
  135.         }
  136.     }
  137.  
  138. //----------------------------------------------------------------------------
  139. // Remove the Same Components in both schematics
  140. //----------------------------------------------------------------------------
  141.     public void RemoveSameElements(Pin PinGrid[][], Schematic ActSchematic) {
  142.         ElectronicComponent TempComponent;
  143.         Modified = true;
  144.  
  145.         for (int ix = 0; ix < ActSchematic.size(); ix++) {
  146.             TempComponent = (ElectronicComponent)ActSchematic.Components.elementAt(ix);
  147.             if (Components.contains(TempComponent)) {
  148.                 TempComponent.RemovePinsGrid(PinGrid);
  149.                 Components.removeElement(TempComponent);
  150.             }
  151.         }
  152.     }
  153.  
  154. //----------------------------------------------------------------------------
  155. // Destroy all components in this schematic
  156. //----------------------------------------------------------------------------
  157.     public void DestroyComponents(Pin PinGrid[][]) {
  158.         ElectronicComponent TempComponent;
  159.         Modified = true;
  160.  
  161.         for (int ix = 0; ix < Components.size(); ix++) {
  162.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  163.             TempComponent.RemovePinsGrid(PinGrid);
  164.         }
  165.         Components.removeAllElements();
  166.     }
  167.  
  168. //----------------------------------------------------------------------------
  169. // add an electronic component
  170. //----------------------------------------------------------------------------
  171.     public void addComponent (ElectronicComponent NewComponent) {
  172.         Modified = true;
  173.         Components.addElement(NewComponent);
  174.     }
  175.  
  176. //----------------------------------------------------------------------------
  177. // draw this schematic
  178. //----------------------------------------------------------------------------
  179.     public void draw(Graphics g, int x, int y, int GridStep) {
  180.         ElectronicComponent TempComponent;
  181.  
  182.         for (int ix = 0; ix < Components.size(); ix++) {
  183.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  184.             TempComponent.draw(g, x, y, GridStep);
  185.         }
  186.     }
  187.  
  188. //----------------------------------------------------------------------------
  189. // return the total number of components in this schematic
  190. //----------------------------------------------------------------------------
  191.     public int size() {
  192.         return Components.size();
  193.     }
  194.  
  195. //----------------------------------------------------------------------------
  196. // Remove all components in this schematic
  197. //----------------------------------------------------------------------------
  198.     public void RemoveAllComponents() {
  199.         ElectronicComponent TempComponent;
  200.         Modified = true;
  201.  
  202.         for (int ix = 0; ix < Components.size(); ix++) {
  203.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  204.             TempComponent.Selected = false;
  205.         }
  206.         Components.removeAllElements();
  207.     }
  208.  
  209. //----------------------------------------------------------------------------
  210. // Check if the specified component is a member of the schematic
  211. //----------------------------------------------------------------------------
  212.     public boolean InSchematic(ElectronicComponent ActComponent) {
  213.         return Components.contains(ActComponent);
  214.     }
  215.  
  216. //----------------------------------------------------------------------------
  217. // Check if there are component(s) in the schematic, and if so, put them
  218. // in the SelectSchematic.
  219. //----------------------------------------------------------------------------
  220.     public void CheckIfComponentsInSelectBox(Schematic SelectSchematic, int x1, int y1, int x2, int y2) {
  221.         ElectronicComponent TempComponent;
  222.  
  223.         int swap;
  224.         if (x1 > x2) {
  225.             swap = x1;
  226.             x1 = x2;
  227.             x2 = swap;
  228.         }
  229.         if (y1 > y2) {
  230.             swap = y1;
  231.             y1 = y2;
  232.             y2 = swap;
  233.         }
  234.  
  235.         for (int ix = 0; ix < Components.size(); ix++) {
  236.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  237.             if (TempComponent.CheckIfComponentInSelectBox(x1, y1, x2, y2)) {
  238. //              System.out.println ("Component added to SelectSchematic");
  239.                 SelectSchematic.addComponent(TempComponent);
  240.             }
  241.         }
  242.     }
  243.  
  244. //----------------------------------------------------------------------------
  245. // Adjust the position of all components
  246. //----------------------------------------------------------------------------
  247.     public void AdjustPosition (Pin PinGrid[][], int x, int y) {
  248.         ElectronicComponent TempComponent;
  249.  
  250.         for (int ix = 0; ix < Components.size(); ix++) {
  251.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  252.             TempComponent.AdjustPosition (PinGrid, x, y);
  253.         }
  254.     }
  255.  
  256. //----------------------------------------------------------------------------
  257. // Remove all components from the PinGrid
  258. //----------------------------------------------------------------------------
  259.     public void RemovePinsGrid(Pin PinGrid[][]) {
  260.         ElectronicComponent TempComponent;
  261.  
  262.         for (int ix = 0; ix < Components.size(); ix++) {
  263.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  264.             TempComponent.RemovePinsGrid(PinGrid);
  265.         }
  266.     }
  267.  
  268. //----------------------------------------------------------------------------
  269. // Check if the position of all components are legal, and if not,
  270. // adjust them.
  271. //----------------------------------------------------------------------------
  272.     public void CheckPosition() {
  273.         ElectronicComponent TempComponent;
  274.  
  275.         for (int ix = 0; ix < Components.size(); ix++) {
  276.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  277.             TempComponent.CheckPosition();
  278.         }
  279.     }
  280.  
  281. //----------------------------------------------------------------------------
  282. // Place all pins of all components in the PinGrid.
  283. //----------------------------------------------------------------------------
  284.     public void PlacePinsHere(Pin PinGrid[][]) {
  285.         ElectronicComponent TempComponent;
  286.  
  287.         for (int ix = 0; ix < Components.size(); ix++) {
  288.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  289.             TempComponent.PlacePinsHere(PinGrid);
  290.         }
  291.     }
  292.  
  293. //----------------------------------------------------------------------------
  294. // Check if there is an Component body at position x, y which could be
  295. // clicked
  296. //----------------------------------------------------------------------------
  297.     public ElectronicComponent CheckIfComponentClicked(int x, int y) {
  298.         ElectronicComponent TempComponent;
  299.  
  300.         for (int ix = 0; ix < Components.size(); ix++) {
  301.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  302.             if (TempComponent.CheckIfComponentClicked(x, y)) {
  303.  
  304.                 return TempComponent;
  305.             }
  306.         }
  307.         return null;    // there's no component clicked.
  308.     }
  309.  
  310. //----------------------------------------------------------------------------
  311. // Do a simulation cycle
  312. //----------------------------------------------------------------------------
  313.     public void Simulate() {
  314.         ElectronicComponent TempComponent;
  315. //      System.out.println ("Schematic.Simulate()");
  316.  
  317.         for (int ix = 0; ix < Components.size(); ix++) {
  318.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  319.             SimulateCycleID++;
  320.             if (SimulateCycleID == -1) SimulateCycleID++;
  321.             TempComponent.Simulate(SimulateCycleID);
  322.         }
  323. //      System.out.println ("Schematic.Simulate() ready.");
  324.     }
  325.  
  326. //----------------------------------------------------------------------------
  327. // Initialize components before a simulation cycle
  328. //----------------------------------------------------------------------------
  329.     public void InitBeforeSimulate() {
  330.         ElectronicComponent TempComponent;
  331. //        System.out.println ("Schematic.InitBeforeSimulate()");
  332.  
  333.         for (int ix = 0; ix < Components.size(); ix++) {
  334.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  335.             TempComponent.InitBeforeSimulate();
  336.         }
  337. //        System.out.println ("Schematic.InitBeforeSimulate() ready.");
  338.     }
  339.  
  340. //----------------------------------------------------------------------------
  341. // Convert a String to a Color
  342. //----------------------------------------------------------------------------
  343.     protected Color StringToColor(String clr) {
  344.         if ("red".equals(clr)) {
  345.             return Color.red;
  346.         } else if ("green".equals(clr)) {
  347.             return Color.green;
  348.         } else if ("blue".equals(clr)) {
  349.             return Color.blue;
  350.         } else if ("yellow".equals(clr)) {
  351.             return Color.yellow;
  352.         } else if ("pink".equals(clr)) {
  353.             return Color.pink;
  354.         } else if ("black".equals(clr)) {
  355.             return Color.black;
  356.         } else if ("white".equals(clr)) {
  357.             return Color.white;
  358.         } else if ("gray".equals(clr)) {
  359.             return Color.gray;
  360.         }
  361.         return Color.red;   // Unknown color
  362.     }
  363.  
  364. //----------------------------------------------------------------------------
  365. // Load a component from a file
  366. //----------------------------------------------------------------------------
  367.     public boolean LoadComponent (Pin PinGrid[][], InputStream is, StreamTokenizer st) throws IOException, FileFormatException {
  368.         int XPos = 0, YPos = 0;
  369.         int XPos2 = 0, YPos2 = 0;
  370.         String ComponentName = st.sval;
  371.         ElectronicComponent NewComponent;
  372.         Color ComponentColor = Color.red;
  373.         String Text = "";
  374.         int c;
  375.         // System.out.println ("Load component [" + st.sval + "]" );
  376. scan:
  377.         while (true) {
  378.             switch (st.nextToken()) {
  379.             case StreamTokenizer.TT_EOL:
  380.                 break;
  381.             case StreamTokenizer.TT_WORD:
  382.                 if ("pos".equals(st.sval)) {
  383.                     if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  384.                         XPos = (int)st.nval;
  385.                         if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  386.                             YPos = (int)st.nval;
  387.                         }
  388.                     }
  389.                     // Skip other words on position line
  390.                     while (st.ttype != StreamTokenizer.TT_EOL &&
  391.                       st.ttype != StreamTokenizer.TT_EOF) {
  392.                         st.nextToken();
  393.                     }
  394.                     // System.out.println ("Component position: " + XPos + ", " + YPos);
  395.                 } else if ("color".equals(st.sval)) {
  396.                     if (st.nextToken() == StreamTokenizer.TT_WORD) {
  397.                         ComponentColor = StringToColor(st.sval);
  398.                     }
  399.                 } else if ("Text".equals(st.sval)) {
  400.                     // Read the rest of this line as Text
  401.                     Text = "";
  402.                     while ((c = is.read()) != StreamTokenizer.TT_EOL) {
  403.                         // System.out.println ("Read:" + c);
  404.                         Text += (char)c;
  405.                     }
  406.                 } else if ("pos2".equals(st.sval)) {
  407.                     if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  408.                         XPos2 = (int)st.nval;
  409.                         if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  410.                             YPos2 = (int)st.nval;
  411.                         }
  412.                     }
  413.                     // Skip other words on position line
  414.                     while (st.ttype != StreamTokenizer.TT_EOL &&
  415.                       st.ttype != StreamTokenizer.TT_EOF) {
  416.                         st.nextToken();
  417.                     }
  418.                     // System.out.println ("Component position2: " + XPos2 + ", " + YPos2);
  419.  
  420.                 } else if ("end".equals(st.sval)) {
  421.                     if (st.nextToken() == StreamTokenizer.TT_WORD) {
  422.                         if ("describe".equals(st.sval)) {
  423.                             // System.out.println ("End describe");
  424.                             if ("Vcc".equals(ComponentName)) {
  425.                                 addComponent(new Vcc(PinGrid, XPos, YPos));
  426.                             } else if ("GND".equals(ComponentName)) {
  427.                                 addComponent(new GND(PinGrid, XPos, YPos));
  428.                             } else if ("Wire".equals(ComponentName)) {
  429.                                 addComponent(new Wire(PinGrid, XPos, YPos, XPos2, YPos2));
  430.  
  431.                             } else if ("Buffer".equals(ComponentName)) {
  432.                                 addComponent(new Buffer(PinGrid, XPos, YPos));
  433.                             } else if ("Inverter".equals(ComponentName)) {
  434.                                 addComponent(new Inverter(PinGrid, XPos, YPos));
  435.                             } else if ("TwoAndPort".equals(ComponentName)) {
  436.                                 addComponent(new TwoAndPort(PinGrid, XPos, YPos));
  437.                             } else if ("ThreeAndPort".equals(ComponentName)) {
  438.                                 addComponent(new ThreeAndPort(PinGrid, XPos, YPos));
  439.                             } else if ("TwoNandPort".equals(ComponentName)) {
  440.                                 addComponent(new TwoNandPort(PinGrid, XPos, YPos));
  441.                             } else if ("ThreeNandPort".equals(ComponentName)) {
  442.                                 addComponent(new ThreeNandPort(PinGrid, XPos, YPos));
  443.                             } else if ("TwoOrPort".equals(ComponentName)) {
  444.                                 addComponent(new TwoOrPort(PinGrid, XPos, YPos));
  445.                             } else if ("ThreeOrPort".equals(ComponentName)) {
  446.                                 addComponent(new ThreeOrPort(PinGrid, XPos, YPos));
  447.                             } else if ("TwoNorPort".equals(ComponentName)) {
  448.                                 addComponent(new TwoNorPort(PinGrid, XPos, YPos));
  449.                             } else if ("ThreeNorPort".equals(ComponentName)) {
  450.                                 addComponent(new ThreeNorPort(PinGrid, XPos, YPos));
  451.                             } else if ("TwoXorPort".equals(ComponentName)) {
  452.                                 addComponent(new TwoXorPort(PinGrid, XPos, YPos));
  453.                             } else if ("TwoXnorPort".equals(ComponentName)) {
  454.                                 addComponent(new TwoXnorPort(PinGrid, XPos, YPos));
  455.  
  456.                             } else if ("Switch".equals(ComponentName)) {
  457.                                 addComponent(new Switch(PinGrid, XPos, YPos));
  458.                             } else if ("PushButton".equals(ComponentName)) {
  459.                                 addComponent(new PushButton(PinGrid, XPos, YPos));
  460.  
  461.                             } else if ("SRLatch".equals(ComponentName)) {
  462.                                 addComponent(new SRLatch(PinGrid, XPos, YPos));
  463.                             } else if ("GatedSRLatch".equals(ComponentName)) {
  464.                                 addComponent(new GatedSRLatch(PinGrid, XPos, YPos));
  465.                             } else if ("DLatch".equals(ComponentName)) {
  466.                                 addComponent(new DLatch(PinGrid, XPos, YPos));
  467.                             } else if ("DFlipFlop".equals(ComponentName)) {
  468.                                 addComponent(new DFlipFlop(PinGrid, XPos, YPos));
  469.                             } else if ("JKFlipFlop".equals(ComponentName)) {
  470.                                 addComponent(new JKFlipFlop(PinGrid, XPos, YPos));
  471.                             } else if ("EdgeTriggeredTFlipFlop".equals(ComponentName)) {
  472.                                 addComponent(new EdgeTriggeredTFlipFlop(PinGrid, XPos, YPos));
  473.                             } else if ("TFlipFlop".equals(ComponentName)) {
  474.                                 addComponent(new TFlipFlop(PinGrid, XPos, YPos));
  475.                             } else if ("OctalDFlipFlop".equals(ComponentName)) {
  476.                                 addComponent(new OctalDFlipFlop(PinGrid, XPos, YPos));
  477.                             } else if ("OctalLatch".equals(ComponentName)) {
  478.                                 addComponent(new OctalLatch(PinGrid, XPos, YPos));
  479.  
  480.                             } else if ("Oscilator".equals(ComponentName)) {
  481.                                 addComponent(new Oscilator(PinGrid, XPos, YPos));
  482.                             } else if ("BCDToSevenSegDecoder".equals(ComponentName)) {
  483.                                 addComponent(new BCDToSevenSegDecoder(PinGrid, XPos, YPos));
  484.                             } else if ("ThreeToEightLineDecoder".equals(ComponentName)) {
  485.                                 addComponent(new ThreeToEightLineDecoder(PinGrid, XPos, YPos));
  486.                             } else if ("FourBitBinaryCounter".equals(ComponentName)) {
  487.                                 addComponent(new FourBitBinaryCounter(PinGrid, XPos, YPos));
  488.                             } else if ("EightBitSerInShiftReg".equals(ComponentName)) {
  489.                                 addComponent(new EightBitSerInShiftReg(PinGrid, XPos, YPos));
  490.                             } else if ("EightBitParInShiftReg".equals(ComponentName)) {
  491.                                 addComponent(new EightBitParInShiftReg(PinGrid, XPos, YPos));
  492.  
  493.                             } else if ("Caption".equals(ComponentName)) {
  494.                                 addComponent(new Caption(XPos, YPos, Text));
  495.  
  496.                             } else if ("LED".equals(ComponentName)) {
  497.                                 addComponent(new LED(PinGrid, ComponentColor, XPos, YPos));
  498.                             } else if ("BiColorLED".equals(ComponentName)) {
  499.                                 addComponent(new BiColorLED(PinGrid, XPos, YPos));
  500.                             } else if ("SevenSegmentDisplay".equals(ComponentName)) {
  501.                                 addComponent(new SevenSegmentDisplay(PinGrid, ComponentColor, XPos, YPos));
  502.                             } else if ("Probe".equals(ComponentName)) {
  503.                                 addComponent(new Probe(PinGrid, XPos, YPos, Text, false, false));
  504.                             } else if ("ProbeUp".equals(ComponentName)) {
  505.                                 addComponent(new Probe(PinGrid, XPos, YPos, Text, true, false));
  506.                             } else if ("ProbeDn".equals(ComponentName)) {
  507.                                 addComponent(new Probe(PinGrid, XPos, YPos, Text, false, true));
  508.  
  509.                             } else {
  510.                                 return false;   // Unknown component
  511.                             }
  512.                             return true;
  513.                         }
  514.                     }
  515.                 }
  516.                 break;
  517.  
  518.             default:
  519.                 break scan;
  520.             }
  521.         }
  522.         return true;
  523.     }
  524.  
  525. //----------------------------------------------------------------------------
  526. // Create a schematic by parsing an input stream
  527. //----------------------------------------------------------------------------
  528.     Schematic (Pin PinGrid[][], InputStream is) throws IOException, FileFormatException {
  529.         this();
  530.         int FileVersionHigh = 0, FileVersionLow = 0;
  531.         String message;
  532.         boolean SchematicCorrupt = false;
  533.         StreamTokenizer st = new StreamTokenizer(is);
  534.  
  535.         // System.out.println ("Schematic open");
  536.         st.eolIsSignificant(true);
  537.         st.commentChar('#');
  538. scan:
  539.         while (true) {
  540.             switch (st.nextToken()) {
  541.             case StreamTokenizer.TT_EOL:
  542.                 break;
  543.             case StreamTokenizer.TT_WORD:
  544.                 if ("version".equals(st.sval)) {
  545.                     if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  546.                         FileVersionHigh = (int)st.nval;
  547.                         if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
  548.                             FileVersionLow = (int)st.nval;
  549.                         }
  550.                     }
  551.                     // Skip other words on version line
  552.                     while (st.ttype != StreamTokenizer.TT_EOL &&
  553.                       st.ttype != StreamTokenizer.TT_EOF) {
  554.                         st.nextToken();
  555.                     }
  556.                     // System.out.println ("File version " + FileVersionHigh + "." + FileVersionLow);
  557.                 } else if ("describe".equals(st.sval)) {
  558.                     if (st.nextToken() == StreamTokenizer.TT_WORD) {
  559.                         if ("component".equals(st.sval)) {
  560.                             if (st.nextToken() == StreamTokenizer.TT_WORD) {
  561. //                              System.out.println ("Describe component [" + st.sval + "]" );
  562.                                 if (!LoadComponent (PinGrid, is, st)) {
  563.                                     SchematicCorrupt = true;
  564.                                 }
  565.                             }
  566.                         }
  567.                     }
  568.                 } else {
  569.                     SchematicCorrupt = true;
  570.                 }
  571.                 break;
  572.             default:
  573.                 break scan;
  574.             }
  575.         }
  576.         is.close();
  577.         if (st.ttype != StreamTokenizer.TT_EOF) {
  578.             throw new FileFormatException(st.toString());
  579.         }
  580.         Modified = false;
  581.         if (SchematicCorrupt) {
  582.             String DlgButtons[] = { "OK" };
  583.             message = "Unknown data in schematic. Schematic could be corrupt.";
  584.             SimpleDialog ExceptionDialog = new SimpleDialog(null, "Loading schematic", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_WARNING);
  585.         }
  586.     }
  587.  
  588. //----------------------------------------------------------------------------
  589. // Save the schematic to the specified output stream.
  590. //----------------------------------------------------------------------------
  591.     public void Save (FileOutputStream os) throws IOException, FileFormatException {
  592.         ElectronicComponent TempComponent;
  593.         PrintStream myPrintStream = new PrintStream(os);
  594.         // System.out.println ("Schematic() save");
  595.         myPrintStream.println ("# Digsim file");
  596.         myPrintStream.println ("version 1 0");
  597.  
  598.         for (int ix = 0; ix < Components.size(); ix++) {
  599.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  600.             TempComponent.Save (myPrintStream);
  601.         }
  602.  
  603.         os.close();
  604.     }
  605.  
  606. //----------------------------------------------------------------------------
  607. // Plave a junction at the specified Position, if possible.
  608. // A junction is made by splitting a Wire in two parts.
  609. //----------------------------------------------------------------------------
  610.     public boolean PlaceJunction(Pin PinGrid[][], int x, int y, int gs) {
  611.         ElectronicComponent TempComponent;
  612.         Wire TempWire;
  613.         boolean JunctionPlaced = false;
  614.         // System.out.println ("Place junction pos " + x + ", " + y);
  615.  
  616.         for (int ix = 0; ix < Components.size(); ix++) {
  617.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  618.             if (TempComponent instanceof Wire) {
  619.                 TempWire = (Wire)TempComponent;
  620.                 if (TempWire.TryPlaceJunction(this, PinGrid, x, y, gs)) {
  621.                     JunctionPlaced = true;
  622.                 }
  623.             }
  624.         }
  625.         return JunctionPlaced;
  626.     }
  627.  
  628. //----------------------------------------------------------------------------
  629. // Test if there occured a Short circuit in this schematic
  630. //----------------------------------------------------------------------------
  631.     public ElectronicComponent TestShortCircuit() {
  632.         ElectronicComponent TempComponent;
  633.         int o;
  634.  
  635.         for (int ix = 0; ix < Components.size(); ix++) {
  636.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  637.             for (o = 0; o < TempComponent.Outputs; o++) {
  638.                 if (TempComponent.OPin[o].ShortCircuit) return TempComponent;
  639.             }
  640.         }
  641.         return null;
  642.     }
  643.  
  644. //----------------------------------------------------------------------------
  645. // Test if there occured a Loop in this schematic
  646. //----------------------------------------------------------------------------
  647.     public ElectronicComponent TestLoop() {
  648.         ElectronicComponent TempComponent;
  649.         int i;
  650.  
  651.         for (int ix = 0; ix < Components.size(); ix++) {
  652.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  653.             for (i = 0; i < TempComponent.Inputs; i++) {
  654.                 if (TempComponent.IPin[i].Looping) return TempComponent;
  655.             }
  656.         }
  657.         return null;
  658.     }
  659.  
  660. //----------------------------------------------------------------------------
  661. // Get a vector with all analyzer probes in this schematic
  662. //----------------------------------------------------------------------------
  663.     public Vector getProbes() {
  664.         ElectronicComponent TempComponent;
  665.         Vector v = new Vector();
  666.  
  667.         for (int ix = 0; ix < Components.size(); ix++) {
  668.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  669.             if (TempComponent instanceof Probe) {
  670.                 v.addElement (TempComponent);
  671.             }
  672.         }
  673.         return v;
  674.     }
  675.  
  676. //----------------------------------------------------------------------------
  677. // Swap two components in the schematic.
  678. // It's used in the analyzer when the user swaps two probes.
  679. //----------------------------------------------------------------------------
  680.     public void SwapComponents(ElectronicComponent c1, ElectronicComponent c2) {
  681.         int ix1 = Components.indexOf(c1);
  682.         int ix2 = Components.indexOf(c2);
  683.         if (ix1 == -1 || ix2 == -1) return;
  684.         Components.setElementAt(c1, ix2);
  685.         Components.setElementAt(c2, ix1);
  686.     }
  687.  
  688. //----------------------------------------------------------------------------
  689. // Check if there are probes in this schematic
  690. //----------------------------------------------------------------------------
  691.     public boolean ProbesInSchematic() {
  692.         ElectronicComponent TempComponent;
  693.  
  694.         for (int ix = 0; ix < Components.size(); ix++) {
  695.             TempComponent = (ElectronicComponent)Components.elementAt(ix);
  696.             if (TempComponent instanceof Probe) {
  697.                 return true;
  698.             }
  699.         }
  700.         return false;
  701.     }
  702. }