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
.IProgressMonitor
;
20 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
21 import org
.eclipse
.jface
.action
.MenuManager
;
22 import org
.eclipse
.jface
.viewers
.ISelection
;
23 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
24 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
25 import org
.eclipse
.swt
.dnd
.DND
;
26 import org
.eclipse
.swt
.dnd
.DropTarget
;
27 import org
.eclipse
.swt
.dnd
.Transfer
;
28 import org
.eclipse
.swt
.graphics
.Color
;
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
.PlatformUI
;
39 import org
.eclipse
.ui
.forms
.ManagedForm
;
40 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
41 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
42 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
43 import org
.eclipse
.ui
.part
.EditorPart
;
45 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
46 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
47 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
48 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
49 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
53 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
54 import eu
.etaxonomy
.taxeditor
.editor
.CdmDataTransfer
;
55 import eu
.etaxonomy
.taxeditor
.editor
.IDropTargetable
;
56 import eu
.etaxonomy
.taxeditor
.editor
.IMultiPageTaxonEditorPage
;
57 import eu
.etaxonomy
.taxeditor
.editor
.ISecuredEditor
;
58 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
59 import eu
.etaxonomy
.taxeditor
.editor
.SimpleSelectionProvider
;
60 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
61 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroup
;
62 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AbstractGroupedContainer
;
63 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedGroup
;
64 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.AcceptedNameContainer
;
65 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.ContainerFactory
;
66 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.HomotypicalSynonymGroup
;
67 import eu
.etaxonomy
.taxeditor
.editor
.name
.container
.MisappliedGroup
;
68 import eu
.etaxonomy
.taxeditor
.editor
.name
.dnd
.NameEditorDropTargetListener
;
69 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
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
.preference
.Resources
;
74 import eu
.etaxonomy
.taxeditor
.security
.RequiredPermissions
;
75 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
76 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
80 * TaxonNameEditor class.
88 public class TaxonNameEditor
extends EditorPart
implements
89 IMultiPageTaxonEditorPage
, IConversationEnabled
,
90 IPartContentHasDetails
, IPartChangeListener
,
91 ISelectionListener
, IDropTargetable
, ISecuredEditor
{
93 /** Constant <code>ID="eu.etaxonomy.taxeditor.editor.taxon.name"</code> */
94 public static final String ID
= "eu.etaxonomy.taxeditor.editor.taxon.name";
98 private ManagedForm managedForm
;
99 private ScrolledForm scrolledForm
;
100 private Composite parent
;
101 private ISelectionProvider simpleSelectionProvider
;
103 private final MultiPageTaxonEditor editor
;
105 private TaxonBase selection
;
107 private final ConversationHolder conversation
;
109 private MenuManager menuManager
;
112 private AcceptedGroup acceptedGroup
;
113 private List
<HomotypicalSynonymGroup
> heterotypicSynonymGroups
= new ArrayList
<HomotypicalSynonymGroup
>();
114 private MisappliedGroup misappliedGroup
;
116 private DropTarget target
;
118 private ISelectionService selectionService
;
120 private TaxonBase objectAffectedByLastOperation
;
122 private ICdmEntitySession cdmEntitySession
;
126 * Constructor for TaxonNameEditor.
130 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
133 public TaxonNameEditor(MultiPageTaxonEditor editor
) {
134 this.editor
= editor
;
135 conversation
= editor
.getConversationHolder();
143 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
146 public IUndoContext
getUndoContext() {
147 return editor
.getUndoContext();
154 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
159 public void createPartControl(Composite composite
) {
160 selectionService
= getSite().getWorkbenchWindow().getSelectionService();
161 selectionService
.addSelectionListener(this);
163 createManagedForm(composite
);
165 TaxeditorPartService
.getInstance().addListener(
166 TaxeditorPartService
.PART_ACTIVATED
, this);
175 * a {@link org.eclipse.swt.widgets.Composite} object.
177 protected void createManagedForm(Composite composite
) {
179 managedForm
= new ManagedForm(composite
) {
182 public void dirtyStateChanged() {
183 firePropertyChange(PROP_DIRTY
);
187 public boolean setInput(Object input
) {
188 if (input
instanceof AbstractGroupedContainer
) {
189 selection
= ((AbstractGroupedContainer
) input
).getData();
190 getSite().getSelectionProvider().setSelection(new StructuredSelection(selection
));
191 }else if(input
== null){
193 getSite().getSelectionProvider().setSelection(new StructuredSelection());
197 return super.setInput(input
);
201 scrolledForm
= managedForm
.getForm();
202 parent
= scrolledForm
.getBody();
204 // register the context menu
205 menuManager
= new MenuManager();
206 ISelectionProvider selectionProvider
= getSite().getSelectionProvider();
207 getSite().registerContextMenu(TaxonNameEditor
.ID
, menuManager
,
210 parent
.setData(taxon
);
212 TableWrapLayout layout
= new TableWrapLayout();
213 layout
.leftMargin
= 0;
214 layout
.rightMargin
= 0;
215 layout
.topMargin
= 0;
216 layout
.bottomMargin
= 0;
218 layout
.verticalSpacing
= 0;
219 layout
.horizontalSpacing
= 0;
221 parent
.setLayout(layout
);
222 parent
.setBackground(AbstractUtility
223 .getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
225 createOrUpdateNameComposites();
234 * createNameComposites
237 public void createOrUpdateNameComposites() {
238 // this.taxon = (Taxon)CdmStore.getService(ITaxonService.class).load(this.getTaxon().getUuid());
239 ContainerFactory
.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
240 ContainerFactory
.createOrUpdateHeterotypicSynonymyGroups(this);
241 ContainerFactory
.createOrUpdateMisapplicationsGroup(this);
244 managedForm
.reflow(true);
249 * Getter for the field <code>taxon</code>.
252 * @return a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
254 public Taxon
getTaxon() {
255 return (Taxon
) HibernateProxyHelper
.deproxy(taxon
);
263 public void setDirty() {
264 managedForm
.dirtyStateChanged();
270 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
274 public void setFocus() {
275 PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell().setFocus();
276 if (getSelectedContainer() == null) {
277 throw new IllegalStateException(
278 "There should always be a selected object.");
280 getSelectedContainer().setSelected();
283 boolean doEnable
= permissionsSatisfied();
284 setEnabled(doEnable
);
288 * @see eu.etaxonomy.taxeditor.editor.name.ISecuredEditorPart#permissionsSatisfied()
291 public boolean permissionsSatisfied() {
292 TaxonNode taxonNode
= ((TaxonEditorInput
)getEditorInput()).getTaxonNode();
293 boolean doEnable
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, RequiredPermissions
.TAXONNODE_EDIT
);
299 public void setInput(IEditorInput input
) {
300 this.setInputWithNotify(input
);
307 * eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder
312 * getConversationHolder
315 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
319 public ConversationHolder
getConversationHolder() {
327 * eu.etaxonomy.cdm.persistence.hibernate.ICdmPostCrudObserver#update(eu
328 * .etaxonomy.cdm.persistence.hibernate.CdmCrudEvent)
332 public void update(CdmDataChangeMap events
) {
337 * Redraws this editor return true on success
342 public boolean redraw() {
349 * Redraws the editor controls
352 public boolean redraw(boolean focus
) {
354 createOrUpdateNameComposites();
365 * getMultiPageTaxonEditor
368 * @return a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
371 public MultiPageTaxonEditor
getMultiPageTaxonEditor() {
379 * eu.etaxonomy.taxeditor.store.operations.IPostOperationEnabled#postOperation
384 public boolean postOperation(CdmBase objectAffectedByOperation
) {
386 editor
.changed(objectAffectedByOperation
);
390 if (objectAffectedByOperation
instanceof TaxonBase
) {
391 objectAffectedByLastOperation
= (TaxonBase
) objectAffectedByOperation
;
399 * Getter for the field <code>managedForm</code>.
402 * @return the managedForm
404 public ManagedForm
getManagedForm() {
413 * @return a {@link org.eclipse.swt.widgets.Composite} object.
416 public Composite
getControl() {
417 return this.getManagedForm().getForm().getBody();
422 * Getter for the field <code>menu</code>.
427 public Menu
getMenu() {
428 if (menu
== null || menu
.isDisposed()) {
429 // Creating the menu because it was either not initialised or
430 // disposed while refreshing the editor
431 menu
= menuManager
.createContextMenu(parent
);
441 * @return true if there are empty names
443 public boolean checkForEmptyNames() {
444 for (AbstractGroupedContainer container
: getGroupedContainers()) {
445 if (container
.getName() == null
446 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
453 public Set
<AbstractGroupedContainer
> getEmptyContainers() {
454 Set
<AbstractGroupedContainer
> containersWithEmptyNames
= new HashSet
<AbstractGroupedContainer
>();
456 for (AbstractGroupedContainer container
: getGroupedContainers()) {
457 if (container
.getName() == null
458 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
459 containersWithEmptyNames
.add(container
);
463 return containersWithEmptyNames
;
468 public void doSave(IProgressMonitor monitor
) {
470 monitor
.beginTask("Saving names", getGroupedContainers().size());
473 // check for empty names
474 for (AbstractGroupedContainer container
: getGroupedContainers()) {
476 monitor
.subTask("Saving composite: "
477 + container
.getTaxonBase().getTitleCache());
478 container
.persistName();
480 // In case the progress monitor was canceled throw an exception.
481 if (monitor
.isCanceled()) {
482 throw new OperationCanceledException();
485 // Otherwise declare this step as done.
490 // Stop the progress monitor.
498 public void doSaveAs() {
503 public void init(IEditorSite site
, IEditorInput input
)
504 throws PartInitException
{
506 if (!(input
!= null)) {
507 throw new PartInitException("Invalid Input: Must be IEditorInput");
510 if (input
.getAdapter(Taxon
.class) != null) {
511 taxon
= CdmBase
.deproxy(input
.getAdapter(Taxon
.class), Taxon
.class);
513 throw new PartInitException("Invalid Input: Taxon cannot be null");
519 simpleSelectionProvider
= new SimpleSelectionProvider();
520 getSite().setSelectionProvider(simpleSelectionProvider
);
527 private void createDragSupport() {
528 // Listen for names being dragged outside of existing homotypic groups -
529 // user wants to create a new group
530 Transfer
[] types
= new Transfer
[] { CdmDataTransfer
.getInstance() };
531 int operations
= DND
.DROP_MOVE
;
532 if (target
== null) {
533 target
= new DropTarget(parent
, operations
);
534 target
.setTransfer(types
);
535 target
.addDropListener(new NameEditorDropTargetListener(this));
541 * Getter for the field <code>acceptedNameContainer</code>.
545 * {@link eu.etaxonomy.taxeditor.editor.name.container.AcceptedNameContainer}
548 public AcceptedNameContainer
getAcceptedNameContainer() {
549 return getAcceptedGroup().getAcceptedNameContainer();
557 * @param homotypicalGroup
558 * a {@link eu.etaxonomy.cdm.model.name.HomotypicalGroup} object.
560 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
563 public HomotypicalSynonymGroup
getHomotypicalGroupContainer(
564 HomotypicalGroup homotypicalGroup
) {
565 for (HomotypicalSynonymGroup group
: getHeterotypicSynonymGroups()) {
566 if (group
.getGroup().equals(homotypicalGroup
)) {
579 * @return a Set containing all composites that have been edited
581 public Set
<AbstractGroupedContainer
> getDirtyNames() {
582 Set
<AbstractGroupedContainer
> dirtyNames
= new HashSet
<AbstractGroupedContainer
>();
584 for (AbstractGroupedContainer composite
: getGroupedContainers()) {
585 if (composite
.isDirty()) {
586 dirtyNames
.add(composite
);
595 * getGroupedContainers
598 * @return a {@link java.util.List} object.
600 public List
<AbstractGroupedContainer
> getGroupedContainers() {
601 List
<AbstractGroupedContainer
> groupedComposites
= new ArrayList
<AbstractGroupedContainer
>();
603 for (AbstractGroup group
: getAllGroups()) {
604 groupedComposites
.addAll(group
.getGroupedContainers());
607 return groupedComposites
;
615 * @return a {@link java.util.List} object.
617 public List
<AbstractGroup
> getAllGroups() {
618 List
<AbstractGroup
> allGroups
= new ArrayList
<AbstractGroup
>();
620 allGroups
.add(getAcceptedGroup());
622 // List<HomotypicalSynonymGroup> grps = getHeterotypicSynonymGroups(); // UNUSED => remove
624 heterotypicSynonymGroups
= getHeterotypicSynonymGroups();
626 if (heterotypicSynonymGroups
!= null) {
627 allGroups
.addAll(heterotypicSynonymGroups
);
630 if (misappliedGroup
!= null) {
631 allGroups
.add(misappliedGroup
);
640 * @see org.eclipse.ui.part.EditorPart#isDirty()
644 public boolean isDirty() {
645 return editor
.isDirty();
651 * @see org.eclipse.ui.part.WorkbenchPart#dispose()
655 public void dispose() {
656 conversation
.unregisterForDataStoreChanges(this);
663 * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed()
667 public boolean isSaveAsAllowed() {
674 * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.
675 * IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
679 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
688 * @return a {@link eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor}
692 public TaxonNameEditor
getEditor() {
698 * Getter for the field <code>selectedObject</code>.
702 * {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer}
705 public AbstractGroupedContainer
getSelectedContainer() {
707 TaxonBase selectedTaxonBase
= null;
709 TaxonEditorInput input
= (TaxonEditorInput
) editor
.getEditorInput();
710 if (input
.getInitiallySelectedTaxonBase() != null) {
711 selectedTaxonBase
= input
.getInitiallySelectedTaxonBase();
713 if (selection
!= null) {
714 selectedTaxonBase
= selection
;
718 return (selectedTaxonBase
!= null) ?
getContainer(selectedTaxonBase
)
719 : getAcceptedNameContainer();
728 public void dragEntered() {
730 getControl().setBackground(
731 AbstractUtility
.getColor(Resources
.COLOR_DRAG_ENTER
));
740 public void dragLeft() {
741 getControl().setBackground(
742 AbstractUtility
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
748 * setMisapplicationsGroup
751 * @param misappliedGroup
753 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
756 public void setMisapplicationsGroup(MisappliedGroup misappliedGroup
) {
757 this.misappliedGroup
= misappliedGroup
;
764 * eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage#isRedrawing()
774 public boolean isRedrawing() {
783 * @return a {@link org.eclipse.ui.forms.widgets.FormToolkit} object.
785 public FormToolkit
getToolkit() {
786 return managedForm
.getToolkit();
791 * getHeterotypicSynonymGroups
794 * @return a {@link java.util.List} object.
796 public List
<HomotypicalSynonymGroup
> getHeterotypicSynonymGroups() {
797 return heterotypicSynonymGroups
;
802 * addHeterotypicSynonymGroup
807 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
810 public void addHeterotypicSynonymGroup(HomotypicalSynonymGroup group
) {
811 heterotypicSynonymGroups
.add(group
);
816 * getHomotypicSynonymGroup
820 * {@link eu.etaxonomy.taxeditor.editor.name.container.HomotypicalSynonymGroup}
823 public AcceptedGroup
getAcceptedGroup() {
824 return acceptedGroup
;
829 * @param acceptedGroup
831 public void setAcceptedGroup(AcceptedGroup acceptedGroup
) {
832 this.acceptedGroup
= acceptedGroup
;
837 * Getter for the field <code>misappliedGroup</code>.
841 * {@link eu.etaxonomy.taxeditor.editor.name.container.MisappliedGroup}
844 public MisappliedGroup
getMisappliedGroup() {
845 return misappliedGroup
;
848 // TODO not very useful at the moment
856 public boolean isActive() {
857 IWorkbenchPart activePart
= AbstractUtility
.getActivePart();
858 return editor
.equals(activePart
);
869 public boolean onComplete() {
870 getContainer(objectAffectedByLastOperation
).setSelected();
876 public void partChanged(Integer eventType
, IWorkbenchPartReference partRef
) {
877 if (!partRef
.getPart(false).equals(editor
)) {
878 // getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
883 * @param retainedGroup
885 public void removeGroup(AbstractGroup group
) {
889 //if (heterotypicSynonymGroups != null) {
890 heterotypicSynonymGroups
.remove(group
);
899 public AbstractGroupedContainer
getContainer(TaxonBase taxonBase
) {
900 List
<AbstractGroupedContainer
> groupedContainers
= getGroupedContainers();
901 for (AbstractGroupedContainer container
: groupedContainers
) {
902 if (container
.getData().equals(taxonBase
)
903 && container
.getNameViewer().getTextWidget() != null) {
907 return getAcceptedNameContainer();
911 * @see eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage#setOnError()
914 public void setOnError() {
915 Color disabledColor
= AbstractUtility
.getColor(Resources
.COLOR_EDITOR_ERROR
);
916 setEnabled(false, disabledColor
);
920 * @see eu.etaxonomy.taxeditor.editor.IMultiPageTaxonEditorPage#setDisabled()
923 public void setDisabled(){
924 Color disabledColor
= AbstractUtility
.getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
);
928 protected void setEnabled(boolean enabled
, Color background
) {
930 for(AbstractGroupedContainer groupedContainer
: getGroupedContainers()){
931 groupedContainer
.setEnabled(enabled
);
934 // send an empty selection to the current provider - TODO only on error ???
936 getManagedForm().setInput(null);
938 for (AbstractGroupedContainer groupedContainer
: getGroupedContainers()) {
939 groupedContainer
.setBackground(background
);
942 getControl().setBackground(background
);
946 public void setEnabled(boolean enabled
) {
947 Color background
= AbstractUtility
.getColor(enabled ? Resources
.COLOR_COMPOSITE_BACKGROUND
: Resources
.COLOR_TEXT_DISABLED_BACKGROUND
);
948 setEnabled(enabled
, background
);