Merge branch 'hotfix/5.44.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / element / ImageElement.java
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 */
9 package eu.etaxonomy.taxeditor.ui.element;
10
11 import java.io.IOException;
12 import java.io.InputStream;
13
14 import org.apache.http.HttpException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.e4.core.contexts.IEclipseContext;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.e4.ui.workbench.modeling.EPartService;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.swt.events.PaintEvent;
24 import org.eclipse.swt.events.PaintListener;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.Rectangle;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Event;
32 import org.eclipse.ui.forms.widgets.TableWrapData;
33
34 import eu.etaxonomy.cdm.common.URI;
35 import eu.etaxonomy.cdm.common.UriUtils;
36 import eu.etaxonomy.taxeditor.model.AbstractUtility;
37 import eu.etaxonomy.taxeditor.model.MessagingUtils;
38 import eu.etaxonomy.taxeditor.store.StoreUtil;
39 import eu.etaxonomy.taxeditor.view.e4.AbstractCdmDataViewer;
40 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
41 import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
42
43 /**
44 * @author n.hoffmann
45 * @created Sep 24, 2010
46 */
47 public class ImageElement extends AbstractCdmFormElement implements PaintListener{
48
49 /**
50 * @author pplitzner
51 * @since Jul 17, 2019
52 */
53 public class LoadImageJob extends Job {
54 public LoadImageJob(String name) {
55 super(name);
56 }
57
58 @Override
59 protected IStatus run(IProgressMonitor monitor) {
60 IRunnableWithProgress runnable = getLoadImageRunnable(postRunnable);
61 try {
62 runnable.run(monitor);
63 } catch (Exception e) {
64 MessagingUtils.messageDialog("Could not load image", getClass(), e.getMessage() + ": " + getImageUri(), e);
65 }
66
67 return Status.OK_STATUS;
68 }
69 }
70
71 private URI imageUri;
72 private Image image;
73
74 private Composite container;
75
76 private final Runnable postRunnable = new Runnable(){
77 @Override
78 public void run() {
79 try{
80 if (getFormFactory() != null && getFormFactory().getContext() != null){
81 EPartService partService = getFormFactory().getContext().get(EPartService.class);
82 IEclipseContext context = getFormFactory().getContext().getActiveChild();
83 DetailsPartE4 detailsView = AbstractUtility.getDetailsView(partService);
84 if(detailsView!=null){
85 AbstractCdmDataViewer viewer = detailsView.getViewer();
86 if(viewer!=null){
87 viewer.reflow();
88 }
89 }
90
91 MPart mPartSupplemental = partService.findPart("eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4");
92 if(mPartSupplemental!=null){
93 SupplementalDataPartE4 supplementalPart = (SupplementalDataPartE4)mPartSupplemental.getObject();
94 if(supplementalPart!=null){
95 AbstractCdmDataViewer viewer = (supplementalPart).getViewer();
96 if(viewer!=null){
97 viewer.refresh();
98 }
99 }
100 }
101 }
102 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
103 }
104 catch(IllegalStateException e){
105 //when migrating to E4 this execption should not be thrown anymore
106 }
107 }
108 };
109
110 protected ImageElement(CdmFormFactory formFactory, ICdmFormElement parentElement, URI imageUri, int style) {
111 super(formFactory, parentElement);
112
113 container = new Composite(getLayoutComposite(), style);
114 container.setLayoutData(LayoutConstants.FILL(2, 1));
115
116 container.addPaintListener(this);
117 }
118
119 public void initImageUri(URI uri) throws IOException, HttpException {
120 this.imageUri = uri;
121
122 InputStream imageStream = UriUtils.getInputStream(imageUri);
123
124 image = new Image(Display.getCurrent(), imageStream);
125 }
126
127
128 public URI getImageUri() {
129 return imageUri;
130 }
131
132 public void loadImage(){
133 if(getImageUri() != null){
134 Job job = new LoadImageJob("Loading image");
135 job.schedule();
136 }
137 }
138
139 public IRunnableWithProgress getLoadImageRunnable(final Runnable postRunnable){
140
141 final Display display = getLayoutComposite().getDisplay();
142
143 IRunnableWithProgress runnable = new IRunnableWithProgress(){
144
145 @Override
146 public void run(IProgressMonitor monitor) {
147 monitor.beginTask("Loading: " + getImageUri(), IProgressMonitor.UNKNOWN);
148
149 // redraw the image container
150 display.asyncExec(new Runnable(){
151 @Override
152 public void run() {
153 if(! getLayoutComposite().isDisposed() && container!=null){
154 Event untypedEvent = new Event();
155 untypedEvent.widget = container;
156 PaintEvent event = new PaintEvent(untypedEvent);
157 event.gc = new GC(container);
158 paintControl(event);
159
160 }
161 }
162 });
163
164 // execute the external runnable
165 if(postRunnable != null){
166 display.asyncExec(postRunnable);
167 }
168 monitor.done();
169 }
170
171 };
172
173 return runnable;
174 }
175
176 private Rectangle calculateImageBounds(Image image, Control control){
177 Rectangle imageBounds = image.getBounds();
178 Rectangle containerBounds = control.getBounds();
179
180 Integer imgWidth = imageBounds.width;
181 Integer imgHeight = imageBounds.height;
182
183 Float ratio = imgHeight.floatValue()/imgWidth.floatValue();
184 Integer width = containerBounds.width;
185 Integer height = ((Float) (width * ratio)).intValue();
186
187 return new Rectangle(containerBounds.x, containerBounds.y, width, height);
188 }
189
190 public void dispose(){
191 if(image!=null){
192 image.dispose();
193 image = null;
194 }
195 imageUri = null;
196 if(container!=null){
197 container.dispose();
198 container = null;
199 }
200 }
201
202 /** {@inheritDoc} */
203 @Override
204 public void paintControl(PaintEvent e) {
205 TableWrapData layoutData = LayoutConstants.FILL(2, 1);
206 Control control = (Control) e.widget;
207 if(image != null){
208 Rectangle imageMaxBounds = calculateImageBounds(image, control);
209 layoutData.heightHint = imageMaxBounds.height;
210 e.gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, imageMaxBounds.width, imageMaxBounds.height);
211 }else{
212 layoutData.heightHint = 10;
213 e.gc.drawRectangle(0, 0, 0, 10);
214 }
215 control.setLayoutData(layoutData);
216 e.gc.dispose();
217 }
218
219 }