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