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

  1. //****************************************************************************
  2. // ---- version information ----
  3. //
  4. // AnalyzerPanel.java    v 1.00b2
  5. // Written by:           I. van Rienen / E-mail ivr@bart.nl
  6. // URL:                  http://www/bart.nl/~ivr
  7. // Initial release:           v 1.00b2 06-03-1996
  8. // Released in public domain: v 1.00b2 06-03-1996
  9. //
  10. // ---- Description ----
  11. // Java class containing methods for the the logic analyzer.
  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.awt.*;
  29. import java.util.Vector;
  30.  
  31. class AnalyzerPanel extends Panel {
  32.     static final int CHANNEL_HEIGHT = 50;
  33.     static final int DISPLAY_OFFSET = 5;
  34.     static final int CLOCK_WIDTH = 10;
  35.     static final int LINE_OFFSET = 5;
  36.     static final int BUTTON_X_OFFSET = 5;
  37.     static final int BUTTON_Y_OFFSET = 22;
  38.     static final int BUTTON_CLOCK_NONE = 48;
  39.     static final int BUTTON_CLOCK_NONE_PRESSED = 24;
  40.     static final int BUTTON_CLOCK_UP_PRESSED = 72;
  41.     static final int BUTTON_CLOCK_UP_SELECTED = 96;
  42.     static final int BUTTON_CLOCK_DN_PRESSED = 120;
  43.     static final int BUTTON_CLOCK_DN_SELECTED = 144;
  44.     static final Color TextColor = Color.black;
  45.     static final Color BackGroundColor = Color.black;
  46.     protected Scrollbar myVertical;
  47.     DigSim applet;
  48.     Image OffScreenImage = null;
  49.     Image ImageBuffer = null;
  50.     Image CopyImage = null;
  51.     Graphics cig;
  52.     int ButtonOffset = 0;
  53.     Graphics og;
  54.     Dimension ImageSize;
  55.     protected Font AnalyzerFont;
  56.     protected FontMetrics AnalyzerFontMetrics;
  57.     Vector probes = null;
  58.     boolean ImageButtonsDisabled = false;
  59.     Probe PressedProbe = null;
  60.     Probe DragProbe = null;
  61.     int CurrentCol = 0;
  62.     boolean ImageUpdated = false;
  63.     Frame frame;
  64.  
  65. //----------------------------------------------------------------------------
  66. // The constructor of the Logic Analyzer Panel
  67. //----------------------------------------------------------------------------
  68.     public AnalyzerPanel(DigSim app, Frame f) {
  69.         applet = app;
  70.         frame = f;
  71.  
  72.         setLayout( new BorderLayout());
  73.         AnalyzerFont = new Font("TimesRoman",Font.PLAIN, 14);
  74.         AnalyzerFontMetrics = getFontMetrics(AnalyzerFont);
  75.  
  76.         super.setLayout( new BorderLayout());
  77.  
  78.         LoadButtonsImage();
  79.         myVertical = new Scrollbar(Scrollbar.VERTICAL);
  80.         add( "East", myVertical);
  81.  
  82.         ImageSize = new Dimension (400, 250);
  83.         CheckOffScreenImage();
  84.         repaint();
  85.     }
  86.  
  87. //----------------------------------------------------------------------------
  88. // Adjust scrollbar to current display settings
  89. //----------------------------------------------------------------------------
  90.     public void AdjustScrollbar() {
  91.         String OSName;
  92.         if (probes == null) return;
  93.         int ttl_height = CHANNEL_HEIGHT * probes.size();
  94.         int vis_height = size().height;
  95.         int max_offs = ttl_height - vis_height;
  96.         if (max_offs < 0) max_offs = 0;
  97.         // Determine OS because of scrollbar implementation differences
  98.         // between Win95/NT and other operating systems.
  99.         OSName = System.getProperty("os.name");
  100.         if (OSName.equals ("Windows 95") ||
  101.             OSName.equals ("Windows NT")) {
  102.             myVertical.setValues (myVertical.getValue(), vis_height, 0, max_offs + vis_height);
  103.         } else {
  104.             myVertical.setValues (myVertical.getValue(), vis_height, 0, max_offs);
  105.         }
  106.     }
  107.  
  108. //----------------------------------------------------------------------------
  109. // Check if there is an off-screen image, and check its size
  110. //----------------------------------------------------------------------------
  111.     public void CheckOffScreenImage() {
  112.         if (OffScreenImage == null) {
  113.             PrepareOffScreenImage();
  114.             return;
  115.         }
  116.         if (size().width > ImageSize.width ||
  117.             size().height > ImageSize.height) {
  118.              ImageSize.width = size().width;
  119.              ImageSize.height = size().height;
  120.              PrepareOffScreenImage();
  121.         }
  122.     }
  123.  
  124. //----------------------------------------------------------------------------
  125. // Create an off-screen image
  126. //----------------------------------------------------------------------------
  127.     public void PrepareOffScreenImage() {
  128.         OffScreenImage = applet.createImage (ImageSize.width, ImageSize.height);
  129.         og = OffScreenImage.getGraphics();
  130.         og.setFont(AnalyzerFont);
  131.  
  132.         repaint();
  133.     }
  134.  
  135. //----------------------------------------------------------------------------
  136. // select an ImageButton
  137. //----------------------------------------------------------------------------
  138.     public void SelectButton(int ButtonType) {
  139.         cig.copyArea (0, ButtonType, 24, 24, 0, -ButtonType);
  140.     }
  141.  
  142. //----------------------------------------------------------------------------
  143. // There is a button pressed
  144. //----------------------------------------------------------------------------
  145.     public void ButtonPressed (int b) {
  146.         Probe ActProbe = (Probe)probes.elementAt (b);
  147.         PressedProbe = ActProbe;
  148.         if (ActProbe.clockup_probe) {
  149.             ActProbe.clockup_probe = false;
  150.             ActProbe.clockdn_probe = true;
  151.         } else if (ActProbe.clockdn_probe) {
  152.             ActProbe.clockup_probe = false;
  153.             ActProbe.clockdn_probe = false;
  154.         } else {
  155.             ActProbe.clockup_probe = true;
  156.             ActProbe.clockdn_probe = false;
  157.         }
  158.         for (int ix = 0; ix < probes.size(); ix++) {
  159.             if (b != ix) {
  160.                 ActProbe = (Probe) probes.elementAt (ix);
  161.                 ActProbe.clockup_probe = false;
  162.                 ActProbe.clockdn_probe = false;
  163.             }
  164.         }
  165.         repaint();
  166.     }
  167.  
  168. //----------------------------------------------------------------------------
  169. // User clicked a mousebutton
  170. //----------------------------------------------------------------------------
  171.     public boolean mouseDown(Event event, int x, int y) {
  172.         Probe ActProbe;
  173.         int ix, yo;
  174.         PressedProbe = null;
  175.         DragProbe = null;
  176.  
  177.         for (ix = 0; ix < probes.size(); ix++) {
  178.             yo = ix * CHANNEL_HEIGHT + BUTTON_Y_OFFSET - myVertical.getValue();
  179.             if ( x >= BUTTON_X_OFFSET && x <= BUTTON_X_OFFSET + 24 &&
  180.                  y >= yo && y <= yo + 24) {
  181.                 ButtonPressed(ix);
  182.             }
  183.         }
  184.         for (ix = 0; ix < probes.size(); ix++) {
  185.             ActProbe = (Probe)probes.elementAt (ix);
  186.             if ( x >= ActProbe.ChannelPos.x && y >= ActProbe.ChannelPos.y &&
  187.                  x <= ActProbe.ChannelPos.x + ActProbe.ChannelDim.width &&
  188.                  y <= ActProbe.ChannelPos.y + ActProbe.ChannelDim.height) {
  189.                 DragProbe = ActProbe;
  190.                 repaint();
  191.                 return true;
  192.             }
  193.         }
  194.  
  195.         return true;
  196.     }
  197.  
  198. //----------------------------------------------------------------------------
  199. // The user releases the mouse button
  200. //----------------------------------------------------------------------------
  201.     public boolean mouseUp(Event event, int x, int y) {
  202.         if (DragProbe != null) {
  203.             DragProbe = null;
  204.             repaint();
  205.         }
  206.         if (PressedProbe != null) {
  207.             PressedProbe = null;
  208.             repaint();
  209.         }
  210.         return true;
  211.     }
  212.  
  213. //----------------------------------------------------------------------------
  214. // The user moves the mouse
  215. //----------------------------------------------------------------------------
  216.     public boolean mouseDrag(Event evt, int x, int y) {
  217.         Probe ActProbe;
  218.         int dpix, ix;
  219.  
  220.         if (DragProbe != null) {
  221.             for (ix = 0; ix < probes.size(); ix++) {
  222.                 ActProbe = (Probe)probes.elementAt (ix);
  223.                 if ( x >= ActProbe.ChannelPos.x && y >= ActProbe.ChannelPos.y &&
  224.                      x <= ActProbe.ChannelPos.x + ActProbe.ChannelDim.width &&
  225.                      y <= ActProbe.ChannelPos.y + ActProbe.ChannelDim.height) {
  226.                     if (ActProbe != DragProbe) {
  227.                         // System.out.println ("Swap probes");
  228.                         // Swap ActProbe and DragProbe.
  229.                         applet.MySchematic.SwapComponents (ActProbe, DragProbe);
  230.                         repaint();
  231.                         return true;
  232.                     }
  233.                 }
  234.             }
  235.         }
  236.         return true;
  237.     }
  238.  
  239. //----------------------------------------------------------------------------
  240. // Draw the history levels of Probe ActProbe
  241. //----------------------------------------------------------------------------
  242.     public void DrawProbeHistory (Graphics g, Probe ActProbe) {
  243.         g.setColor (Color.green);
  244.  
  245.         for (int ix = 0; ix < ActProbe.MAX_HISTORY; ix++) {
  246.             if (ix > 0 && ActProbe.LevelHistory[ix - 1] != ActProbe.LevelHistory[ix]) {
  247.                 og.drawLine (ActProbe.ChannelPos.x + ix * CLOCK_WIDTH, ActProbe.ChannelPos.y + LINE_OFFSET,
  248.                 ActProbe.ChannelPos.x + ix * CLOCK_WIDTH, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET);
  249.             }
  250.             if (ActProbe.LevelHistory[ix] == 5) {
  251.                 g.setColor (Color.green);
  252.                 g.drawLine (ActProbe.ChannelPos.x + ix * CLOCK_WIDTH, ActProbe.ChannelPos.y + LINE_OFFSET,
  253.                             ActProbe.ChannelPos.x + (ix + 1 ) * CLOCK_WIDTH, ActProbe.ChannelPos.y + LINE_OFFSET);
  254.             } else {
  255.                 g.setColor (Color.green);
  256.                 g.drawLine (ActProbe.ChannelPos.x + ix * CLOCK_WIDTH, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET,
  257.                             ActProbe.ChannelPos.x + (ix + 1) * CLOCK_WIDTH, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET);
  258.             }
  259.         }
  260.     }
  261.  
  262. //----------------------------------------------------------------------------
  263. // There are no probes available, display a message
  264. //----------------------------------------------------------------------------
  265.     public void DrawNoProbes(Graphics g) {
  266.         String message = "There are no probes to display.";
  267.         int StringWidth = AnalyzerFontMetrics.stringWidth(message);
  268.         int x = (size().width - StringWidth) / 2;
  269.         g.setColor (Color.black);
  270.         g.drawString (message, x, size().height / 2);
  271.     }
  272.  
  273. //----------------------------------------------------------------------------
  274. // Draw the empty probes
  275. //----------------------------------------------------------------------------
  276.     public void DrawEmptyProbes(Graphics g) {
  277.         probes = applet.MySchematic.getProbes();
  278.         Probe ActProbe;
  279.         int x, yo, w;
  280.         int MaxTextWidth = 30;
  281.         int SliderVal = myVertical.getValue();
  282.  
  283.         g.setColor (Color.lightGray);
  284.         g.fillRect (0, 0, ImageSize.width, ImageSize.height);
  285.  
  286.         if (probes.size() == 0) {
  287.             DrawNoProbes(g);
  288.             return;
  289.         }
  290.         // determine Max. text size
  291.         for (int ix = 0; ix < probes.size(); ix++) {
  292.             ActProbe = (Probe) probes.elementAt (ix);
  293.             w = AnalyzerFontMetrics.stringWidth(ActProbe.IPin[0].getName());
  294.             if (w > MaxTextWidth) MaxTextWidth = w;
  295.         }
  296.  
  297.         for (int ix = 0; ix < probes.size(); ix++) {
  298.             ActProbe = (Probe) probes.elementAt (ix);
  299.             yo = ix * CHANNEL_HEIGHT - SliderVal;
  300.             g.setColor (TextColor);
  301.             g.drawString (ActProbe.IPin[0].getName(), 5, yo + 16);
  302.  
  303.             g.setColor (Color.gray);
  304.             g.drawLine (0, yo + CHANNEL_HEIGHT - 2, size().width, yo + CHANNEL_HEIGHT - 2);
  305.             g.setColor (Color.white);
  306.             g.drawLine (0, yo + CHANNEL_HEIGHT - 1, size().width, yo + CHANNEL_HEIGHT - 1);
  307.             ActProbe.ChannelPos.x  = DISPLAY_OFFSET + MaxTextWidth + 5;
  308.             ActProbe.ChannelPos.y  = DISPLAY_OFFSET + yo;
  309.             ActProbe.ChannelDim.width = size().width - ((DISPLAY_OFFSET) + MaxTextWidth + 20);
  310.             ActProbe.ChannelDim.height = CHANNEL_HEIGHT - 2 * DISPLAY_OFFSET;
  311.             g.setColor (BackGroundColor);
  312.             g.fillRect (ActProbe.ChannelPos.x, ActProbe.ChannelPos.y + 1, ActProbe.ChannelDim.width, ActProbe.ChannelDim.height - 1);
  313.             if (ActProbe.clockup_probe) {
  314.                 if (ActProbe == PressedProbe) {
  315.                     SelectButton (BUTTON_CLOCK_UP_PRESSED);
  316.                 } else {
  317.                     SelectButton (BUTTON_CLOCK_UP_SELECTED);
  318.                 }
  319.             } else if (ActProbe.clockdn_probe) {
  320.                 if (ActProbe == PressedProbe) {
  321.                     SelectButton (BUTTON_CLOCK_DN_PRESSED);
  322.                 } else {
  323.                     SelectButton (BUTTON_CLOCK_DN_SELECTED);
  324.                 }
  325.  
  326.             } else {
  327.                 if (ActProbe == PressedProbe) {
  328.                     SelectButton (BUTTON_CLOCK_NONE_PRESSED);
  329.                 } else {
  330.                     SelectButton (BUTTON_CLOCK_NONE);
  331.                 }
  332.             }
  333.             DrawProbeHistory (g, ActProbe);
  334.             Graphics ibg = ImageBuffer.getGraphics();
  335.             ibg.drawImage (CopyImage, 0, 0, this);
  336.             g.drawImage(ImageBuffer, BUTTON_X_OFFSET, BUTTON_Y_OFFSET + yo, this);
  337.         }
  338.  
  339.         if (DragProbe != null) {
  340.             g.setColor (Color.red);
  341.             g.drawRect (DragProbe.ChannelPos.x, DragProbe.ChannelPos.y, DragProbe.ChannelDim.width, DragProbe.ChannelDim.height);
  342.         }
  343.     }
  344.  
  345. //----------------------------------------------------------------------------
  346. // Copy the off-screen image to the screen
  347. //----------------------------------------------------------------------------
  348.     public synchronized void paint (Graphics g) {
  349.     if (OffScreenImage == null) return;
  350.         AdjustScrollbar();
  351.         if (probes != null && size().height >= CHANNEL_HEIGHT * probes.size()) {
  352.             update (g);
  353.             return;
  354.        }
  355.        if (!ImageUpdated) {
  356. //          System.out.println ("update()");
  357.             update(g);
  358.             return;
  359.         }
  360.         g.drawImage (OffScreenImage, 0, 0, this);
  361.     }
  362.  
  363. //----------------------------------------------------------------------------
  364. // Prepare the off-screen image and copy it to the screen.
  365. //----------------------------------------------------------------------------
  366.     public synchronized void update (Graphics g) {
  367.         AdjustScrollbar();
  368.         CheckOffScreenImage();
  369.         if (OffScreenImage == null) return;
  370.         DrawEmptyProbes (og);
  371.         g.drawImage (OffScreenImage, 0, 0, this);
  372.         ImageUpdated = true;
  373.     }
  374.  
  375. //----------------------------------------------------------------------------
  376. // Handle the events of the analyzer
  377. //----------------------------------------------------------------------------
  378.     public boolean handleEvent(Event ev) {
  379.         if (ev.target instanceof Scrollbar) {
  380.             repaint();
  381.             return true;
  382.         }
  383.         return super.handleEvent(ev);
  384.     }
  385.  
  386. //----------------------------------------------------------------------------
  387. // Handle the actions of the analyzer
  388. //----------------------------------------------------------------------------
  389.     public boolean action(Event ev, Object arg) {
  390.         if (ev.target instanceof Button) {
  391.         }
  392.         return false;
  393.     }
  394.  
  395. //----------------------------------------------------------------------------
  396. // Update the new levels of Probe ActProbe
  397. //----------------------------------------------------------------------------
  398.     public void update(Probe ActProbe) {
  399.         int x = CurrentCol * CLOCK_WIDTH;
  400.         if ( x >= ActProbe.ChannelDim.width) {
  401.             if (ActProbe.clockup_probe &&
  402.                 ActProbe.OldLevel == 0 && ActProbe.IPin[0].getLevel() == 5) {
  403.                     CurrentCol = 0;     // Clock trigger
  404.             } else if (ActProbe.clockdn_probe &&
  405.                 ActProbe.OldLevel == 5 && ActProbe.IPin[0].getLevel() == 0) {
  406.                     CurrentCol = 0;     // Clock trigger
  407.             }
  408.         }
  409.     }
  410.  
  411. //----------------------------------------------------------------------------
  412. // Draw a new Level.
  413. //----------------------------------------------------------------------------
  414.     public void drawLevel (Probe ActProbe) {
  415.         int x = CurrentCol * CLOCK_WIDTH;
  416.         if ( x >= ActProbe.ChannelDim.width) return;
  417.         if (CurrentCol == 0) {
  418.             og.setColor (BackGroundColor);
  419.             og.fillRect (ActProbe.ChannelPos.x + x, ActProbe.ChannelPos.y + 1,
  420.                          CLOCK_WIDTH + 1, ActProbe.ChannelDim.height - 1);
  421.         }
  422.         og.setColor (BackGroundColor);
  423.         og.fillRect (ActProbe.ChannelPos.x + x + 1, ActProbe.ChannelPos.y + 1,
  424.                      CLOCK_WIDTH, ActProbe.ChannelDim.height - 1);
  425.  
  426.         if (CurrentCol > 0 && ActProbe.IPin[0].getLevel() != ActProbe.OldLevel) {
  427.              og.setColor (Color.green);
  428.              og.drawLine (ActProbe.ChannelPos.x + x, ActProbe.ChannelPos.y + LINE_OFFSET,
  429.                           ActProbe.ChannelPos.x + x, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET);
  430.         }
  431.         if (CurrentCol < ActProbe.MAX_HISTORY) {
  432.             ActProbe.LevelHistory[CurrentCol] = ActProbe.IPin[0].getLevel();
  433.         }
  434.         if (ActProbe.IPin[0].getLevel() == 5) {
  435.             og.setColor (Color.green);
  436.             og.drawLine (ActProbe.ChannelPos.x + x, ActProbe.ChannelPos.y + LINE_OFFSET,
  437.                          ActProbe.ChannelPos.x + x + CLOCK_WIDTH, ActProbe.ChannelPos.y + LINE_OFFSET);
  438.         } else {
  439.              og.setColor (Color.green);
  440.              og.drawLine (ActProbe.ChannelPos.x + x, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET,
  441.                           ActProbe.ChannelPos.x + x + CLOCK_WIDTH, ActProbe.ChannelPos.y + ActProbe.ChannelDim.height - LINE_OFFSET);
  442.         }
  443.  
  444.         paint (getGraphics());
  445.     }
  446.  
  447. //----------------------------------------------------------------------------
  448. // Update the new levels of all probes
  449. //----------------------------------------------------------------------------
  450.     public void update() {
  451.         Probe ActProbe;
  452.         int ix;
  453.  
  454.         if (probes == null) return;
  455.         for (ix = 0; ix < probes.size(); ix++) {
  456.             ActProbe = (Probe) probes.elementAt (ix);
  457.             update (ActProbe);
  458.         }
  459.         for (ix = 0; ix < probes.size(); ix++) {
  460.             ActProbe = (Probe) probes.elementAt (ix);
  461.             drawLevel (ActProbe);
  462.             ActProbe.OldLevel = ActProbe.IPin[0].getLevel();
  463.         }
  464.         paint(getGraphics());
  465.         CurrentCol++;
  466.     }
  467.  
  468. //----------------------------------------------------------------------------
  469. // Load the image with the buttons
  470. //----------------------------------------------------------------------------
  471.     public void LoadButtonsImage() {
  472.         Image ButtonsImage;
  473.         MediaTracker tracker = new MediaTracker(this);
  474.         ButtonsImage = applet.getImage(applet.getDocumentBase(), "images/all_analyzer.gif");
  475.         tracker.addImage(ButtonsImage, 0);
  476.  
  477.         try {
  478.             Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  479.             tracker.waitForAll();
  480.         } catch(Exception e) {
  481.             String message = e.toString();
  482.             String DlgButtons[] = { "OK" };
  483.             SimpleDialog ExceptionDialog = new SimpleDialog(null, "Reading imagebuttons", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  484.             return;
  485.         }
  486.         if (tracker.isErrorAny()) {
  487.             ImageButtonsDisabled = true;
  488.             String message = "Can't read images/all_analyzer.gif ImageButtons will be disabled";
  489.             String DlgButtons[] = { "OK" };
  490.             SimpleDialog ErrorDialog = new SimpleDialog(null, "Error while reading imagebuttons", message, DlgButtons, 1, 0, 0, SimpleDialog.IMAGE_STOP);
  491.             return;
  492.         }
  493.         ImageBuffer = applet.createImage (ButtonsImage.getWidth(this), 24);
  494.         CopyImage = applet.createImage (ButtonsImage.getWidth(this), ButtonsImage.getHeight(this));
  495.         cig = CopyImage.getGraphics();
  496.         cig.drawImage (ButtonsImage, 0, 0, this);
  497.     }
  498. }