Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8f697d16480bc9cb17c40465609dad374f5bde11 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsf.common.ui.internal.guiutils;


import org.eclipse.draw2d.ColorConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
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.Composite;

/**
 * A general purpose class for displying an image in a composite. There is no
 * eclipse tool for doing this other than CLabl which is bulkly.
 * 
 * This was original written by Karl Reti.
 * 
 * @author mengbo
 */
public class ImageContainer extends Composite {
	private Image _image = null;

	private boolean _bCleanupImage;

	/**
	 * @param parent
	 */
	public ImageContainer(Composite parent) {
		super(parent, SWT.NONE);// SWT.NO_BACKGROUND );//|
		// SWT.NO_REDRAW_RESIZE);
		setBackground(ColorConstants.white);
		// paint the image
		addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if (_image != null) {
					e.gc.drawImage(_image, 0, 0);
				}
			}

		});

		addControlListener(new ControlAdapter() {
			public void controlResized(ControlEvent e) {
				setSize(_image.getBounds().width, _image.getBounds().height);
			}
		});

		// tidy up
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				if (_bCleanupImage && _image != null && !_image.isDisposed()) {
					_image.dispose();
				}
			}
		});
	}

	/**
	 * @return Returns the image.
	 */
	public Image getImage() {
		return _image;
	}

	/**
	 * @return should the image be disposed of on cleanup. Set false if the
	 *         image is cached.
	 */
	public boolean isCleanupImage() {
		return _bCleanupImage;
	}

	/**
	 * @param image
	 *            The image to set.
	 */
	public void setImage(Image image) {
		setImage(image, false);
	}

	/**
	 * @param image
	 *            The image to set.
	 * @param bCleanupImage 
	 */
	public void setImage(Image image, boolean bCleanupImage) {
		_image = image;
		_bCleanupImage = bCleanupImage;
		setSize(image.getBounds().width, image.getBounds().height);
	}
}

Back to the top