2 * Copyright (C) 2007 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.
10 package eu
.etaxonomy
.taxeditor
.editor
.name
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import org
.apache
.commons
.lang
.StringUtils
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.core
.runtime
.IAdaptable
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
22 import org
.eclipse
.jface
.action
.MenuManager
;
23 import org
.eclipse
.jface
.viewers
.ISelection
;
24 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
25 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
26 import org
.eclipse
.swt
.dnd
.DND
;
27 import org
.eclipse
.swt
.dnd
.DropTarget
;
28 import org
.eclipse
.swt
.dnd
.Transfer
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Menu
;
31 import org
.eclipse
.ui
.IEditorInput
;
32 import org
.eclipse
.ui
.IEditorSite
;
33 import org
.eclipse
.ui
.ISelectionListener
;
34 import org
.eclipse
.ui
.ISelectionService
;
35 import org
.eclipse
.ui
.IWorkbenchPart
;
36 import org
.eclipse
.ui
.IWorkbenchPartReference
;
37 import org
.eclipse
.ui
.PartInitException
;
38 import org
.eclipse
.ui
.forms
.ManagedForm
;
39 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
40 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
41 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
42 import org
.eclipse
.ui
.part
.EditorPart
;
44 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
45 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
46 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
47 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
48 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
49 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
51 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
52 import eu
.etaxonomy
.taxeditor
.editor
.CdmDataTransfer
;
53 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
54 import eu
.etaxonomy
.taxeditor
.editor
.IDropTargetable
;
55 import eu
.etaxonomy
.taxeditor
.editor
.IMultiPageTaxonEditorPage
;
56 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
57 import eu
.etaxonomy
.taxeditor
.editor
.SimpleSelectionProvider
;
58 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
59 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroup
;
60 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
61 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedGroup
;
62 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedNameContainer
;
63 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.ConceptGroup
;
64 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.ContainerFactory
;
65 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.HomotypicalSynonymGroup
;
66 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.MisappliedGroup
;
67 import eu
.etaxonomy
.taxeditor
.editor
.name
.dnd
.NameEditorDropTargetListener
;
68 import eu
.etaxonomy
.taxeditor
.model
.IPartChangeListener
;
69 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
70 import eu
.etaxonomy
.taxeditor
.model
.TaxeditorPartService
;
71 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
72 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
76 * TaxonNameEditor class.
84 public class TaxonNameEditor
extends EditorPart
implements
85 IMultiPageTaxonEditorPage
, IAdaptable
, IConversationEnabled
,
86 IPostOperationEnabled
, IPartContentHasDetails
, IPartChangeListener
,
87 ISelectionListener
, IDropTargetable
{
89 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon.name"</code> */
90 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon.name";
94 private ManagedForm managedForm
;
95 private ScrolledForm scrolledForm
;
96 private Composite parent
;
97 private ISelectionProvider provider
;
99 private final MultiPageTaxonEditor editor
;
101 private TaxonBase selection
;
103 private final ConversationHolder conversation
;
105 private MenuManager menuManager
;
109 private MisappliedGroup misappliedGroup
;
111 private ConceptGroup conceptGroup
;
113 private final List
<HomotypicalSynonymGroup
> heterotypicSynonymGroups
= new ArrayList
<HomotypicalSynonymGroup
>();
115 private DropTarget target
;
117 private ISelectionService selectionService
;
119 private AcceptedGroup acceptedGroup
;
121 private TaxonBase objectAffectedByLastOperation
;
125 * Constructor for TaxonNameEditor.
129 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
132 public TaxonNameEditor(MultiPageTaxonEditor editor
) {
133 this.editor
= editor
;
134 conversation
= editor
.getConversationHolder();
142 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
145 public IUndoContext
getUndoContext() {
146 return editor
.getUndoContext();
153 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
158 public void createPartControl(Composite composite
) {
159 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
160 selectionService
.addSelectionListener(this);
162 createManagedForm(composite
);
164 TaxeditorPartService
.getInstance().addListener(
165 TaxeditorPartService
.PART_ACTIVATED
, this);
174 * a {@link org.eclipse.swt.widgets.Composite} object.
176 protected void createManagedForm(Composite composite
) {
178 managedForm
= new ManagedForm(composite
) {
181 public void dirtyStateChanged() {
182 firePropertyChange(PROP_DIRTY
);
186 public boolean setInput(Object input
) {
187 if (input
instanceof AbstractGroupedContainer
) {
188 selection
= ((AbstractGroupedContainer
) input
).getData();
189 provider
.setSelection(new StructuredSelection(selection
));
191 return super.setInput(input
);
195 scrolledForm
= managedForm
.getForm();
196 parent
= scrolledForm
.getBody();
198 // register the context menu
199 menuManager
= new MenuManager();
200 ISelectionProvider selectionProvider
= getSite().getSelectionProvider();
201 getSite().registerContextMenu(TaxonNameEditor
.ID
, menuManager
,
204 parent
.setData(taxon
);
206 TableWrapLayout layout
= new TableWrapLayout();
207 layout
.leftMargin
= 0;
208 layout
.rightMargin
= 0;
209 layout
.topMargin
= 0;
210 layout
.bottomMargin
= 0;
212 layout
.verticalSpacing
= 0;
213 layout
.horizontalSpacing
= 0;
215 parent
.setLayout(layout
);
216 parent
.setBackground(EditorUtil
217 .getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
219 createOrUpdateNameComposites();
228 * createNameComposites
231 public void createOrUpdateNameComposites() {
233 ContainerFactory
.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
234 ContainerFactory
.createOrUpdateHeterotypicSynonymyGroups(this);
235 ContainerFactory
.createOrUpdateMisapplicationsGroup(this);
236 ContainerFactory
.createOrUpdateConceptGroup(this);
239 managedForm
.reflow(true);
244 * Getter for the field <code>taxon</code>.
247 * @return a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
249 public Taxon
getTaxon() {
250 return (Taxon
) HibernateProxyHelper
.deproxy(taxon
);
258 public void setDirty() {
259 managedForm
.dirtyStateChanged();
265 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
269 public void setFocus() {
270 if (getSelectedContainer() == null) {
271 throw new IllegalStateException(
272 "There should always be a selected object.");
274 getSelectedContainer().setSelected();
279 public void setInput(IEditorInput input
) {
280 this.setInputWithNotify(input
);
287 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
292 * getConversationHolder
295 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
298 public ConversationHolder
getConversationHolder() {
306 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
307 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
310 public void update(CdmDataChangeMap events
) {
315 * Redraws this editor return true on success
319 public boolean redraw() {
326 * Redraws the editor controls
328 public boolean redraw(boolean focus
) {
330 createOrUpdateNameComposites();
341 * getMultiPageTaxonEditor
344 * @return a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
347 public MultiPageTaxonEditor
getMultiPageTaxonEditor() {
355 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
359 public boolean postOperation(CdmBase objectAffectedByOperation
) {
361 editor
.changed(objectAffectedByOperation
);
365 if (objectAffectedByOperation
instanceof TaxonBase
) {
366 objectAffectedByLastOperation
= (TaxonBase
) objectAffectedByOperation
;
374 * Getter for the field <code>managedForm</code>.
377 * @return the managedForm
379 public ManagedForm
getManagedForm() {
388 * @return a {@link org.eclipse.swt.widgets.Composite} object.
390 public Composite
getControl() {
391 return this.getManagedForm().getForm().getBody();
396 * Getter for the field <code>menu</code>.
401 public Menu
getMenu() {
402 if (menu
== null || menu
.isDisposed()) {
403 // Creating the menu because it was either not initialised or
404 // disposed while refreshing the editor
405 menu
= menuManager
.createContextMenu(parent
);
415 * @return true if there are empty names
417 public boolean checkForEmptyNames() {
418 for (AbstractGroupedContainer container
: getGroupedContainers()) {
419 if (container
.getName() == null
420 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
427 public Set
<AbstractGroupedContainer
> getEmptyContainers() {
428 Set
<AbstractGroupedContainer
> containersWithEmptyNames
= new HashSet
<AbstractGroupedContainer
>();
430 for (AbstractGroupedContainer container
: getGroupedContainers()) {
431 if (container
.getName() == null
432 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
433 containersWithEmptyNames
.add(container
);
437 return containersWithEmptyNames
;
442 public void doSave(IProgressMonitor monitor
) {
444 monitor
.beginTask("Saving names", getGroupedContainers().size());
447 // check for empty names
448 for (AbstractGroupedContainer container
: getGroupedContainers()) {
450 monitor
.subTask("Saving composite: "
451 + container
.getTaxonBase().getTitleCache());
452 container
.persistName();
454 // In case the progress monitor was canceled throw an exception.
455 if (monitor
.isCanceled()) {
456 throw new OperationCanceledException();
459 // Otherwise declare this step as done.
464 // Stop the progress monitor.
472 public void doSaveAs() {
477 public void init(IEditorSite site
, IEditorInput input
)
478 throws PartInitException
{
480 if (!(input
instanceof IEditorInput
))
481 throw new PartInitException("Invalid Input: Must be IEditorInput");
483 if (input
.getAdapter(Taxon
.class) != null) {
484 taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
486 throw new PartInitException("Invalid Input: Taxon cannot be null");
492 provider
= new SimpleSelectionProvider();
493 getSite().setSelectionProvider(provider
);
499 private void createDragSupport() {
500 // Listen for names being dragged outside of existing homotypic groups -
501 // user wants to create a new group
502 Transfer
[] types
= new Transfer
[] { CdmDataTransfer
.getInstance() };
503 int operations
= DND
.DROP_MOVE
;
504 if (target
== null) {
505 target
= new DropTarget(parent
, operations
);
506 target
.setTransfer(types
);
507 target
.addDropListener(new NameEditorDropTargetListener(this));
513 * Getter for the field <code>acceptedNameContainer</code>.
517 * {@link eu.etaxonomy.taxeditor.editor.name.container.AcceptedNameContainer}
520 public AcceptedNameContainer
getAcceptedNameContainer() {
521 return getAcceptedGroup().getAcceptedNameContainer();
529 * @param homotypicalGroup
530 * a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
532 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
535 public HomotypicalSynonymGroup
getHomotypicalGroupContainer(
536 HomotypicalGroup homotypicalGroup
) {
537 for (HomotypicalSynonymGroup group
: getHeterotypicSynonymGroups()) {
538 if (group
.getGroup().equals(homotypicalGroup
)) {
548 * Getter for the field <code>conceptGroup</code>.
552 * {@link eu.etaxonomy.taxeditor.editor.name.container.ConceptGroup}
555 public ConceptGroup
getConceptGroup() {
564 * @return a Set containing all composites that have been edited
566 public Set
<AbstractGroupedContainer
> getDirtyNames() {
567 Set
<AbstractGroupedContainer
> dirtyNames
= new HashSet
<AbstractGroupedContainer
>();
569 for (AbstractGroupedContainer composite
: getGroupedContainers()) {
570 if (composite
.isDirty()) {
571 dirtyNames
.add(composite
);
580 * getGroupedContainers
583 * @return a {@link java.util.List} object.
585 public List
<AbstractGroupedContainer
> getGroupedContainers() {
586 List
<AbstractGroupedContainer
> groupedComposites
= new ArrayList
<AbstractGroupedContainer
>();
588 for (AbstractGroup group
: getAllGroups()) {
589 groupedComposites
.addAll(group
.getGroupedContainers());
592 return groupedComposites
;
600 * @return a {@link java.util.List} object.
602 public List
<AbstractGroup
> getAllGroups() {
603 List
<AbstractGroup
> allGroups
= new ArrayList
<AbstractGroup
>();
605 allGroups
.add(getAcceptedGroup());
607 allGroups
.addAll(getHeterotypicSynonymGroups());
609 if (misappliedGroup
!= null) {
610 allGroups
.add(misappliedGroup
);
613 if (conceptGroup
!= null) {
614 allGroups
.add(conceptGroup
);
623 * @see org.eclipse.ui.part.EditorPart#isDirty()
627 public boolean isDirty() {
628 return editor
.isDirty();
634 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
638 public void dispose() {
645 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
649 public boolean isSaveAsAllowed() {
656 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.
657 * IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
660 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
669 * @return a {@link eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor}
672 public TaxonNameEditor
getEditor() {
678 * Getter for the field <code>selectedObject</code>.
682 * {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer}
685 public AbstractGroupedContainer
getSelectedContainer() {
687 TaxonBase selectedTaxonBase
= null;
689 TaxonEditorInput input
= (TaxonEditorInput
) editor
.getEditorInput();
690 if (input
.getInitiallySelectedTaxonBase() != null) {
691 selectedTaxonBase
= input
.getInitiallySelectedTaxonBase();
693 if (selection
!= null) {
694 selectedTaxonBase
= selection
;
698 return (selectedTaxonBase
!= null) ?
getContainer(selectedTaxonBase
)
699 : getAcceptedNameContainer();
707 public void dragEntered() {
709 getControl().setBackground(
710 EditorUtil
.getColor(Resources
.COLOR_DRAG_ENTER
));
718 public void dragLeft() {
719 getControl().setBackground(
720 EditorUtil
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
725 * Setter for the field <code>conceptGroup</code>.
728 * @param conceptGroup
730 * {@link eu.etaxonomy.taxeditor.editor.name.container.ConceptGroup}
733 public void setConceptGroup(ConceptGroup conceptGroup
) {
734 this.conceptGroup
= conceptGroup
;
739 * setMisapplicationsGroup
742 * @param misappliedGroup
744 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
747 public void setMisapplicationsGroup(MisappliedGroup misappliedGroup
) {
748 this.misappliedGroup
= misappliedGroup
;
755 * eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage#isRedrawing()
764 public boolean isRedrawing() {
773 * @return a {@link org.eclipse.ui.forms.widgets.FormToolkit} object.
775 public FormToolkit
getToolkit() {
776 return managedForm
.getToolkit();
781 * getHeterotypicSynonymGroups
784 * @return a {@link java.util.List} object.
786 public List
<HomotypicalSynonymGroup
> getHeterotypicSynonymGroups() {
787 return heterotypicSynonymGroups
;
792 * addHeterotypicSynonymGroup
797 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
800 public void addHeterotypicSynonymGroup(HomotypicalSynonymGroup group
) {
801 heterotypicSynonymGroups
.add(group
);
806 * getHomotypicSynonymGroup
810 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
813 public AcceptedGroup
getAcceptedGroup() {
814 return acceptedGroup
;
819 * @param acceptedGroup
821 public void setAcceptedGroup(AcceptedGroup acceptedGroup
) {
822 this.acceptedGroup
= acceptedGroup
;
827 * Getter for the field <code>misappliedGroup</code>.
831 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
834 public MisappliedGroup
getMisappliedGroup() {
835 return misappliedGroup
;
838 // TODO not very useful at the moment
846 public boolean isActive() {
847 IWorkbenchPart activePart
= EditorUtil
.getActivePart();
848 return editor
.equals(activePart
);
858 public boolean onComplete() {
859 getContainer(objectAffectedByLastOperation
).setSelected();
864 public void partChanged(Integer eventType
, IWorkbenchPartReference partRef
) {
865 if (!partRef
.getPart(false).equals(editor
)) {
866 // getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
871 * @param retainedGroup
873 public void removeGroup(AbstractGroup group
) {
876 getHeterotypicSynonymGroups().remove(group
);
884 public AbstractGroupedContainer
getContainer(TaxonBase taxonBase
) {
885 List
<AbstractGroupedContainer
> groupedContainers
= getGroupedContainers();
886 for (AbstractGroupedContainer container
: groupedContainers
) {
887 if (container
.getData().equals(taxonBase
)
888 && container
.getNameViewer().getTextWidget() != null) {
892 return getAcceptedNameContainer();