Java Tutorial/SWT/StyledText Style — различия между версиями

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

Текущая версия на 15:21, 31 мая 2010

Change the background

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextBackground {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.background = display.getSystemColor(SWT.COLOR_BLUE);
    text.setStyleRange(style1);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Change the Foreground color

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextForeground {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.foreground = display.getSystemColor(SWT.COLOR_RED);
    text.setStyleRange(style1);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Compare StyleRange

similarTo(), compares the display data for two StyleRange objects for equality: the foreground color, the background color, and the font style. It ignores which portion of the text the StyleRanges correspond to (their start and length fields).



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompareStyleRange {
  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");
    // Use the empty constructor and set the fields
    StyleRange sr1 = new StyleRange();
    sr1.start = 7;
    sr1.length = 14;
    sr1.foreground = display.getSystemColor(SWT.COLOR_GREEN);
    sr1.background = display.getSystemColor(SWT.COLOR_WHITE);
    sr1.fontStyle = SWT.BOLD;
    // Use the constructor that accepts the fields
    StyleRange sr2 = new StyleRange(7, 14, display.getSystemColor(SWT.COLOR_GREEN), display
        .getSystemColor(SWT.COLOR_WHITE), SWT.BOLD);
    
    System.out.println(sr2.similarTo(sr1));
    
    
    styledText.setBounds(10, 10, 100, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Dynamic syntax coloring

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ExtendedModifyEvent;
import org.eclipse.swt.custom.ExtendedModifyListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SyntaxColoring {
  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);
    final String PUNCTUATION = "(){}";
    styledText.addExtendedModifyListener(new ExtendedModifyListener() {
      public void modifyText(ExtendedModifyEvent event) {
        int end = event.start + event.length - 1;
        if (event.start <= end) {
          String text = styledText.getText(event.start, end);
          java.util.List ranges = new java.util.ArrayList();
          for (int i = 0, n = text.length(); i < n; i++) {
            if (PUNCTUATION.indexOf(text.charAt(i)) > -1) {
              ranges.add(new StyleRange(event.start + i, 1, display.getSystemColor(SWT.COLOR_BLUE),
                  null, SWT.BOLD));
            }
          }
          if (!ranges.isEmpty()) {
            styledText.replaceStyleRanges(event.start, event.length, (StyleRange[]) ranges
                .toArray(new StyleRange[0]));
          }
        }
      }
    });
    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Make text appear strikeout

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextStrikeout {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText with underline and strike through");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    // make 0123456789 appear strikeout
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.strikeout = true;
    text.setStyleRange(style1);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Make text appear underlined

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextUnderlined {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StyledText with underline and strike through");
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
    // make 0123456789 appear underlined
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.underline = true;
    text.setStyleRange(style1);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Make Text Bold

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class TextBold {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    StyledText text = new StyledText(shell, SWT.BORDER);
    text.setText("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    StyleRange style1 = new StyleRange();
    style1.start = 0;
    style1.length = 10;
    style1.fontStyle = SWT.BOLD;
    text.setStyleRange(style1);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





replaceStyleRanges(): specifies which portion of the StyledText to repaint

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ReplaceStyleRanges {
  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("asdfasdfasdfasdf12345678910234567890");
    StyleRange[] ranges = new StyleRange[2];
    ranges[0] = new StyleRange(0, 3, display.getSystemColor(SWT.COLOR_GREEN), null);
    ranges[1] = new StyleRange(3, 6, display.getSystemColor(SWT.COLOR_BLUE), null);
    styledText.setStyleRanges(ranges);
    
    styledText.replaceStyleRanges(5, 9, ranges);
    
    
    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Set a single StyleRange into a StyledText

styledText.setStyleRange(myStyleRange);





Set two Different styles

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SetDifferentStyles {
  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("asdfasdfasdfasdf12345678910234567890");
    // Turn all of the text orange, with the default background color
    styledText.setStyleRange(new StyleRange(0, 9, display.getSystemColor(SWT.COLOR_GREEN), null));
    // Turn "Gators" blue
    styledText.setStyleRange(new StyleRange(3, 6, display.getSystemColor(SWT.COLOR_WHITE), null));
    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}



Colors used must be valid (not disposed) for the life of the StyleRanges. The offsets and lengths of the StyleRanges must be within the range of existing text-when created.


Store StyleRanges into an array, and called setStyleRanges()

Passing the StyleRanges at once reduces the amount of flashing as the StyledText repaints.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StyleRangeSetArray {
  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("asdfasdfasdfasdf12345678910234567890");
    StyleRange[] ranges = new StyleRange[2];
    ranges[0] = new StyleRange(0, 3, display.getSystemColor(SWT.COLOR_GREEN), null);
    ranges[1] = new StyleRange(3, 6, display.getSystemColor(SWT.COLOR_BLUE), null);
    styledText.setStyleRanges(ranges);
    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





StyledText: set line background

Turn the background blue for the first six lines of the StyledText:



styledText.setLineBackground(0, 6, blue);





StyleRange Constructors

ConstructorDescriptionStyleRange()Creates an empty StyleRange.StyleRange(int start, int length, Color foreground, Color background)Creates a StyleRange with the specified start, length, foreground color, and background color.StyleRange(int start, int length, Color foreground, Color background, int fontStyle)Creates a StyleRange with the specified start, length, foreground color, background color, and font style.

The following code creates two identical StyleRange objects:



StyleRange sr1 = new StyleRange();
sr1.start = 7;
sr1.length = 14;
sr1.foreground = display.getSystemColor(SWT.COLOR_GREEN);
sr1.background = display.getSystemColor(SWT.COLOR_WHITE);
sr1.fontStyle = SWT.BOLD;





Use rise and font with StyleRange

/*******************************************************************************
 * 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;
/*
 * SWT StyledText snippet: use rise and font with StyleRange. 
 *
 * 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.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StyledTextStyleRangeFont {
  static String text = "You can set any font you want in a range. You can also set a baseline rise and all other old features"
      + " like background and foreground, and mix them any way you want. Totally awesome.";
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    StyledText styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setText(text);
    FontData data = styledText.getFont().getFontData()[0];
    Font font1 = new Font(display, data.getName(), data.getHeight() * 2, data.getStyle());
    Font font2 = new Font(display, data.getName(), data.getHeight() * 4 / 5, data.getStyle());
    StyleRange[] styles = new StyleRange[8];
    styles[0] = new StyleRange();
    styles[0].font = font1;
    styles[1] = new StyleRange();
    styles[1].rise = data.getHeight() / 3;
    styles[2] = new StyleRange();
    styles[2].background = display.getSystemColor(SWT.COLOR_GREEN);
    styles[3] = new StyleRange();
    styles[3].foreground = display.getSystemColor(SWT.COLOR_MAGENTA);
    styles[4] = new StyleRange();
    styles[4].font = font2;
    styles[4].foreground = display.getSystemColor(SWT.COLOR_BLUE);
    ;
    styles[4].underline = true;
    styles[5] = new StyleRange();
    styles[5].rise = -data.getHeight() / 3;
    styles[5].strikeout = true;
    styles[5].underline = true;
    styles[6] = new StyleRange();
    styles[6].font = font1;
    styles[6].foreground = display.getSystemColor(SWT.COLOR_YELLOW);
    styles[6].background = display.getSystemColor(SWT.COLOR_BLUE);
    styles[7] = new StyleRange();
    styles[7].rise = data.getHeight() / 3;
    styles[7].underline = true;
    styles[7].fontStyle = SWT.BOLD;
    styles[7].foreground = display.getSystemColor(SWT.COLOR_RED);
    styles[7].background = display.getSystemColor(SWT.COLOR_BLACK);
    int[] ranges = new int[] { 16, 4, 61, 13, 107, 10, 122, 10, 134, 3, 143, 6, 160, 7, 168, 7 };
    styledText.setStyleRanges(ranges, styles);
    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    font1.dispose();
    font2.dispose();
    display.dispose();
  }
}





Using LineBackgroundListener

LineBackgroundEvent Fields

FieldDescriptionint lineOffsetThe zero-based offset, relative to the whole text, of the line the StyledText needs background color information for. Note: this is the character offset, not the line number.String lineTextThe text of the line the StyledText needs background color information for.Color lineBackgroundThe field that holds the color you set. The StyledText uses this field to set the background color for the line.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineBackgroundEvent;
import org.eclipse.swt.custom.LineBackgroundListener;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StyledTextLineBackgroundListener {
  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("\n1234\n124\n\1234\n12314\n\1241234\n");
    styledText.addLineBackgroundListener(new LineBackgroundListener() {
      public void lineGetBackground(LineBackgroundEvent event) {
        if (event.lineText.indexOf("SWT") > -1) {
          event.lineBackground = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
        }
      }
    });
    styledText.setBounds(10, 10, 500, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Using StyleRanges

StyledText uses instances of the StyleRange class to track the colors and styles. Each StyleRange controls a portion of the StyledText"s text:

  1. its starting offset
  2. its length (in characters) of the portion
  3. the foreground color
  4. background color,
  5. font style.

StyleRange Fields

FieldDescriptionColor backgroundThe background color, or null to for the default background color.int fontStyleThe font style (SWT.NORMAL or SWT.BOLD).Color foregroundThe foreground color, or null for the default foreground color.int lengthThe length, in number of characters.int startThe starting offset.