Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a2fdc76bf2908fb7ed60e302d78fd0ac4150aafd (plain) (blame)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*******************************************************************************
 * Copyright (c) 2000, 2004 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.examples.clipboard;
 
import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.dnd.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class ClipboardExample {

	Clipboard clipboard;
	Shell shell;
	Text copyText;
	Text pasteText;
	Text copyRtfText;
	Text pasteRtfText;
	Text copyHtmlText;
	Text pasteHtmlText;
	Table copyFileTable;
	Table pasteFileTable;
	Text text;
	Combo combo;
	StyledText styledText;
	Label status;
	static final int SIZE = 60;
	
public static void main( String[] args) {
	Display display = new Display();
	new ClipboardExample().open(display);
	display.dispose();
}
public void open(Display display) {
	clipboard = new Clipboard(display);
	shell = new Shell (display);
	shell.setText("SWT Clipboard");
	shell.setLayout(new FillLayout());
	
	ScrolledComposite sc = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
	Composite parent = new Composite(sc, SWT.NONE);
	sc.setContent(parent);
	parent.setLayout(new GridLayout(2, true));
	
	Group copyGroup = new Group(parent, SWT.NONE);
	copyGroup.setText("Copy From:");
	GridData data = new GridData(GridData.FILL_BOTH);
	copyGroup.setLayoutData(data);
	copyGroup.setLayout(new GridLayout(3, false));
	
	Group pasteGroup = new Group(parent, SWT.NONE);
	pasteGroup.setText("Paste To:");
	data = new GridData(GridData.FILL_BOTH);
	pasteGroup.setLayoutData(data);
	pasteGroup.setLayout(new GridLayout(3, false));
	
	Group controlGroup = new Group(parent, SWT.NONE);
	controlGroup.setText("Control API:");
	data = new GridData(GridData.FILL_BOTH);
	data.horizontalSpan = 2;
	controlGroup.setLayoutData(data);
	controlGroup.setLayout(new GridLayout(5, false));
	
	Group typesGroup = new Group(parent, SWT.NONE);
	typesGroup.setText("Available Types");
	data = new GridData(GridData.FILL_BOTH);
	data.horizontalSpan = 2;
	typesGroup.setLayoutData(data);
	typesGroup.setLayout(new GridLayout(2, false));
	
	status = new Label(parent, SWT.BORDER);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.horizontalSpan = 2;
	data.heightHint = 60;
	status.setLayoutData(data);
	
	createTextTransfer(copyGroup, pasteGroup);
	createRTFTransfer(copyGroup, pasteGroup);
	createHTMLTransfer(copyGroup, pasteGroup);
	createFileTransfer(copyGroup, pasteGroup);
	createMyTransfer(copyGroup, pasteGroup);
	createControlTransfer(controlGroup);
	createAvailableTypes(typesGroup);
	
	sc.setMinSize(parent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
	sc.setExpandHorizontal(true);
	sc.setExpandVertical(true);
	
	Point size = shell.computeSize(SWT.DEFAULT, SWT.DEFAULT);
	Rectangle monitorArea = shell.getMonitor().getClientArea();
	shell.setSize(Math.min(size.x, monitorArea.width - 20), Math.min(size.y, monitorArea.height - 20));
	shell.open();
	while (!shell.isDisposed ()) {
		if (!display.readAndDispatch ()) display.sleep ();
	}
	clipboard.dispose();
}
void createTextTransfer(Composite copyParent, Composite pasteParent) {
	
	// TextTransfer
	Label l = new Label(copyParent, SWT.NONE);
	l.setText("TextTransfer:"); //$NON-NLS-1$
	copyText = new Text(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	copyText.setText("some\nplain\ntext");
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	copyText.setLayoutData(data);
	Button b = new Button(copyParent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = copyText.getText();
			if (data.length() > 0) {
				status.setText("");
				clipboard.setContents(new Object[] {data}, new Transfer[] {TextTransfer.getInstance()});
			} else {
				status.setText("nothing to copy");
			}
		}
	});
	
	l = new Label(pasteParent, SWT.NONE);
	l.setText("TextTransfer:"); //$NON-NLS-1$
	pasteText = new Text(pasteParent, SWT.READ_ONLY | SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	pasteText.setLayoutData(data);
	b = new Button(pasteParent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = (String)clipboard.getContents(TextTransfer.getInstance());
			if (data != null && data.length() > 0) {
				status.setText("");
				pasteText.setText("begin paste>"+data+"<end paste");
			} else {
				status.setText("nothing to paste");
			}
		}
	});
}
void createRTFTransfer(Composite copyParent, Composite pasteParent){
	//	RTF Transfer
	Label l = new Label(copyParent, SWT.NONE);
	l.setText("RTFTransfer:"); //$NON-NLS-1$
	copyRtfText = new Text(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	copyRtfText.setText("some\nrtf\ntext");
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	copyRtfText.setLayoutData(data);
	Button b = new Button(copyParent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = copyRtfText.getText();
			if (data.length() > 0) {
				status.setText("");
				data = "{\\rtf1{\\colortbl;\\red255\\green0\\blue0;}\\uc1\\b\\i " + data + "}";
				clipboard.setContents(new Object[] {data}, new Transfer[] {RTFTransfer.getInstance()});
			} else {
				status.setText("nothing to copy");
			}
		}
	});
	  
	l = new Label(pasteParent, SWT.NONE);
	l.setText("RTFTransfer:"); //$NON-NLS-1$
	pasteRtfText = new Text(pasteParent, SWT.READ_ONLY | SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	pasteRtfText.setLayoutData(data);
	b = new Button(pasteParent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = (String)clipboard.getContents(RTFTransfer.getInstance());
			if (data != null && data.length() > 0) {
				status.setText("");
				pasteRtfText.setText("start paste>"+data+"<end paste");
			} else {
				status.setText("nothing to paste");
			}
		}
	});
}
void createHTMLTransfer(Composite copyParent, Composite pasteParent){
	//	HTML Transfer
	Label l = new Label(copyParent, SWT.NONE);
	l.setText("HTMLTransfer:"); //$NON-NLS-1$
	copyHtmlText = new Text(copyParent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	copyHtmlText.setText("<b>Hello World</b>");
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	copyHtmlText.setLayoutData(data);
	Button b = new Button(copyParent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = copyHtmlText.getText();
			if (data.length() > 0) {
				status.setText("");
				clipboard.setContents(new Object[] {data}, new Transfer[] {HTMLTransfer.getInstance()});
			} else {
				status.setText("nothing to copy");
			}
		}
	});
	  
	l = new Label(pasteParent, SWT.NONE);
	l.setText("HTMLTransfer:"); //$NON-NLS-1$
	pasteHtmlText = new Text(pasteParent, SWT.READ_ONLY | SWT.MULTI | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	pasteHtmlText.setLayoutData(data);
	b = new Button(pasteParent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String data = (String)clipboard.getContents(HTMLTransfer.getInstance());
			if (data != null && data.length() > 0) {
				status.setText("");
				pasteHtmlText.setText("start paste>"+data+"<end paste");
			} else {
				status.setText("nothing to paste");
			}
		}
	});
}
void createFileTransfer(Composite copyParent, Composite pasteParent){
	//File Transfer
	Label l = new Label(copyParent, SWT.NONE);
	l.setText("FileTransfer:"); //$NON-NLS-1$
	
	Composite c = new Composite(copyParent, SWT.NONE);
	c.setLayout(new GridLayout(2, false));
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	c.setLayoutData(data);
	
	copyFileTable = new Table(c, SWT.MULTI | SWT.BORDER);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	data.horizontalSpan = 2;
	copyFileTable.setLayoutData(data);
	
	Button b = new Button(c, SWT.PUSH);
	b.setText("Select file(s)");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			FileDialog dialog = new FileDialog(shell, SWT.OPEN | SWT.MULTI);
			String result = dialog.open();
			if (result != null && result.length() > 0){
				//copyFileTable.removeAll();
				String separator = System.getProperty("file.separator");
				String path = dialog.getFilterPath();
				String[] names = dialog.getFileNames();
				for (int i = 0; i < names.length; i++) {
					TableItem item = new TableItem(copyFileTable, SWT.NONE);
					item.setText(path+separator+names[i]);
				}
			}
		}
	});
	b = new Button(c, SWT.PUSH);
	b.setText("Select directory");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			DirectoryDialog dialog = new DirectoryDialog(shell, SWT.OPEN);
			String result = dialog.open();
			if (result != null && result.length() > 0){
				//copyFileTable.removeAll();
				TableItem item = new TableItem(copyFileTable, SWT.NONE);
				item.setText(result);
			}
		}
	});
	
	b = new Button(copyParent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			TableItem[] items = copyFileTable.getItems();
			if (items.length > 0){
				status.setText("");
				String[] data = new String[items.length];
				for (int i = 0; i < data.length; i++) {
					data[i] = items[i].getText();
				}
				clipboard.setContents(new Object[] {data}, new Transfer[] {FileTransfer.getInstance()});
			} else {
				status.setText("nothing to copy");
			}
		}
	});
	
	l = new Label(pasteParent, SWT.NONE);
	l.setText("FileTransfer:"); //$NON-NLS-1$
	pasteFileTable = new Table(pasteParent, SWT.MULTI | SWT.BORDER);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	pasteFileTable.setLayoutData(data);
	b = new Button(pasteParent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			String[] data = (String[])clipboard.getContents(FileTransfer.getInstance());
			if (data != null && data.length > 0) {
				status.setText("");
				pasteFileTable.removeAll();
				for (int i = 0; i < data.length; i++) {
					TableItem item = new TableItem(pasteFileTable, SWT.NONE);
					item.setText(data[i]);
				}
			} else {
				status.setText("nothing to paste");
			}
		}
	});	 
}
void createMyTransfer(Composite copyParent, Composite pasteParent){
	//	MyType Transfer
	// TODO
}
void createControlTransfer(Composite parent){
	Label l = new Label(parent, SWT.NONE);
	l.setText("Text:");
	Button b = new Button(parent, SWT.PUSH);
	b.setText("Cut");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			text.cut();
		}
	});
	b = new Button(parent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			text.copy();
		}
	});
	b = new Button(parent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			text.paste();
		}
	});
	text = new Text(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	GridData data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	text.setLayoutData(data);
	
	l = new Label(parent, SWT.NONE);
	l.setText("Combo:");
	b = new Button(parent, SWT.PUSH);
	b.setText("Cut");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			combo.cut();
		}
	});
	b = new Button(parent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			combo.copy();
		}
	});
	b = new Button(parent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			combo.paste();
		}
	});
	combo = new Combo(parent, SWT.NONE);
	combo.setItems(new String[] {"Item 1", "Item 2", "Item 3", "A longer Item"});
	
	l = new Label(parent, SWT.NONE);
	l.setText("StyledText:");
	l = new Label(parent, SWT.NONE);
	l.setVisible(false);
	b = new Button(parent, SWT.PUSH);
	b.setText("Copy");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			styledText.copy();
		}
	});
	b = new Button(parent, SWT.PUSH);
	b.setText("Paste");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			styledText.paste();
		}
	});
	styledText = new StyledText(parent, SWT.BORDER | SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
	data = new GridData(GridData.FILL_HORIZONTAL);
	data.heightHint = data.widthHint = SIZE;
	styledText.setLayoutData(data);
}
void createAvailableTypes(Composite parent){
	final List list = new List(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
	GridData data = new GridData(GridData.FILL_BOTH);
	data.heightHint = 100;
	list.setLayoutData(data);
	Button b = new Button(parent, SWT.PUSH);
	b.setText("Get Available Types");
	b.addSelectionListener(new SelectionAdapter() {
		public void widgetSelected(SelectionEvent e) {
			list.removeAll();
			String[] names = clipboard.getAvailableTypeNames();
			for (int i = 0; i < names.length; i++) {
				list.add(names[i]);
			}
		}
	});
}
}

Back to the top