home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / iis4_07.cab / CoolHeadLines.java < prev    next >
Text File  |  1997-11-01  |  14KB  |  435 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. // Implementatation of CoolHeadLines class
  3. //
  4. // The main applet.
  5. // 
  6. // This is a part of the Internet Information Server SDK Samples
  7. // Copyright (C) 1997 Microsoft Corporation
  8. // All rights reserved.
  9. //
  10. // This source code is only intended as a supplement to the Software 
  11. // Development Kit Reference and related electronic documentation provided.
  12. //
  13. ////////////////////////////////////////////////////////////////////////////
  14.  
  15. import java.applet.*;
  16. import java.awt.*;
  17. import java.util.*;
  18. import java.net.*;
  19. import TextJump;
  20. import DoubleBuffer;
  21.  
  22. public class CoolHeadLines extends Applet implements Runnable
  23. {
  24.     // constants to gather data from <param> tags
  25.     private final String    PARAM_BackColor = "BackColor";
  26.     private final String    PARAM_TextColor = "TextColor";
  27.     private final String    PARAM_HiliteTextColor = "HiliteTextColor";
  28.     private final String    PARAM_ScrollDelay = "ScrollDelay";
  29.     private final String    PARAM_MessageDelay = "MessageDelay";
  30.     private final String    PARAM_NumItems = "NumItems";
  31.     private final String    PARAM_URLPrefix = "URLPrefix";
  32.  
  33.     // data from <param> tags
  34.     private Color            m_colBackColor;
  35.     private Color            m_colTextColor;
  36.     private Color            m_colHiliteTextColor;
  37.     private int                m_iScrollDelay = 10;
  38.     private int                m_iMessageDelay = 4;
  39.     private int                m_iNumItems;
  40.     private String            m_strURLPrefix;
  41.  
  42.     private Thread            m_CoolHeadLines = null;                // worker thread
  43.     private Dimension        m_dimAppletSize;                    // size of the containing applet
  44.     private Vector            m_vectData=null;                    // vector of TextJump objects
  45.     private Font            m_font;                                // font to draw text with
  46.     private FontMetrics        m_fm;                                // size of the font
  47.     private int                m_iFontHeight;                        // maximum height of font
  48.     private DoubleBuffer    m_buff=null;                        // off-screen buffer
  49.     private boolean            m_fStoppedScrolling = false;        // has the image stopped scrolling (when true mouse-clicks are active)
  50.     private int                m_iCurrentMessage = -1;                // what's the current message at the bottom of the screen?
  51.     private int                m_iMaxMessage=0;                        // how many messages can the applet display at once?
  52.     private int                m_iOldMessageSelected = -1;            // what was the last message the mouse moved over?
  53.     private int                m_iOldMessageSelectedYOffset = -1;    // what was the last message location on screen?
  54.  
  55.     ////////////////////////////////////////////////////////////////////////////
  56.     // ctor
  57.     public CoolHeadLines()
  58.     {
  59.         // set some default colors
  60.         m_colBackColor =       new Color(255,255,128);
  61.         m_colTextColor =        new Color(0,0,0);
  62.         m_colHiliteTextColor = new Color(255,0,0);
  63.     }
  64.  
  65.     ////////////////////////////////////////////////////////////////////////////
  66.     // getAppletInfo
  67.     // info about this applet
  68.     public String getAppletInfo()
  69.     {
  70.         return "Name: CoolHeadLines\r\n" +
  71.                "Author: Michael Howard (mikehow@microsoft.com)\r\n" +
  72.                "Created with Microsoft Visual J++ Version 1.1";
  73.     }
  74.  
  75.     ////////////////////////////////////////////////////////////////////////////
  76.     // getParameterInfo
  77.     // <param> info
  78.     public String[][] getParameterInfo()
  79.     {
  80.         String[][] info =
  81.         {
  82.             { PARAM_BackColor,            "String",    "Background Color" },
  83.             { PARAM_TextColor,            "String",    "Text Color" },
  84.             { PARAM_HiliteTextColor,    "String",    "Hilited Text Color" },
  85.             { PARAM_ScrollDelay,        "int",        "msec delay between each scroll" },
  86.             { PARAM_MessageDelay,        "int",        "sec delay between each message" },
  87.             { PARAM_NumItems,            "int",        "Number of items" },
  88.             { PARAM_URLPrefix,            "String",    "Prefix for all .htm or .asp files" },
  89.         };
  90.  
  91.         return info;        
  92.     }
  93.  
  94.     ////////////////////////////////////////////////////////////////////////////
  95.     // init
  96.     // initialize the applet
  97.     public void init()
  98.     {
  99.         String param;
  100.  
  101.         // background color
  102.         param = getParameter(PARAM_BackColor);
  103.         if (param != null)
  104.             m_colBackColor = getColorFromRGB(param);
  105.  
  106.         // text color
  107.         param = getParameter(PARAM_TextColor);
  108.         if (param != null)
  109.             m_colTextColor = getColorFromRGB(param);
  110.  
  111.         // high-lighted text color
  112.         param = getParameter(PARAM_HiliteTextColor);
  113.         if (param != null)
  114.             m_colHiliteTextColor = getColorFromRGB(param);
  115.  
  116.         // scroll delay in msec
  117.         param = getParameter(PARAM_ScrollDelay);
  118.         if (param != null)
  119.             m_iScrollDelay = Integer.parseInt(param);
  120.  
  121.         // pause between messages in seconds
  122.         param = getParameter(PARAM_MessageDelay);
  123.         if (param != null)
  124.             m_iMessageDelay = Integer.parseInt(param);
  125.  
  126.         // number of items to be displayed
  127.         param = getParameter(PARAM_NumItems);
  128.         if (param != null)
  129.             m_iNumItems = Integer.parseInt(param);
  130.  
  131.         // path for .htm and .asp files
  132.         param = getParameter(PARAM_URLPrefix);
  133.         if (param != null)
  134.             m_strURLPrefix = param;
  135.  
  136.         // various vars
  137.         m_dimAppletSize = size();
  138.         m_font            = new Font("Helvetica",Font.PLAIN,12);
  139.         m_fm            = getFontMetrics(m_font);
  140.         m_iFontHeight    = m_fm.getMaxDescent() + m_fm.getLeading() + m_fm.getMaxAscent();
  141.         Rectangle r        = new Rectangle(0,0,m_dimAppletSize.width,m_dimAppletSize.height + m_iFontHeight);
  142.         m_buff            = new DoubleBuffer(this,r);
  143.  
  144.         // fill in the background of the off-screen buffer and the applet
  145.         m_buff.getGraphics().setColor(m_colBackColor);
  146.         m_buff.getGraphics().fillRect(r.x, r.y, r.width, r.height);
  147.         setBackground(m_colBackColor);
  148.  
  149.         // maximum number of messages
  150.         m_iMaxMessage = 1 +( m_dimAppletSize.height / m_iFontHeight);
  151.  
  152.         // get all the headlines from the <param> tags
  153.         m_vectData = new Vector();
  154.         for (int i=0; i<m_iNumItems; i++)
  155.         {
  156.             String strText = new String();
  157.             strText = getParameter("Text"+i);
  158.  
  159.             String strURL = new String();
  160.             strURL = getParameter("URL"+i);
  161.             m_vectData.addElement(new TextJump(strURL,strText));
  162.         }
  163.     }
  164.  
  165.     ////////////////////////////////////////////////////////////////////////////
  166.     // getColorFromRGB
  167.     // converts an RGB triplet (such as "255 255 255") to a Color object
  168.     public Color getColorFromRGB(String strRGB)
  169.     {
  170.         StringTokenizer st  = new StringTokenizer(strRGB);
  171.         int iRed   = Integer.parseInt(st.nextToken());
  172.         int iGreen = Integer.parseInt(st.nextToken());
  173.         int iBlue  = Integer.parseInt(st.nextToken());
  174.         return new Color(iRed,iGreen,iBlue);
  175.     }
  176.  
  177.     ////////////////////////////////////////////////////////////////////////////
  178.     // start
  179.     public void start()
  180.     {
  181.         if (m_CoolHeadLines == null)
  182.         {
  183.             m_CoolHeadLines = new Thread(this);
  184.             m_CoolHeadLines.start();
  185.         }
  186.         else
  187.         {
  188.             // This is so we get a quick refresh when we move back to the homepage
  189.             drawHeadlines();
  190.         }
  191.     }
  192.     
  193.     ////////////////////////////////////////////////////////////////////////////
  194.     // stop
  195.     public void stop()
  196.     {
  197.         if (m_CoolHeadLines != null)
  198.         {
  199.             m_CoolHeadLines.stop();
  200.             m_CoolHeadLines = null;
  201.         }
  202.     }
  203.  
  204.     ////////////////////////////////////////////////////////////////////////////
  205.     // run
  206.     // most of the work is done here, essentially loops until the applet dies
  207.     public void run()
  208.     {
  209.         while (true)
  210.         {
  211.             try
  212.             {
  213.                 // the applet is about to scroll
  214.                 m_fStoppedScrolling = false;
  215.  
  216.                 // there is no old message because we are about to 
  217.                 // scroll which invalidates the last message
  218.                 m_iOldMessageSelected=-1;
  219.                 m_iOldMessageSelectedYOffset=-1;
  220.  
  221.                 // re-draw the headlines
  222.                 drawHeadlines();
  223.  
  224.                 // ok, we've stopped scrolling, we can now accept mouse input
  225.                 m_fStoppedScrolling = true;
  226.  
  227.                 // ZZZzzzz...
  228.                 Thread.sleep(m_iMessageDelay * 1000);
  229.             }
  230.             catch (InterruptedException e)
  231.             {
  232.                 stop();
  233.             }
  234.         }
  235.     }
  236.  
  237.     ////////////////////////////////////////////////////////////////////////////
  238.     // drawHeadlines
  239.     // draws the headlines on the screen
  240.     private void drawHeadlines()
  241.     {
  242.         // get the next message and loop round to message #0 
  243.         // if we reach the last message
  244.         m_iCurrentMessage++;
  245.         m_iCurrentMessage %= m_vectData.size();
  246.  
  247.         // if we are about to display the first message then clear the screen
  248.         if (m_iCurrentMessage == 0)
  249.             for (int i=0; i<m_iMaxMessage; i++)
  250.                 scrollOneLine();
  251.  
  252.         // write in the text in the off-screen buffer. 
  253.         // note the text is drawn BELOW the bottom of the applet so when it
  254.         // gets scrolled it appears to rise from nowhere
  255.         drawString(m_buff.getGraphics(),
  256.                    m_iCurrentMessage,
  257.                    0,
  258.                    m_buff.getSize().height-m_fm.getDescent(),
  259.                    m_colTextColor);
  260.  
  261.         // scroll one line
  262.         scrollOneLine();
  263.     }
  264.  
  265.     ////////////////////////////////////////////////////////////////////////////
  266.     // drawString
  267.     // draw a string at a given location and place a 'dot' at the start
  268.     private void drawString(Graphics g,String str, int x, int y, Color col)
  269.     {
  270.         int iFontWidth = m_fm.charWidth('W');
  271.         int iOvalWidth = Math.min(iFontWidth/2,m_iFontHeight/2);
  272.         g.setFont(m_font);
  273.         g.setColor(col);
  274.         g.drawString(str,x + iFontWidth,y);
  275.         g.fillOval(x + iOvalWidth/2,y-iOvalWidth-2,iOvalWidth,iOvalWidth);
  276.     }
  277.  
  278.     ////////////////////////////////////////////////////////////////////////////
  279.     // drawString
  280.     // draw a string at a given location and place a 'dot' at the start
  281.     // this version takes an index into the vector to get the string
  282.     private void drawString(Graphics g,int iMsg, int x, int y, Color col)
  283.     {
  284.         drawString(g,getStringFromVector(iMsg),x,y,col);
  285.     }
  286.  
  287.     ////////////////////////////////////////////////////////////////////////////
  288.     // getStringFromVector
  289.     // returns a string from the vector of TextJumps. 
  290.     private String getStringFromVector(int iIndex)
  291.     {
  292.         TextJump t = (TextJump)m_vectData.elementAt(iIndex);
  293.         return (String)t.getString();
  294.     }
  295.  
  296.     ////////////////////////////////////////////////////////////////////////////
  297.     // scrollOneLine
  298.     // 
  299.     private void scrollOneLine()
  300.     {
  301.         // smoothly draw the off-screen buffer
  302.         for (int j=0; j < m_iFontHeight; j++)
  303.         {
  304.             getGraphics().drawImage(m_buff.getImage(),0,-j,this);
  305.             try { Thread.sleep(m_iScrollDelay);}
  306.             catch (InterruptedException e) { }
  307.         }
  308.  
  309.         Graphics g = m_buff.getGraphics();
  310.  
  311.         // move image up one-line inside the off-screen buffer
  312.         g.drawImage(m_buff.getImage(),0,-m_iFontHeight,this);
  313.  
  314.         // erase old text at bottom of image
  315.         g.setColor(m_colBackColor);
  316.         g.fillRect(0,
  317.                    m_dimAppletSize.height,
  318.                    m_dimAppletSize.width,
  319.                    m_dimAppletSize.height+m_iFontHeight);
  320.     }
  321.  
  322.     ////////////////////////////////////////////////////////////////////////////
  323.     // getYOffset
  324.     // based on a mouse y-location determine which text row we are on
  325.     private int getYOffset(int y)
  326.     {
  327.         int iY = (m_dimAppletSize.height - y) / m_iFontHeight;
  328.         return iY;
  329.     }
  330.  
  331.     ////////////////////////////////////////////////////////////////////////////
  332.     // mouseMove
  333.     // highlights a row of text as we move over it
  334.     public boolean mouseMove(Event evt, int x, int y)
  335.     {
  336.         // if the image is still scrolling then bail
  337.         if (!m_fStoppedScrolling)
  338.             return true;
  339.  
  340.         // get the message we must be over
  341.         int iYOffset = getYOffset(y);
  342.         int iWhichMsg = m_iCurrentMessage - iYOffset;
  343.  
  344.         // if we are still over the same message then don't do anything
  345.         if (m_iOldMessageSelected == iWhichMsg)
  346.             return true;
  347.  
  348.         // if there was an old message we were over then re-draw it in it's non-highlighted color
  349.         if (m_iOldMessageSelected >= 0)
  350.         {
  351.             drawString(getGraphics(),
  352.                        m_iOldMessageSelected,
  353.                        0,
  354.                        m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  355.                        m_colTextColor);
  356.  
  357.             if (iWhichMsg < 0)
  358.                 m_iOldMessageSelected=-1;
  359.         }
  360.  
  361.         // if we are over a valid message then draw it in a highlighted color
  362.         if (iWhichMsg >= 0)
  363.         {
  364.             // set the old message to be this message
  365.             m_iOldMessageSelected = iWhichMsg;
  366.             m_iOldMessageSelectedYOffset = iYOffset;
  367.  
  368.             drawString(getGraphics(),
  369.                        iWhichMsg,
  370.                        0,
  371.                        m_dimAppletSize.height - (iYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  372.                        m_colHiliteTextColor);
  373.  
  374.             // put the string on the status line
  375.             String strStatusMsg = getStringFromVector(iWhichMsg) + 
  376.                                   " at " + m_strURLPrefix + 
  377.                                   "/" + 
  378.                                   (((TextJump)m_vectData.elementAt(iWhichMsg)).getURL());
  379.             getAppletContext().showStatus(strStatusMsg);
  380.         }
  381.  
  382.         return true;
  383.     }
  384.  
  385.     ////////////////////////////////////////////////////////////////////////////
  386.     // mouseExit
  387.     // if we move the mouse out of the applet and there was an old message
  388.     // highlighted then redraw it as a non-highlighted message
  389.     public boolean mouseExit(Event evt, int x, int y)
  390.     {
  391.         if (!m_fStoppedScrolling)
  392.             return true;
  393.  
  394.         if (m_iOldMessageSelected >= 0)
  395.         {
  396.             drawString(getGraphics(),
  397.                        m_iOldMessageSelected,
  398.                        0,
  399.                        m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  400.                        m_colTextColor);
  401.  
  402.             m_iOldMessageSelected=-1;
  403.         }
  404.  
  405.         return true;
  406.     }
  407.  
  408.     ////////////////////////////////////////////////////////////////////////////
  409.     // mouseDown
  410.     // user is interested in a message
  411.     public boolean mouseDown(Event evt, int x, int y)
  412.     {
  413.         // if the image is still scrolling then bail
  414.         if (!m_fStoppedScrolling)
  415.             return true;
  416.  
  417.         // get the message we must be over
  418.         int iYOffset = getYOffset(y);
  419.         int iWhichMsg = m_iCurrentMessage - iYOffset;
  420.  
  421.         try
  422.         {
  423.             m_CoolHeadLines.suspend();
  424.  
  425.              // Build the url for the sample
  426.             String strSampleURL = m_strURLPrefix + "/" + (((TextJump)m_vectData.elementAt(iWhichMsg)).getURL());
  427.             showStatus(strSampleURL);
  428.             getAppletContext().showDocument(new URL(strSampleURL));
  429.  
  430.         } catch(MalformedURLException e) { }
  431.  
  432.         return true;
  433.     }
  434. }
  435.