home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 127 / dpcs0998.iso / Internet / netobs / Install.exe / t2.z / DynaButtons.cab / DynaButtons.class (.txt) next >
Encoding:
Java Class File  |  1998-02-11  |  10.7 KB  |  368 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Container;
  5. import java.awt.Dimension;
  6. import java.awt.Font;
  7. import java.awt.Graphics;
  8. import java.awt.GridLayout;
  9. import java.awt.Image;
  10. import java.awt.LayoutManager;
  11. import java.awt.MediaTracker;
  12. import java.awt.Panel;
  13. import java.net.URL;
  14.  
  15. public class DynaButtons extends Applet {
  16.    protected int orientation;
  17.    protected static final int VERT = 0;
  18.    protected static final int HORIZ = 1;
  19.    protected int textJustification;
  20.    protected int textAlignment;
  21.    protected Image buttonImage;
  22.    protected Image highliteImage;
  23.    protected Color backgroundColor;
  24.    protected Image backgroundImage;
  25.    protected Color fontColor;
  26.    protected Color highliteFontColor;
  27.    protected Font buttonFont;
  28.    protected int numButtons;
  29.    protected String documentBase;
  30.    protected int BUTTON_WIDTH;
  31.    protected int BUTTON_HEIGHT;
  32.    Panel subPanel;
  33.    boolean subPanelUp;
  34.    private int currentButtonNum = -1;
  35.    protected int xOffset;
  36.    protected int yOffset;
  37.  
  38.    protected void getTextJustification() {
  39.       this.textJustification = Integer.parseInt(((Applet)this).getParameter("TextJustification"));
  40.    }
  41.  
  42.    private void getButtonImages() {
  43.       String imageName = this.modifyStringContext(((Applet)this).getParameter("ButtonImage"));
  44.       String highliteName = this.modifyStringContext(((Applet)this).getParameter("HighliteImage"));
  45.  
  46.       try {
  47.          this.buttonImage = ((Applet)this).getImage(new URL(imageName));
  48.          this.highliteImage = ((Applet)this).getImage(new URL(highliteName));
  49.          MediaTracker tracker = new MediaTracker(this);
  50.          tracker.addImage(this.buttonImage, 0);
  51.          tracker.addImage(this.highliteImage, 1);
  52.  
  53.          try {
  54.             tracker.waitForID(0);
  55.             tracker.waitForID(1);
  56.          } catch (InterruptedException var6) {
  57.             return;
  58.          } catch (Exception var7) {
  59.             System.out.println("Error in tracker: " + var7);
  60.          }
  61.  
  62.          this.BUTTON_WIDTH = this.buttonImage.getWidth(this);
  63.          this.BUTTON_HEIGHT = this.buttonImage.getHeight(this);
  64.       } catch (Exception var8) {
  65.          System.out.println("Error loading Button Images: " + var8);
  66.       }
  67.  
  68.    }
  69.  
  70.    protected void getBackgroundImage() {
  71.       String backgroundimage = this.modifyStringContext(((Applet)this).getParameter("BackgroundImage"));
  72.  
  73.       try {
  74.          this.backgroundImage = ((Applet)this).getImage(new URL(backgroundimage));
  75.       } catch (Exception var6) {
  76.          System.out.println("Error forming URL for background image");
  77.          return;
  78.       }
  79.  
  80.       MediaTracker tracker = new MediaTracker(this);
  81.       tracker.addImage(this.backgroundImage, 0);
  82.  
  83.       try {
  84.          tracker.waitForID(0);
  85.       } catch (InterruptedException var5) {
  86.          System.out.println("Background image loading interrupted");
  87.       }
  88.    }
  89.  
  90.    protected void actionMouseDown(PopObject popObject) {
  91.       URL curURL = popObject.getButtonURL();
  92.       if (curURL != null) {
  93.          ((Applet)this).getAppletContext().showDocument(curURL);
  94.       } else {
  95.          if (!this.subPanelUp) {
  96.             String[] stringList = popObject.getStringList();
  97.             this.subPanel = new Panel();
  98.             this.subPanel.setLayout(new GridLayout(stringList.length, 1));
  99.             int buttonNum = popObject.getButtonNum();
  100.  
  101.             for(int i = 0; i < stringList.length; ++i) {
  102.                URL buttonURL = popObject.getURLList()[i];
  103.                PopObject po = new PopObject(-1, buttonURL, (URL[])null, (String[])null);
  104.                if (this.backgroundColor != null) {
  105.                   this.subPanel.add(new PopButton(this, po, stringList[i], this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundColor));
  106.                } else {
  107.                   this.subPanel.add(new PopButton(this, po, stringList[i], this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundImage));
  108.                }
  109.             }
  110.  
  111.             if (this.orientation == 0) {
  112.                this.subPanel.reshape(this.BUTTON_WIDTH, buttonNum * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  113.             } else {
  114.                this.subPanel.reshape(buttonNum * this.BUTTON_WIDTH, this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  115.             }
  116.  
  117.             ((Container)this).add(this.subPanel);
  118.             ((Container)this).validate();
  119.             this.subPanelUp = true;
  120.             this.currentButtonNum = buttonNum;
  121.          } else {
  122.             ((Container)this).remove(this.subPanel);
  123.             this.subPanelUp = false;
  124.             if (popObject.getButtonNum() == this.currentButtonNum) {
  125.                this.subPanel = new Panel();
  126.                ((Container)this).add(this.subPanel);
  127.                ((Container)this).validate();
  128.                this.currentButtonNum = -1;
  129.             } else {
  130.                this.actionMouseDown(popObject);
  131.             }
  132.          }
  133.  
  134.       }
  135.    }
  136.  
  137.    protected int countSubEntries(int buttonNum) {
  138.       int subCount;
  139.       for(subCount = 0; ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + subCount) != null; ++subCount) {
  140.       }
  141.  
  142.       return subCount;
  143.    }
  144.  
  145.    protected void getTextAlignment() {
  146.       this.textAlignment = Integer.parseInt(((Applet)this).getParameter("TextAlignment"));
  147.    }
  148.  
  149.    public void paint(Graphics g) {
  150.       if (this.backgroundImage != null) {
  151.          int imHeight = this.backgroundImage.getHeight(this);
  152.          int imWidth = this.backgroundImage.getWidth(this);
  153.          Dimension d = ((Component)this).size();
  154.          int x = (d.width + this.xOffset) / imWidth + 1;
  155.          int y = (d.height + this.yOffset) / imHeight + 1;
  156.  
  157.          for(int i = 0; i < y; ++i) {
  158.             for(int j = 0; j < x; ++j) {
  159.                g.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  160.             }
  161.          }
  162.       }
  163.  
  164.    }
  165.  
  166.    private String modifyStringContext(String param) {
  167.       if (param.startsWith(".")) {
  168.          param = param.replace('\\', '/');
  169.          param = this.documentBase + "/" + param;
  170.       } else if (param.startsWith("#")) {
  171.          param = ((Applet)this).getDocumentBase().toString() + param;
  172.       } else if (!param.startsWith("cid:") && !param.startsWith("lifn:") && !param.startsWith("java:") && !param.startsWith("irc:") && !param.startsWith("IOR:") && !param.startsWith("ilu:") && !param.startsWith("https:") && !param.startsWith("http:") && !param.startsWith("hdl:") && !param.startsWith("gopher:") && !param.startsWith("ftp:") && !param.startsWith("finger:") && !param.startsWith("file:") && !param.startsWith("data:") && !param.startsWith("clsid:") && !param.startsWith("md5:") && !param.startsWith("mailserver:") && !param.startsWith("mailto:") && !param.startsWith("mid:") && !param.startsWith("news:") && !param.startsWith("nntp:") && !param.startsWith("path:") && !param.startsWith("prospero:") && !param.startsWith("service:") && !param.startsWith("shttp") && !param.startsWith("snews") && !param.startsWith("STANF:") && !param.startsWith("telnet:") && !param.startsWith("vemmi:") && !param.startsWith("wais:") && !param.startsWith("whois++:")) {
  173.          param = "http://" + param;
  174.       }
  175.  
  176.       return param;
  177.    }
  178.  
  179.    private void getNumButtons() {
  180.       int i;
  181.       for(i = 0; ((Applet)this).getParameter("ButtonText" + i) != null; ++i) {
  182.       }
  183.  
  184.       this.numButtons = i;
  185.    }
  186.  
  187.    private void parseDocumentBase() {
  188.       String fullBase = ((Applet)this).getDocumentBase().toString();
  189.       this.documentBase = fullBase.substring(0, fullBase.lastIndexOf(47));
  190.    }
  191.  
  192.    protected URL[] getURLList(int buttonNum) {
  193.       int subCount = this.countSubEntries(buttonNum);
  194.       URL[] URLList = new URL[subCount];
  195.  
  196.       for(int i = 0; i < subCount; ++i) {
  197.          String theUrl = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum + "_" + i));
  198.  
  199.          try {
  200.             URLList[i] = new URL(theUrl);
  201.          } catch (Exception var8) {
  202.             URLList[i] = null;
  203.          }
  204.       }
  205.  
  206.       return URLList;
  207.    }
  208.  
  209.    protected void setCorrectLayout() {
  210.       ((Container)this).setLayout((LayoutManager)null);
  211.    }
  212.  
  213.    public void actionMouseExit(PopObject message) {
  214.    }
  215.  
  216.    protected String[] getStringList(int buttonNum) {
  217.       int subCount = this.countSubEntries(buttonNum);
  218.       String[] stringList = new String[subCount];
  219.  
  220.       for(int i = 0; i < subCount; ++i) {
  221.          String param = ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + i);
  222.          stringList[i] = param != null ? param : " ";
  223.       }
  224.  
  225.       return stringList;
  226.    }
  227.  
  228.    protected void getBackgroundColor() {
  229.       int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  230.       backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  231.       this.backgroundColor = new Color(backgroundcolor);
  232.    }
  233.  
  234.    private void setBackgroundMode() {
  235.       if (this.backgroundColor != null) {
  236.          ((Component)this).setBackground(this.backgroundColor);
  237.       }
  238.  
  239.    }
  240.  
  241.    private void getBackgroundMode() {
  242.       if (((Applet)this).getParameter("backgroundColor") != null) {
  243.          this.getBackgroundColor();
  244.       } else if (((Applet)this).getParameter("backgroundImage") != null) {
  245.          this.getBackgroundImage();
  246.       }
  247.  
  248.    }
  249.  
  250.    protected void getFontColor() {
  251.       int fontcolor = Integer.parseInt(((Applet)this).getParameter("FontColor"));
  252.       fontcolor = this.convertBGRtoRGB(fontcolor);
  253.       this.fontColor = new Color(fontcolor);
  254.       int highlitefontcolor = Integer.parseInt(((Applet)this).getParameter("HighliteFontColor"));
  255.       highlitefontcolor = this.convertBGRtoRGB(highlitefontcolor);
  256.       this.highliteFontColor = new Color(highlitefontcolor);
  257.    }
  258.  
  259.    protected void initButtonInfo() {
  260.       this.getNumButtons();
  261.       this.getButtonImages();
  262.       this.getButtonFont();
  263.       this.getFontColor();
  264.       this.getTextJustification();
  265.       this.getTextAlignment();
  266.    }
  267.  
  268.    private void initAppletInfo() {
  269.       this.parseDocumentBase();
  270.       this.getOrientation();
  271.       this.getBackgroundMode();
  272.       this.setBackgroundMode();
  273.       this.setCorrectLayout();
  274.    }
  275.  
  276.    public void actionMouseEnter(PopObject message) {
  277.    }
  278.  
  279.    protected void action(PopObject message) {
  280.       switch (message.getMessageType()) {
  281.          case 0:
  282.             this.actionMouseExit(message);
  283.             break;
  284.          case 1:
  285.             this.actionMouseEnter(message);
  286.             break;
  287.          case 2:
  288.             this.actionMouseDown(message);
  289.       }
  290.  
  291.    }
  292.  
  293.    private int convertBGRtoRGB(int BGRColor) {
  294.       int r = (BGRColor & 255) << 16;
  295.       int g = BGRColor & '\uff00';
  296.       int b = (BGRColor & 16711680) >> 16;
  297.       return r + g + b;
  298.    }
  299.  
  300.    protected URL getButtonURL(int buttonNum) {
  301.       URL url;
  302.       try {
  303.          String param = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum));
  304.          url = new URL(param);
  305.       } catch (Exception var5) {
  306.          url = null;
  307.       }
  308.  
  309.       return url;
  310.    }
  311.  
  312.    public void init() {
  313.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  314.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  315.       this.initAppletInfo();
  316.       this.initButtonInfo();
  317.  
  318.       for(int i = 0; i < this.numButtons; ++i) {
  319.          String param = ((Applet)this).getParameter("ButtonText" + i);
  320.          String buttonText = param != null ? param : " ";
  321.          URL buttonURL = this.getButtonURL(i);
  322.          URL[] URLList = this.getURLList(i);
  323.          String[] stringList = this.getStringList(i);
  324.          PopObject popObject = new PopObject(i, buttonURL, URLList, stringList);
  325.          PopButton aButton;
  326.          if (this.backgroundColor != null) {
  327.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundColor);
  328.          } else {
  329.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundImage);
  330.          }
  331.  
  332.          if (this.orientation == 0) {
  333.             ((Component)aButton).reshape(0, i * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  334.          } else {
  335.             ((Component)aButton).reshape(i * this.BUTTON_WIDTH, 0, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  336.          }
  337.  
  338.          ((Container)this).add(aButton);
  339.       }
  340.  
  341.    }
  342.  
  343.    protected void getButtonFont() {
  344.       int fontSize = Integer.parseInt(((Applet)this).getParameter("FontSize"));
  345.       String buttonfont = ((Applet)this).getParameter("ButtonFont");
  346.       int fontStyle = 0;
  347.       if (((Applet)this).getParameter("Bold") != null && ((Applet)this).getParameter("Italic") != null) {
  348.          fontStyle = 3;
  349.       } else if (((Applet)this).getParameter("Bold") != null) {
  350.          fontStyle = 1;
  351.       } else if (((Applet)this).getParameter("Italic") != null) {
  352.          fontStyle = 2;
  353.       }
  354.  
  355.       fontSize += 3;
  356.       this.buttonFont = new Font(buttonfont, fontStyle, fontSize);
  357.    }
  358.  
  359.    private void getOrientation() {
  360.       if (((Applet)this).getParameter("Orientation").equals("Horizontal")) {
  361.          this.orientation = 1;
  362.       } else {
  363.          this.orientation = 0;
  364.       }
  365.  
  366.    }
  367. }
  368.