b6c98cad3a4e4252a31ce464609c2b76f93b819d
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / images / ImageComposite.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.editor.images;
12
13 import java.net.URI;
14 import java.net.URISyntaxException;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.FocusAdapter;
18 import org.eclipse.swt.events.FocusEvent;
19 import org.eclipse.swt.events.PaintEvent;
20 import org.eclipse.swt.events.PaintListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Font;
23 import org.eclipse.swt.graphics.Image;
24 import org.eclipse.swt.widgets.Canvas;
25 import org.eclipse.ui.forms.widgets.TableWrapData;
26 import org.eclipse.ui.views.properties.IPropertySource;
27
28 import eu.etaxonomy.cdm.model.media.ImageFile;
29 import eu.etaxonomy.taxeditor.editor.CompositeBorderDecorator;
30 import eu.etaxonomy.taxeditor.editor.GroupComposite;
31 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
32 import eu.etaxonomy.taxeditor.editor.name.AcceptedNameComposite;
33 import eu.etaxonomy.taxeditor.propertysheet.images.ImagePropertySource;
34 import eu.etaxonomy.taxeditor.store.model.ImageResources;
35
36 /**
37 * @author p.ciardelli
38 * @created 30.03.2009
39 * @version 1.0
40 */
41 public class ImageComposite extends GroupedComposite {
42
43 protected Image image;
44
45 private Canvas canvas;
46
47 private FocusAdapter focusListener;
48
49 private ImageFile imageFile;
50
51 /**
52 * @param groupComposite
53 * @param form
54 * @param taxon
55 * @param url
56 */
57 public ImageComposite(TaxonImageEditor editor, GroupComposite groupComposite, ImageFile imageFile) {
58 super(editor, groupComposite);
59
60
61
62 this.imageFile = imageFile;
63
64 setIsDraggable(false);
65 // setIcon(AcceptedNameComposite.ACCEPTED_ICON);
66 setIndent(AcceptedNameComposite.ACCEPTED_INDENT);
67
68 createImage(imageFile);
69
70 this.setMenu(editor.getMenu());
71
72 createBorderSupport();
73
74 }
75
76 protected void createControl() {
77 super.createControl();
78
79 Color groupBackgroundColor = getDisplay().getSystemColor(SWT.COLOR_WHITE);
80 setBackground(groupBackgroundColor);
81 }
82
83 /**
84 * @param url
85 */
86 private void createImage(ImageFile imageFile) {
87
88 URI uri = null;
89
90 // TODO ImageFile.getUri should return a URI
91 try {
92 uri = new URI(imageFile.getUri());
93 } catch (URISyntaxException e1) {
94 // TODO this should never happen
95 e1.printStackTrace();
96 }
97
98
99 image = ImageResources.getImage(uri);
100
101 // Not optimal - why should getting composite to size to img be so obtuse?!
102 // canvas = new Canvas(this, SWT.NO_REDRAW_RESIZE);
103 canvas = new Canvas(this, SWT.NONE);
104
105 TableWrapData layoutData = new TableWrapData(TableWrapData.LEFT, TableWrapData.TOP);
106 layoutData.heightHint = image.getBounds().height;
107 canvas.setLayoutData(layoutData);
108
109 canvas.addPaintListener (new PaintListener () {
110 public void paintControl (PaintEvent e) {
111 if (image != null) {
112 canvas.setBounds(image.getBounds());
113 e.gc.drawImage (image, 0, 0);
114 }
115 }
116 });
117
118 canvas.setBounds(image.getBounds());
119
120 focusListener = new FocusAdapter() {
121 public void focusGained(FocusEvent e) {
122 setFocus();
123 }
124 };
125 canvas.addFocusListener(focusListener);
126 }
127
128
129 /* (non-Javadoc)
130 * @see eu.etaxonomy.taxeditor.editor.GroupedComposite#getViewerFont()
131 */
132 @Override
133 protected Font getViewerFont() {
134 return null;
135 }
136
137 /* (non-Javadoc)
138 * @see eu.etaxonomy.taxeditor.editor.IHasPropertySource#getPropertySource()
139 */
140 public IPropertySource getPropertySource() {
141 return new ImagePropertySource(getTaxon(), imageFile);
142 }
143
144 public void createBorderSupport() {
145
146 CompositeBorderDecorator borderDecorator = new CompositeBorderDecorator(
147 canvas, managedForm);
148 setBorderDecorator(borderDecorator);
149 borderDecorator.setLoseFocus(false);
150 this.addFocusListener(borderDecorator);
151
152 }
153
154 public void dispose () {
155 super.dispose();
156
157 if (getBorderDecorator() != null) {
158 this.removeFocusListener(getBorderDecorator());
159 }
160
161 if (image != null) {
162 image.dispose();
163 }
164 }
165 }