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