new file

christy [2001-12-09 15:13:10]
new file
Filename
portal/HabitatMonitor.java
diff --git a/portal/HabitatMonitor.java b/portal/HabitatMonitor.java
new file mode 100644
index 0000000..9ffd210
--- /dev/null
+++ b/portal/HabitatMonitor.java
@@ -0,0 +1 @@
+package psl.habitats.portal;

import psl.habitats.ServiceInterface;
import java.awt.Dimension;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Point;

import javax.swing.JList;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.util.Vector;
import java.util.Hashtable;
import java.util.Enumeration;


public class HabitatMonitor extends JFrame {
    
    private static final int WIDTH = 800;
    private static final int HEIGHT = 450;
    private static final int HT_DIFF = 50;
    private final JPanel _mainPanel;
    private final JPanel _logPanel;
    private final JPanel _svcPanel;
    private final JScrollPane _logPane;
    private final JPanel _svcPane;
    private final JLabel _logLabel;
    private final JLabel _svcLabel;
    private final JTextArea _logArea;
    
    private final Hashtable monitors = new Hashtable();
    private final Hashtable messages = new Hashtable();
    
    public HabitatMonitor(String habitatName) { 
	
	setTitle(habitatName + " Monitor");
	// hide so habitat doesn't quit when monitor is closed
	setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	
	// habitat	
	_mainPanel = new JPanel(new GridLayout(0,1));
	_logPanel = new JPanel(new BorderLayout());
	_logLabel = new JLabel(habitatName + " Habitat Monitor:");
	_logPane = new JScrollPane(_logArea = new JTextArea("", 8, 10));
	_logArea.setEditable(false);
	
	// services
	_svcPanel = new JPanel(new BorderLayout());
	_svcPane = new JPanel(new GridLayout(0,2));
	_svcLabel = new JLabel("Service Monitors:");
	
	_logPanel.add(_logLabel, BorderLayout.NORTH);
	_logPanel.add(_logPane, BorderLayout.CENTER);
	
	_svcPanel.add(_svcLabel, BorderLayout.NORTH);
	_svcPanel.add(_svcPane, BorderLayout.CENTER);
	
	_mainPanel.add(_logPanel, BorderLayout.NORTH);
	_mainPanel.add(_svcPanel, BorderLayout.CENTER);
	
	getContentPane().add(_mainPanel);
    }
    
    // add a service monitor
    public void addmonitor(String sname) {
	try {
 	    // check if the monitor is already displayed for this service
	    if (monitors.get(sname) != null) { return; }
	    
	    // add this service to the hashtable 
	    monitors.put(sname, new TextAreaPanel(sname));
	    _svcPane.add((TextAreaPanel)monitors.get(sname));
	    
	    validate();
	    
	    // if service had any messages before this monitor was added 
	    // display them
	    rewritemonitor(sname);
	} catch (Exception e) {
	}
    }
    
    // write a message to a service monitor
    public void writemonitor(String str, String svcname) {
	try {
	    String output = (str + "\n");

	    // if this is the first message, create a new Vector to store messages
	    if (messages.get(svcname) == null) { 
		messages.put(svcname, new Vector());
	    }

	    // add this message to this service's logs
	    ((Vector)(messages.get(svcname))).add(output);
	    
	    // get this service's textArea
	    JTextArea h_ta = ((TextAreaPanel)
			      (monitors.get(svcname))).h_textArea;

	    // display the message and keep display at the end of the textArea
	    h_ta.append(output);
	    h_ta.setCaretPosition(h_ta.getDocument().getLength());
	    
	} catch (Exception e) {
	}
    }


    // write all existing messages to the monitor
    void rewritemonitor(String svcname) {
	try {
	    for (Enumeration e = 
		     ((Vector)(messages.get(svcname))).elements(); 
		 e.hasMoreElements() ;) {	
		
		((TextAreaPanel)
		 (monitors.get(svcname)))
		    .h_textArea.append((String) e.nextElement());
	    }
	} catch (Exception e) {
	}  
    }
    
    // for the service monitors
    class TextAreaPanel extends JPanel
    {
	JTextArea h_textArea = new JTextArea(8, 20);
	
	public TextAreaPanel(String name)
	{
	    setLayout( new BorderLayout() );
	    JScrollPane scrollPane = new JScrollPane(h_textArea);
	    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
	    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
	    add(scrollPane, BorderLayout.CENTER);
	    add(new JLabel(name), BorderLayout.SOUTH);
	}
    }
    
    // position monitor frame relative to the portal frame before display
    public void relativedisplay(HabitatPortal hp) {
	Dimension d = new Dimension(WIDTH, HEIGHT);
	
	if (hp != null) {
	    Point p = hp.getLocation();
	    setLocation(p.x, p.y+(d.height + HT_DIFF));
	    d = hp.getSize();
	}
	
	setSize(d.width, d.height - HT_DIFF);
	show();
    }
    
    // output a message to the habitat monitor
    public void log(String msg) {
	_logArea.append(msg + "\n");
	_logArea.setCaretPosition(_logArea.getDocument().getLength());
    }

    public static void main(String args[]) {
	new HabitatMonitor("HabitatMonitor");
    }
}
\ No newline at end of file