-// $Id$
/**
* Copyright (C) 2014 EDIT
* European Distributed Institute of Taxonomy
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
-import org.eclipse.jface.preference.IPreferenceStore;
-import org.eclipse.jface.viewers.CheckStateChangedEvent;
-import org.eclipse.jface.viewers.CheckboxTreeViewer;
-import org.eclipse.jface.viewers.ICheckStateListener;
-import org.eclipse.jface.viewers.ITreeSelection;
-import org.eclipse.jface.viewers.StyledString.Styler;
-import org.eclipse.jface.viewers.TreePath;
-import org.eclipse.jface.wizard.WizardPage;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.TextStyle;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
+import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Display;
-import eu.etaxonomy.cdm.api.service.ITermService;
import eu.etaxonomy.cdm.api.service.IVocabularyService;
-import eu.etaxonomy.cdm.model.common.CdmBase;
-import eu.etaxonomy.cdm.model.common.DefinedTermBase;
import eu.etaxonomy.cdm.model.common.TermType;
import eu.etaxonomy.cdm.model.common.TermVocabulary;
-import eu.etaxonomy.taxeditor.editor.definedterm.TermContentProvider;
-import eu.etaxonomy.taxeditor.editor.definedterm.TermLabelProvider;
+import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
+import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
+import eu.etaxonomy.taxeditor.l10n.Messages;
+import eu.etaxonomy.taxeditor.preference.EditorPreferencePredicate;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
-import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
-import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
import eu.etaxonomy.taxeditor.store.CdmStore;
/**
* @date 21.07.2014
*
*/
-public class AvailableDistributionPage extends WizardPage implements ICdmEntitySessionEnabled {
+public class AvailableDistributionPage extends AbstractTermSelectionWizardPage {
- private CheckboxTreeViewer viewer;
+ protected AvailableDistributionPage(String pageName) {
+ super(pageName, TermType.NamedArea);
+ this.localPref = true;
- private Set<TermVocabulary<DefinedTermBase>> vocabularies;
- /**
- * @param pageName
- */
- protected AvailableDistributionPage(String pageName) {
- super(pageName);
- // TODO check if configuration exists
- CdmStore.getCurrentSessionManager().bindNullSession();
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets
- * .Composite)
- */
- /** {@inheritDoc} */
@Override
public void createControl(Composite parent) {
- IPreferenceStore preferenceStore = PreferencesUtil.getPreferenceStore();
- String checkedValues = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS);
- String grayedValues = preferenceStore.getString(PreferencesUtil.DISTRIBUTION_AREA_OCCURENCE_STATUS_GRAYED);
- setTitle("Select areas for Distribution Editor");
- setDescription("In order to be able to modify and see the distribution status of taxa,\n"
- + "you have to select the areas which you like to see.");
- Composite composite = new Composite(parent, SWT.NULL);
- composite.setLayout(new GridLayout());
- viewer = new CheckboxTreeViewer(composite, SWT.NULL);
- viewer.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- viewer.setContentProvider(new TermContentProvider());
- Styler styler = new Styler() {
- @Override
- public void applyStyles(TextStyle textStyle) {
- textStyle.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
- }
- };
- viewer.setLabelProvider(new TermLabelProvider(styler));
- viewer.addCheckStateListener(new ICheckStateListener() {
-
- private boolean ignoreCheckEvent = false;
-
- @Override
- public void checkStateChanged(CheckStateChangedEvent event) {
-
-
- if (ignoreCheckEvent ) {
- return;
- }
-
- ignoreCheckEvent = true;
+ setTitle(Messages.AvailableDistributionPage_PAGE_TITLE);
+ setDescription(Messages.AvailableDistributionPage_PAGE_DESCRIPTION);
- try {
- TreePath path = ((ITreeSelection) viewer.getSelection()).getPaths()[0];
- checkChildren(path, event.getChecked());
- checkParents(path.getParentPath());
- }
- finally {
- ignoreCheckEvent = false;
- }
- checkNoneChecked();
- }
- });
- // TODO: write listener or toggle button to select all named areas
-
- rememberCheckedValues(checkedValues, grayedValues);
- setControl(composite);
+ super.createControl(parent);
}
+ @Override
+ protected String getCheckedValuesFromPreferences() {
+ String checkedValues = PreferencesUtil.getStringValue(EditorPreferencePredicate.AvailableDistributionAreaTerms.getKey());
- protected List<TermVocabulary<DefinedTermBase>> initialiseVocabularies() {
- if (vocabularies != null) {
- vocabularies.clear();
- }
- List<TermVocabulary<DefinedTermBase>> vocs = CdmStore.getService(IVocabularyService.class).findByTermType(
- TermType.NamedArea);
- return vocs;
- }
-
- private void rememberCheckedValues(String checkedValues, String grayedValues) {
- viewer.setInput(initialiseVocabularies());
-
- if (grayedValues != null && grayedValues != "") {
- String[] listGrayed = grayedValues.split(",");
- ArrayList listGrayedTerms = new ArrayList();
- getTermsFromStringValues(listGrayed, listGrayedTerms);
- for(Object element : listGrayedTerms){
- if(element != null){
- viewer.setGrayChecked(element, true);
- }
- }
- }
- if (checkedValues != null && checkedValues != "") {
- String[] listChecked = checkedValues.split(",");
- ArrayList<DefinedTermBase<?>> listCheckedTerms = new ArrayList<DefinedTermBase<?>>();
- getTermsFromStringValues(listChecked, listCheckedTerms);
- for(Object element : listCheckedTerms){
- if(element != null){
- viewer.setChecked(element, true);
- }
- }
- }
- }
-
- /**
- * @param split
- * @param termlist
- */
- private void getTermsFromStringValues(String[] split, ArrayList termlist) {
- List<String> listValue = Arrays.asList(split);
- for (String s : listValue) {
- UUID uuid = UUID.fromString(s);
- ITermService termService = CdmStore.getService(ITermService.class);
- DefinedTermBase definedTermBase = termService.load(uuid);
- if(definedTermBase != null){
- termlist.add(definedTermBase);
- }else{
- IVocabularyService vocabularyService = CdmStore.getService(IVocabularyService.class);
- TermVocabulary termVocabulary = vocabularyService.load(uuid);
- termlist.add(termVocabulary);
- }
- }
+ return checkedValues;
}
- private boolean checkNoneChecked() {
-
- if (viewer.getCheckedElements().length == 0) {
- setMessage("Please check at least one item", WARNING);
- return true;
- } else {
- setMessage(null);
- return false;
- }
+ @Override
+ public void dispose() {
+ // CdmStore.getCurrentSessionManager().dispose(this);
+ super.dispose();
}
- /**
- * @return the viewer
- */
- public CheckboxTreeViewer getViewer() {
- return viewer;
+// @Override
+// public ICdmEntitySession getCdmEntitySession() {
+// return CdmStore.getCurrentSessionManager().getNullSession();
+// }
+//
+// @Override
+// public <T extends CdmBase> Collection<T> getRootEntities() {
+// return null;
+// }
+//
+// @Override
+ public Map<Object, List<String>> getPropertyPathsMap() {
+ Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
+ List<String> termsPropertyPaths = Arrays.asList(new String[] {
+ "includes" //$NON-NLS-1$
+ });
+ propertyPathsMap.put("includes", termsPropertyPaths); //$NON-NLS-1$
+ propertyPathsMap.put("terms", termsPropertyPaths); //$NON-NLS-1$
+ return propertyPathsMap;
}
- /**
- *
- * @param path
- */
- private void checkParents(final TreePath path) {
- if (path == null) {
- return;
- }
- TermContentProvider tcp = new TermContentProvider();
- Object treeElement = path.getLastSegment();
+ @Override
+ protected List<TermVocabularyDto> getVocabulariesFromPreference(){
+ List<TermVocabularyDto> vocs = new ArrayList<>();
+
+ if (PreferencesUtil.getPreferenceFromDB(PreferencePredicate.AvailableDistributionAreaVocabularies) == null && PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey()) == null){
+ vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
+ }else{
+ String vocString = PreferencesUtil.getStringValue(PreferencePredicate.AvailableDistributionAreaVocabularies.getKey());
+ if (vocString.equals("")){
+ vocs = CdmStore.getService(IVocabularyService.class).findVocabularyDtoByTermType(type);
+ return vocs;
+ }
+ String[] arrayVocs = vocString.split(";"); //$NON-NLS-1$
- if (treeElement != null) {
- boolean allChecked = true;
- boolean allUnchecked = true;
- for (final Object fieldElement : tcp.getChildren(treeElement)) {
- allChecked = allChecked && this.viewer.getChecked(fieldElement);
- allUnchecked = allUnchecked && !this.viewer.getChecked(fieldElement);
+ Set<UUID> uuidVocs = new HashSet<>();
+ for (String voc: arrayVocs){
+ if (!StringUtils.isBlank(voc)){
+ uuidVocs.add(UUID.fromString(voc));
+ }
}
- if (allUnchecked) {
- this.viewer.setChecked(treeElement, false);
- this.viewer.setGrayed(treeElement, false);
- } else if (allChecked) {
- this.viewer.setChecked(treeElement, true);
- this.viewer.setGrayed(treeElement, false);
- } else if (!allUnchecked && !allChecked) {
- this.viewer.setChecked(treeElement, true);
- this.viewer.setGrayed(treeElement, true);
+ List<TermVocabulary> tempVocs = CdmStore.getService(IVocabularyService.class).find(uuidVocs);
+ for (TermVocabulary voc: tempVocs){
+ vocs.add(new TermVocabularyDto(voc.getUuid(), voc.getRepresentations()));
}
- }
- checkParents(path.getParentPath());
- }
- /**
- *
- * @param path
- * @param checked
- */
- private void checkChildren(final TreePath path, final boolean checked) {
- TermContentProvider tcp = new TermContentProvider();
-
- if (path == null) {
- return;
- }
- final Object element = path.getLastSegment();
- if (element != null) {
- this.viewer.setChecked(element, checked);
- this.viewer.setGrayed(element, false);
- int length = tcp.getChildren(element).length;
- if(length>1){
- this.viewer.expandToLevel(element, CheckboxTreeViewer.ALL_LEVELS);
- this.viewer.setSubtreeChecked(element, checked);
- }
}
+ return vocs;
}
-
- @Override
- public void dispose() {
- CdmStore.getCurrentSessionManager().dispose(this);
- super.dispose();
- }
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
- */
- @Override
- public ICdmEntitySession getCdmEntitySession() {
- return CdmStore.getCurrentSessionManager().getNullSession();
- }
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
- */
@Override
- public <T extends CdmBase> Collection<T> getRootEntities() {
- return null;
- }
+ protected void initialiseVocabularies() {
+ if (getVocabularies() != null) {
+ getVocabularies().clear();
+ }
+ List<TermVocabularyDto> vocs = new ArrayList<>();
+ vocs = getVocabulariesFromPreference();
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
- */
- @Override
- public Map<Object, List<String>> getPropertyPathsMap() {
- Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
- List<String> termsPropertyPaths = Arrays.asList(new String[] {
- "includes"
- });
- propertyPathsMap.put("includes", termsPropertyPaths);
- propertyPathsMap.put("terms", termsPropertyPaths);
- return propertyPathsMap;
+ setVocabularies(vocs);
}
}