summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/server_discovery/Server_Discovery_Browser.java
blob: 8a0172cb45331a29a0e8f8889c2f092b15a1dfd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import com.sun.java.swing.*;
import java.awt.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.*;
import com.sun.java.swing.text.*;
import com.sun.java.swing.event.*;

public class Server_Discovery_Browser
  extends JInternalFrame
{
  public Server_Discovery_Browser (String initial_url)
    {
      super ("Movie Description", true, true, true, true);
      
      HtmlPane html = new HtmlPane (initial_url);
      this.setBounds (200, 25, 400, 400);
      this.getContentPane ().add (html);
    }

  public void pack()
    {
      Dimension size = getPreferredSize ();
      this.setSize (size.width, size.height);
    }
}

// This code was liberated from the jfc demo "Metalworks", which is
// included with the jdk 1.2 beta 3 distribution.
class HtmlPane
  extends JPanel
  implements HyperlinkListener
{
  JEditorPane html_;
  
  public HtmlPane (String initial_url)
    {
      this.setLayout( new BorderLayout() );

      try
        {
          URL url = new URL (initial_url);
          this.html_ = new JEditorPane (initial_url);
          this.html_.setEditable (false);
          this.html_.addHyperlinkListener (this);
        }
      catch (Exception excp)
        {
          System.err.println (excp);
        }
          
      JScrollPane scroller = new JScrollPane ();
      JViewport vp = scroller.getViewport ();
      vp.add (this.html_);
      this.add (scroller, BorderLayout.CENTER);
    }

    /**
     * Notification of a change relative to a 
     * hyperlink.
     */
    public void hyperlinkUpdate (HyperlinkEvent e)
    {
      if (e.getEventType () == HyperlinkEvent.EventType.ACTIVATED)
        {
          this.linkActivated (e.getURL ());
	}
    }

    /**
     * Follows the reference in an
     * link.  The given url is the requested reference.
     * By default this calls <a href="#setPage">setPage</a>,
     * and if an exception is thrown the original previous
     * document is restored and a beep sounded.  If an 
     * attempt was made to follow a link, but it represented
     * a malformed url, this method will be called with a
     * null argument.
     *
     * @param u the URL to follow
     */
    protected void linkActivated (URL u)
    {
      Cursor c = this.html_.getCursor ();
      Cursor waitCursor = Cursor.getPredefinedCursor (Cursor.WAIT_CURSOR);
      this.html_.setCursor (waitCursor);
      SwingUtilities.invokeLater (new PageLoader(u, c));
    }

    /**
     * temporary class that loads synchronously (although
     * later than the request so that a cursor change
     * can be done).
     */
    class PageLoader implements Runnable
    {      
      PageLoader (URL u, Cursor c)
        {
          this.url_ = u;
          this.cursor_ = c;
	}
      
      public void run ()
        {
          if (this.url_ == null)
            {
              // restore the original cursor
              html_.setCursor (this.cursor_);
              
              // PENDING(prinz) remove this hack when 
              // automatic validation is activated.
              Container parent = html_.getParent ();
              parent.repaint ();
            }
          else
            {
              Document doc = html_.getDocument ();
              try
                {
                  html_.setPage (this.url_);
                }
              catch (IOException ioe)
                {
                  html_.setDocument (doc);
                  getToolkit ().beep ();
                }
              finally
                {
                  // schedule the cursor to revert after
                  // the paint has happended.
                  this.url_ = null;
                  SwingUtilities.invokeLater (this);
		}
	    }
	}
      
      URL url_;
      Cursor cursor_;
    }
}