ref #8562: add admin preference page for multilanguage support
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / AbstractTermSelectionWizardPage.java
1 /**
2 * Copyright (C) 2018 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.wizard;
10
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.List;
15
16 import org.eclipse.jface.viewers.CheckboxTreeViewer;
17 import org.eclipse.jface.wizard.WizardPage;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25
26 import eu.etaxonomy.cdm.api.service.IVocabularyService;
27 import eu.etaxonomy.cdm.model.metadata.CdmPreference;
28 import eu.etaxonomy.cdm.model.term.TermType;
29 import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
30 import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
31 import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
32 import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
33 import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 * @author k.luther
38 * @since 04.06.2018
39 *
40 */
41 public abstract class AbstractTermSelectionWizardPage<T extends AbstractTermDto> extends WizardPage {
42
43 protected CheckBoxTreeComposite treeComposite;
44 private List<TermVocabularyDto> vocabularies = new ArrayList<>();
45 boolean localPref;
46 protected CdmPreference pref;
47 protected boolean override;
48
49 protected TermType type;
50
51 protected AbstractTermSelectionWizardPage(String pageName, TermType type) {
52 super(pageName);
53 this.type = type;
54
55 }
56
57 @Override
58 public void createControl(Composite parent){
59 parent.setLayout(new GridLayout());
60 treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
61 treeComposite.getViewer().setComparator(new DefinedTermSorter());
62 rememberCheckedValues(getCheckedValuesFromPreferences());
63 treeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
64 setControl(treeComposite);
65 }
66
67 protected abstract String getCheckedValuesFromPreferences();
68
69 public CheckboxTreeViewer getViewer(){
70 return treeComposite.getViewer();
71 }
72
73 public List<TermVocabularyDto> getVocabularies() {
74 return vocabularies;
75 }
76
77 public void addVocabularies(TermVocabularyDto vocabulary) {
78 this.vocabularies.add(vocabulary);
79 }
80
81 protected void setVocabularies(List<TermVocabularyDto> vocs) {
82
83 vocabularies = vocs;
84 }
85
86 public CdmPreference getPreference() {
87 return pref;
88 }
89
90 protected void rememberCheckedValues(String checkedValues) {
91 initialiseVocabularies();
92
93 treeComposite.getViewer().setInput(getVocabularies());
94 List<T> termsFromStringValues = null;
95 if (checkedValues != null && checkedValues != "") { //$NON-NLS-1$
96 String[] listChecked = checkedValues.split(";"); //$NON-NLS-1$
97 String[] listCheckedComma = checkedValues.split(","); //$NON-NLS-1$
98 List<String> checked = new ArrayList<>();
99 if (listChecked != null ){
100 checked = Arrays.asList(listChecked);
101 }
102 if (listCheckedComma != null && checkedValues.contains(",")){ //$NON-NLS-1$
103 checked = Arrays.asList(listCheckedComma);
104 }
105 termsFromStringValues = getTermsFromStringValues(checked);
106 if (termsFromStringValues != null){
107 treeComposite.setCheckedElements(termsFromStringValues.toArray());
108 }
109
110 }
111 if (termsFromStringValues == null){
112 termsFromStringValues = getTermsFromStringValues(new ArrayList<String>());
113 if (termsFromStringValues != null){
114 treeComposite.setCheckedElements(termsFromStringValues.toArray());
115 }
116 }
117 }
118
119
120
121 protected void initialiseVocabularies() {
122 if (getVocabularies() != null) {
123 getVocabularies().clear();
124 }
125 List<TermVocabularyDto> vocs = new ArrayList<>();
126 vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
127
128 setVocabularies(vocs);
129 }
130
131 protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
132
133 /**
134 * @param listValue
135 * @return
136 */
137 protected abstract List<T> getTermsFromStringValues(List<String> listValue);
138
139 protected Button createAllowOverrideButton(Composite parent) {
140 Button activateCheckButton = new Button(parent, SWT.CHECK);
141 if (localPref){
142 activateCheckButton.setText("Override");
143 activateCheckButton.setSelection(override);
144 }else{
145 activateCheckButton.setText("Allow Override");
146 activateCheckButton.setSelection(override);
147 }
148
149
150
151 activateCheckButton.addSelectionListener(new SelectionAdapter(){
152 @Override
153 public void widgetSelected(SelectionEvent e) {
154 pref.setAllowOverride(activateCheckButton.getSelection());
155 override = activateCheckButton.getSelection();
156
157 }
158 });
159 return activateCheckButton;
160 }
161
162 }