Minor changes.
[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 eu.etaxonomy.cdm.model.media.Media;
17 import eu.etaxonomy.cdm.model.media.MediaRepresentation;
18 import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
19 import eu.etaxonomy.cdm.model.media.MediaUtils;
20 import eu.etaxonomy.taxeditor.model.MessagingUtils;
21 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
22 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
23 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
24 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
25 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
26 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
27 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
28
29 /**
30 * @author pplitzner
31 * @date 25.08.2014
32 *
33 */
34 public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
35
36 private UriWithLabelElement textUri;
37 private MediaRepresentationSection section_mediaRepresentation;
38
39 private boolean isAdvancedMediaView;
40 private MediaRepresentationPart singleMediaRepresentationPart;
41 private ICdmFormElement parentFormElement;
42 private int style;
43
44 /**
45 * Used to store the URI even if it is invalid and thus cannot be stored in CDM
46 */
47 private String uriBuffer;
48
49 public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
50 super(formFactory, formElement);
51 }
52
53 @Override
54 protected void createControls(ICdmFormElement formElement, Media entity, int style) {
55 this.parentFormElement = formElement;
56 this.style = style;
57 }
58
59 @Override
60 public void setEntity(Media entity) {
61 super.setEntity(entity);
62 if(singleMediaRepresentationPart==null){
63 singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
64 }
65 initIsAdvancedMediaView();
66 showAdvancedView();
67 }
68
69 @Override
70 public void handleEvent(Object eventSource){
71 if(eventSource==textUri){
72 textUri.setBackground(getPersistentBackground());
73 URI uri = textUri.getUri();
74 singleMediaRepresentationPart.setUri(uri);
75 if(uri==null){
76 uriBuffer=textUri.getText();
77 }
78 }
79 }
80
81 public void toggleAdvancedMediaView() {
82 if (getEntity().getRepresentations() != null
83 && (getEntity().getRepresentations().size() > 1 ||
84 (getEntity().getRepresentations().size() == 1
85 && getEntity().getRepresentations().iterator().next().getParts().size() > 1))) {
86 MessagingUtils.informationDialog("Toggling not possible",
87 "Media has consists of multiple representations or representatio parts");
88 // toggling is only possible if there are no more than one
89 // MediaRepresentation resp. MediaRepresentationParts
90 return;
91 }
92 isAdvancedMediaView = !isAdvancedMediaView;
93 showAdvancedView();
94 reflowParentScrolledForm(true);
95 }
96
97 private void showAdvancedView() {
98 if(isAdvancedMediaView){
99 if(textUri!=null){
100 removeElementsAndControls(textUri);
101 }
102 section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
103 section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
104 section_mediaRepresentation.setEntity(getEntity());
105 //set buffered uri as text if uri had parsing problems in simple view
106 if(uriBuffer!=null){
107 section_mediaRepresentation.getLayoutComposite().getChildren();
108 }
109 }
110 else{
111 if(section_mediaRepresentation!=null){
112 removeElementsAndControls(section_mediaRepresentation);
113 }
114 textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
115 URI uri = singleMediaRepresentationPart.getUri();
116 textUri.setUri(uri);
117 //set buffered uri as text if uri had parsing problems in advanced view
118 if(uri==null && uriBuffer!=null){
119 textUri.setText(uriBuffer);
120 textUri.getUri();
121 }
122 textUri.getLayoutComposite().layout();
123 }
124 }
125
126 private void initIsAdvancedMediaView(){
127 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
128 isAdvancedMediaView = true;
129 return;
130 }
131 Set<MediaRepresentation> representations = getEntity().getRepresentations();
132 if(representations.size()>1){
133 isAdvancedMediaView = true;
134 return;
135 }
136 if(representations.size()==1){
137 List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
138 if(parts.size()>1){
139 isAdvancedMediaView = true;
140 return;
141 }
142 }
143 isAdvancedMediaView = false;
144 }
145
146 public boolean isAdvancedMediaView() {
147 return isAdvancedMediaView;
148 }
149
150 public void setUriBuffer(String uriBuffer) {
151 this.uriBuffer = uriBuffer;
152 }
153
154 public String getUriBuffer() {
155 return uriBuffer;
156 }
157
158 }