#4389
[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.bulkeditor.BulkEditor;
13 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
14 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
15 import eu.etaxonomy.taxeditor.model.AbstractUtility;
16 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
17
18 /**
19 * Property tester used by the description tree menu.
20 *
21 * @author p.ciardelli
22 * @author n.hoffmann
23 * @version $Id: $
24 */
25 public class DescriptionsMenuPropertyTester extends PropertyTester {
26
27 private static final String MEDIA = "isMedia";
28 private static final String FEATURE_NODE_CONTAINER = "isFeatureNodeContainer";
29 private static final String DESCRIPTION = "isDescription";
30 private static final String DESCRIPTION_ELEMENT = "isDescriptionElement";
31 private static final String DELETABLE = "isDeletable";
32 private static final String IMAGE_GALLERY = "isImageGallery";
33 private static final String TAXON_EDITOR = "isTaxonEditor";
34 private static final String BULK_EDITOR = "isBulkEditor";
35 private static final String DERIVATE_EDITOR = "isDerivateEditor";
36
37 /* (non-Javadoc)
38 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
39 */
40 /** {@inheritDoc} */
41 @Override
42 public boolean test(Object receiver, String property, Object[] args,
43 Object expectedValue) {
44
45 if(TAXON_EDITOR.equals(property)){
46 return isTaxonEditor();
47 }
48 else if(BULK_EDITOR.equals(property)){
49 return isBulkEditor();
50 }
51 else if(DERIVATE_EDITOR.equals(property)){
52 return isDerivateEditor();
53 }
54
55 Object[] selectedElements = ((IStructuredSelection) receiver).toArray();
56
57 if(selectedElements.length == 0){
58 // nothing selected so all tests should fail
59 return false;
60 }
61
62 if(MEDIA.equals(property)){
63 return isMedia(selectedElements);
64 }
65 else if(FEATURE_NODE_CONTAINER.equals(property)){
66 return isFeatureNodeContainer(selectedElements);
67 }
68 else if(DESCRIPTION.equals(property)){
69 return isDescription(selectedElements);
70 }
71 else if(DESCRIPTION_ELEMENT.equals(property)){
72 return isDescriptionElement(selectedElements);
73 }
74 else if(DELETABLE.equals(property)){
75 return isDeletable(selectedElements);
76 }
77 else if(IMAGE_GALLERY.equals(property)){
78 return isImageGallery(selectedElements);
79 }
80 else{
81 return false;
82 }
83 }
84
85 private boolean isImageGallery(Object[] selectedElements) {
86 for (Object object : selectedElements){
87 if(!(object instanceof DescriptionBase) || !((DescriptionBase) object).isImageGallery()){
88 return false;
89 }
90 }
91 return true;
92 }
93
94 private boolean isFeatureNodeContainer(Object[] selectedElements) {
95 for (Object object : selectedElements){
96 if(!(object instanceof FeatureNodeContainer)){
97 return false;
98 }
99 }
100 return true;
101 }
102
103 private boolean isDeletable(Object[] selectedElements) {
104 boolean result = false;
105
106 for (Object object : selectedElements) {
107
108 if((object instanceof DescriptionBase)
109 || (object instanceof DescriptionElementBase)
110 || (object instanceof Media)
111 || (object instanceof FeatureNodeContainer)){
112 result = true;
113 }else{
114 return false;
115 }
116
117 }
118 return result;
119 }
120
121 private boolean isDescriptionElement(Object[] selectedElements) {
122 for (Object object : selectedElements){
123 if(!(object instanceof DescriptionElementBase)){
124 return false;
125 }
126 }
127 return true;
128 }
129
130 private boolean isDescription(Object[] selectedElements) {
131 for (Object object : selectedElements){
132 if(!(object instanceof DescriptionBase)){
133 return false;
134 }
135 }
136 return true;
137 }
138
139 private boolean isMedia(Object[] selectedElements) {
140 for (Object object : selectedElements){
141 if(!(object instanceof Media)){
142 return false;
143 }
144 }
145 return true;
146 }
147
148 private boolean isTaxonEditor() {
149 if(AbstractUtility.getActiveEditor() instanceof MultiPageTaxonEditor){
150 return true;
151 }
152 return false;
153 }
154
155 private boolean isBulkEditor() {
156 if(AbstractUtility.getActiveEditor() instanceof BulkEditor){
157 return true;
158 }
159 return false;
160 }
161
162 private boolean isDerivateEditor() {
163 if(AbstractUtility.getActiveEditor() instanceof DerivateView){
164 return true;
165 }
166 return false;
167 }
168 }