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
.e4
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 import javax
.annotation
.PostConstruct
;
18 import javax
.annotation
.PreDestroy
;
19 import javax
.inject
.Inject
;
21 import org
.apache
.commons
.lang
.StringUtils
;
22 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
23 import org
.eclipse
.core
.commands
.operations
.UndoContext
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.OperationCanceledException
;
26 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
27 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
28 import org
.eclipse
.e4
.ui
.di
.Focus
;
29 import org
.eclipse
.e4
.ui
.di
.Persist
;
30 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
31 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
32 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
33 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
34 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
35 import org
.eclipse
.jface
.viewers
.ISelection
;
36 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
37 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
38 import org
.eclipse
.swt
.dnd
.DND
;
39 import org
.eclipse
.swt
.dnd
.DropTarget
;
40 import org
.eclipse
.swt
.dnd
.Transfer
;
41 import org
.eclipse
.swt
.graphics
.Color
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.ui
.ISelectionListener
;
44 import org
.eclipse
.ui
.IWorkbenchPart
;
45 import org
.eclipse
.ui
.IWorkbenchPartReference
;
46 import org
.eclipse
.ui
.forms
.ManagedForm
;
47 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
48 import org
.eclipse
.ui
.forms
.widgets
.ScrolledForm
;
49 import org
.eclipse
.ui
.forms
.widgets
.TableWrapLayout
;
51 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
52 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
53 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
54 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
55 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
56 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
57 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
58 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
59 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
60 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
61 import eu
.etaxonomy
.taxeditor
.editor
.CdmDataTransfer
;
62 import eu
.etaxonomy
.taxeditor
.editor
.ISecuredEditor
;
63 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
64 import eu
.etaxonomy
.taxeditor
.editor
.e4
.TaxonEditorInputE4
;
65 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
66 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.AbstractGroupE4
;
67 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.AbstractGroupedContainerE4
;
68 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.AcceptedGroupE4
;
69 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.AcceptedNameContainerE4
;
70 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.ContainerFactoryE4
;
71 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.HomotypicalSynonymGroupE4
;
72 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.container
.MisappliedGroupE4
;
73 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.dnd
.NameEditorDropTargetListenerE4
;
74 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
75 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
76 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
77 import eu
.etaxonomy
.taxeditor
.model
.IPartChangeListener
;
78 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
79 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
80 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
81 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
82 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
83 import eu
.etaxonomy
.taxeditor
.model
.TaxeditorPartService
;
84 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
85 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
86 import eu
.etaxonomy
.taxeditor
.security
.RequiredPermissions
;
87 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
88 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
96 public class TaxonNameEditorE4
implements IConversationEnabled
, IDirtyMarkable
, IPartContentHasDetails
,
97 IPartContentHasSupplementalData
, IPartContentHasMedia
, IPartContentHasFactualData
, IPartChangeListener
,
98 ISelectionListener
, ISecuredEditor
, IPostOperationEnabled
, IE4SavablePart
, ITaxonEditor
, IDropTargetableE4
{
102 private ManagedForm managedForm
;
103 private ScrolledForm scrolledForm
;
104 private Composite parent
;
105 private ISelectionProvider simpleSelectionProvider
;
107 private TaxonBase selection
;
109 private ConversationHolder conversation
;
111 private AcceptedGroupE4 acceptedGroup
;
112 private List
<HomotypicalSynonymGroupE4
> heterotypicSynonymGroups
= new ArrayList
<>();
113 private MisappliedGroupE4 misappliedGroup
;
115 private DropTarget target
;
117 private TaxonBase objectAffectedByLastOperation
;
120 private EMenuService menuService
;
123 private ESelectionService selService
;
126 private IEclipseContext context
;
129 private MDirtyable dirty
;
131 private MPart thisPart
;
133 private TaxonEditorInputE4 input
;
135 private UndoContext undoContext
;
138 private IEventBroker eventBroker
;
141 public TaxonNameEditorE4() {
142 undoContext
= new UndoContext();
147 public void createPartControl(Composite parent
, MPart thisPart
) {
148 this.thisPart
= thisPart
;
150 createManagedForm(parent
);
152 TaxeditorPartService
.getInstance().addListener(
153 TaxeditorPartService
.PART_ACTIVATED
, this);
157 protected void createManagedForm(Composite composite
) {
159 managedForm
= new ManagedForm(composite
) {
162 public void dirtyStateChanged() {
163 dirty
.setDirty(true);
167 public boolean setInput(Object input
) {
168 if (input
instanceof AbstractGroupedContainerE4
) {
169 TaxonBase newSelection
= ((AbstractGroupedContainerE4
) input
).getData();
170 if(selection
!=newSelection
|| TaxonNameEditorE4
.this.isDirty()){
171 selection
= newSelection
;
172 selService
.setSelection(new StructuredSelection(selection
));
174 }else if(input
== null){
176 selService
.setSelection(new StructuredSelection());
180 return super.setInput(input
);
184 scrolledForm
= managedForm
.getForm();
185 parent
= scrolledForm
.getBody();
187 parent
.setData(taxon
);
189 TableWrapLayout layout
= new TableWrapLayout();
190 layout
.leftMargin
= 0;
191 layout
.rightMargin
= 0;
192 layout
.topMargin
= 0;
193 layout
.bottomMargin
= 0;
195 layout
.verticalSpacing
= 0;
196 layout
.horizontalSpacing
= 0;
198 parent
.setLayout(layout
);
199 parent
.setBackground(AbstractUtility
200 .getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
203 public void createOrUpdateNameComposites() {
204 ContainerFactoryE4
.createOrUpdateAcceptedTaxonsHomotypicGroup(this);
205 ContainerFactoryE4
.createOrUpdateHeterotypicSynonymyGroups(this);
206 ContainerFactoryE4
.createOrUpdateMisapplicationsGroup(this);
210 managedForm
.reflow(true);
211 managedForm
.refresh();
215 public Taxon
getTaxon() {
216 return HibernateProxyHelper
.deproxy(taxon
);
219 public void setDirty() {
220 managedForm
.dirtyStateChanged();
224 public void setFocus() {
225 //make sure to bind again if maybe in another view the conversation was unbound
226 if(conversation
!=null && !conversation
.isBound()){
230 if (getSelectedContainer() == null) {
231 throw new IllegalStateException(
232 Messages
.TaxonNameEditor_THERE_SHOULD_ALWAYS_BE
);
234 getSelectedContainer().setSelected();
237 boolean doEnable
= permissionsSatisfied();
238 managedForm
.getForm().setEnabled(doEnable
);
240 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, this);
244 public boolean permissionsSatisfied() {
245 TaxonNode taxonNode
= input
.getTaxonNode();
246 boolean doEnable
= CdmStore
.currentAuthentiationHasPermission(taxonNode
, RequiredPermissions
.TAXONNODE_EDIT
);
251 public ConversationHolder
getConversationHolder() {
257 public void update(CdmDataChangeMap events
) {
262 * Redraws this editor return true on success
266 public boolean redraw() {
273 * Redraws the editor controls
275 public boolean redraw(boolean focus
) {
277 createOrUpdateNameComposites();
287 public boolean postOperation(CdmBase objectAffectedByOperation
) {
289 changed(objectAffectedByOperation
);
293 if (objectAffectedByOperation
instanceof TaxonBase
) {
294 objectAffectedByLastOperation
= (TaxonBase
) objectAffectedByOperation
;
300 public ManagedForm
getManagedForm() {
310 * @return true if there are empty names
312 public boolean checkForEmptyNames() {
313 for (AbstractGroupedContainerE4 container
: getGroupedContainers()) {
314 if (container
.getName() == null
315 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
322 public Set
<AbstractGroupedContainerE4
> getEmptyContainers() {
323 Set
<AbstractGroupedContainerE4
> containersWithEmptyNames
= new HashSet
<>();
325 for (AbstractGroupedContainerE4 container
: getGroupedContainers()) {
326 if (container
.getName() == null
327 || StringUtils
.isEmpty(container
.getName().getTitleCache())) {
328 containersWithEmptyNames
.add(container
);
332 return containersWithEmptyNames
;
338 public void save(IProgressMonitor monitor
) {
340 monitor
.beginTask(Messages
.TaxonNameEditor_SAVING_NAMES
, getGroupedContainers().size());
341 if (!conversation
.isBound()) {
346 // check for empty names
347 if (checkForEmptyNames()) {
348 MessageDialog
.openWarning(AbstractUtility
.getShell(), Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED
,
349 Messages
.MultiPageTaxonEditor_NO_NAME_SPECIFIED_MESSAGE
);
352 for (AbstractGroupedContainerE4 container
: getGroupedContainers()) {
354 monitor
.subTask(Messages
.TaxonNameEditor_SAVING_COMPOSITES
355 + container
.getTaxonBase().getTitleCache());
356 container
.persistName();
358 // In case the progress monitor was canceled throw an exception.
359 if (monitor
.isCanceled()) {
360 throw new OperationCanceledException();
363 // Otherwise declare this step as done.
368 // commit the conversation and start a new transaction immediately
369 conversation
.commit(true);
372 dirty
.setDirty(false);
374 // Stop the progress monitor.
378 public void init(TaxonEditorInputE4 input
) {
380 if (!(input
!= null)) {
381 MessagingUtils
.error(this.getClass(), new Exception(Messages
.TaxonNameEditor_INVALID_INPUT
));
385 if (input
.getAdapter(Taxon
.class) != null) {
386 taxon
= CdmBase
.deproxy(input
.getAdapter(Taxon
.class), Taxon
.class);
388 MessagingUtils
.error(this.getClass(), new Exception(Messages
.TaxonNameEditor_INVALID_INPUT_TAXON_NULL
));
393 conversation
= input
.getConversationHolder();
395 createOrUpdateNameComposites();
401 //set initial selection
402 TaxonBase initiallySelectedTaxonBase
= input
.getInitiallySelectedTaxonBase();
403 if(initiallySelectedTaxonBase
!=null){
404 selService
.setSelection(new StructuredSelection(initiallySelectedTaxonBase
));
405 getContainer(initiallySelectedTaxonBase
).setSelected();
409 private void createDragSupport() {
410 // Listen for names being dragged outside of existing homotypic groups -
411 // user wants to create a new group
412 Transfer
[] types
= new Transfer
[] { CdmDataTransfer
.getInstance() };
413 int operations
= DND
.DROP_MOVE
;
414 if (target
== null) {
415 target
= new DropTarget(parent
, operations
);
416 target
.setTransfer(types
);
417 target
.addDropListener(new NameEditorDropTargetListenerE4(this));
421 public AcceptedNameContainerE4
getAcceptedNameContainer() {
422 return getAcceptedGroup().getAcceptedNameContainer();
425 public HomotypicalSynonymGroupE4
getHomotypicalGroupContainer(
426 HomotypicalGroup homotypicalGroup
) {
427 for (HomotypicalSynonymGroupE4 group
: getHeterotypicSynonymGroups()) {
428 if (group
.getGroup().equals(homotypicalGroup
)) {
441 * @return a Set containing all composites that have been edited
443 public Set
<AbstractGroupedContainerE4
> getDirtyNames() {
444 Set
<AbstractGroupedContainerE4
> dirtyNames
= new HashSet
<>();
446 for (AbstractGroupedContainerE4 composite
: getGroupedContainers()) {
447 if (composite
.isDirty()) {
448 dirtyNames
.add(composite
);
455 public List
<AbstractGroupedContainerE4
> getGroupedContainers() {
456 List
<AbstractGroupedContainerE4
> groupedComposites
= new ArrayList
<>();
458 for (AbstractGroupE4 group
: getAllGroups()) {
460 groupedComposites
.addAll(group
.getGroupedContainers());
464 return groupedComposites
;
467 public List
<AbstractGroupE4
> getAllGroups() {
468 List
<AbstractGroupE4
> allGroups
= new ArrayList
<>();
470 allGroups
.add(getAcceptedGroup());
472 heterotypicSynonymGroups
= getHeterotypicSynonymGroups();
474 if (heterotypicSynonymGroups
!= null) {
475 allGroups
.addAll(heterotypicSynonymGroups
);
478 if (misappliedGroup
!= null) {
479 allGroups
.add(misappliedGroup
);
486 public IEclipseContext
getContext() {
490 public boolean isDirty() {
491 return dirty
.isDirty();
495 public void dispose() {
496 if(conversation
!=null){
497 conversation
.unregisterForDataStoreChanges(this);
498 conversation
.close();
503 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, null);
508 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
512 public AbstractGroupedContainerE4
getSelectedContainer() {
513 return (selection
!= null) ?
getContainer(selection
)
514 : getAcceptedNameContainer();
518 public void dragEntered() {
520 getControl().setBackground(
521 AbstractUtility
.getColor(Resources
.COLOR_DRAG_ENTER
));
525 public void dragLeft() {
526 getControl().setBackground(
527 AbstractUtility
.getColor(Resources
.COLOR_COMPOSITE_BACKGROUND
));
531 public void setMisapplicationsGroup(MisappliedGroupE4 misappliedGroup
) {
532 this.misappliedGroup
= misappliedGroup
;
535 public FormToolkit
getToolkit() {
536 return managedForm
.getToolkit();
539 public List
<HomotypicalSynonymGroupE4
> getHeterotypicSynonymGroups() {
540 return heterotypicSynonymGroups
;
543 public void addHeterotypicSynonymGroup(HomotypicalSynonymGroupE4 group
) {
544 heterotypicSynonymGroups
.add(group
);
547 public AcceptedGroupE4
getAcceptedGroup() {
548 return acceptedGroup
;
551 public void setAcceptedGroup(AcceptedGroupE4 acceptedGroup
) {
552 this.acceptedGroup
= acceptedGroup
;
555 public MisappliedGroupE4
getMisappliedGroup() {
556 return misappliedGroup
;
559 public boolean isActive() {
560 return this.equals(AbstractUtility
.getActivePart());
564 public boolean onComplete() {
565 getContainer(objectAffectedByLastOperation
).setSelected();
571 public void partChanged(Integer eventType
, IWorkbenchPartReference partRef
) {
572 if (!partRef
.getPart(false).equals(this)) {
573 // getSelectedObject().colorSelected(AbstractGroupedContainer.SELECTED_NO_FOCUS);
577 public void removeGroup(AbstractGroupE4 group
) {
581 //if (heterotypicSynonymGroups != null) {
582 heterotypicSynonymGroups
.remove(group
);
587 public AbstractGroupedContainerE4
getContainer(TaxonBase taxonBase
) {
588 List
<AbstractGroupedContainerE4
> groupedContainers
= getGroupedContainers();
589 for (AbstractGroupedContainerE4 container
: groupedContainers
) {
590 if (container
.getData().equals(taxonBase
)
591 && container
.getNameViewer().getTextWidget() != null) {
595 return getAcceptedNameContainer();
598 public void setOnError() {
599 Color disabledColor
= AbstractUtility
.getColor(Resources
.COLOR_EDITOR_ERROR
);
600 setEnabled(false, disabledColor
);
603 public void setDisabled(){
604 Color disabledColor
= AbstractUtility
.getColor(Resources
.COLOR_TEXT_DISABLED_BACKGROUND
);
605 setEnabled(false, disabledColor
);
608 protected void setEnabled(boolean enabled
, Color background
) {
610 for(AbstractGroupedContainerE4 groupedContainer
: getGroupedContainers()){
611 groupedContainer
.setEnabled(enabled
);
614 // send an empty selection to the current provider - TODO only on error ???
616 getManagedForm().setInput(null);
618 for (AbstractGroupedContainerE4 groupedContainer
: getGroupedContainers()) {
619 groupedContainer
.setBackground(background
);
622 getControl().setBackground(background
);
626 public void changed(Object element
) {
628 // if the attribute is null then do not set the dirty flag -> hotfix for the problem that for tasks done in service methods the changes are saved automatically
629 if (element
!= null){
630 dirty
.setDirty(true);
632 //TODO: refresh taxon node in taxon navigator
636 if (element
instanceof TaxonBase
) {
637 AbstractGroupedContainerE4 container
= getContainer((TaxonBase
) element
);
638 if (container
!= null) {
642 if (element
instanceof TaxonRelationship
) {
643 AbstractGroupedContainerE4 container
= getContainer(((TaxonRelationship
) element
).getFromTaxon());
644 if (container
!= null) {
650 public void setPartName(){
651 thisPart
.setLabel(this.taxon
.getName().getFullTitleCache());
655 public void forceDirty() {
660 public IUndoContext
getUndoContext() {
665 public Composite
getControl(){
666 return managedForm
.getForm().getBody();
669 public EMenuService
getMenuService() {
673 public ESelectionService
getSelectionService() {
682 public boolean canAttachMedia() {
686 public TaxonEditorInputE4
getEditorInput() {
694 public TaxonNameEditorE4
getEditor() {