Java/SWT JFace Eclipse/StyledText
Содержание
- 1 Demonstrates StyleRanges
- 2 Implements syntax coloring using the StyledText API
- 3 Sample Styled Text
- 4 Search Style Text
- 5 SetLine Background
- 6 Setting the font style, foreground and background colors of StyledText
- 7 Styled Text with highlighted Odd Line
- 8 SWT StyledText
- 9 Text with underline and strike through
Demonstrates StyleRanges
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates StyleRanges
*/
public class StyleRangeTest {
private Color orange;
private Color blue;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
// Create colors for style ranges
orange = new Color(display, 255, 127, 0);
blue = display.getSystemColor(SWT.COLOR_BLUE);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// We created orange, but not blue
orange.dispose();
display.dispose();
}
/**
* Creates the main window contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FillLayout());
// Create the StyledText
StyledText styledText = new StyledText(shell, SWT.BORDER);
// Set the text
styledText.setText("Go Gators");
/*
* The multiple setStyleRange() method // Turn all of the text orange, with
* the default background color styledText.setStyleRange(new StyleRange(0, 9,
* orange, null));
* // Turn "Gators" blue styledText.setStyleRange(new StyleRange(3, 6, blue,
* null));
*/
/*
* The setStyleRanges() method // Create the array to hold the StyleRanges
* StyleRange[] ranges = new StyleRange[2];
* // Create the first StyleRange, making sure not to overlap. Include the
* space. ranges[0] = new StyleRange(0, 3, orange, null);
* // Create the second StyleRange ranges[1] = new StyleRange(3, 6, blue,
* null);
* // Replace all the StyleRanges for the StyledText
* styledText.setStyleRanges(ranges);
*/
/* The replaceStyleRanges() method */
// Create the array to hold the StyleRanges
StyleRange[] ranges = new StyleRange[2];
// Create the first StyleRange, making sure not to overlap. Include the
// space.
ranges[0] = new StyleRange(0, 3, orange, null);
// Create the second StyleRange
ranges[1] = new StyleRange(3, 6, blue, null);
// Replace only the StyleRanges in the affected area
styledText.replaceStyleRanges(0, 9, ranges);
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new StyleRangeTest().run();
}
}
Implements syntax coloring using the StyledText API
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import java.util.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
* This class implements syntax coloring using the StyledText API
*/
public class SyntaxTest {
// Punctuation
private static final String PUNCTUATION = "(){};!&|.+-*/";
// Color for the StyleRanges
private Color red;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
// Get color for style ranges
red = display.getSystemColor(SWT.COLOR_RED);
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
// No need to dispose red
display.dispose();
}
/**
* Creates the main window contents
*
* @param shell the main window
*/
private void createContents(Shell shell) {
shell.setLayout(new FillLayout());
// Create the StyledText
final StyledText styledText = new StyledText(shell, SWT.BORDER);
// Add the syntax coloring handler
styledText.addExtendedModifyListener(new ExtendedModifyListener() {
public void modifyText(ExtendedModifyEvent event) {
// Determine the ending offset
int end = event.start + event.length - 1;
// If they typed something, get it
if (event.start <= end) {
// Get the text
String text = styledText.getText(event.start, end);
// Create a collection to hold the StyleRanges
java.util.List ranges = new java.util.ArrayList();
// Turn any punctuation red
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, red, null, SWT.BOLD));
}
}
// If we have any ranges to set, set them
if (!ranges.isEmpty()) {
styledText.replaceStyleRanges(event.start, event.length,
(StyleRange[]) ranges.toArray(new StyleRange[0]));
}
}
}
});
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new SyntaxTest().run();
}
}
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
/**
* This class contains information for syntax coloring and styling for an
* extension
*/
class SyntaxData {
private String extension;
private Collection keywords;
private String punctuation;
private String comment;
private String multiLineCommentStart;
private String multiLineCommentEnd;
/**
* Constructs a SyntaxData
*
* @param extension the extension
*/
public SyntaxData(String extension) {
this.extension = extension;
}
/**
* Gets the extension
*
* @return String
*/
public String getExtension() {
return extension;
}
/**
* Gets the comment
*
* @return String
*/
public String getComment() {
return comment;
}
/**
* Sets the comment
*
* @param comment The comment to set.
*/
public void setComment(String comment) {
this.rument = comment;
}
/**
* Gets the keywords
*
* @return Collection
*/
public Collection getKeywords() {
return keywords;
}
/**
* Sets the keywords
*
* @param keywords The keywords to set.
*/
public void setKeywords(Collection keywords) {
this.keywords = keywords;
}
/**
* Gets the multi-line comment end
*
* @return String
*/
public String getMultiLineCommentEnd() {
return multiLineCommentEnd;
}
/**
* Sets the multi-line comment end
*
* @param multiLineCommentEnd The multiLineCommentEnd to set.
*/
public void setMultiLineCommentEnd(String multiLineCommentEnd) {
this.multiLineCommentEnd = multiLineCommentEnd;
}
/**
* Gets the multi-line comment start
*
* @return String
*/
public String getMultiLineCommentStart() {
return multiLineCommentStart;
}
/**
* Sets the multi-line comment start
*
* @param multiLineCommentStart The multiLineCommentStart to set.
*/
public void setMultiLineCommentStart(String multiLineCommentStart) {
this.multiLineCommentStart = multiLineCommentStart;
}
/**
* Gets the punctuation
*
* @return String
*/
public String getPunctuation() {
return punctuation;
}
/**
* Sets the punctuation
*
* @param punctuation The punctuation to set.
*/
public void setPunctuation(String punctuation) {
this.punctuation = punctuation;
}
}
//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
/**
* This class manages the syntax coloring and styling data
*/
class SyntaxManager {
// Lazy cache of SyntaxData objects
private static Map data = new Hashtable();
/**
* Gets the syntax data for an extension
*/
public static synchronized SyntaxData getSyntaxData(String extension) {
// Check in cache
SyntaxData sd = (SyntaxData) data.get(extension);
if (sd == null) {
// Not in cache; load it and put in cache
sd = loadSyntaxData(extension);
if (sd != null) data.put(sd.getExtension(), sd);
}
return sd;
}
/**
* Loads the syntax data for an extension
*
* @param extension the extension to load
* @return SyntaxData
*/
private static SyntaxData loadSyntaxData(String extension) {
SyntaxData sd = null;
try {
ResourceBundle rb = ResourceBundle.getBundle("examples.ch11." + extension);
sd = new SyntaxData(extension);
sd.setComment(rb.getString("comment"));
sd.setMultiLineCommentStart(rb.getString("multilinecommentstart"));
sd.setMultiLineCommentEnd(rb.getString("multilinecommentend"));
// Load the keywords
Collection keywords = new ArrayList();
for (StringTokenizer st = new StringTokenizer(rb.getString("keywords"), " "); st
.hasMoreTokens();) {
keywords.add(st.nextToken());
}
sd.setKeywords(keywords);
// Load the punctuation
sd.setPunctuation(rb.getString("punctuation"));
} catch (MissingResourceException e) {
// Ignore
}
return sd;
}
}
Sample Styled Text
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 19, 2004 8:49:16 PM by JACK
* $Id$
*
*****************************************************************************/
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SampleStyledText {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
public SampleStyledText() {
init();
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
styledText.setText("123456789\r\nABCDEFGHI");
StyleRange styleRange1 = new StyleRange();
styleRange1.start = 2;
styleRange1.length = 16;
styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange1.fontStyle = SWT.BOLD;
StyleRange styleRange2 = new StyleRange();
styleRange2.start = 14;
styleRange2.length = 3;
styleRange2.fontStyle = SWT.NORMAL;
styleRange2.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange2.background = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
// styledText.setStyleRange(styleRange1);
// styledText.setStyleRange(styleRange2);
//styledText.setStyleRanges(new StyleRange[]{styleRange1, styleRange2});
//styledText.setStyleRanges(new StyleRange[]{styleRange2, styleRange1});
//styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
// styledText.setSelection(4);
// System.out.println(printStyleRanges(styledText.getStyleRanges()) );
// styledText.insert("000");
System.out.println(printStyleRanges(styledText.getStyleRanges()) );
// styledText.setStyleRanges(new StyleRange[]{styleRange1});
// System.out.println(printStyleRanges(styledText.getStyleRanges()) );
//shell.pack();
shell.setSize(300, 120);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private String printStyleRanges(StyleRange[] styleRanges) {
if(styleRanges == null)
return "null";
else if(styleRanges.length == 0)
return "[]";
StringBuffer sb = new StringBuffer();
for(int i=0; i<styleRanges.length; i++) {
sb.append(styleRanges[i] + "\n");
}
return sb.toString();
}
private void init() {
}
public static void main(String[] args) {
new SampleStyledText();
}
}
Search Style Text
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 22, 2004 12:43:18 AM by JACK
* $Id$
*
*****************************************************************************/
import java.util.LinkedList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.LineStyleEvent;
import org.eclipse.swt.custom.LineStyleListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class SearchStyleText {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
Text keywordText;
Button button;
String keyword;
public SearchStyleText() {
shell.setLayout(new GridLayout(2, false));
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan = 2;
styledText.setLayoutData(gridData);
keywordText = new Text(shell, SWT.SINGLE | SWT.BORDER);
keywordText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
button = new Button(shell, SWT.PUSH);
button.setText("Search");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
keyword = keywordText.getText();
styledText.redraw();
}
});
styledText.addLineStyleListener(new LineStyleListener() {
public void lineGetStyle(LineStyleEvent event) {
if(keyword == null || keyword.length() == 0) {
event.styles = new StyleRange[0];
return;
}
String line = event.lineText;
int cursor = -1;
LinkedList list = new LinkedList();
while( (cursor = line.indexOf(keyword, cursor+1)) >= 0) {
list.add(getHighlightStyle(event.lineOffset+cursor, keyword.length()));
}
event.styles = (StyleRange[]) list.toArray(new StyleRange[list.size()]);
}
});
keyword = "SW";
styledText.setText("AWT, SWING \r\nSWT & JFACE");
shell.pack();
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private StyleRange getHighlightStyle(int startOffset, int length) {
StyleRange styleRange = new StyleRange();
styleRange.start = startOffset;
styleRange.length = length;
styleRange.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
return styleRange;
}
public static void main(String[] args) {
new SearchStyleText();
}
}
SetLine Background
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 22, 2004 12:11:04 AM by JACK
* $Id$
*
*****************************************************************************/
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class SetLineBackground {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
public SetLineBackground() {
init();
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
Font font = new Font(shell.getDisplay(), "Courier New", 12, SWT.NORMAL);
styledText.setFont(font);
styledText.setText("abcdefg\r\nhijklmn");
StyleRange styleRange1 = new StyleRange();
styleRange1.start = 2;
styleRange1.length = 3;
styleRange1.foreground = shell.getDisplay().getSystemColor(SWT.COLOR_BLUE);
styleRange1.background = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
styleRange1.fontStyle = SWT.BOLD;
styledText.setStyleRange(styleRange1);
styledText.setLineBackground(0, 1, shell.getDisplay().getSystemColor(SWT.COLOR_GREEN));
styledText.setLineBackground(1, 1, shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW));
shell.setSize(300, 120);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new SetLineBackground();
}
}
Setting the font style, foreground and background colors of StyledText
/*
* Setting the font style, foreground and background colors of StyledText
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
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 Snippet163 {
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("0123456789 ABCDEFGHIJKLM NOPQRSTUVWXYZ");
// make 0123456789 appear bold
StyleRange style1 = new StyleRange();
style1.start = 0;
style1.length = 10;
style1.fontStyle = SWT.BOLD;
text.setStyleRange(style1);
// make ABCDEFGHIJKLM have a red font
StyleRange style2 = new StyleRange();
style2.start = 11;
style2.length = 13;
style2.foreground = display.getSystemColor(SWT.COLOR_RED);
text.setStyleRange(style2);
// make NOPQRSTUVWXYZ have a blue background
StyleRange style3 = new StyleRange();
style3.start = 25;
style3.length = 13;
style3.background = display.getSystemColor(SWT.COLOR_BLUE);
text.setStyleRange(style3);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Styled Text with highlighted Odd Line
/******************************************************************************
* All Right Reserved.
* Copyright (c) 1998, 2004 Jackwind Li Guojie
*
* Created on Feb 22, 2004 1:29:05 AM by JACK
* $Id$
*
*****************************************************************************/
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class HighlightOddLine {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
public HighlightOddLine() {
shell.setLayout(new GridLayout());
styledText = new StyledText(shell, SWT.MULTI | SWT.WRAP | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
styledText.addLineBackgroundListener(new LineBackgroundListener() {
public void lineGetBackground(LineBackgroundEvent event) {
if(styledText.getLineAtOffset(event.lineOffset) % 2 == 1)
event.lineBackground = shell.getDisplay().getSystemColor(SWT.COLOR_YELLOW);
}
});
styledText.setText("Line 0\r\nLine 1\r\nLine 2\r\nLine 3\r\nLine 4\r\nLine 5\r\nLine 6");
shell.setSize(300, 150);
shell.open();
//textUser.forceFocus();
// Set up the event loop.
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
// If no more entries in event queue
display.sleep();
}
}
display.dispose();
}
private void init() {
}
public static void main(String[] args) {
new HighlightOddLine();
}
}
SWT StyledText
/*
SWT/JFace in Action
GUI Design with Eclipse 3.0
Matthew Scarpino, Stephen Holder, Stanford Ng, and Laurent Mihalkovic
ISBN: 1932394273
Publisher: Manning
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
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.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.ruposite;
public class Ch5Persistent extends Composite {
private static final String END_STYLES_MARK = "***EndStyles***";
private static final String START_STYLES_MARK = "***Styles***";
private static final String START_TEXT_MARK = "***Text***";
private static final String FILE_NAME = "editorData.txt";
private boolean doBold = false;
private StyledText styledText;
public Ch5Persistent(Composite parent) {
super(parent, SWT.NONE);
buildControls();
}
protected void buildControls() {
this.setLayout(new FillLayout());
styledText = new StyledText(this, SWT.MULTI | SWT.V_SCROLL);
load();
styledText.addExtendedModifyListener(new ExtendedModifyListener() {
public void modifyText(ExtendedModifyEvent event) {
if (doBold) {
StyleRange style = new StyleRange(event.start,
event.length, null, null, SWT.BOLD);
styledText.setStyleRange(style);
}
}
});
styledText.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
switch (e.keyCode) {
case SWT.F1:
toggleBold();
break;
default:
//ignore everything else
}
}
});
}
private void toggleBold() {
doBold = !doBold;
if (styledText.getSelectionCount() > 0) {
Point selectionRange = styledText.getSelectionRange();
StyleRange style = new StyleRange(selectionRange.x,
selectionRange.y, null, null, doBold ? SWT.BOLD
: SWT.NORMAL);
styledText.setStyleRange(style);
}
}
private void load() {
File file = new File(FILE_NAME);
if (file.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String currLine = reader.readLine();
StringTokenizer tokenizer = new StringTokenizer(currLine);
tokenizer.nextToken(); //discard START_TEXT_MARKER
String contentLengthString = tokenizer.nextToken();
int contentLength = Integer.parseInt(contentLengthString);
readContent(reader, contentLength);
//find the beginning of the styles section
while (((currLine = reader.readLine()) != null)
&& !START_STYLES_MARK.equals(currLine))
;
readStyles(reader, currLine);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void readStyles(BufferedReader reader, String currLine)
throws IOException {
while (!END_STYLES_MARK.equals(currLine)) {
currLine = reader.readLine();
if (!END_STYLES_MARK.equals(currLine))
buildOneStyle(currLine);
}
}
private void readContent(BufferedReader reader, int contentLength)
throws IOException {
char[] buffer = new char[contentLength];
reader.read(buffer, 0, contentLength);
styledText.append(new String(buffer));
}
private void buildOneStyle(String styleText) {
StringTokenizer tokenizer = new StringTokenizer(styleText);
int startPos = Integer.parseInt(tokenizer.nextToken());
int length = Integer.parseInt(tokenizer.nextToken());
StyleRange style = new StyleRange(startPos, length, null, null,
SWT.BOLD);
styledText.setStyleRange(style);
}
private void save() {
try {
PrintWriter writer = new PrintWriter(new BufferedWriter(
new FileWriter(FILE_NAME)));
String text = styledText.getText();
writer.println(START_TEXT_MARK + " " + text.length());
writer.println(text);
writer.println(START_STYLES_MARK);
StyleRange[] styles = styledText.getStyleRanges();
for (int i = 0; i < styles.length; i++) {
writer.println(styles[i].start + " " + styles[i].length);
}
writer.println(END_STYLES_MARK);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Text with underline and strike through
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;
/*
* Text with underline and strike through
*
* For a list of all SWT example snippets see
* http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
*/
public class Snippet189 {
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);
// make ABCDEFGHIJKLM have a strike through
StyleRange style2 = new StyleRange();
style2.start = 11;
style2.length = 13;
style2.strikeout = true;
text.setStyleRange(style2);
// make NOPQRSTUVWXYZ appear underlined and have a strike through
StyleRange style3 = new StyleRange();
style3.start = 25;
style3.length = 13;
style3.underline = true;
style3.strikeout = true;
text.setStyleRange(style3);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}