Fixes #2383
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / handler / DescriptionsMenuPropertyTester.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
5
6 import org.eclipse.core.expressions.PropertyTester;
7 import org.eclipse.jface.viewers.IStructuredSelection;
8
9 import eu.etaxonomy.cdm.model.description.DescriptionBase;
10 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
11 import eu.etaxonomy.cdm.model.media.Media;
12 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
13
14 /**
15 * Property tester used by the description tree menu.
16 *
17 * @author p.ciardelli
18 * @author n.hoffmann
19 * @version $Id: $
20 */
21 public class DescriptionsMenuPropertyTester extends PropertyTester {
22
23 private static final String MEDIA = "isMedia";
24 private static final String FEATURE_NODE_CONTAINER = "isFeatureNodeContainer";
25 private static final String DESCRIPTION = "isDescription";
26 private static final String DESCRIPTION_ELEMENT = "isDescriptionElement";
27 private static final String DELETABLE = "isDeletable";
28 private static final String IMAGE_GALLERY = "isImageGallery";
29
30 /* (non-Javadoc)
31 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
32 */
33 /** {@inheritDoc} */
34 public boolean test(Object receiver, String property, Object[] args,
35 Object expectedValue) {
36
37 Object[] selectedElements = ((IStructuredSelection) receiver).toArray();
38
39 if(selectedElements.length == 0){
40 // nothing selected so all tests should fail
41 return false;
42 }
43
44 if(MEDIA.equals(property)){
45 return isMedia(selectedElements);
46 }
47 else if(FEATURE_NODE_CONTAINER.equals(property)){
48 return isFeatureNodeContainer(selectedElements);
49 }
50 else if(DESCRIPTION.equals(property)){
51 return isDescription(selectedElements);
52 }
53 else if(DESCRIPTION_ELEMENT.equals(property)){
54 return isDescriptionElement(selectedElements);
55 }
56 else if(DELETABLE.equals(property)){
57 return isDeletable(selectedElements);
58 }
59 else if(IMAGE_GALLERY.equals(property)){
60 return isImageGallery(selectedElements);
61 }
62 else{
63 return false;
64 }
65 }
66
67 private boolean isImageGallery(Object[] selectedElements) {
68 for (Object object : selectedElements){
69 if(!(object instanceof DescriptionBase) || !((DescriptionBase) object).isImageGallery()){
70 return false;
71 }
72 }
73 return true;
74 }
75
76 private boolean isFeatureNodeContainer(Object[] selectedElements) {
77 for (Object object : selectedElements){
78 if(!(object instanceof FeatureNodeContainer)){
79 return false;
80 }
81 }
82 return true;
83 }
84
85 private boolean isDeletable(Object[] selectedElements) {
86 boolean result = false;
87
88 for (Object object : selectedElements) {
89
90 if((object instanceof DescriptionBase)
91 || (object instanceof DescriptionElementBase)
92 || (object instanceof Media)
93 || (object instanceof FeatureNodeContainer)){
94 result = true;
95 }else{
96 return false;
97 }
98
99 }
100 return result;
101 }
102
103 private boolean isDescriptionElement(Object[] selectedElements) {
104 for (Object object : selectedElements){
105 if(!(object instanceof DescriptionElementBase)){
106 return false;
107 }
108 }
109 return true;
110 }
111
112 private boolean isDescription(Object[] selectedElements) {
113 for (Object object : selectedElements){
114 if(!(object instanceof DescriptionBase)){
115 return false;
116 }
117 }
118 return true;
119 }
120
121 private boolean isMedia(Object[] selectedElements) {
122 for (Object object : selectedElements){
123 if(!(object instanceof Media)){
124 return false;
125 }
126 }
127 return true;
128 }
129 }