Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / menu / FeatureMenuPreferences.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.preference.menu;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.UUID;
17
18 import org.apache.commons.lang3.StringUtils;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.CLabel;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Combo;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26
27 import eu.etaxonomy.cdm.api.service.ITermService;
28 import eu.etaxonomy.cdm.api.service.IVocabularyService;
29 import eu.etaxonomy.cdm.model.common.CdmClass;
30 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
31 import eu.etaxonomy.cdm.model.term.TermType;
32 import eu.etaxonomy.cdm.persistence.dto.TermDto;
33 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
34 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
35 import eu.etaxonomy.taxeditor.editor.definedterm.FeatureDtoContentProvider;
36 import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
37 import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.preference.GeneralTermPreferencePage;
40 import eu.etaxonomy.taxeditor.preference.LocalOrDefaultEnum;
41 import eu.etaxonomy.taxeditor.preference.wizard.CheckBoxTreeComposite;
42 import eu.etaxonomy.taxeditor.store.CdmStore;
43
44 /**
45 * <p>FeatureMenuPreferences class.</p>
46 *
47 * @author p.ciardelli
48 * @author n.hoffmann
49 * @created 17.09.2008
50 * @version 1.0
51 */
52 public class FeatureMenuPreferences extends GeneralTermPreferencePage{
53
54 /** Constant <code>PLUGIN_ID="eu.etaxonomy.taxeditor.preferences.feat"{trunked}</code> */
55 public static final String PLUGIN_ID = "eu.etaxonomy.taxeditor.preferences.taxonFeature"; //$NON-NLS-1$
56
57 /**
58 * <p>Constructor for FeatureMenuPreference.</p>
59 */
60 public FeatureMenuPreferences() {
61 super();
62 isAdminPreference = false;
63 setPredicate(PreferencePredicate.TaxonFeatures);
64 type = TermType.Feature;
65 }
66
67 @Override
68 protected Control createContents(Composite parent) {
69 Composite container = new Composite(parent, SWT.NULL);
70 getPreferenceValues();
71 if (isLocalPref() && !allowOverride){
72 final CLabel description = new CLabel(parent, SWT.NULL);
73 description.setText(Messages.NameDetailsViewConfiguration_description_not_available);
74 this.setDefaultButtonActivated(false);
75 return parent;
76
77 }
78 final GridLayout gridLayout = new GridLayout();
79 gridLayout.numColumns = 2;
80 container.setLayout(gridLayout);
81 parent.setLayout(new GridLayout());
82 useLocalOrAdmin = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
83 if (!isAdminPreference){
84 useLocalOrAdmin.add(LocalOrDefaultEnum.Default.getLabel(), 0);
85 useLocalOrAdmin.add(LocalOrDefaultEnum.Local.getLabel(), 1);
86
87 }else{
88 useLocalOrAdmin.add(LocalOrDefaultEnum.AllowOverride.getLabel(), 0);
89 useLocalOrAdmin.add(LocalOrDefaultEnum.Database.getLabel(), 1);
90 }
91
92
93 treeComposite = new CheckBoxTreeComposite(container, new FeatureDtoContentProvider(true, false, false), new TermDtoLabelProvider(), SWT.SCROLL_PAGE);
94 treeComposite.getViewer().setComparator(new DefinedTermSorter());
95 rememberCheckedValues(getCheckedValuesFromPreferences());
96
97 if (isLocalPref()){
98 if (isOverride){
99 useLocalOrAdmin.select(1);
100 }else{
101 useLocalOrAdmin.select(0);
102 if (treeComposite.getViewer().getTree() != null && treeComposite.getViewer().getTree().getTopItem() != null) {
103 treeComposite.getViewer().getTree().setSelection(treeComposite.getViewer().getTree().getTopItem());
104 }
105 treeComposite.setEnabled(false);
106
107 //treeComposite.getViewer().getTree().getVerticalBar().setEnabled(true);
108 }
109 }else{
110 if (allowOverride){
111 useLocalOrAdmin.select(0);
112 }else{
113 useLocalOrAdmin.select(1);
114 }
115 }
116 useLocalOrAdmin.addSelectionListener(this);
117
118 GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
119 data.heightHint = 300;
120 treeComposite.setLayoutData(data);
121 treeComposite.getViewer().addSelectionChangedListener(this);
122
123 return container;
124
125 }
126
127
128
129 @Override
130 public boolean performOk(){
131 if (isApply()){
132 boolean result = super.performOk();
133 if (result){
134 TermEditorInput.getPreferredTaxonFeatureTree(true);
135 }
136 }
137 return true;
138
139 }
140
141 @Override
142 protected void initialiseVocabularies() {
143 if (getVocabularies() != null) {
144 getVocabularies().clear();
145 }
146 List<TermVocabularyDto> vocs = new ArrayList<>();
147 Set<CdmClass> availableFor = new HashSet<>();
148 availableFor.add(CdmClass.TAXON);
149 vocs = CdmStore.getService(IVocabularyService.class).findFeatureVocabularyDtoByTermTypes(availableFor);
150
151 setVocabularies(vocs);
152 // super.initialiseVocabularies();
153
154
155
156 // List<TermVocabularyDto> vocs = new ArrayList<>();
157 // vocs.add(CdmStore.getService(IVocabularyService.class).findVocabularyDtoByVocabularyUuid(VocabularyEnum.NameFeature.getUuid()));
158 // this.getVocabularies().removeAll(vocs);
159
160 }
161
162 @Override
163 protected List<TermDto> getTermsFromStringValues(List<String> listValue) {
164 Collection<TermDto> termlist = new ArrayList<>();
165 ITermService termService = CdmStore.getService(ITermService.class);
166 List<UUID> uuidList = new ArrayList();
167 for (String s : listValue) {
168 if (!StringUtils.isBlank(s)){
169 UUID uuid = UUID.fromString(s);
170 uuidList.add(uuid);
171 }
172 }
173 if (!uuidList.isEmpty()){
174 termlist = termService.findFeatureByUUIDsAsDto(uuidList);
175 }
176
177 if (listValue == null || listValue.isEmpty() ){
178 termlist = termService.findFeatureByTitleAsDto("");
179
180 }
181
182 return new ArrayList(termlist);
183 }
184
185
186
187 }