ca2f7bda4df380e3fdd4266c259607d1e0692148
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / group / authority / e4 / CdmAuthorityEditorE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9 package eu.etaxonomy.taxeditor.editor.group.authority.e4;
10
11 import javax.annotation.PostConstruct;
12 import javax.annotation.PreDestroy;
13 import javax.inject.Inject;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.e4.ui.di.Focus;
17 import org.eclipse.e4.ui.di.Persist;
18 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
19 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.layout.FillLayout;
22 import org.eclipse.swt.widgets.Composite;
23
24 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
25 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
26 import eu.etaxonomy.cdm.model.common.Group;
27 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
28 import eu.etaxonomy.taxeditor.editor.group.authority.CdmAuthorityEditorInput;
29 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
30 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
31 import eu.etaxonomy.taxeditor.ui.group.grantedauthority.CdmAuthorityCompositeViewer;
32
33 /**
34 * Editor responsible for editing authorities related to a particular {@link Group} entity.
35 *
36 * @author cmathew
37 * @created Mar 28, 2013
38 *
39 */
40 public class CdmAuthorityEditorE4 implements IConversationEnabled, IDirtyMarkable {
41
42 public static final String ID = "eu.etaxonomy.taxeditor.editor.group.authority"; //$NON-NLS-1$
43
44 @Inject
45 private MDirtyable dirty;
46 private ConversationHolder conversation;
47 private CdmAuthorityCompositeViewer viewer;
48
49 private CdmAuthorityEditorInput input;
50
51 @Inject
52 private MPart thisPart;
53
54 private Composite container;
55
56 @Inject
57 public CdmAuthorityEditorE4() {
58
59 }
60
61 /**
62 * Create contents of the editor part.
63 * @param parent
64 */
65 @PostConstruct
66 public void createPartControl(Composite parent) {
67 container = new Composite(parent, SWT.NONE);
68 container.setLayout(new FillLayout(SWT.HORIZONTAL));
69
70 }
71
72 @Focus
73 public void setFocus() {
74 if(input!=null){
75 input.bind();
76 }
77 }
78
79 @Persist
80 public void doSave(IProgressMonitor monitor) {
81 try {
82 monitor.beginTask(Messages.CdmAuthorityEditor_SAVING_AUTHORITY_EDITOR, 1);
83 getConversationHolder().commit(true);
84 input.merge();
85 dirty.setDirty(false);
86 monitor.worked(1);
87 } finally {
88 monitor.done();
89 viewer.save();
90 }
91 }
92
93 public void init(CdmAuthorityEditorInput input) {
94 this.input = input;
95
96 conversation = input.getConversationHolder();
97
98 this.viewer = new CdmAuthorityCompositeViewer(container, this,input.getGroup());
99
100 thisPart.setLabel(input.getName());
101 container.layout();
102 }
103
104 public boolean isDirty() {
105 return dirty.isDirty();
106 }
107
108 @Override
109 public void update(CdmDataChangeMap changeEvents) {
110 }
111
112 @Override
113 public void changed(Object element) {
114 //FIXME : should be optimised to split changes into adding / removing authorities
115 // vs updating authorites
116 dirty.setDirty(viewer.isDirty());
117 // if the change has happened on the group then refresh the table
118 if(element instanceof Group) {
119 //FIXME: activating (setting focus) on the editor happens on every change
120 // This should be changed to only when the drop is successful
121 //FIXME E4 migrate/delete
122 // getSite().getPage().activate(this);
123 }
124
125 }
126
127 @Override
128 public void forceDirty() {
129 changed(null);
130 }
131
132 @Override
133 public ConversationHolder getConversationHolder() {
134 return conversation;
135 }
136
137 @PreDestroy
138 public void dispose() {
139 if(conversation!=null){
140 conversation.unregisterForDataStoreChanges(this);
141 conversation.close();
142 conversation = null;
143 }
144 if(input!=null){
145 input.getCdmEntitySession().dispose();
146 }
147 }
148 }