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
.SWT
;
27 import org
.eclipse
.swt
.dnd
.DND
;
28 import org
.eclipse
.swt
.dnd
.DropTarget
;
29 import org
.eclipse
.swt
.dnd
.Transfer
;
30 import org
.eclipse
.swt
.graphics
.Color
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Display
;
33 import org
.eclipse
.swt
.widgets
.Menu
;
34 import org
.eclipse
.ui
.IEditorInput
;
35 import org
.eclipse
.ui
.IEditorSite
;
36 import org
.eclipse
.ui
.ISelectionListener
;
37 import org
.eclipse
.ui
.ISelectionService
;
38 import org
.eclipse
.ui
.IWorkbenchPart
;
39 import org
.eclipse
.ui
.IWorkbenchPartReference
;
40 import org
.eclipse
.ui
.PartInitException
;
41 import org
.eclipse
.ui
.forms
.ManagedForm
;
42 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
43 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
44 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
45 import org
.eclipse
.ui
.part
.EditorPart
;
47 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
48 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
49 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
50 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
51 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
53 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
54 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
55 import eu
.etaxonomy
.taxeditor
.editor
.CdmDataTransfer
;
56 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
57 import eu
.etaxonomy
.taxeditor
.editor
.IDropTargetable
;
58 import eu
.etaxonomy
.taxeditor
.editor
.IMultiPageTaxonEditorPage
;
59 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
60 import eu
.etaxonomy
.taxeditor
.editor
.SimpleSelectionProvider
;
61 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
62 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroup
;
63 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
64 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedGroup
;
65 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedNameContainer
;
66 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.ContainerFactory
;
67 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.HomotypicalSynonymGroup
;
68 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.MisappliedGroup
;
69 import eu
.etaxonomy
.taxeditor
.editor
.name
.dnd
.NameEditorDropTargetListener
;
70 import eu
.etaxonomy
.taxeditor
.model
.IPartChangeListener
;
71 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
72 import eu
.etaxonomy
.taxeditor
.model
.TaxeditorPartService
;
73 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
74 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
78 * TaxonNameEditor class.
86 public class TaxonNameEditor
extends EditorPart
implements
87 IMultiPageTaxonEditorPage
, IAdaptable
, IConversationEnabled
,
88 IPostOperationEnabled
, IPartContentHasDetails
, IPartChangeListener
,
89 ISelectionListener
, IDropTargetable
{
91 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon.name"</code> */
92 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon.name";
96 private ManagedForm managedForm
;
97 private ScrolledForm scrolledForm
;
98 private Composite parent
;
99 private ISelectionProvider simpleSelectionProvider
;
101 private final MultiPageTaxonEditor editor
;
103 private TaxonBase selection
;
105 private final ConversationHolder conversation
;
107 private MenuManager menuManager
;
110 private AcceptedGroup acceptedGroup
;
111 private List
<HomotypicalSynonymGroup
> heterotypicSynonymGroups
= new ArrayList
<HomotypicalSynonymGroup
>();
112 private MisappliedGroup misappliedGroup
;
114 private DropTarget target
;
116 private ISelectionService selectionService
;
118 private TaxonBase objectAffectedByLastOperation
;
122 * Constructor for TaxonNameEditor.
126 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
129 public TaxonNameEditor(MultiPageTaxonEditor editor
) {
130 this.editor
= editor
;
131 conversation
= editor
.getConversationHolder();
139 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
142 public IUndoContext
getUndoContext() {
143 return editor
.getUndoContext();
150 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
155 public void createPartControl(Composite composite
) {
156 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
157 selectionService
.addSelectionListener(this);
159 createManagedForm(composite
);
161 TaxeditorPartService
.getInstance().addListener(
162 TaxeditorPartService
.PART_ACTIVATED
, this);
171 * a {@link org.eclipse.swt.widgets.Composite} object.
173 protected void createManagedForm(Composite composite
) {
175 managedForm
= new ManagedForm(composite
) {
178 public void dirtyStateChanged() {
179 firePropertyChange(PROP_DIRTY
);
183 public boolean setInput(Object input
) {
184 if (input
instanceof AbstractGroupedContainer
) {
185 selection
= ((AbstractGroupedContainer
) input
).getData();
186 getSite().getSelectionProvider().setSelection(new StructuredSelection(selection
));
187 }else if(input
== null){
189 getSite().getSelectionProvider().setSelection(new StructuredSelection());
193 return super.setInput(input
);
197 scrolledForm
= managedForm
.getForm();
198 parent
= scrolledForm
.getBody();
200 // register the context menu
201 menuManager
= new MenuManager();
202 ISelectionProvider selectionProvider
= getSite().getSelectionProvider();
203 getSite().registerContextMenu(TaxonNameEditor
.ID
, menuManager
,
206 parent
.setData(taxon
);
208 TableWrapLayout layout
= new TableWrapLayout();
209 layout
.leftMargin
= 0;
210 layout
.rightMargin
= 0;
211 layout
.topMargin
= 0;
212 layout
.bottomMargin
= 0;
214 layout
.verticalSpacing
= 0;
215 layout
.horizontalSpacing
= 0;
217 parent
.setLayout(layout
);
218 parent
.setBackground(EditorUtil
219 .getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
221 createOrUpdateNameComposites();
230 * createNameComposites
233 public void createOrUpdateNameComposites() {
235 ContainerFactory
.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
236 ContainerFactory
.createOrUpdateHeterotypicSynonymyGroups(this);
237 ContainerFactory
.createOrUpdateMisapplicationsGroup(this);
240 managedForm
.reflow(true);
245 * Getter for the field <code>taxon</code>.
248 * @return a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
250 public Taxon
getTaxon() {
251 return (Taxon
) HibernateProxyHelper
.deproxy(taxon
);
259 public void setDirty() {
260 managedForm
.dirtyStateChanged();
266 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
270 public void setFocus() {
271 if (getSelectedContainer() == null) {
272 throw new IllegalStateException(
273 "There should always be a selected object.");
275 getSelectedContainer().setSelected();
280 public void setInput(IEditorInput input
) {
281 this.setInputWithNotify(input
);
288 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
293 * getConversationHolder
296 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
299 public ConversationHolder
getConversationHolder() {
307 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
308 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
311 public void update(CdmDataChangeMap events
) {
316 * Redraws this editor return true on success
320 public boolean redraw() {
327 * Redraws the editor controls
329 public boolean redraw(boolean focus
) {
331 createOrUpdateNameComposites();
342 * getMultiPageTaxonEditor
345 * @return a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
348 public MultiPageTaxonEditor
getMultiPageTaxonEditor() {
356 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
360 public boolean postOperation(CdmBase objectAffectedByOperation
) {
362 editor
.changed(objectAffectedByOperation
);
366 if (objectAffectedByOperation
instanceof TaxonBase
) {
367 objectAffectedByLastOperation
= (TaxonBase
) objectAffectedByOperation
;
375 * Getter for the field <code>managedForm</code>.
378 * @return the managedForm
380 public ManagedForm
getManagedForm() {
389 * @return a {@link org.eclipse.swt.widgets.Composite} object.
391 public Composite
getControl() {
392 return this.getManagedForm().getForm().getBody();
397 * Getter for the field <code>menu</code>.
402 public Menu
getMenu() {
403 if (menu
== null || menu
.isDisposed()) {
404 // Creating the menu because it was either not initialised or
405 // disposed while refreshing the editor
406 menu
= menuManager
.createContextMenu(parent
);
416 * @return true if there are empty names
418 public boolean checkForEmptyNames() {
419 for (AbstractGroupedContainer container
: getGroupedContainers()) {
420 if (container
.getName() == null
421 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
428 public Set
<AbstractGroupedContainer
> getEmptyContainers() {
429 Set
<AbstractGroupedContainer
> containersWithEmptyNames
= new HashSet
<AbstractGroupedContainer
>();
431 for (AbstractGroupedContainer container
: getGroupedContainers()) {
432 if (container
.getName() == null
433 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
434 containersWithEmptyNames
.add(container
);
438 return containersWithEmptyNames
;
443 public void doSave(IProgressMonitor monitor
) {
445 monitor
.beginTask("Saving names", getGroupedContainers().size());
448 // check for empty names
449 for (AbstractGroupedContainer container
: getGroupedContainers()) {
451 monitor
.subTask("Saving composite: "
452 + container
.getTaxonBase().getTitleCache());
453 container
.persistName();
455 // In case the progress monitor was canceled throw an exception.
456 if (monitor
.isCanceled()) {
457 throw new OperationCanceledException();
460 // Otherwise declare this step as done.
465 // Stop the progress monitor.
473 public void doSaveAs() {
478 public void init(IEditorSite site
, IEditorInput input
)
479 throws PartInitException
{
481 if (!(input
instanceof IEditorInput
))
482 throw new PartInitException("Invalid Input: Must be IEditorInput");
484 if (input
.getAdapter(Taxon
.class) != null) {
485 taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
487 throw new PartInitException("Invalid Input: Taxon cannot be null");
493 simpleSelectionProvider
= new SimpleSelectionProvider();
494 getSite().setSelectionProvider(simpleSelectionProvider
);
500 private void createDragSupport() {
501 // Listen for names being dragged outside of existing homotypic groups -
502 // user wants to create a new group
503 Transfer
[] types
= new Transfer
[] { CdmDataTransfer
.getInstance() };
504 int operations
= DND
.DROP_MOVE
;
505 if (target
== null) {
506 target
= new DropTarget(parent
, operations
);
507 target
.setTransfer(types
);
508 target
.addDropListener(new NameEditorDropTargetListener(this));
514 * Getter for the field <code>acceptedNameContainer</code>.
518 * {@link eu.etaxonomy.taxeditor.editor.name.container.AcceptedNameContainer}
521 public AcceptedNameContainer
getAcceptedNameContainer() {
522 return getAcceptedGroup().getAcceptedNameContainer();
530 * @param homotypicalGroup
531 * a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
533 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
536 public HomotypicalSynonymGroup
getHomotypicalGroupContainer(
537 HomotypicalGroup homotypicalGroup
) {
538 for (HomotypicalSynonymGroup group
: getHeterotypicSynonymGroups()) {
539 if (group
.getGroup().equals(homotypicalGroup
)) {
552 * @return a Set containing all composites that have been edited
554 public Set
<AbstractGroupedContainer
> getDirtyNames() {
555 Set
<AbstractGroupedContainer
> dirtyNames
= new HashSet
<AbstractGroupedContainer
>();
557 for (AbstractGroupedContainer composite
: getGroupedContainers()) {
558 if (composite
.isDirty()) {
559 dirtyNames
.add(composite
);
568 * getGroupedContainers
571 * @return a {@link java.util.List} object.
573 public List
<AbstractGroupedContainer
> getGroupedContainers() {
574 List
<AbstractGroupedContainer
> groupedComposites
= new ArrayList
<AbstractGroupedContainer
>();
576 for (AbstractGroup group
: getAllGroups()) {
577 groupedComposites
.addAll(group
.getGroupedContainers());
580 return groupedComposites
;
588 * @return a {@link java.util.List} object.
590 public List
<AbstractGroup
> getAllGroups() {
591 List
<AbstractGroup
> allGroups
= new ArrayList
<AbstractGroup
>();
593 allGroups
.add(getAcceptedGroup());
595 List
<HomotypicalSynonymGroup
> grps
= getHeterotypicSynonymGroups();
597 heterotypicSynonymGroups
= getHeterotypicSynonymGroups();
599 if (heterotypicSynonymGroups
!= null) {
600 allGroups
.addAll(heterotypicSynonymGroups
);
603 if (misappliedGroup
!= null) {
604 allGroups
.add(misappliedGroup
);
613 * @see org.eclipse.ui.part.EditorPart#isDirty()
617 public boolean isDirty() {
618 return editor
.isDirty();
624 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
628 public void dispose() {
635 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
639 public boolean isSaveAsAllowed() {
646 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.
647 * IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
650 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
659 * @return a {@link eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor}
662 public TaxonNameEditor
getEditor() {
668 * Getter for the field <code>selectedObject</code>.
672 * {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer}
675 public AbstractGroupedContainer
getSelectedContainer() {
677 TaxonBase selectedTaxonBase
= null;
679 TaxonEditorInput input
= (TaxonEditorInput
) editor
.getEditorInput();
680 if (input
.getInitiallySelectedTaxonBase() != null) {
681 selectedTaxonBase
= input
.getInitiallySelectedTaxonBase();
683 if (selection
!= null) {
684 selectedTaxonBase
= selection
;
688 return (selectedTaxonBase
!= null) ?
getContainer(selectedTaxonBase
)
689 : getAcceptedNameContainer();
697 public void dragEntered() {
699 getControl().setBackground(
700 EditorUtil
.getColor(Resources
.COLOR_DRAG_ENTER
));
708 public void dragLeft() {
709 getControl().setBackground(
710 EditorUtil
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
716 * setMisapplicationsGroup
719 * @param misappliedGroup
721 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
724 public void setMisapplicationsGroup(MisappliedGroup misappliedGroup
) {
725 this.misappliedGroup
= misappliedGroup
;
732 * eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage#isRedrawing()
741 public boolean isRedrawing() {
750 * @return a {@link org.eclipse.ui.forms.widgets.FormToolkit} object.
752 public FormToolkit
getToolkit() {
753 return managedForm
.getToolkit();
758 * getHeterotypicSynonymGroups
761 * @return a {@link java.util.List} object.
763 public List
<HomotypicalSynonymGroup
> getHeterotypicSynonymGroups() {
764 return heterotypicSynonymGroups
;
769 * addHeterotypicSynonymGroup
774 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
777 public void addHeterotypicSynonymGroup(HomotypicalSynonymGroup group
) {
778 heterotypicSynonymGroups
.add(group
);
783 * getHomotypicSynonymGroup
787 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
790 public AcceptedGroup
getAcceptedGroup() {
791 return acceptedGroup
;
796 * @param acceptedGroup
798 public void setAcceptedGroup(AcceptedGroup acceptedGroup
) {
799 this.acceptedGroup
= acceptedGroup
;
804 * Getter for the field <code>misappliedGroup</code>.
808 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
811 public MisappliedGroup
getMisappliedGroup() {
812 return misappliedGroup
;
815 // TODO not very useful at the moment
823 public boolean isActive() {
824 IWorkbenchPart activePart
= EditorUtil
.getActivePart();
825 return editor
.equals(activePart
);
835 public boolean onComplete() {
836 getContainer(objectAffectedByLastOperation
).setSelected();
841 public void partChanged(Integer eventType
, IWorkbenchPartReference partRef
) {
842 if (!partRef
.getPart(false).equals(editor
)) {
843 // getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
848 * @param retainedGroup
850 public void removeGroup(AbstractGroup group
) {
854 //if (heterotypicSynonymGroups != null) {
855 heterotypicSynonymGroups
.remove(group
);
864 public AbstractGroupedContainer
getContainer(TaxonBase taxonBase
) {
865 List
<AbstractGroupedContainer
> groupedContainers
= getGroupedContainers();
866 for (AbstractGroupedContainer container
: groupedContainers
) {
867 if (container
.getData().equals(taxonBase
)
868 && container
.getNameViewer().getTextWidget() != null) {
872 return getAcceptedNameContainer();
876 public void setDisabled(){
877 for(AbstractGroupedContainer groupedContainer
: getGroupedContainers()){
878 groupedContainer
.setDisabled(true);
881 // send an empty selection to the current provider
882 getManagedForm().setInput(null);
884 // coloring the widgets
886 // TODO move this to resources and make configurable via Preferences
887 Color disabledColor
= Display
.getDefault().getSystemColor(SWT
.COLOR_RED
);
889 for(AbstractGroupedContainer groupedContainer
: getGroupedContainers()){
890 groupedContainer
.setBackground(disabledColor
);
892 getControl().setBackground(disabledColor
);
896 public void setEnabled(boolean enabled
) {
897 for(AbstractGroupedContainer groupedContainer
: getGroupedContainers()){
898 groupedContainer
.setEnabled(getManagedForm().setInput(selection
));
901 // send an empty selection to the current provider
902 getManagedForm().setInput(null);