ref #8260 Complete refactoring of expansion state saving
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / media / MediaDetailElement.java
1 /**
2 * Copyright (C) 2014 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.section.media;
10
11 import java.net.URI;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.apache.sanselan.ImageReadException;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Label;
18
19 import eu.etaxonomy.cdm.common.UriUtils;
20 import eu.etaxonomy.cdm.common.media.ImageInfo;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.media.ImageFile;
23 import eu.etaxonomy.cdm.model.media.Media;
24 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
25 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
26 import eu.etaxonomy.cdm.model.media.MediaUtils;
27 import eu.etaxonomy.taxeditor.model.MessagingUtils;
28 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
29 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30 import eu.etaxonomy.taxeditor.store.StoreUtil;
31 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
32 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33 import eu.etaxonomy.taxeditor.ui.element.ImageElement;
34 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
35 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
37
38 /**
39 * @author pplitzner
40 * @date 25.08.2014
41 *
42 */
43 public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
44
45 private UriWithLabelElement textUri;
46 private MediaRepresentationSection section_mediaRepresentation;
47
48 private boolean isAdvancedMediaView;
49 private MediaRepresentationPart singleMediaRepresentationPart;
50 private ICdmFormElement parentFormElement;
51 private int style;
52
53 /**
54 * Used to store the URI even if it is invalid and thus cannot be stored in CDM
55 */
56 private String uriBuffer;
57 private ImageElement element_image;
58 private Label lblNoImage;
59
60 public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
61 super(formFactory, formElement);
62 }
63
64 @Override
65 protected void createControls(ICdmFormElement formElement, Media entity, int style) {
66
67 this.parentFormElement = formElement;
68 this.style = style;
69
70 }
71
72 @Override
73 public void setEntity(Media entity) {
74 super.setEntity(entity);
75 if(singleMediaRepresentationPart==null){
76 singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
77 }
78 initIsAdvancedMediaView();
79 showAdvancedView();
80 }
81
82 @Override
83 public void handleEvent(Object eventSource){
84 if(eventSource==textUri){
85 textUri.setBackground(getPersistentBackground());
86 URI uri = textUri.parseText();
87 singleMediaRepresentationPart.setUri(uri);
88 if(uri==null){
89 uriBuffer=textUri.getText();
90 }
91 else{
92 createImageElement(uri);
93 }
94 }
95 }
96
97 private void createImageElement(URI uri) {
98 ImageInfo imageInfo;
99 try {
100 if(uri == null){
101 return;
102 }
103 //first check if uri refers to an actual (non-image) file
104 UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
105 imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
106 singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
107 if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
108 ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
109 image.setHeight(imageInfo.getHeight());
110 image.setWidth(imageInfo.getWidth());
111 }
112 singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
113 singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
114 element_image = formFactory.createImageElement(parentFormElement, uri, style);
115 element_image.initImageUri(uri);
116 element_image.loadImage();
117 disposeErrorLabel();
118 } catch (Exception e){
119 if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
120 disposeErrorLabel();
121 handleException(uri, "No preview available for this file type");
122 }
123 else{
124 disposeErrorLabel();
125 handleException(uri, "No file found");
126 }
127 }
128 }
129
130 private void disposeErrorLabel() {
131 if(lblNoImage!=null){
132 lblNoImage.dispose();
133 }
134 lblNoImage = null;
135 }
136
137 public void toggleAdvancedMediaView() {
138 if (getEntity().getRepresentations() != null
139 && (getEntity().getRepresentations().size() > 1 ||
140 (getEntity().getRepresentations().size() == 1
141 && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
142 MessagingUtils.informationDialog("Toggling not possible",
143 "Media consists of multiple representations or representatio parts");
144 // toggling is only possible if there are no more than one
145 // MediaRepresentation resp. MediaRepresentationParts
146 return;
147 }
148 isAdvancedMediaView = !isAdvancedMediaView;
149 showAdvancedView();
150 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
151 }
152
153 private void showAdvancedView() {
154
155 if(isAdvancedMediaView){
156 if(textUri!=null){
157 removeElementsAndControls(textUri);
158 }
159 if(element_image!=null){
160 element_image.dispose();
161 element_image = null;
162 }
163 section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, StoreUtil.getSectionStyle(MediaRepresentationSection.class, getEntity().getClass().getCanonicalName()));
164 section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
165 section_mediaRepresentation.setEntity(getEntity());
166 //set buffered uri as text if uri had parsing problems in simple view
167 if(uriBuffer!=null){
168 section_mediaRepresentation.getLayoutComposite().getChildren();
169 }
170 }
171 else{
172 if(section_mediaRepresentation!=null){
173 removeElementsAndControls(section_mediaRepresentation);
174 }
175 textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
176 URI uri = singleMediaRepresentationPart.getUri();
177 textUri.setParsedText(uri);
178 //set buffered uri as text if uri had parsing problems in advanced view
179 if(uri==null && uriBuffer!=null){
180 textUri.setText(uriBuffer);
181 textUri.parseText();
182 }
183 textUri.getLayoutComposite().layout();
184
185 createImageElement(singleMediaRepresentationPart.getUri());
186 }
187 }
188
189 private void initIsAdvancedMediaView(){
190 if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
191 isAdvancedMediaView = true;
192 return;
193 }
194 Set<MediaRepresentation> representations = getEntity().getRepresentations();
195 if(representations.size()>1){
196 isAdvancedMediaView = true;
197 return;
198 }
199 if(representations.size()==1){
200 List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
201 if(parts.size()>1){
202 isAdvancedMediaView = true;
203 return;
204 }
205 }
206 isAdvancedMediaView = false;
207 }
208
209 private void handleException(URI uri, String labelText) {
210 if(element_image!=null){
211 element_image.unloadImage();
212 element_image.loadImage();
213 element_image.dispose();
214 }
215 if(lblNoImage==null){
216 lblNoImage = formFactory.createLabel(getLayoutComposite(), labelText);
217 lblNoImage.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
218 lblNoImage.setAlignment(SWT.CENTER);
219 }
220 }
221
222 public boolean isAdvancedMediaView() {
223 return isAdvancedMediaView;
224 }
225
226 public void setUriBuffer(String uriBuffer) {
227 this.uriBuffer = uriBuffer;
228 }
229
230 public String getUriBuffer() {
231 return uriBuffer;
232 }
233
234 }