//
// RemoteSlaveDisplayImpl.java
//

/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2017 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

package visad;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.*;
import javax.swing.*;
import visad.browser.Convert;

/** RemoteSlaveDisplayImpl is an implementation of a slaved display that
    receives its images from a RemoteDisplay (via RMI). */
public class RemoteSlaveDisplayImpl extends UnicastRemoteObject
  implements RemoteSlaveDisplay, MouseListener, MouseMotionListener
{

  private RemoteDisplay display;
  private BufferedImage image;
  private JComponent component;
  private Vector listen = new Vector();

  /** Construct a new slaved display linked to the given RemoteDisplay */
  public RemoteSlaveDisplayImpl(RemoteDisplay d)
    throws VisADException, RemoteException
  {
    display = d;
    if (display != null) {
      display.addSlave(this);
    }

    component = new JComponent() {
      public void paint(Graphics g) {
        if (image != null) g.drawImage(image, 0, 0, this);
      }

      public Dimension getPreferredSize() {
        if (image == null) return new Dimension(256, 256);
        return new Dimension(image.getWidth(), image.getHeight());
      }
    };
    component.addMouseListener(this);
    component.addMouseMotionListener(this);
  }

  /** Remove the link from this slaved display to its remote display */
  public void unlink() throws VisADException, RemoteException {
    display.removeSlave(this);
  }

  /** Get this slave display's component, for adding to a user interface */
  public JComponent getComponent() {
    return component;
  }

  /** Add a display listener to this slave display.
      The following display events are supported: */
  public void addDisplayListener(DisplayListener l) {
    synchronized (listen) {
      listen.add(l);
    }
  }

  /** Remove a display listener from this slave display */
  public void removeDisplayListener(DisplayListener l) {
    synchronized (listen) {
      listen.remove(l);
    }
  }

  /** Get this slave display's current image */
  public BufferedImage getImage() {
    return image;
  }

  /** Update this slave display with the given RLE-encoded image pixels */
  public void sendImage(int[] pixels, int width, int height, int type)
    throws RemoteException
  {
    // decode pixels
    int[] decoded = Convert.decodeRLE(pixels);

    // build image from decoded pixels
    BufferedImage img = new BufferedImage(width, height, type);
    img.setRGB(0, 0, width, height, decoded, 0, width);

    // wait for image to finish, just in case
    MediaTracker mt = new MediaTracker(component);
    mt.addImage(image, 0);
    try {
      mt.waitForID(0);
    }
    catch (InterruptedException exc) { }
    image = img;

    // redraw display using new image
    component.repaint();

    // notify listeners of display change
    DisplayEvent e = new DisplayEvent(display, DisplayEvent.FRAME_DONE);
    synchronized (listen) {
      for (int i=0; i 

Ads help maintain this website.