a2a6de62061cf58a6979d624fa55a0e59315df85
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / media / MediaDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.ui.section.media;
11
12 import java.io.IOException;
13 import java.net.URI;
14 import java.util.List;
15 import java.util.Set;
16
17 import org.apache.http.HttpException;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.widgets.Label;
20
21 import eu.etaxonomy.cdm.common.media.ImageInfo;
22 import eu.etaxonomy.cdm.model.media.Media;
23 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
24 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
25 import eu.etaxonomy.cdm.model.media.MediaUtils;
26 import eu.etaxonomy.taxeditor.model.MessagingUtils;
27 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
28 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
31 import eu.etaxonomy.taxeditor.ui.element.ImageElement;
32 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
33 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
35
36 /**
37 * @author pplitzner
38 * @date 25.08.2014
39 *
40 */
41 public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
42
43 private UriWithLabelElement textUri;
44 private MediaRepresentationSection section_mediaRepresentation;
45
46 private boolean isAdvancedMediaView;
47 private MediaRepresentationPart singleMediaRepresentationPart;
48 private ICdmFormElement parentFormElement;
49 private int style;
50
51 /**
52 * Used to store the URI even if it is invalid and thus cannot be stored in CDM
53 */
54 private String uriBuffer;
55 private ImageElement element_image;
56 private Label lblNoImage;
57
58 public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
59 super(formFactory, formElement);
60 }
61
62 @Override
63 protected void createControls(ICdmFormElement formElement, Media entity, int style) {
64 this.parentFormElement = formElement;
65 this.style = style;
66 }
67
68 @Override
69 public void setEntity(Media entity) {
70 super.setEntity(entity);
71 if(singleMediaRepresentationPart==null){
72 singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
73 }
74 initIsAdvancedMediaView();
75 showAdvancedView();
76 }
77
78 @Override
79 public void handleEvent(Object eventSource){
80 if(eventSource==textUri){
81 textUri.setBackground(getPersistentBackground());
82 URI uri = textUri.parseText();
83 singleMediaRepresentationPart.setUri(uri);
84 if(uri==null){
85 uriBuffer=textUri.getText();
86 }
87 else{
88
89 ImageInfo imageInfo;
90 try {
91 imageInfo = ImageInfo.NewInstance(uri, 10000);
92 singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
93 singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
94 element_image = formFactory.createImageElement(parentFormElement, uri, style);
95 loadImage(singleMediaRepresentationPart.getUri());
96 } catch (Exception e) {
97 handleException();
98 }
99 }
100 }
101 }
102
103 public void toggleAdvancedMediaView() {
104 if (getEntity().getRepresentations() != null
105 && (getEntity().getRepresentations().size() > 1 ||
106 (getEntity().getRepresentations().size() == 1
107 && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
108 MessagingUtils.informationDialog("Toggling not possible",
109 "Media has consists of multiple representations or representatio parts");
110 // toggling is only possible if there are no more than one
111 // MediaRepresentation resp. MediaRepresentationParts
112 return;
113 }
114 isAdvancedMediaView = !isAdvancedMediaView;
115 showAdvancedView();
116 reflowParentScrolledForm(true);
117 }
118
119 private void showAdvancedView() {
120 if(isAdvancedMediaView){
121 if(textUri!=null){
122 removeElementsAndControls(textUri);
123 }
124 if(element_image!=null){
125 element_image.dispose();
126 element_image = null;
127 }
128 section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
129 section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
130 section_mediaRepresentation.setEntity(getEntity());
131 //set buffered uri as text if uri had parsing problems in simple view
132 if(uriBuffer!=null){
133 section_mediaRepresentation.getLayoutComposite().getChildren();
134 }
135 }
136 else{
137 if(section_mediaRepresentation!=null){
138 removeElementsAndControls(section_mediaRepresentation);
139 }
140 textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
141 URI uri = singleMediaRepresentationPart.getUri();
142 textUri.setParsedText(uri);
143 //set buffered uri as text if uri had parsing problems in advanced view
144 if(uri==null && uriBuffer!=null){
145 textUri.setText(uriBuffer);
146 textUri.parseText();
147 }
148 textUri.getLayoutComposite().layout();
149
150 element_image = formFactory.createImageElement(parentFormElement, null, style);
151 try {
152 loadImage(singleMediaRepresentationPart.getUri());
153 } catch (Exception e) {
154 handleException();
155 }
156 }
157 }
158
159 private void initIsAdvancedMediaView(){
160 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
161 isAdvancedMediaView = true;
162 return;
163 }
164 Set<MediaRepresentation> representations = getEntity().getRepresentations();
165 if(representations.size()>1){
166 isAdvancedMediaView = true;
167 return;
168 }
169 if(representations.size()==1){
170 List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
171 if(parts.size()>1){
172 isAdvancedMediaView = true;
173 return;
174 }
175 }
176 isAdvancedMediaView = false;
177 }
178
179 private void loadImage(URI uri) throws IOException, HttpException{
180 element_image.initImageUri(uri);
181 element_image.loadImage();
182 if(uri == null){
183 return;
184 }
185 lblNoImage.dispose();
186 lblNoImage = null;
187 }
188
189 private void handleException() {
190 element_image.unloadImage();
191 element_image.loadImage();
192 element_image.dispose();
193 if(lblNoImage==null){
194 lblNoImage = formFactory.createLabel(getLayoutComposite(), "No Image found");
195 lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
196 lblNoImage.setAlignment(SWT.CENTER);
197 }
198 }
199
200 public boolean isAdvancedMediaView() {
201 return isAdvancedMediaView;
202 }
203
204 public void setUriBuffer(String uriBuffer) {
205 this.uriBuffer = uriBuffer;
206 }
207
208 public String getUriBuffer() {
209 return uriBuffer;
210 }
211
212 }