home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Netfusion / data1.cab / Components / DynaButtons / DynaButtons.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-12-10  |  11.3 KB  |  388 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.       String target = popObject.getButtonTarget();
  93.       if (curURL != null) {
  94.          if (target != null) {
  95.             ((Applet)this).getAppletContext().showDocument(curURL, target);
  96.          } else {
  97.             ((Applet)this).getAppletContext().showDocument(curURL);
  98.          }
  99.  
  100.       } else {
  101.          if (!this.subPanelUp) {
  102.             String[] stringList = popObject.getStringList();
  103.             this.subPanel = new Panel();
  104.             this.subPanel.setLayout(new GridLayout(stringList.length, 1));
  105.             int buttonNum = popObject.getButtonNum();
  106.  
  107.             for(int i = 0; i < stringList.length; ++i) {
  108.                URL buttonURL = popObject.getURLList()[i];
  109.                String buttonTarget = popObject.getTargetList()[i];
  110.                PopObject po = new PopObject(-1, buttonURL, buttonTarget, (URL[])null, (String[])null, (String[])null);
  111.                if (this.backgroundColor != null) {
  112.                   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));
  113.                } else {
  114.                   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));
  115.                }
  116.             }
  117.  
  118.             if (this.orientation == 0) {
  119.                this.subPanel.reshape(this.BUTTON_WIDTH, buttonNum * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  120.             } else {
  121.                this.subPanel.reshape(buttonNum * this.BUTTON_WIDTH, this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT * stringList.length);
  122.             }
  123.  
  124.             ((Container)this).add(this.subPanel);
  125.             ((Container)this).validate();
  126.             this.subPanelUp = true;
  127.             this.currentButtonNum = buttonNum;
  128.          } else {
  129.             ((Container)this).remove(this.subPanel);
  130.             this.subPanelUp = false;
  131.             if (popObject.getButtonNum() == this.currentButtonNum) {
  132.                this.subPanel = new Panel();
  133.                ((Container)this).add(this.subPanel);
  134.                ((Container)this).validate();
  135.                this.currentButtonNum = -1;
  136.             } else {
  137.                this.actionMouseDown(popObject);
  138.             }
  139.          }
  140.  
  141.       }
  142.    }
  143.  
  144.    protected int countSubEntries(int buttonNum) {
  145.       int subCount;
  146.       for(subCount = 0; ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + subCount) != null; ++subCount) {
  147.       }
  148.  
  149.       return subCount;
  150.    }
  151.  
  152.    protected void getTextAlignment() {
  153.       this.textAlignment = Integer.parseInt(((Applet)this).getParameter("TextAlignment"));
  154.    }
  155.  
  156.    public void paint(Graphics g) {
  157.       if (this.backgroundImage != null) {
  158.          int imHeight = this.backgroundImage.getHeight(this);
  159.          int imWidth = this.backgroundImage.getWidth(this);
  160.          Dimension d = ((Component)this).size();
  161.          int x = (d.width + this.xOffset) / imWidth + 1;
  162.          int y = (d.height + this.yOffset) / imHeight + 1;
  163.  
  164.          for(int i = 0; i < y; ++i) {
  165.             for(int j = 0; j < x; ++j) {
  166.                g.drawImage(this.backgroundImage, j * imWidth - this.xOffset, i * imHeight - this.yOffset, this);
  167.             }
  168.          }
  169.       }
  170.  
  171.    }
  172.  
  173.    private String modifyStringContext(String param) {
  174.       if (param.startsWith(".")) {
  175.          param = param.replace('\\', '/');
  176.          param = this.documentBase + "/" + param;
  177.       } else if (param.startsWith("#")) {
  178.          param = ((Applet)this).getDocumentBase().toString() + param;
  179.       } 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++:") && !param.startsWith("javascript:")) {
  180.          param = "http://" + param;
  181.       }
  182.  
  183.       return param;
  184.    }
  185.  
  186.    private void getNumButtons() {
  187.       int i;
  188.       for(i = 0; ((Applet)this).getParameter("ButtonText" + i) != null; ++i) {
  189.       }
  190.  
  191.       this.numButtons = i;
  192.    }
  193.  
  194.    private void parseDocumentBase() {
  195.       String fullBase = ((Applet)this).getDocumentBase().toString();
  196.       this.documentBase = fullBase.substring(0, fullBase.lastIndexOf(47));
  197.    }
  198.  
  199.    protected URL[] getURLList(int buttonNum) {
  200.       int subCount = this.countSubEntries(buttonNum);
  201.       URL[] URLList = new URL[subCount];
  202.  
  203.       for(int i = 0; i < subCount; ++i) {
  204.          String theUrl = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum + "_" + i));
  205.  
  206.          try {
  207.             URLList[i] = new URL(theUrl);
  208.          } catch (Exception var8) {
  209.             URLList[i] = null;
  210.          }
  211.       }
  212.  
  213.       return URLList;
  214.    }
  215.  
  216.    protected String[] getTargetList(int buttonNum) {
  217.       int subCount = this.countSubEntries(buttonNum);
  218.       String[] targetList = new String[subCount];
  219.  
  220.       for(int i = 0; i < subCount; ++i) {
  221.          targetList[i] = ((Applet)this).getParameter("Target" + buttonNum + "_" + i);
  222.       }
  223.  
  224.       return targetList;
  225.    }
  226.  
  227.    protected void setCorrectLayout() {
  228.       ((Container)this).setLayout((LayoutManager)null);
  229.    }
  230.  
  231.    public void actionMouseExit(PopObject message) {
  232.    }
  233.  
  234.    protected String[] getStringList(int buttonNum) {
  235.       int subCount = this.countSubEntries(buttonNum);
  236.       String[] stringList = new String[subCount];
  237.  
  238.       for(int i = 0; i < subCount; ++i) {
  239.          String param = ((Applet)this).getParameter("ButtonText" + buttonNum + "_" + i);
  240.          stringList[i] = param != null ? param : " ";
  241.       }
  242.  
  243.       return stringList;
  244.    }
  245.  
  246.    protected void getBackgroundColor() {
  247.       int backgroundcolor = Integer.parseInt(((Applet)this).getParameter("BackgroundColor"));
  248.       backgroundcolor = this.convertBGRtoRGB(backgroundcolor);
  249.       this.backgroundColor = new Color(backgroundcolor);
  250.    }
  251.  
  252.    private void setBackgroundMode() {
  253.       if (this.backgroundColor != null) {
  254.          ((Component)this).setBackground(this.backgroundColor);
  255.       }
  256.  
  257.    }
  258.  
  259.    private void getBackgroundMode() {
  260.       if (((Applet)this).getParameter("backgroundColor") != null) {
  261.          this.getBackgroundColor();
  262.       } else if (((Applet)this).getParameter("backgroundImage") != null) {
  263.          this.getBackgroundImage();
  264.       }
  265.  
  266.    }
  267.  
  268.    protected void getFontColor() {
  269.       int fontcolor = Integer.parseInt(((Applet)this).getParameter("FontColor"));
  270.       fontcolor = this.convertBGRtoRGB(fontcolor);
  271.       this.fontColor = new Color(fontcolor);
  272.       int highlitefontcolor = Integer.parseInt(((Applet)this).getParameter("HighliteFontColor"));
  273.       highlitefontcolor = this.convertBGRtoRGB(highlitefontcolor);
  274.       this.highliteFontColor = new Color(highlitefontcolor);
  275.    }
  276.  
  277.    protected void initButtonInfo() {
  278.       this.getNumButtons();
  279.       this.getButtonImages();
  280.       this.getButtonFont();
  281.       this.getFontColor();
  282.       this.getTextJustification();
  283.       this.getTextAlignment();
  284.    }
  285.  
  286.    private void initAppletInfo() {
  287.       this.parseDocumentBase();
  288.       this.getOrientation();
  289.       this.getBackgroundMode();
  290.       this.setBackgroundMode();
  291.       this.setCorrectLayout();
  292.    }
  293.  
  294.    public void actionMouseEnter(PopObject message) {
  295.    }
  296.  
  297.    protected void action(PopObject message) {
  298.       switch (message.getMessageType()) {
  299.          case 0:
  300.             this.actionMouseExit(message);
  301.             break;
  302.          case 1:
  303.             this.actionMouseEnter(message);
  304.             break;
  305.          case 2:
  306.             this.actionMouseDown(message);
  307.       }
  308.  
  309.    }
  310.  
  311.    private int convertBGRtoRGB(int BGRColor) {
  312.       int r = (BGRColor & 255) << 16;
  313.       int g = BGRColor & '\uff00';
  314.       int b = (BGRColor & 16711680) >> 16;
  315.       return r + g + b;
  316.    }
  317.  
  318.    protected URL getButtonURL(int buttonNum) {
  319.       URL url;
  320.       try {
  321.          String param = this.modifyStringContext(((Applet)this).getParameter("URL" + buttonNum));
  322.          url = new URL(param);
  323.       } catch (Exception var5) {
  324.          url = null;
  325.       }
  326.  
  327.       return url;
  328.    }
  329.  
  330.    public void init() {
  331.       this.xOffset = Integer.parseInt(((Applet)this).getParameter("X Position"));
  332.       this.yOffset = Integer.parseInt(((Applet)this).getParameter("Y Position"));
  333.       this.initAppletInfo();
  334.       this.initButtonInfo();
  335.  
  336.       for(int i = 0; i < this.numButtons; ++i) {
  337.          String param = ((Applet)this).getParameter("ButtonText" + i);
  338.          String targetParam = ((Applet)this).getParameter("Target" + i);
  339.          String buttonText = param != null ? param : " ";
  340.          URL buttonURL = this.getButtonURL(i);
  341.          URL[] URLList = this.getURLList(i);
  342.          String[] stringList = this.getStringList(i);
  343.          String[] theTargetList = this.getTargetList(i);
  344.          PopObject popObject = new PopObject(i, buttonURL, targetParam, URLList, theTargetList, stringList);
  345.          PopButton aButton;
  346.          if (this.backgroundColor != null) {
  347.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundColor);
  348.          } else {
  349.             aButton = new PopButton(this, popObject, buttonText, this.fontColor, this.highliteFontColor, this.buttonFont, this.textJustification, this.textAlignment, this.buttonImage, this.highliteImage, this.backgroundImage);
  350.          }
  351.  
  352.          if (this.orientation == 0) {
  353.             ((Component)aButton).reshape(0, i * this.BUTTON_HEIGHT, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  354.          } else {
  355.             ((Component)aButton).reshape(i * this.BUTTON_WIDTH, 0, this.BUTTON_WIDTH, this.BUTTON_HEIGHT);
  356.          }
  357.  
  358.          ((Container)this).add(aButton);
  359.       }
  360.  
  361.    }
  362.  
  363.    protected void getButtonFont() {
  364.       int fontSize = Integer.parseInt(((Applet)this).getParameter("FontSize"));
  365.       String buttonfont = ((Applet)this).getParameter("ButtonFont");
  366.       int fontStyle = 0;
  367.       if (((Applet)this).getParameter("Bold") != null && ((Applet)this).getParameter("Italic") != null) {
  368.          fontStyle = 3;
  369.       } else if (((Applet)this).getParameter("Bold") != null) {
  370.          fontStyle = 1;
  371.       } else if (((Applet)this).getParameter("Italic") != null) {
  372.          fontStyle = 2;
  373.       }
  374.  
  375.       fontSize += 3;
  376.       this.buttonFont = new Font(buttonfont, fontStyle, fontSize);
  377.    }
  378.  
  379.    private void getOrientation() {
  380.       if (((Applet)this).getParameter("Orientation").equals("Horizontal")) {
  381.          this.orientation = 1;
  382.       } else {
  383.          this.orientation = 0;
  384.       }
  385.  
  386.    }
  387. }
  388.