merge fix for #2924 to trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / TermBasePropertyTester.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 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.editor.definedterm;
11
12 import org.eclipse.core.expressions.PropertyTester;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14
15 import eu.etaxonomy.cdm.model.common.CdmBase;
16 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
17 import eu.etaxonomy.cdm.model.common.Marker;
18 import eu.etaxonomy.cdm.model.common.MarkerType;
19 import eu.etaxonomy.cdm.model.common.TermBase;
20 import eu.etaxonomy.cdm.model.common.TermVocabulary;
21
22 /**
23 * @author l.morris
24 * @date 9 Jan 2012
25 *
26 */
27 public class TermBasePropertyTester extends PropertyTester {
28
29 private static final String IS_MODIFIABLE = "isModifiable";
30
31
32 /* (non-Javadoc)
33 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
34 */
35 @Override
36 public boolean test(Object receiver, String property, Object[] args,
37 Object expectedValue) {
38
39 IStructuredSelection selection = (IStructuredSelection) receiver;
40 CdmBase selectedElement = (CdmBase)selection.getFirstElement();
41 if (selectedElement.isInstanceOf(TermBase.class)){
42 TermBase term = CdmBase.deproxy(selectedElement, TermBase.class);
43
44 if(IS_MODIFIABLE.equals(property)){
45 return isModifiable(term);
46 }
47 }
48
49 return false;
50 }
51
52
53 /**
54 * Checks whether there is a {@link Marker} with the type {@link MarkerType#MODIFIABLE()} and if there is then return its value.
55 *
56 * @return The markers value if it exists
57 */
58 public static boolean isModifiable(TermBase termBase) {
59 if (termBase == null){
60 return true;
61 }
62
63 TermVocabulary vocabulary = null;
64
65 if(termBase instanceof DefinedTermBase){
66 vocabulary = ((DefinedTermBase) termBase).getVocabulary();
67 }else if(termBase instanceof TermVocabulary){
68 vocabulary = (TermVocabulary) termBase;
69 }
70
71 if(vocabulary == null){
72 return true;
73 }
74
75 for(Marker vocabularyMarker : vocabulary.getMarkers()){
76 if(vocabularyMarker.getMarkerType().equals(MarkerType.MODIFIABLE())){
77 return vocabularyMarker.getValue();
78 }
79 }
80
81 return true;
82 }
83
84 }