home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 9 / IOPROG_9.ISO / contrib / iis4 / iis4_07.cab / CoolHeadLines.java < prev    next >
Encoding:
Java Source  |  1997-09-04  |  13.1 KB  |  425 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 = getParameter("Text"+i);
  157.             String strURL = getParameter("URL"+i);
  158.             m_vectData.addElement(new TextJump(strURL,strText));
  159.         }
  160.     }
  161.  
  162.     ////////////////////////////////////////////////////////////////////////////
  163.     // getColorFromRGB
  164.     // converts an RGB triplet (such as "255 255 255") to a Color object
  165.     public Color getColorFromRGB(String strRGB)
  166.     {
  167.         StringTokenizer st  = new StringTokenizer(strRGB);
  168.         int iRed   = Integer.parseInt(st.nextToken());
  169.         int iGreen = Integer.parseInt(st.nextToken());
  170.         int iBlue  = Integer.parseInt(st.nextToken());
  171.         return new Color(iRed,iGreen,iBlue);
  172.     }
  173.  
  174.     ////////////////////////////////////////////////////////////////////////////
  175.     // start
  176.     public void start()
  177.     {
  178.         if (m_CoolHeadLines == null)
  179.         {
  180.             m_CoolHeadLines = new Thread(this);
  181.             m_CoolHeadLines.start();
  182.         }
  183.         else
  184.         {
  185.             // This is so we get a quick refresh when we move back to the homepage
  186.             drawHeadlines();
  187.         }
  188.     }
  189.     
  190.     ////////////////////////////////////////////////////////////////////////////
  191.     // stop
  192.     public void stop()
  193.     {
  194.         if (m_CoolHeadLines != null)
  195.         {
  196.             m_CoolHeadLines.stop();
  197.             m_CoolHeadLines = null;
  198.         }
  199.     }
  200.  
  201.     ////////////////////////////////////////////////////////////////////////////
  202.     // run
  203.     // most of the work is done here, essentially loops until the applet dies
  204.     public void run()
  205.     {
  206.         while (true)
  207.         {
  208.             try
  209.             {
  210.                 // the applet is about to scroll
  211.                 m_fStoppedScrolling = false;
  212.  
  213.                 // there is no old message because we are about to 
  214.                 // scroll which invalidates the last message
  215.                 m_iOldMessageSelected=-1;
  216.                 m_iOldMessageSelectedYOffset=-1;
  217.  
  218.                 // re-draw the headlines
  219.                 drawHeadlines();
  220.  
  221.                 // ok, we've stopped scrolling, we can now accept mouse input
  222.                 m_fStoppedScrolling = true;
  223.  
  224.                 // ZZZzzzz...
  225.                 Thread.sleep(m_iMessageDelay * 1000);
  226.             }
  227.             catch (InterruptedException e)
  228.             {
  229.                 stop();
  230.             }
  231.         }
  232.     }
  233.  
  234.     ////////////////////////////////////////////////////////////////////////////
  235.     // drawHeadlines
  236.     // draws the headlines on the screen
  237.     private void drawHeadlines()
  238.     {
  239.         // get the next message and loop round to message #0 
  240.         // if we reach the last message
  241.         m_iCurrentMessage++;
  242.         m_iCurrentMessage %= m_vectData.size();
  243.  
  244.         // if we are about to display the first message then clear the screen
  245.         if (m_iCurrentMessage == 0)
  246.             for (int i=0; i<m_iMaxMessage; i++)
  247.                 scrollOneLine();
  248.  
  249.         // write in the text in the off-screen buffer. 
  250.         // note the text is drawn BELOW the bottom of the applet so when it
  251.         // gets scrolled it appears to rise from nowhere
  252.         drawString(m_buff.getGraphics(),
  253.                    m_iCurrentMessage,
  254.                    0,
  255.                    m_buff.getSize().height-m_fm.getDescent(),
  256.                    m_colTextColor);
  257.  
  258.         // scroll one line
  259.         scrollOneLine();
  260.     }
  261.  
  262.     ////////////////////////////////////////////////////////////////////////////
  263.     // drawString
  264.     // draw a string at a given location and place a 'dot' at the start
  265.     private void drawString(Graphics g,String str, int x, int y, Color col)
  266.     {
  267.         int iFontWidth = m_fm.charWidth('W');
  268.         int iOvalWidth = Math.min(iFontWidth/2,m_iFontHeight/2);
  269.         g.setFont(m_font);
  270.         g.setColor(col);
  271.         g.drawString(str,x + iFontWidth,y);
  272.         g.fillOval(x + iOvalWidth/2,y-iOvalWidth-2,iOvalWidth,iOvalWidth);
  273.     }
  274.  
  275.     ////////////////////////////////////////////////////////////////////////////
  276.     // drawString
  277.     // draw a string at a given location and place a 'dot' at the start
  278.     // this version takes an index into the vector to get the string
  279.     private void drawString(Graphics g,int iMsg, int x, int y, Color col)
  280.     {
  281.         drawString(g,getStringFromVector(iMsg),x,y,col);
  282.     }
  283.  
  284.     ////////////////////////////////////////////////////////////////////////////
  285.     // getStringFromVector
  286.     // returns a string from the vector of TextJumps. 
  287.     private String getStringFromVector(int iIndex)
  288.     {
  289.         TextJump t = (TextJump)m_vectData.elementAt(iIndex);
  290.         return (String)t.getString();
  291.     }
  292.  
  293.     ////////////////////////////////////////////////////////////////////////////
  294.     // scrollOneLine
  295.     // 
  296.     private void scrollOneLine()
  297.     {
  298.         // smoothly draw the off-screen buffer
  299.         for (int j=0; j < m_iFontHeight; j++)
  300.         {
  301.             getGraphics().drawImage(m_buff.getImage(),0,-j,this);
  302.             try { Thread.sleep(m_iScrollDelay);}
  303.             catch (InterruptedException e) { }
  304.         }
  305.  
  306.         Graphics g = m_buff.getGraphics();
  307.  
  308.         // move image up one-line inside the off-screen buffer
  309.         g.drawImage(m_buff.getImage(),0,-m_iFontHeight,this);
  310.  
  311.         // erase old text at bottom of image
  312.         g.setColor(m_colBackColor);
  313.         g.fillRect(0,
  314.                    m_dimAppletSize.height,
  315.                    m_dimAppletSize.width,
  316.                    m_dimAppletSize.height+m_iFontHeight);
  317.     }
  318.  
  319.     ////////////////////////////////////////////////////////////////////////////
  320.     // getYOffset
  321.     // based on a mouse y-location determine which text row we are on
  322.     private int getYOffset(int y)
  323.     {
  324.         int iY = (m_dimAppletSize.height - y) / m_iFontHeight;
  325.         return iY;
  326.     }
  327.  
  328.     ////////////////////////////////////////////////////////////////////////////
  329.     // mouseMove
  330.     // highlights a row of text as we move over it
  331.     public boolean mouseMove(Event evt, int x, int y)
  332.     {
  333.         // if the image is still scrolling then bail
  334.         if (!m_fStoppedScrolling)
  335.             return true;
  336.  
  337.         // get the message we must be over
  338.         int iYOffset = getYOffset(y);
  339.         int iWhichMsg = m_iCurrentMessage - iYOffset;
  340.  
  341.         // if we are still over the same message then don't do anything
  342.         if (m_iOldMessageSelected == iWhichMsg)
  343.             return true;
  344.  
  345.         // if there was an old message we were over then re-draw it in it's non-highlighted color
  346.         if (m_iOldMessageSelected >= 0)
  347.         {
  348.             drawString(getGraphics(),
  349.                        m_iOldMessageSelected,
  350.                        0,
  351.                        m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  352.                        m_colTextColor);
  353.  
  354.             if (iWhichMsg < 0)
  355.                 m_iOldMessageSelected=-1;
  356.         }
  357.  
  358.         // if we are over a valid message then draw it in a highlighted color
  359.         if (iWhichMsg >= 0)
  360.         {
  361.             // set the old message to be this message
  362.             m_iOldMessageSelected = iWhichMsg;
  363.             m_iOldMessageSelectedYOffset = iYOffset;
  364.  
  365.             drawString(getGraphics(),
  366.                        iWhichMsg,
  367.                        0,
  368.                        m_dimAppletSize.height - (iYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  369.                        m_colHiliteTextColor);
  370.         }
  371.  
  372.         return true;
  373.     }
  374.  
  375.     ////////////////////////////////////////////////////////////////////////////
  376.     // mouseExit
  377.     // if we move the mouse out of the applet and there was an old message
  378.     // highlighted then redraw it as a non-highlighted message
  379.     public boolean mouseExit(Event evt, int x, int y)
  380.     {
  381.         if (!m_fStoppedScrolling)
  382.             return true;
  383.  
  384.         if (m_iOldMessageSelected >= 0)
  385.         {
  386.             drawString(getGraphics(),
  387.                        m_iOldMessageSelected,
  388.                        0,
  389.                        m_dimAppletSize.height - (m_iOldMessageSelectedYOffset * m_iFontHeight) - m_fm.getDescent() + 1,
  390.                        m_colTextColor);
  391.  
  392.             m_iOldMessageSelected=-1;
  393.         }
  394.  
  395.         return true;
  396.     }
  397.  
  398.     ////////////////////////////////////////////////////////////////////////////
  399.     // mouseDown
  400.     // user is interested in a message
  401.     public boolean mouseDown(Event evt, int x, int y)
  402.     {
  403.         // if the image is still scrolling then bail
  404.         if (!m_fStoppedScrolling)
  405.             return true;
  406.  
  407.         // get the message we must be over
  408.         int iYOffset = getYOffset(y);
  409.         int iWhichMsg = m_iCurrentMessage - iYOffset;
  410.  
  411.         try
  412.         {
  413.             m_CoolHeadLines.suspend();
  414.  
  415.              // Build the url for the sample
  416.             String strSampleURL = m_strURLPrefix + "/" + (((TextJump)m_vectData.elementAt(iWhichMsg)).getURL());
  417.             showStatus(strSampleURL);
  418.             getAppletContext().showDocument(new URL(strSampleURL));
  419.  
  420.         } catch(MalformedURLException e) { }
  421.  
  422.         return true;
  423.     }
  424. }
  425.