hengheng123456789

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  297 Posts :: 68 Stories :: 144 Comments :: 0 Trackbacks

1、拖拽Label的文字。

 public static void main (String [] args) {
  
  Display display = new Display ();
  final Shell shell = new Shell (display);
  shell.setLayout(new FillLayout());
  final Label label1 = new Label (shell, SWT.BORDER);
  label1.setText ("TEXT");
  final Label label2 = new Label (shell, SWT.BORDER);
  setDragDrop (label1);
  setDragDrop (label2);
  shell.setSize (200, 200);
  shell.open ();
  while (!shell.isDisposed ()) {
   if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
 }
 
 public static void setDragDrop (final Label label) {
  
  Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
  int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
  
  final DragSource source = new DragSource (label, operations);
  source.setTransfer(types);
  source.addDragListener (new DragSourceListener () {
   public void dragStart(DragSourceEvent event) {
    event.doit = (label.getText ().length () != 0);
   }
   public void dragSetData (DragSourceEvent event) {
    event.data = label.getText ();
   }
   public void dragFinished(DragSourceEvent event) {
    if (event.detail == DND.DROP_MOVE)
     label.setText ("");
   }
  });

  DropTarget target = new DropTarget(label, operations);
  target.setTransfer(types);
  target.addDropListener (new DropTargetAdapter() {
   public void drop(DropTargetEvent event) {
    if (event.data == null) {
     event.detail = DND.DROP_NONE;
     return;
    }
    label.setText ((String) event.data);
   }
  });
 }

2、随意拖拽Tree子节点。

 public static void main (String [] args) {
  
  final Display display = new Display ();
  final Shell shell = new Shell (display);
  shell.setLayout(new FillLayout());
  //建立用于拖拽的树
  final Tree tree = new Tree(shell, SWT.BORDER);
  for (int i = 0; i < 3; i++) {
   TreeItem item = new TreeItem(tree, SWT.NONE);
   item.setText("item "+i);
   for (int j = 0; j < 3; j++) {
    TreeItem subItem = new TreeItem(item, SWT.NONE);
    subItem.setText("item "+i+" "+j);
    for (int k = 0; k < 3; k++) {
     TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
     subsubItem.setText("item "+i+" "+j+" "+k);
    }
   }
  }
  
  Transfer[] types = new Transfer[] {TextTransfer.getInstance()};
  int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
  
  //建立拖拽 源————树tree对象
  final DragSource source = new DragSource (tree, operations);
  source.setTransfer(types);
  final TreeItem[] dragSourceItem = new TreeItem[1];
  source.addDragListener (new DragSourceListener () {
   public void dragStart(DragSourceEvent event) {
    TreeItem[] selection = tree.getSelection(); // 获取所选tree的节点
    if (selection.length > 0 && selection[0].getItemCount() == 0) //判断所选节点是否存在并且为末端节点
    {
     event.doit = true; //启动拖拽功能
     dragSourceItem[0] = selection[0]; //保存拖拽节点信息
    } else {
     event.doit = false;
    }
   };
   public void dragSetData (DragSourceEvent event) {
    event.data = dragSourceItem[0].getText(); //保存拖拽节点信息到事件数据中
   }
   public void dragFinished(DragSourceEvent event) {
    if (event.detail == DND.DROP_MOVE)
     dragSourceItem[0].dispose();
     dragSourceItem[0] = null;
   }
  });
  
  //建立拖拽 目的————树tree对象
  DropTarget target = new DropTarget(tree, operations);
  target.setTransfer(types);
  target.addDropListener (new DropTargetAdapter() {
   //处理拖拽到目的上空时的方法
   public void dragOver(DropTargetEvent event) {
    event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; //响应拖拽动作,FEEDBACK_EXPAND表示拖拽到可以展开的节点时展开相应节点。
    if (event.item != null) {
     TreeItem item = (TreeItem)event.item;
     //处理拖拽动作响应,根据拖拽的位置不同产生不同的响应
     Point pt = display.map(null, tree, event.x, event.y); //获取拖拽当前位置点
     Rectangle bounds = item.getBounds(); //获取目的节点的边框
     
     //根据拖拽当前位置点与目的节点的边框位置设置响应,如显示为FEEDBACK_INSERT_BEFORE、FEEDBACK_INSERT_AFTER
     if (pt.y < bounds.y + bounds.height/3) {
      event.feedback |= DND.FEEDBACK_INSERT_BEFORE;
     } else if (pt.y > bounds.y + 2*bounds.height/3) {
      event.feedback |= DND.FEEDBACK_INSERT_AFTER;
     } else {
      event.feedback |= DND.FEEDBACK_SELECT;
     }
    }
   }
   //处理放下时的方法
   public void drop(DropTargetEvent event) {
    if (event.data == null) {
     event.detail = DND.DROP_NONE;
     return;
    }
    String text = (String)event.data;
    //如果放下的位置没有节点,则新建立一个节点,以根节点为父节点
    if (event.item == null)
    {
     TreeItem item = new TreeItem(tree, SWT.NONE);
     item.setText(text);
    }
    //如果放下的位置有节点
    else
    {
     TreeItem item = (TreeItem)event.item;
     Point pt = display.map(null, tree, event.x, event.y);
     Rectangle bounds = item.getBounds();
     TreeItem parent = item.getParentItem();
     if (parent != null) {
      TreeItem[] items = parent.getItems();
      int index = 0;
      for (int i = 0; i < items.length; i++) {
       if (items[i] == item) {
        index = i;
        break;
       }
      }
      if (pt.y < bounds.y + bounds.height/3) {
       //以当前节点的父节点为父建立一个子节点,并且插入到当前节点前
       TreeItem newItem = new TreeItem(parent, SWT.NONE, index);
       newItem.setText(text);
      } else if (pt.y > bounds.y + 2*bounds.height/3) {
       //以当前节点的父节点为父建立一个子节点,并且插入到当前节点前
       TreeItem newItem = new TreeItem(parent, SWT.NONE, index+1);
       newItem.setText(text);
      } else {
       //以当前为父节点建立一个子节点
       TreeItem newItem = new TreeItem(item, SWT.NONE);
       newItem.setText(text);
      }
      
     } else {
      TreeItem[] items = tree.getItems();
      int index = 0;
      for (int i = 0; i < items.length; i++) {
       if (items[i] == item) {
        index = i;
        break;
       }
      }
      if (pt.y < bounds.y + bounds.height/3) {
       TreeItem newItem = new TreeItem(tree, SWT.NONE, index);
       newItem.setText(text);
      } else if (pt.y > bounds.y + 2*bounds.height/3) {
       TreeItem newItem = new TreeItem(tree, SWT.NONE, index+1);
       newItem.setText(text);
      } else {
       TreeItem newItem = new TreeItem(item, SWT.NONE);
       newItem.setText(text);
      }
     }
     
     
    }
   }
  });

  shell.setSize (400, 400);
  shell.open ();
  while (!shell.isDisposed ()) {
   if (!display.readAndDispatch ()) display.sleep ();
  }
  display.dispose ();
 }

posted on 2006-11-07 18:05 哼哼 阅读(1474) 评论(0)  编辑  收藏 所属分类: SWT

只有注册用户登录后才能发表评论。


网站导航: