Java Tutorial/SWT/StyledText Event

Материал из Java эксперт
Перейти к: навигация, поиск

Add Paint event listener to StyledText

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class StyledTextPaint {

 static String SEARCH_STRING = "box";
 public static void main(String[] args) {
   final Display display = new Display();
   Shell shell = new Shell(display);
   shell.setBounds(10, 10, 250, 250);
   final StyledText text = new StyledText(shell, SWT.NONE);
   text.setBounds(10, 10, 200, 200);
   text.addListener(SWT.Paint, new Listener() {
     public void handleEvent(Event event) {
         System.out.println("paint");
         
       
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>





Add Paint Object Listener

   <source lang="java">

/*******************************************************************************

* Copyright (c) 2000, 2006 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; /*

* example snippet: StyledText bulleted list example
*
* 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.Bullet; import org.eclipse.swt.custom.PaintObjectEvent; import org.eclipse.swt.custom.PaintObjectListener; import org.eclipse.swt.custom.ST; import org.eclipse.swt.custom.StyleRange; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.GlyphMetrics; import org.eclipse.swt.graphics.TextLayout; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextPaintObjectListener {

 public static void main(String[] args) {
   Display display = new Display();
   Shell shell = new Shell(display);
   shell.setText("StyledText Bullet Example");
   shell.setLayout(new FillLayout());
   final StyledText styledText = new StyledText(shell, SWT.FULL_SELECTION | SWT.BORDER | SWT.WRAP
       | SWT.V_SCROLL);
   StringBuffer text = new StringBuffer();
   text.append("Here is StyledText with some bulleted lists:\n\n");
   for (int i = 0; i < 4; i++)
     text.append("Red Bullet List Item " + i + "\n");
   text.append("\n");
   for (int i = 0; i < 2; i++)
     text.append("Numbered List Item " + i + "\n");
   for (int i = 0; i < 4; i++)
     text.append("Sub List Item " + i + "\n");
   for (int i = 0; i < 2; i++)
     text.append("Numbered List Item " + (2 + i) + "\n");
   text.append("\n");
   for (int i = 0; i < 4; i++)
     text.append("Custom Draw List Item " + i + "\n");
   styledText.setText(text.toString());
   StyleRange style0 = new StyleRange();
   style0.metrics = new GlyphMetrics(0, 0, 40);
   style0.foreground = display.getSystemColor(SWT.COLOR_RED);
   Bullet bullet0 = new Bullet(style0);
   StyleRange style1 = new StyleRange();
   style1.metrics = new GlyphMetrics(0, 0, 50);
   style1.foreground = display.getSystemColor(SWT.COLOR_BLUE);
   Bullet bullet1 = new Bullet(ST.BULLET_NUMBER | ST.BULLET_TEXT, style1);
   bullet1.text = ".";
   StyleRange style2 = new StyleRange();
   style2.metrics = new GlyphMetrics(0, 0, 80);
   style2.foreground = display.getSystemColor(SWT.COLOR_GREEN);
   Bullet bullet2 = new Bullet(ST.BULLET_TEXT, style2);
   bullet2.text = "\u2713";
   StyleRange style3 = new StyleRange();
   style3.metrics = new GlyphMetrics(0, 0, 50);
   Bullet bullet3 = new Bullet(ST.BULLET_CUSTOM, style2);
   styledText.setLineBullet(2, 4, bullet0);
   styledText.setLineBullet(7, 2, bullet1);
   styledText.setLineBullet(9, 4, bullet2);
   styledText.setLineBullet(13, 2, bullet1);
   styledText.setLineBullet(16, 4, bullet3);
   styledText.addPaintObjectListener(new PaintObjectListener() {
     public void paintObject(PaintObjectEvent event) {
       Display display = event.display;
       StyleRange style = event.style;
       Font font = style.font;
       if (font == null)
         font = styledText.getFont();
       TextLayout layout = new TextLayout(display);
       layout.setAscent(event.ascent);
       layout.setDescent(event.descent);
       layout.setFont(font);
       layout.setText("\u2023 1." + event.bulletIndex + ")");
       layout.draw(event.gc, event.x + 10, event.y);
       layout.dispose();
     }
   });
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>





After all VerifyKeyListeners are notified, any VerifyListeners are then notified.

This happens before the change is effected, so you still have veto power.

VerifyEvent Fields

FieldDescriptionint startThe zero-based offset of the start. Changing this value has no effect on event processing.int endThe zero-based offset of the end. Changing this value has no effect on event processing.String textThe text that will be inserted. Changing this value changes the text to be inserted.


Against cut-and-paste: Modify the data in VerifyEvent to change the effect of user"s keystrokes

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextVerifyListenerStopPasting {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyListener(new VerifyListener() {
     public void verifyText(VerifyEvent event) {
       if (event.text.length() > 1) {
         event.text = "Stop pasting!";
       }
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Allow Arrow Key

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextArrowKeyVerifyKeyListener {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyKeyListener(new VerifyKeyListener() {
     public void verifyKey(VerifyEvent event) {
       System.out.println(event.character);
       event.doit = false;
       // Allow arrow keys
       if (event.keyCode == SWT.ARROW_UP || event.keyCode == SWT.ARROW_DOWN
         || event.keyCode == SWT.ARROW_LEFT || event.keyCode == SWT.ARROW_RIGHT)
         event.doit = true;
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Allow backspace and delete

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextVerifyListenerBackspaceDelete {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyKeyListener(new VerifyKeyListener() {
     public void verifyKey(VerifyEvent event) {
       System.out.println(event.character);
       event.doit = false;
       // Allow backspace and delete
       if (event.character == "\u0008" || event.character == "\u007F") {
         event.doit = true;
       }
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Allow return

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextVerifyListenerReturn {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyKeyListener(new VerifyKeyListener() {
     public void verifyKey(VerifyEvent event) {
       System.out.println(event.character);
       event.doit = false;
       // Allow return
       if (event.character == "\r")
         event.doit = true;
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Dragging text in a StyledText widget

   <source lang="java">

/*******************************************************************************

* 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();
 }

}</source>





Filtering Change with VerifyKeyListeners

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.custom.VerifyKeyListener; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextVerifyKeyListener {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyKeyListener(new VerifyKeyListener() {
     public void verifyKey(VerifyEvent e) {
       System.out.println(e.character);
       e.doit = false;
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>



VerifyEvent derives from KeyEvent.

FieldDescriptionchar characterThe character that the typed key represents. Changing this value has no effect on event processing.boolean doitwhether this event should be processed. Setting doit to false cancels event processing.int keyCodeThe code of the typed key. Changing this value has no effect on event processing.int stateMaskPossible values are combinations of SWT.ALT, SWT.ruMAND, SWT.CONTROL, SWT.CTRL, SWT.MOD1, SWT.MOD2, SWT.MOD3, SWT.MOD4, and SWT.SHIFT.


Handling Events

When users type, delete from, cut from, or paste to a StyledText, four events fire:

  1. key verification,
  2. verification,
  3. modification, and
  4. extended modification.

Before the StyledText allows the changes to itself, It first processes the key verification and the verification. These handlers, VerifyKeyListeners and VerifyListeners, can allow, veto, or alter the requested text change. After the change has happened, ModifyListeners and ExtendedModifyListeners react to the changes.


Print out VerifyEvent detail

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextVerifyListenerEventInforation {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyListener(new VerifyListener() {
     public void verifyText(VerifyEvent event) {
       System.out.println(event.start);
       System.out.println(event.end);
       System.out.println(event.text);
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Reacting to Change

All ModifyListeners are notified first then all ExtendedModifyListeners. These notifications occur after the text has already changed.



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextModifyListener {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addModifyListener(new ModifyListener() {
     public void modifyText(ModifyEvent event) {
       System.out.println("Character Count: " + styledText.getCharCount());
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Use a verify listener in StyledText

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextListenerVerify {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   shell.setLayout(new GridLayout());
   StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
   styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
   styledText.setText("text");
   // use a verify listener to keep the offsets up to date
   styledText.addVerifyListener(new VerifyListener() {
     public void verifyText(VerifyEvent e) {
       System.out.println(e.start);
       System.out.println(e.start);
       System.out.println(e.text.length());
     }
   });
   shell.setSize(400, 400);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch())
       display.sleep();
   }
   display.dispose();
 }

}</source>





Use ExtendedModifyEvent

Call addExtendedModifyListener() to add an ExtendedModifyListener.

ExtendedModifyEvent Fields

FieldDescriptionint startThe zero-based offset, relative to the start of the StyledText, of the first position of the changed text.int lengthThe length of the changed text, in characters.String replacedTextThe text that was replaced by this change.



   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ExtendedModifyEvent; import org.eclipse.swt.custom.ExtendedModifyListener; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextExtendedModifyListener {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   final StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addExtendedModifyListener(new ExtendedModifyListener() {
     public void modifyText(ExtendedModifyEvent event) {
       System.out.println(event.start);
       System.out.println(event.length); 
       System.out.println(event.replacedText);
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>





Veto the event by setting its doit member to false.

   <source lang="java">

import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.VerifyEvent; import org.eclipse.swt.events.VerifyListener; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class StyledTextThrowEAway {

 public static void main(String[] args) {
   final Display display = new Display();
   final Shell shell = new Shell(display);
   StyledText styledText = new StyledText(shell, SWT.V_SCROLL | SWT.BORDER);
   styledText.setText("12345");
   styledText.addVerifyListener(new VerifyListener() {
     public void verifyText(VerifyEvent event) {
       // If the text contains E or e, throw it all away
       if (event.text.indexOf("e") > -1 || event.text.indexOf("E") > -1) {
         event.text = "";
       }
     }
   });
   styledText.setBounds(10, 10, 100, 100);
   shell.open();
   while (!shell.isDisposed()) {
     if (!display.readAndDispatch()) {
       display.sleep();
     }
   }
   display.dispose();
 }

}</source>