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
|
/****************************************************************************
* Copyright (c) 2007 Composent, Inc. 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:
* Composent, Inc. - initial API and implementation
*****************************************************************************/
package org.eclipse.ecf.internal.example.collab.ui;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipInputStream;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
*
*/
public class ShowImageShell {
Shell shell;
ID senderID;
ImageWrapper imageWrapper;
List imageData;
public ShowImageShell(Display display, ID senderID, ImageWrapper imageWrapper, final DisposeListener disposeListener) {
this.shell = new Shell(display);
this.senderID = senderID;
this.imageWrapper = imageWrapper;
this.shell.setBounds(0, 0, imageWrapper.width, imageWrapper.height);
this.imageData = new ArrayList();
this.shell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
disposeListener.widgetDisposed(e);
ShowImageShell.this.senderID = null;
ShowImageShell.this.imageWrapper = null;
ShowImageShell.this.imageData = null;
}
});
}
public void setText(String text) {
shell.setText(text);
}
public void open() {
shell.open();
}
public ID getSenderID() {
return senderID;
}
public void addData(byte[] bytes) {
this.imageData.add(bytes);
}
public void showImage() {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
if (imageData != null) {
for (final Iterator i = imageData.iterator(); i.hasNext();) {
bos.write((byte[]) i.next());
}
}
bos.flush();
} catch (final IOException e) {
// should not happen
}
imageData.clear();
final byte[] uncompressedData = uncompress(bos.toByteArray());
shell.getDisplay().asyncExec(new Runnable() {
public void run() {
final Image image = new Image(shell.getDisplay(), imageWrapper.createImageData(uncompressedData));
ShowImageShell.this.shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(image, 0, 0);
}
});
ShowImageShell.this.shell.redraw();
}
});
}
private static byte[] uncompress(byte[] source) {
final ZipInputStream ins = new ZipInputStream(new ByteArrayInputStream(source));
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
int read = 0;
final byte[] buf = new byte[16192];
try {
ins.getNextEntry();
while ((read = ins.read(buf)) > 0) {
bos.write(buf, 0, read);
}
bos.flush();
ins.close();
} catch (final IOException e) {
// Should not happen
}
return bos.toByteArray();
}
}
|