Revision 6fd5b6eb
Added by Patrick Plitzner over 6 years ago
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/element/ImageElement.java | ||
---|---|---|
1 | 1 |
/** |
2 |
* Copyright (C) 2007 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
2 |
* Copyright (C) 2007 EDIT
|
|
3 |
* European Distributed Institute of Taxonomy
|
|
4 |
* http://www.e-taxonomy.eu
|
|
5 |
*
|
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms.
|
|
8 |
*/
|
|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.taxeditor.ui.element; |
11 | 11 |
|
... | ... | |
33 | 33 |
import org.eclipse.ui.forms.widgets.TableWrapData; |
34 | 34 |
|
35 | 35 |
import eu.etaxonomy.cdm.common.UriUtils; |
36 |
import eu.etaxonomy.taxeditor.model.AbstractUtility; |
|
36 | 37 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
37 |
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin; |
|
38 | 38 |
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmDataViewerE4; |
39 | 39 |
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4; |
40 | 40 |
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4; |
... | ... | |
46 | 46 |
*/ |
47 | 47 |
public class ImageElement extends AbstractCdmFormElement implements PaintListener{ |
48 | 48 |
|
49 |
private URI imageUri;
|
|
50 |
private Image image;
|
|
49 |
private URI imageUri;
|
|
50 |
private Image image;
|
|
51 | 51 |
|
52 |
private Composite container;
|
|
52 |
private Composite container;
|
|
53 | 53 |
|
54 |
private final Runnable postRunnable = new Runnable(){
|
|
54 |
private final Runnable postRunnable = new Runnable(){
|
|
55 | 55 |
@Override |
56 | 56 |
public void run() { |
57 |
//FIXME E4 migrate |
|
58 | 57 |
try{ |
59 |
EPartService partService = TaxeditorStorePlugin.getDefault().getWorkbench().getService(EPartService.class); |
|
60 |
MPart mPartDetails = partService.findPart("eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4"); |
|
61 |
if(mPartDetails!=null){ |
|
62 |
DetailsPartE4 detailsPart = (DetailsPartE4)mPartDetails.getObject(); |
|
63 |
if(detailsPart!=null && detailsPart.getViewer()!=null){ |
|
64 |
((AbstractCdmDataViewerE4) detailsPart.getViewer()).reflow(); |
|
58 |
EPartService partService = getFormFactory().getContext().get(EPartService.class); |
|
59 |
DetailsPartE4 detailsView = AbstractUtility.getDetailsView(partService); |
|
60 |
if(detailsView!=null){ |
|
61 |
AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) detailsView.getViewer(); |
|
62 |
if(viewer!=null){ |
|
63 |
viewer.reflow(); |
|
64 |
} |
|
65 | 65 |
} |
66 |
} |
|
67 |
MPart mPartSupplemental = partService.findPart("eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4"); |
|
68 |
if(mPartSupplemental!=null){ |
|
69 |
SupplementalDataPartE4 supplementalPart = (SupplementalDataPartE4)mPartSupplemental.getObject(); |
|
70 |
if(supplementalPart!=null && supplementalPart.getViewer()!=null){ |
|
71 |
((AbstractCdmDataViewerE4) (supplementalPart).getViewer()).reflow(); |
|
66 |
|
|
67 |
MPart mPartSupplemental = partService.findPart("eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4"); |
|
68 |
if(mPartSupplemental!=null){ |
|
69 |
SupplementalDataPartE4 supplementalPart = (SupplementalDataPartE4)mPartSupplemental.getObject(); |
|
70 |
if(supplementalPart!=null){ |
|
71 |
AbstractCdmDataViewerE4 viewer = (AbstractCdmDataViewerE4) (supplementalPart).getViewer(); |
|
72 |
if(viewer!=null){ |
|
73 |
viewer.refresh(); |
|
74 |
} |
|
75 |
} |
|
72 | 76 |
} |
73 | 77 |
} |
74 |
} |
|
75 | 78 |
catch(IllegalStateException e){ |
76 | 79 |
//when migrating to E4 this execption should not be thrown anymore |
77 | 80 |
} |
78 | 81 |
} |
79 | 82 |
}; |
80 | 83 |
|
81 |
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
|
|
82 |
super(formFactory, parentElement);
|
|
83 |
|
|
84 |
container = new Composite(getLayoutComposite(), style);
|
|
85 |
container.setLayoutData(LayoutConstants.FILL(2, 1));
|
|
86 |
|
|
87 |
container.addPaintListener(this);
|
|
88 |
}
|
|
89 |
|
|
90 |
public void initImageUri(URI uri) throws IOException, HttpException {
|
|
91 |
this.imageUri = uri;
|
|
92 |
InputStream imageStream = UriUtils.getInputStream(imageUri);
|
|
93 |
image = new Image(Display.getCurrent(), imageStream);
|
|
94 |
}
|
|
95 |
|
|
96 |
|
|
97 |
public URI getImageUri() {
|
|
98 |
return imageUri;
|
|
99 |
}
|
|
100 |
|
|
101 |
public void loadImage(){
|
|
102 |
if(getImageUri() != null){
|
|
103 |
Job job = new Job("Loading image") {
|
|
104 |
|
|
105 |
@Override
|
|
106 |
protected IStatus run(IProgressMonitor monitor) {
|
|
107 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
|
108 |
try {
|
|
109 |
runnable.run(monitor);
|
|
110 |
} catch (Exception e) {
|
|
111 |
MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
|
112 |
}
|
|
113 |
|
|
114 |
return Status.OK_STATUS;
|
|
115 |
}
|
|
116 |
};
|
|
117 |
job.schedule();
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
public void unloadImage() {
|
|
122 |
Job job = new Job("Unloading image") {
|
|
123 |
|
|
124 |
@Override
|
|
125 |
protected IStatus run(IProgressMonitor monitor) {
|
|
126 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
|
127 |
try {
|
|
128 |
runnable.run(monitor);
|
|
129 |
} catch (Exception e) {
|
|
130 |
MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
|
131 |
}
|
|
132 |
|
|
133 |
return Status.OK_STATUS;
|
|
134 |
}
|
|
135 |
};
|
|
136 |
job.schedule();
|
|
137 |
|
|
138 |
}
|
|
139 |
|
|
140 |
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
|
|
141 |
|
|
142 |
final Display display = getLayoutComposite().getDisplay();
|
|
143 |
|
|
144 |
IRunnableWithProgress runnable = new IRunnableWithProgress(){
|
|
145 |
|
|
146 |
@Override
|
|
147 |
public void run(IProgressMonitor monitor) {
|
|
148 |
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
|
|
149 |
|
|
150 |
// redraw the image container
|
|
151 |
display.asyncExec(new Runnable(){
|
|
152 |
@Override
|
|
153 |
public void run() {
|
|
154 |
if(! getLayoutComposite().isDisposed() && container!=null){
|
|
155 |
Event untypedEvent = new Event();
|
|
156 |
untypedEvent.widget = container;
|
|
157 |
PaintEvent event = new PaintEvent(untypedEvent);
|
|
158 |
event.gc = new GC(container);
|
|
159 |
paintControl(event);
|
|
160 |
getLayoutComposite().layout();
|
|
161 |
}
|
|
162 |
}
|
|
163 |
});
|
|
164 |
|
|
165 |
// execute the external runnable
|
|
166 |
if(postRunnable != null){
|
|
167 |
display.asyncExec(postRunnable);
|
|
168 |
}
|
|
169 |
monitor.done();
|
|
170 |
}
|
|
171 |
|
|
172 |
};
|
|
173 |
|
|
174 |
return runnable;
|
|
175 |
}
|
|
176 |
|
|
177 |
private Rectangle calculateImageBounds(Image image, Control control){
|
|
178 |
Rectangle imageBounds = image.getBounds();
|
|
179 |
Rectangle containerBounds = control.getBounds();
|
|
180 |
|
|
181 |
Integer imgWidth = imageBounds.width;
|
|
182 |
Integer imgHeight = imageBounds.height;
|
|
183 |
|
|
184 |
Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
|
|
185 |
Integer width = containerBounds.width;
|
|
186 |
Integer height = ((Float) (width * ratio)).intValue();
|
|
187 |
|
|
188 |
return new Rectangle(containerBounds.x, containerBounds.y, width, height);
|
|
189 |
}
|
|
190 |
|
|
191 |
public void dispose(){
|
|
192 |
if(image!=null){
|
|
193 |
image.dispose();
|
|
194 |
image = null;
|
|
195 |
}
|
|
196 |
imageUri = null;
|
|
197 |
if(container!=null){
|
|
198 |
container.dispose();
|
|
199 |
container = null;
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
/** {@inheritDoc} */
|
|
204 |
@Override
|
|
205 |
public void paintControl(PaintEvent e) {
|
|
206 |
TableWrapData layoutData = LayoutConstants.FILL(2, 1);
|
|
207 |
Control control = (Control) e.widget;
|
|
208 |
if(image != null){
|
|
209 |
Rectangle imageMaxBounds = calculateImageBounds(image, control);
|
|
210 |
layoutData.heightHint = imageMaxBounds.height;
|
|
211 |
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
|
|
212 |
}else{
|
|
213 |
layoutData.heightHint = 10;
|
|
214 |
e.gc.drawRectangle(0, 0, 0, 10);
|
|
215 |
}
|
|
216 |
control.setLayoutData(layoutData);
|
|
217 |
e.gc.dispose();
|
|
218 |
}
|
|
84 |
protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
|
|
85 |
super(formFactory, parentElement);
|
|
86 |
|
|
87 |
container = new Composite(getLayoutComposite(), style);
|
|
88 |
container.setLayoutData(LayoutConstants.FILL(2, 1));
|
|
89 |
|
|
90 |
container.addPaintListener(this);
|
|
91 |
}
|
|
92 |
|
|
93 |
public void initImageUri(URI uri) throws IOException, HttpException {
|
|
94 |
this.imageUri = uri;
|
|
95 |
InputStream imageStream = UriUtils.getInputStream(imageUri);
|
|
96 |
image = new Image(Display.getCurrent(), imageStream);
|
|
97 |
}
|
|
98 |
|
|
99 |
|
|
100 |
public URI getImageUri() {
|
|
101 |
return imageUri;
|
|
102 |
}
|
|
103 |
|
|
104 |
public void loadImage(){
|
|
105 |
if(getImageUri() != null){
|
|
106 |
Job job = new Job("Loading image") {
|
|
107 |
|
|
108 |
@Override
|
|
109 |
protected IStatus run(IProgressMonitor monitor) {
|
|
110 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
|
111 |
try {
|
|
112 |
runnable.run(monitor);
|
|
113 |
} catch (Exception e) {
|
|
114 |
MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
|
115 |
}
|
|
116 |
|
|
117 |
return Status.OK_STATUS;
|
|
118 |
}
|
|
119 |
};
|
|
120 |
job.schedule();
|
|
121 |
}
|
|
122 |
}
|
|
123 |
|
|
124 |
public void unloadImage() {
|
|
125 |
Job job = new Job("Unloading image") {
|
|
126 |
|
|
127 |
@Override
|
|
128 |
protected IStatus run(IProgressMonitor monitor) {
|
|
129 |
IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
|
|
130 |
try {
|
|
131 |
runnable.run(monitor);
|
|
132 |
} catch (Exception e) {
|
|
133 |
MessagingUtils.messageDialog("Could not unload image", getClass(), e.getMessage() + ": " + getImageUri(), e);
|
|
134 |
}
|
|
135 |
|
|
136 |
return Status.OK_STATUS;
|
|
137 |
}
|
|
138 |
};
|
|
139 |
job.schedule();
|
|
140 |
|
|
141 |
}
|
|
142 |
|
|
143 |
public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
|
|
144 |
|
|
145 |
final Display display = getLayoutComposite().getDisplay();
|
|
146 |
|
|
147 |
IRunnableWithProgress runnable = new IRunnableWithProgress(){
|
|
148 |
|
|
149 |
@Override
|
|
150 |
public void run(IProgressMonitor monitor) {
|
|
151 |
monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
|
|
152 |
|
|
153 |
// redraw the image container
|
|
154 |
display.asyncExec(new Runnable(){
|
|
155 |
@Override
|
|
156 |
public void run() {
|
|
157 |
if(! getLayoutComposite().isDisposed() && container!=null){
|
|
158 |
Event untypedEvent = new Event();
|
|
159 |
untypedEvent.widget = container;
|
|
160 |
PaintEvent event = new PaintEvent(untypedEvent);
|
|
161 |
event.gc = new GC(container);
|
|
162 |
paintControl(event);
|
|
163 |
getLayoutComposite().layout();
|
|
164 |
}
|
|
165 |
}
|
|
166 |
});
|
|
167 |
|
|
168 |
// execute the external runnable
|
|
169 |
if(postRunnable != null){
|
|
170 |
display.asyncExec(postRunnable);
|
|
171 |
}
|
|
172 |
monitor.done();
|
|
173 |
}
|
|
174 |
|
|
175 |
};
|
|
176 |
|
|
177 |
return runnable;
|
|
178 |
}
|
|
179 |
|
|
180 |
private Rectangle calculateImageBounds(Image image, Control control){
|
|
181 |
Rectangle imageBounds = image.getBounds();
|
|
182 |
Rectangle containerBounds = control.getBounds();
|
|
183 |
|
|
184 |
Integer imgWidth = imageBounds.width;
|
|
185 |
Integer imgHeight = imageBounds.height;
|
|
186 |
|
|
187 |
Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
|
|
188 |
Integer width = containerBounds.width;
|
|
189 |
Integer height = ((Float) (width * ratio)).intValue();
|
|
190 |
|
|
191 |
return new Rectangle(containerBounds.x, containerBounds.y, width, height);
|
|
192 |
}
|
|
193 |
|
|
194 |
public void dispose(){
|
|
195 |
if(image!=null){
|
|
196 |
image.dispose();
|
|
197 |
image = null;
|
|
198 |
}
|
|
199 |
imageUri = null;
|
|
200 |
if(container!=null){
|
|
201 |
container.dispose();
|
|
202 |
container = null;
|
|
203 |
}
|
|
204 |
}
|
|
205 |
|
|
206 |
/** {@inheritDoc} */
|
|
207 |
@Override
|
|
208 |
public void paintControl(PaintEvent e) {
|
|
209 |
TableWrapData layoutData = LayoutConstants.FILL(2, 1);
|
|
210 |
Control control = (Control) e.widget;
|
|
211 |
if(image != null){
|
|
212 |
Rectangle imageMaxBounds = calculateImageBounds(image, control);
|
|
213 |
layoutData.heightHint = imageMaxBounds.height;
|
|
214 |
e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
|
|
215 |
}else{
|
|
216 |
layoutData.heightHint = 10;
|
|
217 |
e.gc.drawRectangle(0, 0, 0, 10);
|
|
218 |
}
|
|
219 |
control.setLayoutData(layoutData);
|
|
220 |
e.gc.dispose();
|
|
221 |
}
|
|
219 | 222 |
|
220 | 223 |
} |
Also available in: Unified diff
ref #6925 Fix some FIXME E4 tags