-// $Id$
/**
* Copyright (C) 2007 EDIT
* European Distributed Institute of Taxonomy
package eu.etaxonomy.taxeditor.ui.dialog.selection;
-import java.lang.reflect.Field;
import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
+import java.util.regex.Pattern;
-import org.eclipse.core.runtime.CoreException;
+import org.apache.commons.lang.StringUtils;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.OperationCanceledException;
-import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
+import org.eclipse.swt.graphics.Cursor;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
-import org.eclipse.ui.IMemento;
-import org.eclipse.ui.dialogs.FilteredItemsSelectionDialog;
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
-import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
+import eu.etaxonomy.cdm.api.service.dto.EntityDTOBase;
+import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
+import eu.etaxonomy.cdm.common.CdmUtils;
import eu.etaxonomy.cdm.model.common.CdmBase;
import eu.etaxonomy.cdm.model.common.ICdmBase;
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
+import eu.etaxonomy.taxeditor.l10n.Messages;
import eu.etaxonomy.taxeditor.model.MessagingUtils;
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
*
* @author n.hoffmann
* @created 04.06.2009
- * @version 1.0
*/
public abstract class AbstractFilteredCdmResourceSelectionDialog<T extends ICdmBase> extends
- FilteredItemsSelectionDialog implements IConversationEnabled {
+ SearchDialog {//implements IConversationEnabled {
- private final ConversationHolder conversation;
+// private final ConversationHolder conversation = null;
protected List<UuidAndTitleCache<T>> model;
private final Set<T> transientCdmObjects = new HashSet<T>();
private final String settings;
+ protected final Integer limitOfInitialElements = null;
+
+ private T selectedObject;
+
+ protected Set<UUID> cdmBaseToBeFiltered;
+
+ protected Job searchJob;
- protected T cdmBaseToBeFiltered;
/**
* <p>Constructor for AbstractFilteredCdmResourceSelectionDialog.</p>
* @param cdmObject a T object.
* @param <T> a T object.
*/
- protected AbstractFilteredCdmResourceSelectionDialog(Shell shell, ConversationHolder conversation, String title, boolean multi, String settings, T cdmObject) {
- super(shell, multi);
- setTitle(title);
- setMessage("Use * for wildcard, or ? to see all entries");
+ protected AbstractFilteredCdmResourceSelectionDialog(Shell shell, //ConversationHolder conversation,
+ String title, boolean multi, String settings, T cdmObject) {
+ super(shell, title);
+ setShellStyle(SWT.DIALOG_TRIM);
+ setMessage(Messages.SearchDialog_patternLabel);
this.settings = settings;
-
- this.conversation = conversation;
-
+ if (cdmObject != null){
+ this.cdmBaseToBeFiltered = new HashSet<>();
+ this.cdmBaseToBeFiltered.add(cdmObject.getUuid());
+ }
+ Cursor cursor = shell.getCursor();
+ shell.setCursor(shell.getDisplay().getSystemCursor(SWT.CURSOR_WAIT));
init();
-
- initModel();
-
- String objectTitle = getTitle(cdmObject);
- if (objectTitle != null) {
- setInitialPattern(objectTitle);
- }
-
+ shell.setCursor(cursor);
setListLabelProvider(createListLabelProvider());
- setDetailsLabelProvider(createDetailsLabelProvider());
- setSelectionHistory(new ResourceSelectionHistory());
}
/**
* @return a TYPE object.
*/
protected static <TYPE extends CdmBase> TYPE getSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog<TYPE> dialog) {
- //dialog.setInitialPattern("");
- int result = dialog.open();
-
- if (result == Window.CANCEL) {
- return null;
- }
-
- UUID uuid = dialog.getSelectedUuidAndTitleCache().getUuid();
- if(uuid == null){
- return null;
- }
- return dialog.getCdmObjectByUuid(uuid);
+ UuidAndTitleCache result = getUuidAndTitleCacheSelectionFromDialog(dialog);
+ if (result != null){
+ return dialog.getCdmObjectByUuid(result.getUuid());
+ } else {
+ return null;
+ }
}
+ /**
+ * <p>getSelectionFromDialog</p>
+ *
+ * @param dialog a {@link eu.etaxonomy.taxeditor.ui.dialog.selection.AbstractFilteredCdmResourceSelectionDialog} object.
+ * @param <TYPE> a TYPE object.
+ * @return a TYPE object.
+ */
+ protected static UuidAndTitleCache getUuidAndTitleCacheSelectionFromDialog(AbstractFilteredCdmResourceSelectionDialog dialog) {
+ if (dialog == null){
+ return null;
+ }
+ int result = dialog.open();
+
+ if (result == Window.CANCEL) {
+ return null;
+ }
+
+ UuidAndTitleCache uuid = dialog.getSelectedUuidAndTitleCache();
+
+ return uuid;
+ }
+
/**
* Check if object was created during the life of this dialog. If not,
* retrieve it from the CdmStore.
*/
abstract protected T getPersistentObject(UUID uuid);
- /**
- * @param cdmObject
- */
-// protected void addObjectToModel(T cdmObject) {
-// model.add(new UuidAndTitleCache(cdmObject.getClass(), cdmObject.getUuid(), getTitle(cdmObject)));
-// transientCdmObjects.add(cdmObject);
-// }
/**
* <p>isObjectTransient</p>
}
- /** {@inheritDoc} */
- @Override
- public void refresh() {
- initModel();
- filterExcludedObjects();
- super.refresh();
- }
-
- /**
- * <p>initModel</p>
- */
- abstract protected void initModel();
-
- /* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createFilter()
- */
- /** {@inheritDoc} */
- @Override
- protected ItemsFilter createFilter() {
- return new ItemsFilter() {
-
- /**
- * Always returns false to enforce refiltering even if the pattern is equal
- */
- @Override
- public boolean equalsFilter(ItemsFilter filter) {
- return false;
- }
-
- @Override
- public boolean isConsistentItem(Object item) {
- return false;
- }
-
- @Override
- public boolean matchItem(Object item) {
- String text = null;
- if(item instanceof UuidAndTitleCache){
- text = ((UuidAndTitleCache) item).getTitleCache();
- }else if(item instanceof String){
- text = (String) item;
- }
- return text != null ? matches(text) : false;
- }
-
- };
- }
-
/**
* Set the filter input to the Agent's title cache
* @param cdmObject a T object.
*/
protected void setPattern(T cdmObject) {
- // FilteredSelection does some very tricky caching to make sure it
- // runs with high performance.
- // This works for most use cases, but we want to change the model while the dialog is open
- // and all the clever caching prevents the content provider from knowing that the model has changed
- // I am aware, that this is a hack, but the FilteredSelectionDialog API does not offer a convenient
- // way to solve the problem.
- try {
- Field lastCompletedFilter = this.getClass().getSuperclass().getSuperclass().getDeclaredField("lastCompletedFilter");
- lastCompletedFilter.setAccessible(true);
- lastCompletedFilter.set(this, null);
- } catch (SecurityException e) {
- MessagingUtils.error(getClass(), e);
- } catch (NoSuchFieldException e) {
- MessagingUtils.error(getClass(), e);
- } catch (IllegalArgumentException e) {
- MessagingUtils.error(getClass(), e);
- } catch (IllegalAccessException e) {
- MessagingUtils.error(getClass(), e);
- }
-
- // this also is not the nicest way to do it.
- // I am still amazed, that FilteredSelectionDialog does not offer any methods to change its data
- // once it was opened. Am I doing it wrong?
String pattern = getTitle(cdmObject);
- ((Text) getPatternControl()).setText(pattern);
+ getSearchField().setText(pattern);
}
+
+
/* (non-Javadoc)
* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#fillContentProvider(org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.AbstractContentProvider, org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.ItemsFilter, org.eclipse.core.runtime.IProgressMonitor)
*/
/** {@inheritDoc} */
+
@Override
- protected void fillContentProvider(AbstractContentProvider contentProvider,
- ItemsFilter itemsFilter, IProgressMonitor progressMonitor)
- throws CoreException {
+ protected void fillContentProvider(IProgressMonitor progressMonitor)
+ {
try {
+ if (model == null){
+ model = new ArrayList<UuidAndTitleCache<T>>();
+ }
if(model != null){
- progressMonitor.beginTask("Looking for entities", model.size());
- for(UuidAndTitleCache<T> element : model){
- contentProvider.add(element, itemsFilter);
- if (progressMonitor.isCanceled()) {
- throw new OperationCanceledException();
- }
- progressMonitor.worked(1);
+ if (progressMonitor != null){
+ progressMonitor.beginTask("Looking for entities", model.size());
+ }
+ sort();
+
+ contentProvider.reset();
+ Iterator<UuidAndTitleCache<T>> iterator = model.iterator();
+ UuidAndTitleCache<T> element;
+ while(iterator.hasNext()){
+ element = iterator.next();
+
+ if (cdmBaseToBeFiltered == null || !cdmBaseToBeFiltered.contains(element.getUuid())){
+ contentProvider.add(element);
+ }
+ if (progressMonitor != null){
+ if (progressMonitor.isCanceled()) {
+ return;
+ }
+ progressMonitor.worked(1);
+ }
}
+ this.refresh();
}else{
+
MessagingUtils.warn(getClass(), "Model for Filtered Selection is null:" + this.getClass().getSimpleName());
}
}
finally {
- progressMonitor.done();
+ if (progressMonitor != null) {
+ progressMonitor.done();
+ }
}
}
- /* (non-Javadoc)
+
+ protected void sort() {
+ Collections.sort(model, getItemsComparator());
+ }
+
+ /* (non-Javadoc)
* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getDialogSettings()
*/
/** {@inheritDoc} */
- @Override
+
protected IDialogSettings getDialogSettings() {
IDialogSettings settings = TaxeditorStorePlugin.getDefault().getDialogSettings().getSection(getSettings());
* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getElementName(java.lang.Object)
*/
/** {@inheritDoc} */
- @Override
+
public String getElementName(Object item) {
return ((UuidAndTitleCache) item).getTitleCache();
}
* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#getItemsComparator()
*/
/** {@inheritDoc} */
+
@Override
- protected Comparator getItemsComparator() {
+ protected Comparator getItemsComparator() {
return new Comparator<UuidAndTitleCache>() {
@Override
public int compare(UuidAndTitleCache entity1,
UuidAndTitleCache entity2) {
Collator collator = Collator.getInstance();
- return collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
+ if (entity1 == entity2){
+ return 0;
+ }
+
+ if (entity1 == null && entity2 != null){
+ return -1;
+ }
+ if (entity2 == null && entity1 != null){
+ return 1;
+ }
+ if (entity1.getUuid().equals(entity2.getUuid())){
+ return 0;
+ }
+ if (entity1.getTitleCache() == null && entity2.getTitleCache() != null){
+ return -1;
+ }
+ if (entity2.getTitleCache() == null){
+ return 1;
+ }
+ int result = collator.compare(entity1.getTitleCache(), entity2.getTitleCache());
+ if (result == 0){
+ result = entity1.getUuid().compareTo(entity2.getUuid());
+ }
+ return result;
}
};
}
- /* (non-Javadoc)
- * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#validateItem(java.lang.Object)
- */
- /** {@inheritDoc} */
- @Override
- protected IStatus validateItem(Object item) {
- return Status.OK_STATUS;
- }
/**
* <p>getSelectedUuidAndTitleCache</p>
* @return a {@link eu.etaxonomy.cdm.model.common.UuidAndTitleCache} object.
*/
protected UuidAndTitleCache getSelectedUuidAndTitleCache() {
- Object[] result = getResult();
- return result[0] == null ? null : (UuidAndTitleCache) result[0];
+ Object result = getResult();
+ if (result instanceof UuidAndTitleCache){
+ return (UuidAndTitleCache) result;
+ }
+ return null;
}
/**
+ * @return
+ */
+ private Object getResult() {
+ StructuredSelection selection = getCurrentSelection();
+ if (selection == null){
+ return null;
+ }
+ return selection.getFirstElement();
+ }
+
+ /**
* <p>Getter for the field <code>settings</code>.</p>
*
* @return a {@link java.lang.String} object.
return settings;
}
- /**
- *
- * @author n.hoffmann
- * @created Oct 19, 2009
- * @version 1.0
- */
- private class ResourceSelectionHistory extends SelectionHistory {
- /*
- * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#restoreItemFromMemento(org.eclipse.ui.IMemento)
- */
- @Override
- protected Object restoreItemFromMemento(IMemento element) {
- return element.getString("resource"); //$NON-NLS-1$
- }
- /*
- * @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog.SelectionHistory#storeItemToMemento(java.lang.Object,
- * org.eclipse.ui.IMemento)
- */
- @Override
- protected void storeItemToMemento(Object item, IMemento element) {
- element.putString("resource", item.toString()); //$NON-NLS-1$
- }
- }
+
/**
* <p>getNewWizardLinkText</p>
*
* @return a {@link java.lang.String} object.
*/
- protected abstract String getNewWizardLinkText();
+ protected abstract String[] getNewWizardText();
/**
* <p>getNewEntityWizard</p>
}
UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
String titleCache = uuidAndTitleCache.getTitleCache();
- if(PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
+ if(PreferencesUtil.getBooleanValue(IPreferenceKeys.SHOW_ID_IN_ENTITY_SELECTION_DIAOLOG)){
titleCache += " ["+uuidAndTitleCache.getId()+"]";
}
+ if (element instanceof EntityDTOBase){
+ titleCache += "(" + ((IdentifiedEntityDTO)element).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO)element).getIdentifier().getIdentifier() + ")";
+ }
+
return titleCache;
}
};
* @see org.eclipse.ui.dialogs.FilteredItemsSelectionDialog#createExtendedContentArea(org.eclipse.swt.widgets.Composite)
*/
/** {@inheritDoc} */
- @Override
- protected Control createExtendedContentArea(Composite parent) {
- String newWizardLinkText = getNewWizardLinkText();
- if(newWizardLinkText != null){
- Link link = new Link(parent, SWT.NONE);
- link.setText(newWizardLinkText);
- link.addSelectionListener(getNewWizardLinkSelectionListener());
- return link;
- }
- return null;
- }
- protected SelectionListener getNewWizardLinkSelectionListener(){
+// @Override
+// protected Control createExtendedContentArea(Composite parent) {
+// String newWizardLinkText = getNewWizardLinkText();
+//// if(newWizardLinkText != null){
+//// newButton1 = this.createButton(this.getShell(), new_id, newWizardLinkText, false);
+////
+//// newButton1.addSelectionListener(getNewWizardLinkSelectionListener());
+//// return newButton1;
+//// }
+// return null;
+// }
+
+ @Override
+ protected void createButtonsForButtonBar(Composite parent) {
+ String[] newButtonText = getNewWizardText();
+
+ if (newButtonText!= null){
+ this.newButton1 = createButton(parent, this.new_id, newButtonText[0], false);
+ newButton1.addSelectionListener(getNewWizardButtonSelectionListener());
+
+
+ if (newButtonText.length > 1){
+ newButton2 = createButton(parent, this.new_id2, newButtonText[1], false);
+ newButton2.addSelectionListener(getNewWizardButtonSelectionListener());
+
+ }
+
+ }
+ Button space = createButton(parent, this.space_id, " ", false);
+ space.setEnabled(false);
+ space.setVisible(false);
+ GridData gridData = new GridData();
+ gridData.grabExcessHorizontalSpace = false;
+ gridData.widthHint = 3;
+ space.setLayoutData(gridData);
+ GridLayout gridLayout = new GridLayout();
+ gridLayout.makeColumnsEqualWidth= false;
+ if (newButtonText != null){
+ gridLayout.numColumns=newButtonText.length+2;
+ }else{
+ gridLayout.numColumns=2;
+ }
+ parent.setLayout(gridLayout);
+
+ super.createButtonsForButtonBar(parent);
+ super.getButton(IDialogConstants.OK_ID).setEnabled(false);
+ }
+
+ protected SelectionListener getNewWizardButtonSelectionListener(){
return new SelectionAdapter() {
- /* (non-Javadoc)
- * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
- */
@Override
public void widgetSelected(SelectionEvent e) {
-
- AbstractNewEntityWizard wizard = getNewEntityWizard(e.text);
-
- wizard.init(null, null);
- if(wizard.getEntity() != null) {
- WizardDialog dialog = new WizardDialog(getShell(), wizard);
- int status = dialog.open();
-
- if (status == IStatus.OK) {
-
- T entity = (T) wizard.getEntity();
- refresh();
- setPattern(entity);
- getConversationHolder().bind();
- }
- //FIXME : Need to make sure this is a stable fix (ticket 3822)
- getConversationHolder().commit();
- }
+ Object source = e.getSource();
+ String text = null;
+ if (source instanceof Button){
+ Button sourceButton = (Button) source;
+ text = sourceButton.getText();
+ }
+ AbstractNewEntityWizard wizard = getNewEntityWizard(text);
+ if(wizard!=null){
+ if (wizard.getEntity() == null){
+ wizard.init(null, null);
+ }
+ if(wizard.getEntity() != null) {
+ WizardDialog dialog = new WizardDialog(getShell(), wizard);
+ int status = dialog.open();
+
+ if (status == IStatus.OK) {
+
+ T entity = (T) wizard.getEntity();
+ refresh();
+ setPattern(entity);
+
+// if (getConversationHolder() != null){
+// getConversationHolder().bind();
+// }
+ }
+ //FIXME : Need to make sure this is a stable fix (ticket 3822)
+// if (getConversationHolder() != null){
+// getConversationHolder().commit();
+// }
+ }
+ }
}
};
}
* <p>getConversationHolder</p>
*
* @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
- */
- @Override
- public ConversationHolder getConversationHolder() {
- return conversation;
- }
+// */
+// @Override
+// public ConversationHolder getConversationHolder() {
+// return conversation;
+// }
/** {@inheritDoc} */
- @Override
- public void update(CdmDataChangeMap changeEvents) {}
+// @Override
+// public void update(CdmDataChangeMap changeEvents) {}
/**
* Don't want to add for example a taxon or synonym to itself
UuidAndTitleCache uuidAndTitleCacheToRemove = null;
for (UuidAndTitleCache uuidAndTitleCache : model){
- if ((cdmBaseToBeFiltered.getUuid()).equals(uuidAndTitleCache.getUuid())) {
+ if (cdmBaseToBeFiltered != null && cdmBaseToBeFiltered.contains(uuidAndTitleCache.getUuid())) {
uuidAndTitleCacheToRemove = uuidAndTitleCache;
}
}
model.remove(uuidAndTitleCacheToRemove);
}
}
+ @Override
+ void createFilterButton(Composite searchAndFilter){
+ //as default no filter button available
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected void search() {
+ Control control =getSearchField();
+ String pattern = null;
+ if (control != null){
+ pattern = ((Text)control).getText();
+ if (pattern.equals("*") || pattern.equals("?")){
+ callService(null);
+ }else if (StringUtils.isNotBlank(pattern)){
+// callService(CdmUtils.replaceNonWordCharacters(pattern, "."));
+ callService(pattern);
+ }
+ fillContentProvider(null);
+ }
+
+// if (pattern.equals("?")){
+// model = CdmStore.getService(INameService.class).getUuidAndTitleCache(null, null);
+// }else if (pattern != null){
+// model = CdmStore.getService(INameService.class).getUuidAndTitleCache(limitOfInitialElements, pattern);
+// }
+ }
+
+ abstract void callService(String pattern);
+
}