Dragging text in a StyledText widget : StyledText Event « SWT « Java Tutorial






Dragging text in a StyledText widget
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * Dragging text in a StyledText widget
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class DragTextInStyledText {
  static String string1 = "A drag source is the provider of data in a Drag and Drop data transfer as well as "
      + "the originator of the Drag and Drop operation. The data provided by the drag source "
      + "may be transferred to another location in the same widget, to a different widget "
      + "within the same application, or to a different application altogether. For example, "
      + "you can drag text from your application and drop it on an email application, or you "
      + "could drag an item in a tree and drop it below a different node in the same tree.";

  static String string2 = "A drop target receives data in a Drag and Drop operation. The data received by "
      + "the drop target may have come from the same widget, from a different widget within "
      + "the same application, or from a different application altogether. For example, you "
      + "can drag text from an email application and drop it on your application, or you could "
      + "drag an item in a tree and drop it below a different node in the same tree.";

  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    int style = SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER;
    final StyledText text1 = new StyledText(shell, style);
    text1.setText(string1);
    DragSource source = new DragSource(text1, DND.DROP_COPY | DND.DROP_MOVE);
    source.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    source.addDragListener(new DragSourceAdapter() {
      Point selection;

      public void dragStart(DragSourceEvent e) {
        selection = text1.getSelection();
        e.doit = selection.x != selection.y;
      }

      public void dragSetData(DragSourceEvent e) {
        e.data = text1.getText(selection.x, selection.y - 1);
      }

      public void dragFinished(DragSourceEvent e) {
        if (e.detail == DND.DROP_MOVE) {
          text1.replaceTextRange(selection.x, selection.y - selection.x, "");
        }
        selection = null;
      }
    });

    final StyledText text2 = new StyledText(shell, style);
    text2.setText(string2);
    DropTarget target = new DropTarget(text2, DND.DROP_DEFAULT | DND.DROP_MOVE | DND.DROP_COPY
        | DND.DROP_LINK);
    target.setTransfer(new Transfer[] { TextTransfer.getInstance() });
    target.addDropListener(new DropTargetAdapter() {
      public void dragEnter(DropTargetEvent e) {
        if (e.detail == DND.DROP_DEFAULT)
          e.detail = DND.DROP_COPY;
      }

      public void dragOperationChanged(DropTargetEvent e) {
        if (e.detail == DND.DROP_DEFAULT)
          e.detail = DND.DROP_COPY;
      }

      public void drop(DropTargetEvent e) {
        text2.insert((String) e.data);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}








17.45.StyledText Event
17.45.1.Handling Events
17.45.2.Filtering Change with VerifyKeyListenersFiltering Change with VerifyKeyListeners
17.45.3.Allow backspace and deleteAllow backspace and delete
17.45.4.Allow Arrow Key
17.45.5.Allow return
17.45.6.After all VerifyKeyListeners are notified, any VerifyListeners are then notified.
17.45.7.Print out VerifyEvent detail
17.45.8.Against cut-and-paste: Modify the data in VerifyEvent to change the effect of user's keystrokes
17.45.9.Veto the event by setting its doit member to false.
17.45.10.Reacting to Change
17.45.11.Add Paint event listener to StyledTextAdd Paint event listener to StyledText
17.45.12.Use a verify listener in StyledTextUse a verify listener in StyledText
17.45.13.Add Paint Object ListenerAdd Paint Object Listener
17.45.14.Dragging text in a StyledText widgetDragging text in a StyledText widget
17.45.15.Use ExtendedModifyEvent