summaryrefslogtreecommitdiffstats
path: root/examples/org.eclipse.swt.examples/src/org/eclipse/swt/examples/texteditor/TextEditor.java
blob: 92532bfcc434f4b4259879e1d1466554dadb1da5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package org.eclipse.swt.examples.texteditor;

/*

 * (c) Copyright IBM Corp. 2000, 2001, 2002.

 * All Rights Reserved

 */

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import java.util.*;
import java.io.*;

/**

 */
public class TextEditor {  
	Shell shell;
	ToolBar toolBar;
	StyledText text;

	Images images = new Images();
	Vector cachedStyles = new Vector();
	Color RED = null; 
	Color BLUE = null; 
	Color GREEN = null; 
	Font font = null;
	
	boolean isBold = false;
	
	ExtendedModifyListener extendedModifyListener;
	VerifyKeyListener verifyKeyListener;
	static ResourceBundle resources = ResourceBundle.getBundle("examples_texteditor");

Menu createEditMenu() {
	Menu bar = shell.getMenuBar ();
	Menu menu = new Menu (bar);
	
	MenuItem item = new MenuItem (menu, SWT.PUSH);
	item.setText (resources.getString("Cut_menuitem"));
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			text.cut();
		}
	});

	item = new MenuItem (menu, SWT.PUSH);
	item.setText (resources.getString("Copy_menuitem"));
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			text.copy();
		}
	});

	item = new MenuItem (menu, SWT.PUSH);
	item.setText (resources.getString("Paste_menuitem"));
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			text.paste();
		}
	});

	new MenuItem (menu, SWT.SEPARATOR);
	
	item = new MenuItem (menu, SWT.PUSH);
	item.setText (resources.getString("Font_menuitem"));
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			setFont();
		}
	});
	return menu;
}


/*

 * Set the text state to bold.

 */
void bold(boolean bold) {
	isBold = bold;
}
/*

 * Clear all style data for the selected text.

 */
void clear() {
	Point sel = text.getSelectionRange();
	if ((sel != null) && (sel.y != 0)) {
		StyleRange style;
		style = new StyleRange(sel.x, sel.y, null, null, SWT.NORMAL);
		text.setStyleRange(style);
	}
	text.setSelectionRange(sel.x + sel.y, 0);
}
/*

 * Set the foreground color for the selected text.

 */
void fgColor(int color) {
	Point sel = text.getSelectionRange();
	if ((sel == null) || (sel.y == 0)) return;
	Color fg;
	if (color == SWT.COLOR_RED) {
		fg = RED;
	} else if (color == SWT.COLOR_GREEN) {
		fg = GREEN;
	} else if (color == SWT.COLOR_BLUE) {
		fg = BLUE;
	} else {
		fg = null;
	}
	StyleRange style;
	for (int i = sel.x; i<sel.x+sel.y; i++) {
		StyleRange range = text.getStyleRangeAtOffset(i);
		if (range == null) {style = new StyleRange(i, 1, fg, null, SWT.NORMAL);}
		else {style = new StyleRange(i, 1, fg, null, range.fontStyle);};
		text.setStyleRange(style);
	}
	text.setSelectionRange(sel.x + sel.y, 0);
}
void createMenuBar () {
	Menu bar = new Menu (shell, SWT.BAR);
	shell.setMenuBar (bar);

	MenuItem editItem = new MenuItem (bar, SWT.CASCADE);
	editItem.setText (resources.getString("Edit_menuitem"));
	editItem.setMenu (createEditMenu ());
}

void createShell (Display display) {
	shell = new Shell (display);
	shell.setText (resources.getString("Window_title"));	
	images.loadAll (display);
	GridLayout layout = new GridLayout();
	layout.numColumns = 1;
	shell.setSize(500, 300);
	shell.setLayout(layout);
	shell.addDisposeListener (new DisposeListener () {
		public void widgetDisposed (DisposeEvent e) {
			if (font != null) font.dispose();
			images.freeAll ();
			RED.dispose();
			GREEN.dispose();
			BLUE.dispose();
		}
	});
}
void createStyledText() {
	initializeColors();
	text = new StyledText (shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
	GridData spec = new GridData();
	spec.horizontalAlignment = spec.FILL;
	spec.grabExcessHorizontalSpace = true;
	spec.verticalAlignment = spec.FILL;
	spec.grabExcessVerticalSpace = true;
	text.setLayoutData(spec);
	extendedModifyListener = new ExtendedModifyListener() {
		public void modifyText(ExtendedModifyEvent e) {
			handleExtendedModify(e);
		}
	};
	text.addExtendedModifyListener(extendedModifyListener);
	verifyKeyListener = new VerifyKeyListener() {
		public void verifyKey(VerifyEvent e) {
			handleVerifyKey(e);
		}
	};
	text.addVerifyKeyListener(verifyKeyListener);
}

void createToolBar() {
	toolBar = new ToolBar(shell, SWT.NULL);
	
	ToolItem item = new ToolItem(toolBar, SWT.CHECK);
	item.setImage(images.Bold);
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			bold(((ToolItem)event.widget).getSelection());
		}
	});
	
	item = new ToolItem(toolBar, SWT.SEPARATOR);

	item = new ToolItem(toolBar, SWT.PUSH);
	item.setImage(images.Red);
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			fgColor(SWT.COLOR_RED);
		}
	});
	item = new ToolItem(toolBar, SWT.PUSH);
	item.setImage(images.Green);
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			fgColor(SWT.COLOR_GREEN);
		}
	});
	item = new ToolItem(toolBar, SWT.PUSH);
	item.setImage(images.Blue);
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			fgColor(SWT.COLOR_BLUE);
		}
	});
	
	item = new ToolItem(toolBar, SWT.SEPARATOR);

	item = new ToolItem(toolBar, SWT.PUSH);
	item.setImage(images.Erase);
	item.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent event) {
			clear();
		}
	});
}
void displayError(String msg) {
	MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);
	box.setMessage(msg);
	box.open();
}
/*

 * Cache the style information for text that has been cut or copied.

 */
void handleCutCopy() {
	// Save the cut/copied style info so that during paste we will maintain

	// the style information.  Cut/copied text is put in the clipboard in

	// RTF format, but is not pasted in RTF format.  The other way to 

	// handle the pasting of styles would be to access the Clipboard directly and 

	// parse the RTF text.

	cachedStyles = new Vector();
	Point sel = text.getSelectionRange();
	int startX = sel.x;
	for (int i=sel.x; i<=sel.x+sel.y-1; i++) {
		StyleRange style = text.getStyleRangeAtOffset(i);
		if (style != null) {
			style.start = style.start - startX;
			if (!cachedStyles.isEmpty()) {
				StyleRange lastStyle = (StyleRange)cachedStyles.lastElement();
				if (lastStyle.similarTo(style)) {
					lastStyle.length++;
				} else {
					cachedStyles.addElement(style);
				}
			} else {
				cachedStyles.addElement(style);
			}
		}
	}
}
void handleExtendedModify(ExtendedModifyEvent event) {
	if (event.length == 0) return;
	StyleRange style;
	if (event.length == 1 || text.getTextRange(event.start, event.length).equals(text.getLineDelimiter())) {
		// Have the new text take on the style of the text to its right (during

		// typing) if no style information is active.

		int caretOffset = text.getCaretOffset();
		style = null;
		if (caretOffset < text.getCharCount()) style = text.getStyleRangeAtOffset(caretOffset);
		if (style != null) {
			style.start = event.start;
			style.length = event.length;
			int fontStyle = SWT.NORMAL;
			if (isBold) fontStyle = SWT.BOLD;
			style.fontStyle = fontStyle;
			text.setStyleRange(style);
		} else if (isBold) {
			StyleRange newStyle = new StyleRange(event.start, event.length, null, null, SWT.BOLD);
			text.setStyleRange(newStyle);
		}
	} else {
		// paste occurring

		for (int i=0; i<cachedStyles.size(); i++) {
			style = (StyleRange)cachedStyles.elementAt(i);
			StyleRange newStyle = (StyleRange)style.clone();
			newStyle.start = style.start + event.start;
			text.setStyleRange(newStyle);
		}
	}
} 
/*

 * Intercept the cut and copy keys so that during paste we can maintain

 * style information.

 */
void handleVerifyKey(VerifyEvent event) {
	int input;
	if (event.keyCode != 0) input = event.keyCode | event.stateMask;
	else input = event.character | event.stateMask;
	if (isCut(input) || isCopy(input)) {
		handleCutCopy();
	} 
}

public static void main (String [] args) {
	Display display = new Display ();
	TextEditor example = new TextEditor ();
	Shell shell = example.open (display);
	while (!shell.isDisposed ())
		if (!display.readAndDispatch ()) display.sleep ();
	display.dispose ();
}

public Shell open (Display display) {
	createShell (display);
	createMenuBar ();
	createToolBar ();
	createStyledText ();
	shell.open ();
	return shell;
}

void setFont() {
	FontDialog fontDialog = new FontDialog(shell);
	fontDialog.setFontData((text.getFont()).getFontData()[0]);
	FontData fontData = fontDialog.open();
	if(fontData != null) {
		if(font != null)
			font.dispose();
		font = new Font(shell.getDisplay(), fontData);
		text.setFont(font);
	}
}

void initializeColors() {
	Display display = Display.getDefault();
	RED = new Color (display, new RGB(255,0,0));
	BLUE = new Color (display, new RGB(0,0,255));
	GREEN = new Color (display, new RGB(0,255,0));
}
boolean isCopy(int input) {

	if (input == (SWT.INSERT | SWT.CTRL)) return true;

	if (input == ('\u0003' | SWT.CTRL)) return true;

	return false;

}
boolean isCut(int input) {

	if (input == (SWT.DEL | SWT.SHIFT)) return true;

	if (input == ('\u0018' | SWT.CTRL)) return true;

	return false;

}


}