117
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2007 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * D Port:
|
|
11 * Thomas Demmer <t_demmer AT web DOT de>
|
|
12 *******************************************************************************/
|
|
13 module clipboard.Snippets282;
|
|
14
|
|
15 /*
|
|
16 * Copy/paste image to/from clipboard.
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21
|
|
22 import dwt.DWT;
|
|
23 import dwt.dnd.Clipboard;
|
|
24 import dwt.dnd.ImageTransfer;
|
|
25 import dwt.dnd.Transfer;
|
|
26 import dwt.graphics.Image;
|
|
27 import dwt.graphics.ImageData;
|
|
28 import dwt.layout.GridLayout;
|
|
29 import dwt.layout.GridData;
|
|
30
|
|
31 import dwt.widgets.Button;
|
|
32 import dwt.widgets.Composite;
|
|
33 import dwt.widgets.Display;
|
|
34 import dwt.widgets.Event;
|
|
35 import dwt.widgets.FileDialog;
|
|
36 import dwt.widgets.Listener;
|
|
37 import dwt.widgets.Shell;
|
|
38
|
|
39 import dwt.dwthelper.utils;
|
|
40
|
|
41 public static void main(String[] args) {
|
|
42 Display display = new Display();
|
|
43 Clipboard clipboard = new Clipboard(display);
|
|
44 Shell shell = new Shell(display, DWT.SHELL_TRIM);
|
|
45 shell.setLayout(new GridLayout());
|
|
46 shell.setText("Clipboard ImageTransfer");
|
|
47
|
|
48 Button imageButton = new Button(shell, DWT.NONE );
|
|
49 GridData gd = new GridData(DWT.FILL, DWT.FILL, true, true);
|
|
50 gd.minimumHeight = 400;
|
|
51 gd.minimumWidth = 600;
|
|
52 imageButton.setLayoutData(gd);
|
|
53
|
|
54 Composite buttons = new Composite(shell, DWT.NONE);
|
|
55 buttons.setLayout(new GridLayout(4, true));
|
|
56 Button button = new Button(buttons, DWT.PUSH);
|
|
57 button.setText("Open");
|
|
58 button.addListener(DWT.Selection, new class() Listener {
|
|
59 public void handleEvent(Event event) {
|
|
60 FileDialog dialog = new FileDialog (shell, DWT.OPEN);
|
|
61 dialog.setText("Open an image file or cancel");
|
|
62 String string = dialog.open ();
|
|
63 if (string !is null) {
|
|
64 Image image = imageButton.getImage();
|
|
65 if (image !is null) image.dispose();
|
|
66 image = new Image(display, string);
|
|
67 imageButton.setImage(image);
|
|
68 }
|
|
69 }
|
|
70 });
|
|
71
|
|
72 button = new Button(buttons, DWT.PUSH);
|
|
73 button.setText("Copy");
|
|
74 button.addListener(DWT.Selection, new class() Listener {
|
|
75 public void handleEvent(Event event) {
|
|
76 Image image = imageButton.getImage();
|
|
77 if (image !is null) {
|
|
78 ImageTransfer transfer = ImageTransfer.getInstance();
|
|
79
|
|
80 Transfer[] xfer = [ transfer ];
|
|
81 Object[] td = [ image.getImageData ];
|
|
82
|
|
83 clipboard.setContents(td,xfer);
|
|
84 }
|
|
85 }
|
|
86 });
|
|
87 button = new Button(buttons, DWT.PUSH);
|
|
88 button.setText("Paste");
|
|
89 button.addListener(DWT.Selection, new class() Listener {
|
|
90 public void handleEvent(Event event) {
|
|
91 ImageTransfer transfer = ImageTransfer.getInstance();
|
|
92 ImageData imageData = cast(ImageData)clipboard.getContents(transfer);
|
|
93 if (imageData !is null) {
|
|
94 imageButton.setText("");
|
|
95 Image image = imageButton.getImage();
|
|
96 if (image !is null) image.dispose();
|
|
97 image = new Image(display, imageData);
|
|
98 imageButton.setImage(image);
|
|
99 } else {
|
|
100 imageButton.setText("No image");
|
|
101 }
|
|
102 }
|
|
103 });
|
|
104
|
|
105 button = new Button(buttons, DWT.PUSH);
|
|
106 button.setText("Clear");
|
|
107 button.addListener(DWT.Selection, new class() Listener {
|
|
108 public void handleEvent(Event event) {
|
|
109 imageButton.setText("");
|
|
110 Image image = imageButton.getImage();
|
|
111 if (image !is null) image.dispose();
|
|
112 imageButton.setImage(null);
|
|
113 }
|
|
114 });
|
|
115 shell.pack();
|
|
116 shell.open();
|
|
117 while (!shell.isDisposed()) {
|
|
118 if (!display.readAndDispatch())
|
|
119 display.sleep();
|
|
120 }
|
|
121 display.dispose();
|
|
122 }
|
|
123
|