Refresh view after media view has been switched
[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.URISyntaxException;
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.preference.IPreferenceKeys;
21 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
22 import eu.etaxonomy.taxeditor.preference.Resources;
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 public MediaDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
45 super(formFactory, formElement);
46 }
47
48 @Override
49 protected void createControls(ICdmFormElement formElement, Media entity, int style) {
50 this.parentFormElement = formElement;
51 this.style = style;
52 }
53
54 @Override
55 public void setEntity(Media entity) {
56 super.setEntity(entity);
57 if(singleMediaRepresentationPart==null){
58 singleMediaRepresentationPart = MediaUtils.initFirstMediaRepresentationPart(entity, true);
59 }
60 initIsAdvancedMediaView();
61 showAdvancedView();
62 }
63
64 @Override
65 public void handleEvent(Object eventSource) {
66 if(eventSource==textUri){
67 textUri.setBackground(getPersistentBackground());
68 try {
69 singleMediaRepresentationPart.setUri(textUri.getUri());
70 } catch (URISyntaxException e) {
71 textUri.setBackground(getColor(Resources.COLOR_PARSE_ERROR));
72 }
73 }
74 }
75
76 public void toggleAdvancedMediaView(){
77 isAdvancedMediaView = !isAdvancedMediaView;
78 showAdvancedView();
79 reflowParentScrolledForm(true);
80 }
81
82 private void showAdvancedView() {
83 if(isAdvancedMediaView){
84 if(textUri!=null){
85 removeElementsAndControls(textUri);
86 }
87 section_mediaRepresentation = formFactory.createMediaRepresentationSection(getConversationHolder(), parentFormElement, style);
88 section_mediaRepresentation.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
89 section_mediaRepresentation.setEntity(getEntity());
90 }
91 else{
92 if(section_mediaRepresentation!=null){
93 removeElementsAndControls(section_mediaRepresentation);
94 }
95 textUri = formFactory.createUriWithLabelElement(parentFormElement, "Media URI", null, style);
96 textUri.setUri(singleMediaRepresentationPart.getUri());
97 textUri.getLayoutComposite().layout();
98 }
99 }
100
101 private void initIsAdvancedMediaView(){
102 if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ADVANCED_MEDIA_SECTION)){
103 isAdvancedMediaView = true;
104 return;
105 }
106 Set<MediaRepresentation> representations = getEntity().getRepresentations();
107 if(representations.size()>1){
108 isAdvancedMediaView = true;
109 return;
110 }
111 if(representations.size()==1){
112 List<MediaRepresentationPart> parts = representations.iterator().next().getParts();
113 if(parts.size()>1){
114 isAdvancedMediaView = true;
115 return;
116 }
117 }
118 isAdvancedMediaView = false;
119 }
120
121 public boolean isAdvancedMediaView() {
122 return isAdvancedMediaView;
123 }
124
125 }