2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.preference
.wizard
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.List
;
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
;
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
;
41 public abstract class AbstractTermSelectionWizardPage
<T
extends AbstractTermDto
> extends WizardPage
{
43 protected CheckBoxTreeComposite treeComposite
;
44 private List
<TermVocabularyDto
> vocabularies
= new ArrayList
<>();
46 protected CdmPreference pref
;
47 protected boolean override
;
49 protected TermType type
;
51 protected AbstractTermSelectionWizardPage(String pageName
, TermType type
) {
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
);
67 protected abstract String
getCheckedValuesFromPreferences();
69 public CheckboxTreeViewer
getViewer(){
70 return treeComposite
.getViewer();
73 public List
<TermVocabularyDto
> getVocabularies() {
77 public void addVocabularies(TermVocabularyDto vocabulary
) {
78 this.vocabularies
.add(vocabulary
);
81 protected void setVocabularies(List
<TermVocabularyDto
> vocs
) {
86 public CdmPreference
getPreference() {
90 protected void rememberCheckedValues(String checkedValues
) {
91 initialiseVocabularies();
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
);
102 if (listCheckedComma
!= null && checkedValues
.contains(",")){ //$NON-NLS-1$
103 checked
= Arrays
.asList(listCheckedComma
);
105 termsFromStringValues
= getTermsFromStringValues(checked
);
106 if (termsFromStringValues
!= null){
107 treeComposite
.setCheckedElements(termsFromStringValues
.toArray());
111 if (termsFromStringValues
== null){
112 termsFromStringValues
= getTermsFromStringValues(new ArrayList
<String
>());
113 if (termsFromStringValues
!= null){
114 treeComposite
.setCheckedElements(termsFromStringValues
.toArray());
121 protected void initialiseVocabularies() {
122 if (getVocabularies() != null) {
123 getVocabularies().clear();
125 List
<TermVocabularyDto
> vocs
= new ArrayList
<>();
126 vocs
= CdmStore
.getService(IVocabularyService
.class).findVocabularyDtoByTermType(type
);
128 setVocabularies(vocs
);
131 protected abstract List
<TermVocabularyDto
> getVocabulariesFromPreference();
137 protected abstract List
<T
> getTermsFromStringValues(List
<String
> listValue
);
139 protected Button
createAllowOverrideButton(Composite parent
) {
140 Button activateCheckButton
= new Button(parent
, SWT
.CHECK
);
142 activateCheckButton
.setText("Override");
143 activateCheckButton
.setSelection(override
);
145 activateCheckButton
.setText("Allow Override");
146 activateCheckButton
.setSelection(override
);
151 activateCheckButton
.addSelectionListener(new SelectionAdapter(){
153 public void widgetSelected(SelectionEvent e
) {
154 pref
.setAllowOverride(activateCheckButton
.getSelection());
155 override
= activateCheckButton
.getSelection();
159 return activateCheckButton
;