6c9e0f4834458e80b99f3bc47936837a0fad3679
[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 java.util.HashSet;
13 import java.util.Set;
14
15 import org.eclipse.core.expressions.PropertyTester;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19 import eu.etaxonomy.cdm.model.common.Marker;
20 import eu.etaxonomy.cdm.model.common.MarkerType;
21 import eu.etaxonomy.cdm.model.common.TermBase;
22 import eu.etaxonomy.cdm.model.common.TermVocabulary;
23 import eu.etaxonomy.taxeditor.ui.section.vocabulary.AbstractTermBaseDetailElement;
24
25 /**
26 * @author l.morris
27 * @date 9 Jan 2012
28 *
29 */
30 public class TermBasePropertyTester extends PropertyTester {
31
32 private static final String IS_MODIFIABLE = "isModifiable";
33
34
35 /* (non-Javadoc)
36 * @see org.eclipse.core.expressions.IPropertyTester#test(java.lang.Object, java.lang.String, java.lang.Object[], java.lang.Object)
37 */
38 @Override
39 public boolean test(Object receiver, String property, Object[] args,
40 Object expectedValue) {
41
42 IStructuredSelection selection = (IStructuredSelection) receiver;
43 TermBase selectedElement = (TermBase) selection.getFirstElement();
44
45 if(IS_MODIFIABLE.equals(property)){
46 return isModifiable(selectedElement);
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 }