001: // DraggableResourceList.java
002: // $Id: DraggableList.java,v 1.4 1999/03/05 14:45:27 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.jigadmin.widgets;
006:
007: import java.awt.dnd.DragGestureListener;
008: import java.awt.dnd.DragGestureEvent;
009: import java.awt.dnd.DragSourceListener;
010: import java.awt.dnd.DragSource;
011: import java.awt.dnd.DragSourceDropEvent;
012: import java.awt.dnd.DragSourceDragEvent;
013: import java.awt.dnd.DragSourceEvent;
014: import java.awt.dnd.DnDConstants;
015: import java.awt.dnd.InvalidDnDOperationException;
016:
017: import java.awt.datatransfer.StringSelection;
018:
019: import javax.swing.JList;
020: import javax.swing.ListModel;
021:
022: import org.w3c.jigadmin.editors.TransferableResourceCell;
023: import org.w3c.jigadmin.editors.ResourceCell;
024:
025: import java.util.Vector;
026:
027: /**
028: * A JList that accepts drag event.
029: * @version $Revision: 1.4 $
030: * @author Benoît Mahé (bmahe@w3.org)
031: */
032: public class DraggableList extends JList implements DragGestureListener {
033: DragSource dragSource = DragSource.getDefaultDragSource();
034:
035: final static DragSourceListener dsl = new DragSourceListener() {
036: public void dragDropEnd(DragSourceDropEvent DragSourceDropEvent) {
037: //nothing
038: }
039:
040: public void dragEnter(DragSourceDragEvent DragSourceDragEvent) {
041: //nothing
042: }
043:
044: public void dragExit(DragSourceEvent DragSourceEvent) {
045: //nothing
046: }
047:
048: public void dragOver(DragSourceDragEvent DragSourceDragEvent) {
049: //nothing
050: }
051:
052: public void dropActionChanged(
053: DragSourceDragEvent DragSourceDragEvent) {
054: //nothing
055: }
056: };
057:
058: public DraggableList() {
059: super ();
060: dragSource.createDefaultDragGestureRecognizer(this ,
061: DnDConstants.ACTION_COPY, this );
062: }
063:
064: public DraggableList(Object[] listData) {
065: super (listData);
066: dragSource.createDefaultDragGestureRecognizer(this ,
067: DnDConstants.ACTION_COPY, this );
068: }
069:
070: public DraggableList(ListModel dataModel) {
071: super (dataModel);
072: dragSource.createDefaultDragGestureRecognizer(this ,
073: DnDConstants.ACTION_COPY, this );
074: }
075:
076: public DraggableList(Vector listData) {
077: super (listData);
078: dragSource.createDefaultDragGestureRecognizer(this ,
079: DnDConstants.ACTION_COPY, this );
080: }
081:
082: private void startDrag(DragGestureEvent dragGestureEvent) {
083: Object selected = getSelectedValue();
084: if (selected instanceof String) {
085: String sel = (String) selected;
086: StringSelection selection = new StringSelection(sel);
087: dragSource.startDrag(dragGestureEvent,
088: DragSource.DefaultCopyDrop, selection, dsl);
089: } else if (selected instanceof ResourceCell) {
090: TransferableResourceCell trans = new TransferableResourceCell(
091: (ResourceCell) selected);
092: dragSource.startDrag(dragGestureEvent,
093: DragSource.DefaultCopyDrop, trans, dsl);
094: } else {
095: String sel = selected.toString();
096: StringSelection selection = new StringSelection(sel);
097: dragSource.startDrag(dragGestureEvent,
098: DragSource.DefaultCopyDrop, selection, dsl);
099: }
100: }
101:
102: public void dragGestureRecognized(DragGestureEvent dragGestureEvent) {
103: try {
104: startDrag(dragGestureEvent);
105: } catch (InvalidDnDOperationException ex) {
106: ex.printStackTrace();
107: }
108: }
109: }
|