ref #6694 Adapt to service method changes
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / FeatureTreeEditor.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
10 package eu.etaxonomy.taxeditor.featuretree.e4;
11
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import javax.annotation.PostConstruct;
20 import javax.annotation.PreDestroy;
21 import javax.inject.Inject;
22 import javax.inject.Named;
23
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.e4.core.di.annotations.Optional;
26 import org.eclipse.e4.ui.di.Focus;
27 import org.eclipse.e4.ui.di.Persist;
28 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
29 import org.eclipse.e4.ui.services.IServiceConstants;
30 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
31 import org.eclipse.jface.viewers.ISelectionChangedListener;
32 import org.eclipse.jface.viewers.IStructuredSelection;
33 import org.eclipse.jface.viewers.SelectionChangedEvent;
34 import org.eclipse.jface.wizard.WizardDialog;
35 import org.eclipse.swt.SWT;
36 import org.eclipse.swt.events.ModifyEvent;
37 import org.eclipse.swt.events.ModifyListener;
38 import org.eclipse.swt.events.SelectionAdapter;
39 import org.eclipse.swt.events.SelectionEvent;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Shell;
42
43 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
44 import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
45 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
46 import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
47 import eu.etaxonomy.cdm.model.description.Feature;
48 import eu.etaxonomy.cdm.model.description.FeatureNode;
49 import eu.etaxonomy.cdm.model.description.FeatureTree;
50 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
51 import eu.etaxonomy.taxeditor.model.AbstractUtility;
52 import eu.etaxonomy.taxeditor.model.MessagingUtils;
53 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
54 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
55 import eu.etaxonomy.taxeditor.store.CdmStore;
56 import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
57
58 /**
59 *
60 * @author pplitzner
61 * @date 06.06.2017
62 *
63 */
64 public class FeatureTreeEditor implements ICdmEntitySessionEnabled,
65 ModifyListener, ISelectionChangedListener {
66
67 private ConversationHolder conversation;
68
69 private ICdmEntitySession cdmEntitySession;
70
71 @Inject
72 private ESelectionService selService;
73
74 @Inject
75 private MDirtyable dirty;
76
77 private FeatureTreeEditorComposite composite;
78
79 @Inject
80 public FeatureTreeEditor() {
81 }
82
83 /** {@inheritDoc} */
84 @PostConstruct
85 public void createControl(Composite parent, @Optional@Named(IServiceConstants.ACTIVE_SHELL) Shell shell){
86 if (CdmStore.isActive()){
87 if(conversation == null){
88 conversation = CdmStore.createConversation();
89 }
90 if(cdmEntitySession!=null){
91 cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
92 }
93 }
94 else{
95 return;
96 }
97 composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
98 composite.init(new FeatureNodeDragListener(composite.getViewer()),
99 new FeatureNodeDropAdapter(this, composite.getViewer()), this, new SelectionAdapter() {
100 @Override
101 public void widgetSelected(SelectionEvent e) {
102 if(isDirty()){
103 if(MessagingUtils.confirmDialog("Editor has to be saved", "You have to save before loading another feature tree. Save now?")){
104 save();
105 }
106 else{
107 return;
108 }
109
110 }
111 FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
112 if (tree != null) {
113 composite.setSelectedTree(tree, FeatureTreeEditor.this);
114 }
115 }
116 }, new AddButtonListener(), new RemoveSelectionListener(), new FeatureTreeExportListener(shell, composite));
117 }
118
119 public void setDirty(boolean isDirty){
120 this.dirty.setDirty(isDirty);
121 }
122
123 public boolean isDirty(){
124 return dirty.isDirty();
125 }
126
127 public FeatureTree getSelectedFeatureTree(){
128 return composite.getFeatureTree();
129 }
130
131 /** {@inheritDoc} */
132 @Override
133 public void modifyText(ModifyEvent e) {
134 composite.getFeatureTree().setTitleCache(composite.getText_title().getText(), true);
135 setDirty(true);
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public void selectionChanged(SelectionChangedEvent event) {
141 IStructuredSelection selection = (IStructuredSelection) event
142 .getSelection();
143
144 composite.getBtnAdd().setEnabled(selection.size() <= 1);
145 composite.getBtnRemove().setEnabled(selection.size() > 0);
146 //propagate selection
147 selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event));
148 }
149
150 @Focus
151 public void focus(){
152 if(composite!=null){
153 composite.getViewer().getControl().setFocus();
154 }
155 if(conversation!=null && !conversation.isBound()){
156 conversation.bind();
157 }
158 if(cdmEntitySession != null) {
159 cdmEntitySession.bind();
160 }
161 }
162
163 @Persist
164 public void save(){
165 if (!conversation.isBound()) {
166 conversation.bind();
167 }
168
169 // commit the conversation and start a new transaction immediately
170 conversation.commit(true);
171
172 CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(composite.getFeatureTree());
173
174 this.setDirty(false);
175 }
176
177 @PreDestroy
178 public void dispose(){
179 if(conversation!=null){
180 conversation.close();
181 }
182 if(cdmEntitySession != null) {
183 cdmEntitySession.dispose();
184 }
185 }
186
187 @Override
188 public ICdmEntitySession getCdmEntitySession() {
189 return cdmEntitySession;
190 }
191
192 @Override
193 public Map<Object, List<String>> getPropertyPathsMap() {
194 List<String> propertyPaths = Arrays.asList(new String[] {
195 "children", //$NON-NLS-1$
196 "feature", //$NON-NLS-1$
197 "featureTree", //$NON-NLS-1$
198 });
199 Map<Object, List<String>> propertyPathMap =
200 new HashMap<Object, List<String>>();
201 propertyPathMap.put(FeatureNode.class,propertyPaths);
202 return propertyPathMap;
203 }
204
205 /**
206 * {@inheritDoc}
207 */
208 @Override
209 public List<FeatureTree> getRootEntities() {
210 List<FeatureTree> root = new ArrayList<>();
211 root.add(composite.getFeatureTree());
212 return root;
213 }
214
215 private class AddButtonListener extends SelectionAdapter {
216 @Override
217 public void widgetSelected(SelectionEvent e) {
218 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
219 WizardDialog dialog = new WizardDialog(e.widget.getDisplay().getActiveShell(), wizard);
220
221 if (dialog.open() == IStatus.OK) {
222 FeatureNode parent = ((FeatureTree) composite.getViewer().getInput()).getRoot();
223 Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
224 for (Feature feature : additionalFeatures) {
225 if(!getSelectedFeatureTree().getDistinctFeatures().contains(feature)){
226 CdmStore.getService(IFeatureNodeService.class).addChildFeatureNode(parent.getUuid(), feature.getUuid());
227 }
228 }
229 setDirty(true);
230 composite.getViewer().refresh();
231 composite.getViewer().expandToLevel(parent, 1);
232 }
233 }
234
235 }
236
237 private class RemoveSelectionListener extends SelectionAdapter {
238 @Override
239 public void widgetSelected(SelectionEvent e) {
240 IStructuredSelection selection = (IStructuredSelection) composite.getViewer()
241 .getSelection();
242
243 for (Object selectedObject : selection.toArray()) {
244 FeatureNode featureNode = (FeatureNode) selectedObject;
245 CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
246
247 }
248 setDirty(true);
249 composite.getViewer().refresh();
250 }
251 }
252
253 }