Book Home Java Enterprise in a Nutshell Search this book

6.4. A Data Source

Example 6-2 shows the ColorSource class. This is a simple JComponent subclass that displays a small block of a solid color and makes that color available for transfer via both cut-and-paste and drag-and-drop. The copy() method copies the color to the clipboard, making it available for pasting, while the dragGestureRecognized() method initiates a drag operation. This example relies upon the TransferableColor class of Example 6-1, of course. For simplicity, the copy() method is invoked when the user clicks on the component--there is no Ctrl-C keyboard binding or Edit menu command.

Example 6-2. ColorSource.java

import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;

/**
 * This simple component displays a solid color and allows that color
 * to be dragged. Also, it copies the color to the clipboard when the 
 * user clicks on it.
 */
public class ColorSource extends JComponent
       implements ClipboardOwner, DragGestureListener, DragSourceListener
{
  Color color;                // The color to display
  TransferableColor tcolor;   // The color, encapsulated for data transfer
  DragSource dragSource;      // We need this object for drag-and-drop

  /** A ColorSource normally displays itself with this border */
  protected static Border defaultBorder = new BevelBorder(BevelBorder.LOWERED);
  /** When we are the clipboard owner, uses this border */
  protected static Border highlightBorder = 
    new CompoundBorder(defaultBorder, new LineBorder(Color.black, 2));

  /** Create a new ColorSource object that displays the specified color */
  public ColorSource(Color color) {
    // Save the color.  Encapsulate it in a Transferable object so that
    // it can be used with cut-and-paste and drag-and-drop.
    this.color = color;
    this.tcolor = new TransferableColor(color);

    // Set our default border
    this.setBorder(defaultBorder);

    // Listen for mouse clicks, and copy the color to the clipboard
    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) { copy(); }
    });

    // Set up a DragGestureRecognizer that will detect when the user 
    // begins a drag.  When it detects one, it will notify us by calling
    // the dragGestureRecognized() method of the DragGestureListener
    // interface we implement below
    this.dragSource = DragSource.getDefaultDragSource();
    dragSource.createDefaultDragGestureRecognizer(this, // Look for drags on us
                  DnDConstants.ACTION_COPY_OR_MOVE,  // Recognize these types
                  this);                             // Tell us when recognized
  }

  // These are component methods that make this class work as a component.
  // They specify how big the component is, and what it it looks like.
  protected static Dimension mysize = new Dimension(25, 25);
  public Dimension getMinimumSize() { return mysize; }
  public Dimension getPreferredSize() { return mysize; }
  public void paintComponent(Graphics g) {
    g.setColor(color);
    Dimension s = this.getSize();
    Insets i = this.getInsets();
    g.fillRect(i.left, i.top, 
               s.width-i.left-i.right, s.height-i.top-i.bottom);
  }

  // The methods below support cut-and-paste

  /** This method copies the color to the clipboard */
  public void copy() {
    // Get system clipboard
    Clipboard c = this.getToolkit().getSystemClipboard();

    // Put our TransferableColor object on the clipboard.
    // Also, we'll get notification when we no longer own the clipboard.
    c.setContents(tcolor, this);

    // Set a special border on ourselves that indicates that we're the
    // current color available for pasting
    this.setBorder(highlightBorder);
  }

  // This ClipboardOwner method is called when something else is
  // placed on the clipboard.  It means that our color is no longer
  // available for pasting, and we should not display the highlight border.
  public void lostOwnership(Clipboard clipboard, Transferable contents) {
    this.setBorder(defaultBorder);
  }

  // The methods below support drag-and-drop

  // This DragGestureListener method is called when the DragGestureRecognizer
  // detects that the user has dragged the mouse.  It is responsible
  // for beginning the drag-and-drop process.
  public void dragGestureRecognized(DragGestureEvent e) {
    // Create an image we can drag along with us.
    // Not all systems support this, but it doesn't hurt to try.
    Image colorblock = this.createImage(25,25);
    Graphics g = colorblock.getGraphics();
    g.setColor(color);
    g.fillRect(0,0,25,25);

    // Start dragging our transferable color object
    e.startDrag(DragSource.DefaultMoveDrop,    // The initial drag cursor
                colorblock, new Point(0,0),    // The image to drag
                tcolor,                        // The data being dragged
                this);                         // Who to notify during drag
  }

  // These methods implement DragSourceListener.
  // Since we passed this object to startDrag, these methods will be
  // called at interesting points during the drag.  We could use them,
  // for example, to implement custom cursors or other "drag-over" effects.
  public void dragEnter(DragSourceDragEvent e) {}
  public void dragExit(DragSourceEvent e) {}
  public void dragDropEnd(DragSourceDropEvent e) {}
  public void dragOver(DragSourceDragEvent e) {}
  public void dropActionChanged(DragSourceDragEvent e) {}
}


Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.