home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 September / pcp155a.iso / linux / ldp / Outline.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-05-02  |  13.3 KB  |  459 lines

  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Component;
  4. import java.awt.Cursor;
  5. import java.awt.Event;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.net.URL;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import netscape.javascript.JSObject;
  15.  
  16. public class Outline extends Applet {
  17.    private static final Font theFont = new Font("Helvetica", 0, 11);
  18.    private transient Hashtable leafAtRow = new Hashtable();
  19.    private int selectedRow = -1;
  20.    private transient boolean jdk10 = true;
  21.    private String dataFile;
  22.    // $FF: renamed from: fd FolderData
  23.    private FolderData field_0;
  24.    private String iconFile;
  25.    private String target = "_self";
  26.    private boolean useWarnings = false;
  27.    private String jsFunction = "";
  28.    private Color colBack;
  29.    private Color colFore;
  30.    private Color colText;
  31.    private Color colLink;
  32.    private Color colSelect;
  33.    private transient int rowNo;
  34.    private transient long timeLastClicked;
  35.    private transient int lastRowClicked;
  36.    private transient Image imgGraphics;
  37.    private transient int iconHeight;
  38.    private transient int iconWidth;
  39.  
  40.    public Outline() {
  41.       this.colBack = Color.white;
  42.       this.colFore = Color.gray;
  43.       this.colText = Color.black;
  44.       this.colLink = new Color(0, 0, 128);
  45.       this.colSelect = new Color(0, 0, 128);
  46.       this.timeLastClicked = 0L;
  47.       this.lastRowClicked = -1;
  48.       this.iconHeight = 16;
  49.       this.iconWidth = 16;
  50.       if (!System.getProperty("java.version").substring(0, 3).equals("1.0")) {
  51.          try {
  52.             ((Component)this).setCursor(Cursor.getPredefinedCursor(0));
  53.             this.jdk10 = false;
  54.          } catch (Exception var2) {
  55.             this.jdk10 = true;
  56.          }
  57.       } else {
  58.          this.jdk10 = true;
  59.       }
  60.  
  61.    }
  62.  
  63.    public String getAppletInfo() {
  64.       return "Name:\t\tOutline Tree Control\r\nAuthor:\t\tDavid Griffiths\r\nAvailable from:\thttp://www.demon.co.uk/davidg\r\nCreated with Sun JDK 1.1";
  65.    }
  66.  
  67.    public String getDataFile() {
  68.       return this.dataFile;
  69.    }
  70.  
  71.    public void setDataFile(String dataFile) {
  72.       this.dataFile = dataFile;
  73.       if (this.getIconFile() != null) {
  74.          this.field_0 = new FolderData(this.getDataFile(), this.getIcon(this.getIconFile(), 3), this.getIcon(this.getIconFile(), 2), this.getIcon(this.getIconFile(), 0), this.getIcon(this.getIconFile(), 1), this.getTarget(), this);
  75.       }
  76.  
  77.    }
  78.  
  79.    public String getIconFile() {
  80.       return this.iconFile;
  81.    }
  82.  
  83.    public void setIconFile(String iconFile) {
  84.       this.iconFile = iconFile;
  85.       if (this.getDataFile() != null) {
  86.          this.field_0 = new FolderData(this.getDataFile(), this.getIcon(this.getIconFile(), 3), this.getIcon(this.getIconFile(), 2), this.getIcon(this.getIconFile(), 0), this.getIcon(this.getIconFile(), 1), this.getTarget(), this);
  87.       }
  88.  
  89.    }
  90.  
  91.    public String getTarget() {
  92.       return this.target;
  93.    }
  94.  
  95.    public void setTarget(String target) {
  96.       this.target = target;
  97.    }
  98.  
  99.    public void setWarnings(String warnings) {
  100.       warnings = warnings.toLowerCase();
  101.       if (!warnings.equals("true") && !warnings.equals("on") && !warnings.equals("1") && !warnings.equals("yes")) {
  102.          this.setWarnings(false);
  103.       } else {
  104.          this.setWarnings(true);
  105.       }
  106.  
  107.    }
  108.  
  109.    public void setWarnings(boolean useWarnings) {
  110.       this.useWarnings = useWarnings;
  111.    }
  112.  
  113.    public void toggleWarnings() {
  114.       this.useWarnings = !this.useWarnings;
  115.    }
  116.  
  117.    public boolean getWarnings() {
  118.       return this.useWarnings;
  119.    }
  120.  
  121.    public void setJSFunction(String jsFunction) {
  122.       this.jsFunction = jsFunction;
  123.       if (jsFunction.equals("")) {
  124.          this.setWarnings(false);
  125.       }
  126.  
  127.    }
  128.  
  129.    public String getJSFunction() {
  130.       return this.jsFunction;
  131.    }
  132.  
  133.    public boolean callWarning(URL href, String frame, String warning) {
  134.       String sHref = href.toString();
  135.       return this.callWarning(sHref, frame, warning);
  136.    }
  137.  
  138.    public boolean callWarning(String href, String frame, String warning) {
  139.       try {
  140.          JSObject window = JSObject.getWindow(this);
  141.          Object[] args = new Object[]{href, frame, warning};
  142.          window.call(this.getJSFunction(), args);
  143.          boolean var6 = true;
  144.          return var6;
  145.       } catch (Exception e) {
  146.          ((Throwable)e).printStackTrace();
  147.          boolean args = false;
  148.          return args;
  149.       }
  150.    }
  151.  
  152.    private void setColBack(Color colBack) {
  153.       this.colBack = colBack;
  154.       ((Component)this).setBackground(this.colBack);
  155.    }
  156.  
  157.    private void setColBackValue(String col) {
  158.       this.setColBack(this.strToCol(col));
  159.    }
  160.  
  161.    private Color getColBack() {
  162.       return this.colBack;
  163.    }
  164.  
  165.    private void setColFore(Color colFore) {
  166.       this.colFore = colFore;
  167.       ((Component)this).setForeground(this.colFore);
  168.    }
  169.  
  170.    private void setColForeValue(String col) {
  171.       this.setColFore(this.strToCol(col));
  172.    }
  173.  
  174.    private Color strToCol(String c) {
  175.       Color strToCol = null;
  176.       if (c != null) {
  177.          strToCol = new Color(Integer.parseInt(c.substring(1), 16));
  178.       }
  179.  
  180.       return strToCol;
  181.    }
  182.  
  183.    private Color getColFore() {
  184.       return this.colFore;
  185.    }
  186.  
  187.    private void setColText(Color colText) {
  188.       this.colText = colText;
  189.    }
  190.  
  191.    private void setColTextValue(String col) {
  192.       this.setColText(this.strToCol(col));
  193.    }
  194.  
  195.    private Color getColText() {
  196.       return this.colText;
  197.    }
  198.  
  199.    private void setColLink(Color colLink) {
  200.       this.colLink = colLink;
  201.    }
  202.  
  203.    private void setColLinkValue(String col) {
  204.       this.setColLink(this.strToCol(col));
  205.    }
  206.  
  207.    private Color getColLink() {
  208.       return this.colLink;
  209.    }
  210.  
  211.    private void setColSelect(Color colSelect) {
  212.       this.colSelect = colSelect;
  213.    }
  214.  
  215.    private void setColSelectValue(String col) {
  216.       this.setColSelect(this.strToCol(col));
  217.    }
  218.  
  219.    private Color getColSelect() {
  220.       return this.colSelect;
  221.    }
  222.  
  223.    public void init() {
  224.       this.setColBackValue(this.getParam("BGCOLOR", "#FFFFFF"));
  225.       this.setColForeValue(this.getParam("FORECOLOR", "#808080"));
  226.       this.setColTextValue(this.getParam("TEXTCOLOR", "#000000"));
  227.       this.setColLinkValue(this.getParam("LINKCOLOR", "#0000C0"));
  228.       this.setColSelectValue(this.getParam("SELECTCOLOR", "#0000C0"));
  229.       this.setTarget(this.getParam("TARGET", "_self"));
  230.       this.setWarnings(this.getParam("WARNINGS", "false"));
  231.       this.setJSFunction(this.getParam("WARNFUNC", ""));
  232.       this.setIconFile(this.getParam("ICONFILE", "icons.gif"));
  233.       this.setDataFile(this.getParam("DATAFILE", "data.xml"));
  234.       System.out.println(this.getAppletInfo());
  235.    }
  236.  
  237.    private String getParam(String name, String dflt) {
  238.       String s = ((Applet)this).getParameter(name);
  239.       if (s == null) {
  240.          s = dflt;
  241.       }
  242.  
  243.       return s;
  244.    }
  245.  
  246.    private void repaintRow(int row) {
  247.       ((Component)this).repaint(0, row * this.iconHeight - 3, ((Component)this).size().width, this.iconHeight + 6);
  248.    }
  249.  
  250.    public void paint(Graphics g) {
  251.       this.drawTree(g);
  252.    }
  253.  
  254.    private void drawTree(Graphics g) {
  255.       this.rowNo = 0;
  256.       this.leafAtRow.clear();
  257.       this.drawBranch(g, this.field_0.getRootFolder());
  258.    }
  259.  
  260.    private void drawBranch(Graphics g, Folder parent) {
  261.       int lastRow = -1;
  262.       Folder f = null;
  263.       int childNo = 0;
  264.       lastRow = this.rowNo;
  265.       this.drawRow(g, parent, this.rowNo++);
  266.       if (parent.hasChildren()) {
  267.          Enumeration children = parent.getSubFolders().elements();
  268.          int count = parent.getSubFolders().size();
  269.          if (parent.isOpen() && count > 0) {
  270.             while(children.hasMoreElements()) {
  271.                f = (Folder)children.nextElement();
  272.                lastRow = this.rowNo;
  273.                if (f.hasChildren() && f.isOpen()) {
  274.                   this.drawBranch(g, f);
  275.                } else {
  276.                   this.drawRow(g, f, this.rowNo++);
  277.                }
  278.             }
  279.  
  280.             g.setColor(this.getColBack());
  281.             if (!f.hasChildren()) {
  282.                g.drawLine(19 * f.level - 9, (this.rowNo - 1) * this.iconHeight + 11, 19 * f.level - 9, (this.rowNo - 1) * this.iconHeight + 15);
  283.             }
  284.  
  285.             if (lastRow != this.rowNo && f.isOpen()) {
  286.                g.drawLine(19 * f.level - 9, (lastRow + 1) * this.iconHeight, 19 * f.level - 9, this.rowNo * this.iconHeight);
  287.             }
  288.          }
  289.       }
  290.  
  291.    }
  292.  
  293.    public void drawRow(Graphics g, Folder folder, int row) {
  294.       this.leafAtRow.put(String.valueOf("").concat(String.valueOf(row)), folder);
  295.       this.drawConnectors(g, folder, row);
  296.       this.drawIcon(g, folder, row);
  297.       this.drawCaption(g, folder, row);
  298.    }
  299.  
  300.    private void drawCaption(Graphics g, Folder folder, int row) {
  301.       String caption = folder.getText();
  302.       g.setFont(theFont);
  303.       FontMetrics fm = g.getFontMetrics();
  304.       int textHeight = fm.getHeight();
  305.       int textWidth = fm.stringWidth(caption);
  306.       int descent = fm.getDescent();
  307.       if (row == this.selectedRow) {
  308.          g.setColor(this.getColSelect());
  309.          g.fillRect(19 * (folder.level + 1) + 6 - descent, row * this.iconHeight + fm.getDescent(), textWidth + (descent << 1), textHeight);
  310.          g.setColor(this.getColBack());
  311.       } else {
  312.          g.setColor(this.getColBack());
  313.          if (folder.getHRef() != null) {
  314.             g.setColor(this.getColLink());
  315.          } else {
  316.             g.setColor(this.getColText());
  317.          }
  318.       }
  319.  
  320.       g.drawString(caption, 19 * (folder.level + 1) + 6, row * this.iconHeight + textHeight - 1);
  321.       if (folder.getHRef() != null) {
  322.          g.drawLine(19 * (folder.level + 1) + 6, row * this.iconHeight + textHeight + 1, 19 * (folder.level + 1) + 6 + fm.stringWidth(caption), row * this.iconHeight + textHeight + 1);
  323.       }
  324.  
  325.    }
  326.  
  327.    private void drawConnectors(Graphics g, Folder folder, int row) {
  328.       g.setColor(this.getColFore());
  329.  
  330.       for(int i = 0; i < folder.level - 1; ++i) {
  331.          for(int j = row * this.iconHeight; j < (row + 1) * this.iconHeight; j += 2) {
  332.             g.drawLine(19 * i + 10, j, 19 * i + 10, j);
  333.          }
  334.       }
  335.  
  336.       if (folder.isOpen() && folder.hasChildren()) {
  337.          for(int j = row * this.iconHeight; j < row * this.iconHeight + 8; j += 2) {
  338.             g.drawLine(19 * folder.level - 9, j, 19 * folder.level - 9, j);
  339.          }
  340.       } else {
  341.          for(int j = row * this.iconHeight; j < (row + 1) * this.iconHeight; j += 2) {
  342.             g.drawLine(19 * folder.level - 9, j, 19 * folder.level - 9, j);
  343.          }
  344.       }
  345.  
  346.       if (folder.level > 0) {
  347.          for(int i = 19 * folder.level - 7; i < 19 * folder.level + 2; i += 2) {
  348.             g.drawLine(i, row * this.iconHeight + 10, i, row * this.iconHeight + 10);
  349.          }
  350.       }
  351.  
  352.    }
  353.  
  354.    private void drawIcon(Graphics g, Folder folder, int row) {
  355.       if (folder.hasChildren()) {
  356.          g.setColor(this.getColFore());
  357.          g.drawRect(19 * folder.level - 13, row * this.iconHeight + 6, 8, 8);
  358.          g.setColor(this.getColText());
  359.          g.drawLine(19 * folder.level - 11, row * this.iconHeight + 10, 19 * folder.level - 7, row * this.iconHeight + 10);
  360.          if (!folder.isOpen()) {
  361.             g.drawLine(19 * folder.level - 9, row * this.iconHeight + 8, 19 * folder.level - 9, row * this.iconHeight + 12);
  362.          }
  363.       }
  364.  
  365.       g.drawImage(folder.getIcon(), 19 * (folder.level + 1) - 17, row * this.iconHeight, this);
  366.    }
  367.  
  368.    public boolean mouseUp(Event event, int i, int j) {
  369.       int row = j / this.iconHeight;
  370.       int col = i / 19;
  371.       int oldSelectedRow = this.selectedRow;
  372.       if (row == this.lastRowClicked && System.currentTimeMillis() - this.timeLastClicked < (long)300) {
  373.          this.timeLastClicked = 0L;
  374.       } else {
  375.          this.timeLastClicked = System.currentTimeMillis();
  376.          this.lastRowClicked = row;
  377.          if (this.leafAtRow.containsKey(String.valueOf("").concat(String.valueOf(row)))) {
  378.             Folder f = (Folder)this.leafAtRow.get(String.valueOf("").concat(String.valueOf(row)));
  379.             int indentLevel = f.level;
  380.             boolean clickedPlusMinus = col == indentLevel - 2;
  381.             if (!clickedPlusMinus) {
  382.                this.selectedRow = row;
  383.             }
  384.  
  385.             this.repaintRow(oldSelectedRow);
  386.             if ((clickedPlusMinus || this.selectedRow == row) && f.hasChildren()) {
  387.                f.toggleOpen();
  388.                ((Component)this).repaint(0, row * this.iconHeight - 3, ((Component)this).size().width, ((Component)this).size().height);
  389.             } else {
  390.                this.repaintRow(this.selectedRow);
  391.             }
  392.  
  393.             if (f.getHRef() != null && !clickedPlusMinus) {
  394.                if (this.getWarnings() && f.getWarning() != null) {
  395.                   if (!this.callWarning(f.getHRef(), f.getTarget(), f.getWarning())) {
  396.                      ((Applet)this).getAppletContext().showDocument(f.getHRef(), f.getTarget());
  397.                   }
  398.                } else {
  399.                   ((Applet)this).getAppletContext().showDocument(f.getHRef(), f.getTarget());
  400.                }
  401.             }
  402.          }
  403.       }
  404.  
  405.       return true;
  406.    }
  407.  
  408.    public boolean mouseMove(Event event, int i, int j) {
  409.       if (!this.jdk10) {
  410.          int row = j / this.iconHeight;
  411.          if (this.leafAtRow.containsKey(String.valueOf("").concat(String.valueOf(row)))) {
  412.             Folder f = (Folder)this.leafAtRow.get(String.valueOf("").concat(String.valueOf(row)));
  413.             if (f.getHRef() != null) {
  414.                ((Component)this).setCursor(Cursor.getPredefinedCursor(12));
  415.                ((Applet)this).showStatus(String.valueOf("").concat(String.valueOf(f.getHRef())));
  416.             } else {
  417.                ((Component)this).setCursor(Cursor.getPredefinedCursor(0));
  418.                ((Applet)this).showStatus("");
  419.             }
  420.          } else {
  421.             ((Component)this).setCursor(Cursor.getPredefinedCursor(0));
  422.             ((Applet)this).showStatus("");
  423.          }
  424.       }
  425.  
  426.       return true;
  427.    }
  428.  
  429.    private Image initIcons(String iconFile) {
  430.       Image getIcon = null;
  431.  
  432.       try {
  433.          getIcon = ((Applet)this).getImage(((Applet)this).getCodeBase(), iconFile);
  434.          MediaTracker mt = new MediaTracker(this);
  435.          mt.addImage(getIcon, 0);
  436.          mt.waitForID(0);
  437.          this.iconHeight = getIcon.getHeight(this) / 4;
  438.          this.iconWidth = getIcon.getWidth(this);
  439.       } catch (InterruptedException var4) {
  440.          ((Applet)this).showStatus(String.valueOf(String.valueOf("Error occurred whilst loading ").concat(String.valueOf(this.getIconFile()))).concat(String.valueOf(" file")));
  441.       }
  442.  
  443.       return getIcon;
  444.    }
  445.  
  446.    private Image getIcon(String iconFile, int icon) {
  447.       if (this.imgGraphics == null) {
  448.          this.imgGraphics = this.initIcons(iconFile);
  449.       }
  450.  
  451.       Image getIcon = ((Component)this).createImage(this.iconWidth, this.iconHeight);
  452.       Graphics gDec = getIcon.getGraphics();
  453.       gDec.setColor(this.getColBack());
  454.       gDec.fillRect(0, 0, this.iconWidth, this.iconHeight);
  455.       gDec.drawImage(this.imgGraphics, 0, -this.iconHeight * icon, this);
  456.       return getIcon;
  457.    }
  458. }
  459.