SWT TextEditor

In this section, we are going to show you how to create a TextEditor using
SWT in Java.
In SWT, the classes ToolBar and ToolItem are allowed to create
the TextEditor. The class ToolBar provides the toolBar items and the
class ToolItem adds the item to the toolbar. The method setText()
of class ToolItem sets the string specified and the method addListener()
calls the Event class to perform the action on the ToolItem.
In this example the class StyledText is used to display and edit the
text and the method styledText.copy()
copies the selected text to the clipboard. The method styledText.cut() is
used to moves
the selected text to the clipboard. Apart from this styledText.paste()
method is defined to replaces the selection with the text on the clipboard. The method setText()
sets the specified content and the method setFont() sets the specified
font to render the text.
We have defined a variable unsaved of Boolean type to check whether there is
any change after saving. The method saveChanges() is created to save the
unsaved changes before discarding the text. To display the alert message, MessageBox
is used.
The method loadText() is created to load the text from the selected
file. For this, we have used the class BufferedReader to read the file.
The method styledText.setText(buffer.toString()) sets the content by
converting the data of StringBuffer into string.
The method saveText() is created to save the content of the StyledText
to the file. In this method, the FileWriter class writes the content of StyledText.
The class FileDialog is used to navigate the file system and
and allows to select or enter a file name.
Here is the code of TextEditor.java
import java.io.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.jface.resource.*;
import org.eclipse.swt.custom.StyledText;
public class TextEditor {
Display display = new Display();
Shell shell = new Shell(display);
StyledText styledText;
boolean unsaved;
File file;
String lastDirectory;
public TextEditor() {
shell.setLayout(new GridLayout());
ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT );
ToolItem item1 = new ToolItem(toolBar, SWT.PUSH);
item1.setText("New");
item1.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if(saveChanges()) {
file = null;
styledText.setText("");
}
}
});
ToolItem item2 = new ToolItem(toolBar, SWT.PUSH);
item2.setText("Open");
item2.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
loadText();
}
});
ToolItem item3 = new ToolItem(toolBar, SWT.PUSH);
item3.setText("Save");
item3.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
saveText();
}
});
ToolItem item4 = new ToolItem(toolBar, SWT.PUSH);
item4.setText("Copy");
item4.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
styledText.copy();
}
});
ToolItem item5 = new ToolItem(toolBar, SWT.PUSH);
item5.setText("Cut");
item5.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
styledText.cut();
}
});
ToolItem item6 = new ToolItem(toolBar, SWT.PUSH);
item6.setText("Paste");
item6.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
styledText.paste();
}
});
ToolItem item7 = new ToolItem(toolBar, SWT.PUSH);
item7.setText("Exit");
item7.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
if(saveChanges())
shell.dispose();
}
});
toolBar.pack();
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(),"Book Antiqua",12,
SWT.NORMAL);
styledText.setFont(font);
styledText.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
unsaved = true;
}
});
shell.setText("Editor");
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
boolean saveChanges() {
if(! unsaved)
return true;
MessageBox box = new MessageBox(shell,SWT.ICON_WARNING|SWT.YES|
SWT.NO | SWT.CANCEL);
box.setMessage("save changes? " );
box.setText("Editor");
int condition = box.open();
if(condition == SWT.YES) {
return saveText();
}else if(condition == SWT.NO) {
return true;
}else{
return false;
}
}
boolean loadText() {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
if (lastDirectory != null)
dialog.setFilterPath(lastDirectory);
String selectedFile = dialog.open();
if (selectedFile == null) {
System.out.println("File is not opened");
return false;
}
file = new File(selectedFile);
lastDirectory = file.getParent();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer buffer = new StringBuffer();
String line = null;
while((line = reader.readLine()) != null) {
buffer.append(line);
buffer.append("\r\n");
}
styledText.setText(buffer.toString());
return true;
}catch(IOException e) {}
return false;
}
boolean saveText() {
if (file == null) {
FileDialog fileDialog = new FileDialog(shell, SWT.SAVE);
if (lastDirectory != null)
fileDialog.setFilterPath(lastDirectory);
String selectedFile = fileDialog.open();
if (selectedFile == null) {
System.out.println("File is not saved");
return false;
}
file = new File(selectedFile);
lastDirectory = file.getParent();
}
try {
FileWriter writer = new FileWriter(file);
writer.write(styledText.getText());
writer.close();
unsaved = false;
return true;
} catch (IOException e) {}
return false;
}
public static void main(String[] args) {
new TextEditor();
}
}
|
Output will be displayed as

Download Source Code