Revision 96755ebc
Added by Patrick Plitzner over 7 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/ImageElement.java | ||
---|---|---|
1 | 1 |
// $Id$ |
2 | 2 |
/** |
3 | 3 |
* Copyright (C) 2007 EDIT |
4 |
* European Distributed Institute of Taxonomy
|
|
4 |
* European Distributed Institute of Taxonomy |
|
5 | 5 |
* http://www.e-taxonomy.eu |
6 |
*
|
|
6 |
* |
|
7 | 7 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
8 | 8 |
* See LICENSE.TXT at the top of this package for the full license terms. |
9 | 9 |
*/ |
... | ... | |
35 | 35 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
36 | 36 |
|
37 | 37 |
/** |
38 |
* <p>ImageElement class.</p> |
|
39 |
* |
|
40 | 38 |
* @author n.hoffmann |
41 | 39 |
* @created Sep 24, 2010 |
42 | 40 |
* @version 1.0 |
... | ... | |
45 | 43 |
|
46 | 44 |
private URI imageUri; |
47 | 45 |
private Image image; |
48 |
|
|
46 |
|
|
49 | 47 |
private Composite container; |
50 | 48 |
|
51 |
/** |
|
52 |
* <p>Constructor for ImageElement.</p> |
|
53 |
* |
|
54 |
* @param formFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object. |
|
55 |
* @param parentElement a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement} object. |
|
56 |
* @param imageUri a {@link java.net.URI} object. |
|
57 |
* @param style a int. |
|
58 |
*/ |
|
59 | 49 |
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) { |
60 | 50 |
super(formFactory, parentElement); |
61 |
|
|
51 |
|
|
62 | 52 |
container = new Composite(getLayoutComposite(), style); |
63 | 53 |
container.setLayoutData(LayoutConstants.FILL(2, 1)); |
64 |
|
|
54 |
|
|
65 | 55 |
container.addPaintListener(this); |
66 | 56 |
} |
67 | 57 |
|
68 |
/** |
|
69 |
* <p>Setter for the field <code>imageUri</code>.</p> |
|
70 |
* |
|
71 |
* @param uri a {@link java.net.URI} object. |
|
72 |
* @throws HttpException |
|
73 |
* @throws IOException |
|
74 |
*/ |
|
75 | 58 |
public void initImageUri(URI uri) throws IOException, HttpException { |
76 | 59 |
this.imageUri = uri; |
77 | 60 |
InputStream imageStream = UriUtils.getInputStream(imageUri); |
78 | 61 |
image = new Image(Display.getCurrent(), imageStream); |
79 | 62 |
} |
80 |
|
|
81 |
|
|
82 |
/** |
|
83 |
* <p>Getter for the field <code>imageUri</code>.</p> |
|
84 |
* |
|
85 |
* @return the imageUri |
|
86 |
*/ |
|
63 |
|
|
64 |
|
|
87 | 65 |
public URI getImageUri() { |
88 | 66 |
return imageUri; |
89 | 67 |
} |
90 |
|
|
68 |
|
|
91 | 69 |
public void loadImage(){ |
92 | 70 |
loadImage(null); |
93 | 71 |
} |
94 |
|
|
95 |
|
|
96 |
|
|
72 |
|
|
97 | 73 |
public void loadImage(final Runnable postRunnable){ |
98 | 74 |
if(getImageUri() != null){ |
99 | 75 |
Job job = new Job("Loading image") { |
100 |
|
|
76 |
|
|
101 | 77 |
@Override |
102 | 78 |
protected IStatus run(IProgressMonitor monitor) { |
103 | 79 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable); |
... | ... | |
106 | 82 |
} catch (Exception e) { |
107 | 83 |
MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e); |
108 | 84 |
} |
109 |
|
|
85 |
|
|
110 | 86 |
return Status.OK_STATUS; |
111 | 87 |
} |
112 | 88 |
}; |
113 | 89 |
job.schedule(); |
114 | 90 |
} |
115 | 91 |
} |
116 |
|
|
117 |
|
|
118 | 92 |
|
119 | 93 |
public void unloadImage(final Runnable postRunnable) { |
120 | 94 |
Job job = new Job("Unloading image") { |
121 |
|
|
95 |
|
|
122 | 96 |
@Override |
123 | 97 |
protected IStatus run(IProgressMonitor monitor) { |
124 | 98 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable); |
... | ... | |
127 | 101 |
} catch (Exception e) { |
128 | 102 |
MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e); |
129 | 103 |
} |
130 |
|
|
104 |
|
|
131 | 105 |
return Status.OK_STATUS; |
132 | 106 |
} |
133 | 107 |
}; |
134 | 108 |
job.schedule(); |
135 |
|
|
109 |
|
|
136 | 110 |
} |
137 |
|
|
111 |
|
|
138 | 112 |
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){ |
139 |
|
|
140 |
final Display display = getLayoutComposite().getDisplay();
|
|
141 |
|
|
113 |
|
|
114 |
final Display display = getLayoutComposite().getDisplay(); |
|
115 |
|
|
142 | 116 |
IRunnableWithProgress runnable = new IRunnableWithProgress(){ |
143 |
|
|
117 |
|
|
144 | 118 |
@Override |
145 | 119 |
public void run(IProgressMonitor monitor) { |
146 | 120 |
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN); |
147 |
|
|
121 |
|
|
148 | 122 |
// redraw the image container |
149 | 123 |
display.asyncExec(new Runnable(){ |
150 | 124 |
@Override |
... | ... | |
159 | 133 |
} |
160 | 134 |
} |
161 | 135 |
}); |
162 |
|
|
136 |
|
|
163 | 137 |
// execute the external runnable |
164 | 138 |
if(postRunnable != null){ |
165 | 139 |
display.asyncExec(postRunnable); |
166 | 140 |
} |
167 |
monitor.done();
|
|
141 |
monitor.done(); |
|
168 | 142 |
} |
169 | 143 |
|
170 | 144 |
}; |
171 |
|
|
145 |
|
|
172 | 146 |
return runnable; |
173 | 147 |
} |
174 |
|
|
148 |
|
|
175 | 149 |
private Rectangle calculateImageBounds(Image image, Control control){ |
176 | 150 |
Rectangle imageBounds = image.getBounds(); |
177 | 151 |
Rectangle containerBounds = control.getBounds(); |
178 |
|
|
152 |
|
|
179 | 153 |
Integer imgWidth = imageBounds.width; |
180 | 154 |
Integer imgHeight = imageBounds.height; |
181 |
|
|
155 |
|
|
182 | 156 |
Float ratio = imgHeight.floatValue()/imgWidth.floatValue(); |
183 | 157 |
Integer width = containerBounds.width; |
184 | 158 |
Integer height = ((Float) (width * ratio)).intValue(); |
185 |
|
|
159 |
|
|
186 | 160 |
return new Rectangle(containerBounds.x, containerBounds.y, width, height); |
187 | 161 |
} |
188 | 162 |
|
189 |
|
|
190 |
/* (non-Javadoc) |
|
191 |
* @see org.eclipse.swt.events.PaintListener#paintControl(org.eclipse.swt.events.PaintEvent) |
|
192 |
*/ |
|
193 | 163 |
/** {@inheritDoc} */ |
194 | 164 |
@Override |
195 | 165 |
public void paintControl(PaintEvent e) { |
Also available in: Unified diff
Trim code