*/
package eu.etaxonomy.taxeditor.ui.section.media;
+import java.net.URI;
import java.util.List;
import java.util.Set;
+import org.apache.sanselan.ImageReadException;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Label;
+
+import eu.etaxonomy.cdm.common.UriUtils;
+import eu.etaxonomy.cdm.common.media.ImageInfo;
+import eu.etaxonomy.cdm.model.media.ImageFile;
import eu.etaxonomy.cdm.model.media.Media;
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
import eu.etaxonomy.cdm.model.media.MediaUtils;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.ui.element.ImageElement;
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
private ICdmFormElement parentFormElement;
private int style;
+ /**
+ * Used to store the URI even if it is invalid and thus cannot be stored in CDM
+ */
+ private String uriBuffer;
+ private ImageElement element_image;
+ private Label lblNoImage;
+
public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
super(formFactory, formElement);
}
public void handleEvent(Object eventSource){
if(eventSource==textUri){
textUri.setBackground(getPersistentBackground());
- singleMediaRepresentationPart.setUri(textUri.getUri());
+ URI uri = textUri.parseText();
+ singleMediaRepresentationPart.setUri(uri);
+ if(uri==null){
+ uriBuffer=textUri.getText();
+ }
+ else{
+ createImageElement(uri);
+ }
}
}
- public void toggleAdvancedMediaView(){
+ private void createImageElement(URI uri) {
+ ImageInfo imageInfo;
+ try {
+ if(uri == null){
+ return;
+ }
+ //first check if uri refers to an actual (non-image) file
+ UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
+ imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
+ singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
+ if(singleMediaRepresentationPart instanceof ImageFile){
+ ((ImageFile) singleMediaRepresentationPart).setHeight(imageInfo.getHeight());
+ ((ImageFile) singleMediaRepresentationPart).setWidth(imageInfo.getWidth());
+ }
+ singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
+ singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
+ element_image = formFactory.createImageElement(parentFormElement, uri, style);
+ element_image.initImageUri(uri);
+ element_image.loadImage();
+ disposeErrorLabel();
+ } catch (Exception e){
+ if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
+ disposeErrorLabel();
+ handleException(uri, "No preview available for this file type");
+ }
+ else{
+ disposeErrorLabel();
+ handleException(uri, "No file found");
+ }
+ }
+ }
+
+ private void disposeErrorLabel() {
+ if(lblNoImage!=null){
+ lblNoImage.dispose();
+ }
+ lblNoImage = null;
+ }
+
+ public void toggleAdvancedMediaView() {
+ if (getEntity().getRepresentations() != null
+ && (getEntity().getRepresentations().size() > 1 ||
+ (getEntity().getRepresentations().size() == 1
+ && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
+ MessagingUtils.informationDialog("Toggling not possible",
+ "Media has consists of multiple representations or representatio parts");
+ // toggling is only possible if there are no more than one
+ // MediaRepresentation resp. MediaRepresentationParts
+ return;
+ }
isAdvancedMediaView = !isAdvancedMediaView;
showAdvancedView();
reflowParentScrolledForm(true);
if(textUri!=null){
removeElementsAndControls(textUri);
}
+ if(element_image!=null){
+ element_image.dispose();
+ element_image = null;
+ }
section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
section_mediaRepresentation.setEntity(getEntity());
+ //set buffered uri as text if uri had parsing problems in simple view
+ if(uriBuffer!=null){
+ section_mediaRepresentation.getLayoutComposite().getChildren();
+ }
}
else{
if(section_mediaRepresentation!=null){
removeElementsAndControls(section_mediaRepresentation);
}
textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
- textUri.setUri(singleMediaRepresentationPart.getUri());
+ URI uri = singleMediaRepresentationPart.getUri();
+ textUri.setParsedText(uri);
+ //set buffered uri as text if uri had parsing problems in advanced view
+ if(uri==null && uriBuffer!=null){
+ textUri.setText(uriBuffer);
+ textUri.parseText();
+ }
textUri.getLayoutComposite().layout();
+
+ createImageElement(singleMediaRepresentationPart.getUri());
}
}
isAdvancedMediaView = false;
}
+ private void handleException(URI uri, String labelText) {
+ if(element_image!=null){
+ element_image.unloadImage();
+ element_image.loadImage();
+ element_image.dispose();
+ }
+ if(lblNoImage==null){
+ lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
+ lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+ lblNoImage.setAlignment(SWT.CENTER);
+ }
+ }
+
public boolean isAdvancedMediaView() {
return isAdvancedMediaView;
}
+ public void setUriBuffer(String uriBuffer) {
+ this.uriBuffer = uriBuffer;
+ }
+
+ public String getUriBuffer() {
+ return uriBuffer;
+ }
+
}