*/
package eu.etaxonomy.taxeditor.preference.wizard;
+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.StyledString.Styler;
-import org.eclipse.jface.viewers.TreePath;
-import org.eclipse.jface.viewers.ViewerComparator;
-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.model.common.CdmBase;
-import eu.etaxonomy.taxeditor.editor.definedterm.TermContentProvider;
-import eu.etaxonomy.taxeditor.editor.definedterm.TermLabelProvider;
+import eu.etaxonomy.cdm.api.service.IVocabularyService;
+import eu.etaxonomy.cdm.model.common.TermType;
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+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 AbstractAreaSelectionWizard implements ICdmEntitySessionEnabled {
+public class AvailableDistributionPage extends AbstractTermSelectionWizardPage {
- /**
- * @param pageName
- */
protected AvailableDistributionPage(String pageName) {
- super(pageName);
+ super(pageName, TermType.NamedArea);
this.localPref = true;
+
}
- /*
- * (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());
- setViewer(new CheckboxTreeViewer(composite, SWT.NULL));
- ((CheckboxTreeViewer)getViewer()).getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
- getViewer().setContentProvider(new TermContentProvider());
- Styler styler = new Styler() {
- @Override
- public void applyStyles(TextStyle textStyle) {
- textStyle.foreground = Display.getCurrent().getSystemColor(SWT.COLOR_BLACK);
- }
- };
- getViewer().setLabelProvider(new TermLabelProvider(styler));
- getViewer().setComparator(new ViewerComparator());
- ((CheckboxTreeViewer)getViewer()).addCheckStateListener(new ICheckStateListener() {
-
- private boolean ignoreCheckEvent = false;
-
- @Override
- public void checkStateChanged(CheckStateChangedEvent event) {
-
-
- if (ignoreCheckEvent ) {
- return;
- }
+ setTitle(Messages.AvailableDistributionPage_PAGE_TITLE);
+ setDescription(Messages.AvailableDistributionPage_PAGE_DESCRIPTION);
- ignoreCheckEvent = true;
-
- try {
-
- checkChildren(event.getElement(), event.getChecked());
- grayParents(event.getElement());
- ((CheckboxTreeViewer)getViewer()).refresh();
- }
- 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());
-
-
-
-
- private boolean checkNoneChecked() {
-
- if (((CheckboxTreeViewer)getViewer()).getCheckedElements().length == 0) {
- setMessage("Please check at least one item", WARNING);
- return true;
- } else {
- setMessage(null);
- return false;
- }
- }
-
-
- /**
- *
- * @param path
- */
- private void checkParents(final TreePath path) {
- if (path == null) {
- return;
- }
- TermContentProvider tcp = new TermContentProvider();
- Object treeElement = path.getLastSegment();
-
- if (treeElement != null) {
- boolean allChecked = true;
- boolean allUnchecked = true;
- for (final Object fieldElement : tcp.getChildren(treeElement)) {
- allChecked = allChecked && ((CheckboxTreeViewer)getViewer()).getChecked(fieldElement);
- allUnchecked = allUnchecked && !((CheckboxTreeViewer)getViewer()).getChecked(fieldElement);
- }
- if (allUnchecked) {
- ((CheckboxTreeViewer)getViewer()).setChecked(treeElement, false);
- ((CheckboxTreeViewer)getViewer()).setGrayed(treeElement, false);
- } else if (allChecked) {
- ((CheckboxTreeViewer)getViewer()).setChecked(treeElement, true);
- ((CheckboxTreeViewer)getViewer()).setGrayed(treeElement, false);
- } else if (!allUnchecked && !allChecked) {
- ((CheckboxTreeViewer)getViewer()).setChecked(treeElement, true);
- ((CheckboxTreeViewer)getViewer()).setGrayed(treeElement, true);
- }
- }
- checkParents(path.getParentPath());
+ return checkedValues;
}
-// /**
-// *
-// * @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);
-// }
-// }
-// }
-
- /**
- *
- * @param path
- * @param checked
- */
- private void checkChildren(final Object element, final boolean checked) {
- TermContentProvider tcp = new TermContentProvider();
-
- if (element == null) {
- return;
- }
- // final Object element = path.getLastSegment();
-// if (element != null) {
- ((CheckboxTreeViewer)getViewer()).setChecked(element, checked);
- ((CheckboxTreeViewer)getViewer()).setGrayed(element, false);
-// int length = tcp.getChildren(element).length;
-// if(length>1){
-//
-// ((CheckboxTreeViewer)getViewer()).expandToLevel(element, CheckboxTreeViewer.ALL_LEVELS);
-// ((CheckboxTreeViewer)getViewer()).setSubtreeChecked(element, checked);
-// }
-// }
- }
- /**
- *
- * @param path
- * @param checked
- */
- private void grayParents(final Object element) {
- TermContentProvider tcp = new TermContentProvider();
-
- if (element == null) {
- return;
- }
- // final Object element = path.getLastSegment();
- Object parent = tcp.getParent(element);
- if (parent != null) {
- boolean allChecked = true;
- boolean allUnchecked = true;
- for (final Object fieldElement : tcp.getChildren(parent)) {
- allChecked = allChecked && ((CheckboxTreeViewer)getViewer()).getChecked(fieldElement);
- allUnchecked = allUnchecked && !((CheckboxTreeViewer)getViewer()).getChecked(fieldElement);
- }
- if (allUnchecked) {
- ((CheckboxTreeViewer)getViewer()).setChecked(parent, false);
- ((CheckboxTreeViewer)getViewer()).setGrayed(parent, false);
- } else {
- // ((CheckboxTreeViewer)getViewer()).setChecked(parent, false);
- boolean result = ((CheckboxTreeViewer)getViewer()).setGrayed(parent, true);
-
-
- }
-
- grayParents(tcp.getParent(parent));
- }
- }
-
@Override
public void dispose() {
- CdmStore.getCurrentSessionManager().dispose(this);
+ // 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;
- }
-
- /* (non-Javadoc)
- * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
- */
- @Override
+// @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"
+ "includes" //$NON-NLS-1$
});
- propertyPathsMap.put("includes", termsPropertyPaths);
- propertyPathsMap.put("terms", termsPropertyPaths);
+ propertyPathsMap.put("includes", termsPropertyPaths); //$NON-NLS-1$
+ propertyPathsMap.put("terms", termsPropertyPaths); //$NON-NLS-1$
return propertyPathsMap;
}
+ @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$
+ Set<UUID> uuidVocs = new HashSet<>();
+ for (String voc: arrayVocs){
+ if (!StringUtils.isBlank(voc)){
+ uuidVocs.add(UUID.fromString(voc));
+ }
+ }
+ List<TermVocabulary> tempVocs = CdmStore.getService(IVocabularyService.class).find(uuidVocs);
+ for (TermVocabulary voc: tempVocs){
+ vocs.add(new TermVocabularyDto(voc.getUuid(), voc.getRepresentations()));
+ }
+ }
+ return vocs;
+ }
+ @Override
+ protected void initialiseVocabularies() {
+ if (getVocabularies() != null) {
+ getVocabularies().clear();
+ }
+ List<TermVocabularyDto> vocs = new ArrayList<>();
+ vocs = getVocabulariesFromPreference();
+
+ setVocabularies(vocs);
+ }
}