import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.apache.commons.lang.StringUtils;
+import org.eclipse.jface.viewers.ISelectionChangedListener;
+import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.CLabel;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import eu.etaxonomy.cdm.api.service.ITermService;
import eu.etaxonomy.cdm.api.service.IVocabularyService;
+import eu.etaxonomy.cdm.model.metadata.CdmPreference;
+import eu.etaxonomy.cdm.model.metadata.IPreferencePredicate;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
import eu.etaxonomy.cdm.model.term.TermType;
import eu.etaxonomy.cdm.model.term.TermVocabulary;
import eu.etaxonomy.taxeditor.editor.definedterm.DefinedTermSorter;
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoContentProvider;
import eu.etaxonomy.taxeditor.editor.definedterm.TermDtoLabelProvider;
+import eu.etaxonomy.taxeditor.l10n.Messages;
import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
import eu.etaxonomy.taxeditor.preference.wizard.CheckBoxTreeComposite;
import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.TermStore;
/**
* @author k.luther
* @since 05.06.2019
*
*/
-abstract public class GeneralTermPreference extends CdmPreferencePage {
+abstract public class GeneralTermPreference extends CdmPreferencePage implements ISelectionChangedListener, SelectionListener{
protected CheckBoxTreeComposite treeComposite;
private List<TermVocabularyDto> vocabularies = new ArrayList<>();
+ protected Combo useLocalOrAdmin ;
- boolean localPref;
+ protected boolean isOverride;
+ protected boolean allowOverride;
+ protected CdmPreference pref;
+ protected IPreferencePredicate predicate;
+ private boolean localPref;
+
+ public GeneralTermPreference(){
+ super();
+
+ }
+
+ public boolean isLocalPref() {
+ return localPref;
+ }
+
+ public void setLocalPref(boolean localPref) {
+ this.localPref = localPref;
+ }
protected TermType type;
this.vocabularies = vocabularies;
}
+ public IPreferencePredicate getPredicate() {
+ return predicate;
+ }
+
+ public void setPredicate(PreferencePredicate predicate) {
+ this.predicate = predicate;
+ }
+
/**
* {@inheritDoc}
*/
@Override
protected Control createContents(Composite parent) {
+ Composite container = new Composite(parent, SWT.NULL);
+ getPreferenceValues();
+ if (isLocalPref() && !allowOverride){
+ final CLabel description = new CLabel(parent, SWT.NULL);
+ description.setText(Messages.NameDetailsViewConfiguration_description_not_available);
+ this.setDefaultButtonActivated(false);
+ return parent;
+
+ }
+ final GridLayout gridLayout = new GridLayout();
+ gridLayout.numColumns = 2;
+ container.setLayout(gridLayout);
parent.setLayout(new GridLayout());
- treeComposite = new CheckBoxTreeComposite(parent, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
+ useLocalOrAdmin = new Combo(parent, SWT.BORDER | SWT.READ_ONLY);
+ if (localPref){
+ useLocalOrAdmin.add(LocalOrDefaultEnum.Default.getLabel(), 0);
+ useLocalOrAdmin.add(LocalOrDefaultEnum.Local.getLabel(), 1);
+
+ }else{
+ useLocalOrAdmin.add(LocalOrDefaultEnum.AllowOverride.getLabel(), 0);
+ useLocalOrAdmin.add(LocalOrDefaultEnum.Database.getLabel(), 1);
+ }
+
+
+ treeComposite = new CheckBoxTreeComposite(container, new TermDtoContentProvider(), new TermDtoLabelProvider(), SWT.NONE);
treeComposite.getViewer().setComparator(new DefinedTermSorter());
rememberCheckedValues(getCheckedValuesFromPreferences());
- treeComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
- return treeComposite;
+
+ if (isLocalPref()){
+ if (isOverride){
+ useLocalOrAdmin.select(1);
+ }else{
+ useLocalOrAdmin.select(0);
+ }
+ }else{
+ if (allowOverride){
+ useLocalOrAdmin.select(0);
+ }else{
+ useLocalOrAdmin.select(1);
+ }
+ }
+ useLocalOrAdmin.addSelectionListener(this);
+
+ GridData data = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
+ data.heightHint = 300;
+ treeComposite.setLayoutData(data);
+ treeComposite.getViewer().addSelectionChangedListener(this);
+
+ return container;
+
}
- protected abstract String getCheckedValuesFromPreferences();
+ public String getCheckedValuesFromPreferences(){
+ String values;
+ if (localPref){
+ values = PreferencesUtil.getStringValue(predicate.getKey(), this.localPref);
+ }else{
+ values = pref != null ?pref.getValue(): null;
+ }
+ return values;
+ }
protected void rememberCheckedValues(String checkedValues) {
initialiseVocabularies();
}else{
List<AbstractTermDto> termsFromStringValues = getTermsFromStringValues(new ArrayList<String>());
treeComposite.setCheckedElements(termsFromStringValues.toArray());
+ pref = null;
}
}
}
}
if (listValue.isEmpty()){
- List<DefinedTermBase> terms = CdmStore.getTermManager().getPreferredTerms(type);
+ List<DefinedTermBase> terms = CdmStore.getTermManager().getAllTerms(type, null);
for (DefinedTermBase term: terms){
termlist.add(TermDto.fromTerm(term, true));
}
}
protected abstract List<TermVocabularyDto> getVocabulariesFromPreference();
+
+
+ protected void getPreferenceValues(){
+ pref = PreferencesUtil.getPreferenceFromDB(predicate);
+ this.allowOverride = pref!= null?pref.isAllowOverride():true;
+ this.isOverride = PreferencesUtil.getBooleanValue(PreferencesUtil.createOverridePreferenceString(predicate.getKey()));
+ if (pref == null){
+ pref = CdmPreference.NewTaxEditorInstance(predicate, null);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void selectionChanged(SelectionChangedEvent event) {
+ this.setApply(true);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ this.setApply(true);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void widgetDefaultSelected(SelectionEvent e) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ protected void performDefaults(){
+ rememberCheckedValues("");
+
+ useLocalOrAdmin.select(0);
+ setApply(true);
+
+ }
+
+ @Override
+ public boolean performOk() {
+ if (!isApply()){
+ return true;
+ }
+ Object[] checkedElements = treeComposite.getViewer().getCheckedElements();
+ List<UUID> listUIIDChecked = new ArrayList<>();
+ Collection<TermDto> preferredTermDtos = new ArrayList<>();
+ List<DefinedTermBase> preferredTerms = new ArrayList<>();
+ for (Object o : checkedElements) {
+ if(o instanceof TermDto){
+ TermDto termDto = (TermDto) o;
+ listUIIDChecked.add(termDto.getUuid());
+ preferredTermDtos.add(termDto);
+ if (vocabularies.size() > 1){
+ preferredTerms.add(CdmStore.getService(ITermService.class).load(termDto.getUuid()));
+ }
+
+ }
+ }
+ String saveCheckedElements = StringUtils.join(listUIIDChecked, ";"); //$NON-NLS-1$
+
+ PreferencesUtil.setStringValue(predicate.getKey(), saveCheckedElements);
+ String text = this.useLocalOrAdmin.getText();
+ if (text.equals(LocalOrDefaultEnum.Database.getLabel()) || text.equals(LocalOrDefaultEnum.Default.getLabel())){
+ PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(predicate.getKey()), false);
+ }else if (text.equals(LocalOrDefaultEnum.Local.getLabel())){
+ PreferencesUtil.setBooleanValue(PreferencesUtil.createOverridePreferenceString(predicate.getKey()), true);
+ }
+ if (vocabularies.size() == 1){
+ CdmStore.getTermManager().setPreferredTermsByDto(preferredTermDtos, vocabularies.get(0));
+ }else{
+ CdmStore.getTermManager().setPreferredTerms(preferredTerms, TermStore.getTerms(type, null));
+ }
+ PreferencesUtil.firePreferencesChanged(this.getClass());
+ return true;
+ }
}