2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.ui
.element
;
11 import java
.io
.IOException
;
12 import java
.io
.InputStream
;
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
;
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
;
45 * @created Sep 24, 2010
47 public class ImageElement
extends AbstractCdmFormElement
implements PaintListener
{
53 public class LoadImageJob
extends Job
{
54 public LoadImageJob(String name
) {
59 protected IStatus
run(IProgressMonitor monitor
) {
60 IRunnableWithProgress runnable
= getLoadImageRunnable(postRunnable
);
62 runnable
.run(monitor
);
63 } catch (Exception e
) {
64 MessagingUtils
.messageDialog("Could not load image", getClass(), e
.getMessage() + ": " + getImageUri(), e
);
67 return Status
.OK_STATUS
;
74 private Composite container
;
76 private final Runnable postRunnable
= new Runnable(){
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();
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();
102 StoreUtil
.reflowParentScrolledForm(getLayoutComposite(), true);
104 catch(IllegalStateException e
){
105 //when migrating to E4 this execption should not be thrown anymore
110 protected ImageElement(CdmFormFactory formFactory
, ICdmFormElement parentElement
, URI imageUri
, int style
) {
111 super(formFactory
, parentElement
);
113 container
= new Composite(getLayoutComposite(), style
);
114 container
.setLayoutData(LayoutConstants
.FILL(2, 1));
116 container
.addPaintListener(this);
119 public void initImageUri(URI uri
) throws IOException
, HttpException
{
122 InputStream imageStream
= UriUtils
.getInputStream(imageUri
);
124 image
= new Image(Display
.getCurrent(), imageStream
);
128 public URI
getImageUri() {
132 public void loadImage(){
133 if(getImageUri() != null){
134 Job job
= new LoadImageJob("Loading image");
139 public IRunnableWithProgress
getLoadImageRunnable(final Runnable postRunnable
){
141 final Display display
= getLayoutComposite().getDisplay();
143 IRunnableWithProgress runnable
= new IRunnableWithProgress(){
146 public void run(IProgressMonitor monitor
) {
147 monitor
.beginTask("Loading: " + getImageUri(), IProgressMonitor
.UNKNOWN
);
149 // redraw the image container
150 display
.asyncExec(new Runnable(){
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
);
164 // execute the external runnable
165 if(postRunnable
!= null){
166 display
.asyncExec(postRunnable
);
176 private Rectangle
calculateImageBounds(Image image
, Control control
){
177 Rectangle imageBounds
= image
.getBounds();
178 Rectangle containerBounds
= control
.getBounds();
180 Integer imgWidth
= imageBounds
.width
;
181 Integer imgHeight
= imageBounds
.height
;
183 Float ratio
= imgHeight
.floatValue()/imgWidth
.floatValue();
184 Integer width
= containerBounds
.width
;
185 Integer height
= ((Float
) (width
* ratio
)).intValue();
187 return new Rectangle(containerBounds
.x
, containerBounds
.y
, width
, height
);
190 public void dispose(){
204 public void paintControl(PaintEvent e
) {
205 TableWrapData layoutData
= LayoutConstants
.FILL(2, 1);
206 Control control
= (Control
) e
.widget
;
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
);
212 layoutData
.heightHint
= 10;
213 e
.gc
.drawRectangle(0, 0, 0, 10);
215 control
.setLayoutData(layoutData
);