3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.workingSet
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Arrays
;
14 import java
.util
.HashMap
;
15 import java
.util
.Iterator
;
16 import java
.util
.List
;
19 import javax
.annotation
.PostConstruct
;
20 import javax
.annotation
.PreDestroy
;
21 import javax
.inject
.Inject
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.e4
.ui
.di
.Focus
;
25 import org
.eclipse
.e4
.ui
.di
.Persist
;
26 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
27 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
28 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
32 import org
.eclipse
.jface
.viewers
.SelectionChangedEvent
;
33 import org
.eclipse
.jface
.viewers
.TreeSelection
;
34 import org
.eclipse
.swt
.SWT
;
35 import org
.eclipse
.swt
.custom
.SashForm
;
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
.layout
.GridData
;
41 import org
.eclipse
.swt
.layout
.GridLayout
;
42 import org
.eclipse
.swt
.widgets
.Button
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Display
;
45 import org
.eclipse
.ui
.forms
.widgets
.FormToolkit
;
47 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
48 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureNodeService
;
49 import eu
.etaxonomy
.cdm
.api
.service
.IFeatureTreeService
;
50 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
51 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
52 import eu
.etaxonomy
.cdm
.model
.description
.Character
;
53 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
54 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
55 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.FeatureNodeDragListener
;
56 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.FeatureNodeDropAdapter
;
57 import eu
.etaxonomy
.taxeditor
.featuretree
.e4
.FeatureTreeEditorComposite
;
58 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
59 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
61 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
62 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySession
;
63 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
64 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
65 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.FeatureTreeSelectionDialog
;
66 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
67 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4ViewerPart
;
74 public class CharacterEditor
75 implements ICdmEntitySessionEnabled
, ISelectionChangedListener
, ModifyListener
, IE4ViewerPart
76 , IPartContentHasDetails
, IPartContentHasSupplementalData
, IE4SavablePart
{
78 private FeatureTreeEditorComposite characterTreeEditorComposite
;
79 private FeatureTreeEditorComposite propertiesTreeEditorComposite
;
80 private FeatureTreeEditorComposite structuresTreeEditorComposite
;
81 private final FormToolkit formToolkit
= new FormToolkit(Display
.getDefault());
82 private Button btnAddCharacter
;
85 private ESelectionService selService
;
88 private MDirtyable dirty
;
90 private ConversationHolder conversation
;
92 private ICdmEntitySession cdmEntitySession
;
93 private Composite composite
;
95 public CharacterEditor() {
99 * Create contents of the view part.
102 public void createControls(Composite parent
, EMenuService menuService
) {
103 if (CdmStore
.isActive()){
104 if(conversation
== null){
105 conversation
= CdmStore
.createConversation();
107 if(cdmEntitySession
!=null){
108 cdmEntitySession
= CdmStore
.getCurrentSessionManager().newSession(this, true);
114 parent
.setLayout(new GridLayout(1, false));
116 SashForm sashForm
= new SashForm(parent
, SWT
.NONE
);
117 sashForm
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 1, 1));
118 formToolkit
.adapt(sashForm
);
119 formToolkit
.paintBordersFor(sashForm
);
121 composite
= new Composite(sashForm
, SWT
.NONE
);
122 formToolkit
.adapt(composite
);
123 formToolkit
.paintBordersFor(composite
);
124 GridLayout gl_composite
= new GridLayout(1, false);
125 gl_composite
.marginWidth
= 0;
126 composite
.setLayout(gl_composite
);
128 structuresTreeEditorComposite
= addFeatureTreeEditor(composite
);
129 initFeatureTreeComposite(structuresTreeEditorComposite
, null, null);
130 structuresTreeEditorComposite
.getLabel_title().setText("Structures");
132 Composite composite_1
= new Composite(sashForm
, SWT
.NONE
);
133 formToolkit
.adapt(composite_1
);
134 formToolkit
.paintBordersFor(composite_1
);
135 GridLayout gl_composite_1
= new GridLayout(1, false);
136 gl_composite_1
.marginWidth
= 0;
137 composite_1
.setLayout(gl_composite_1
);
139 propertiesTreeEditorComposite
= addFeatureTreeEditor(composite_1
);
140 initFeatureTreeComposite(propertiesTreeEditorComposite
, null, null);
141 propertiesTreeEditorComposite
.getLabel_title().setText("Properties");
142 //TODO: fix drag and drop
143 // initFeatureTreeComposite(treeViewerProperties, new CharacterDragListener(treeViewerStructures.getViewer(), treeViewerProperties.getViewer()), null);
145 Composite composite_3
= new Composite(sashForm
, SWT
.NONE
);
146 formToolkit
.adapt(composite_3
);
147 formToolkit
.paintBordersFor(composite_3
);
148 GridLayout gl_composite_3
= new GridLayout(2, false);
149 gl_composite_3
.marginWidth
= 0;
150 composite_3
.setLayout(gl_composite_3
);
152 btnAddCharacter
= new Button(composite_3
, SWT
.NONE
);
153 btnAddCharacter
.setText(">>");
154 formToolkit
.adapt(btnAddCharacter
, true, true);
156 characterTreeEditorComposite
= addFeatureTreeEditor(composite_3
);
157 initFeatureTreeComposite(characterTreeEditorComposite
, new FeatureNodeDragListener(characterTreeEditorComposite
.getViewer()), new FeatureNodeDropAdapter(dirty
, characterTreeEditorComposite
.getViewer()));
158 characterTreeEditorComposite
.getLabel_title().setText("Characters");
160 //add context menu to character viewer//create context menu
161 menuService
.registerContextMenu(characterTreeEditorComposite
.getViewer().getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.characterEditor");
166 private FeatureTreeEditorComposite
addFeatureTreeEditor(Composite composite_3
) {
167 FeatureTreeEditorComposite featureTreeEditorComposite
= new FeatureTreeEditorComposite(composite_3
, SWT
.NONE
);
168 featureTreeEditorComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true, 1, 1));
169 return featureTreeEditorComposite
;
172 private void initFeatureTreeComposite(FeatureTreeEditorComposite featureTreeEditorComposite
,
173 FeatureNodeDragListener featureNodeDragListener
, FeatureNodeDropAdapter featureNodeDropAdapter
) {
174 featureTreeEditorComposite
.init(featureNodeDragListener
, featureNodeDropAdapter
, this,
175 new FeatureTreeChooserListener(featureTreeEditorComposite
));
180 if(characterTreeEditorComposite
!=null){
181 characterTreeEditorComposite
.getViewer().getControl().setFocus();
183 if(conversation
!=null && !conversation
.isBound()){
186 if(cdmEntitySession
!= null) {
187 cdmEntitySession
.bind();
192 btnAddCharacter
.addSelectionListener(new SelectionAdapter() {
197 public void widgetSelected(SelectionEvent e
) {
198 ISelection structureTreeSelection
= structuresTreeEditorComposite
.getViewer().getSelection();
199 ISelection propertyTreeSelection
= propertiesTreeEditorComposite
.getViewer().getSelection();
200 if(structureTreeSelection
==null || propertyTreeSelection
==null || characterTreeEditorComposite
.getFeatureTree()==null){
201 MessagingUtils
.warningDialog("Cannot perform action", CharacterEditor
.this,
202 "You have to select a structure, a property and a feature tree to perform this action.");
205 //get selected structures and properties
206 Iterator
<FeatureNode
> structureIterator
= ((TreeSelection
)structureTreeSelection
).iterator();
208 //create all combinations of structures and properties
209 while(structureIterator
.hasNext()){
210 FeatureNode structureNode
= structureIterator
.next();
211 Iterator
<FeatureNode
> propertiesIterator
= ((TreeSelection
)propertyTreeSelection
).iterator();
212 while(propertiesIterator
.hasNext()){
213 FeatureNode propertyNode
= propertiesIterator
.next();
214 //create new Character
215 String label
= structureNode
.getFeature().generateTitle()+" "+propertyNode
.getFeature().generateTitle();
216 Character character
= Character
.NewInstance(structureNode
, propertyNode
, "", label
, label
);
217 character
= HibernateProxyHelper
.deproxy(CdmStore
.getService(ITermService
.class).merge(character
), Character
.class);
219 //add new Character to feature tree
220 FeatureNode parent
= ((FeatureTree
) characterTreeEditorComposite
.getViewer().getInput()).getRoot();
221 CdmStore
.getService(IFeatureNodeService
.class).addChildFeatureNode(parent
.getUuid(), character
.getUuid());
226 characterTreeEditorComposite
.getViewer().refresh();
233 public void save(IProgressMonitor monitor
){
234 if (!conversation
.isBound()) {
238 // commit the conversation and start a new transaction immediately
239 conversation
.commit(true);
241 CdmStore
.getService(IFeatureTreeService
.class).saveOrUpdate(characterTreeEditorComposite
.getFeatureTree());
243 this.setDirty(false);
247 public void dispose(){
248 if(conversation
!=null){
249 conversation
.close();
252 if(cdmEntitySession
!= null) {
253 cdmEntitySession
.dispose();
254 cdmEntitySession
= null;
259 public Map
<Object
, List
<String
>> getPropertyPathsMap() {
260 List
<String
> propertyPaths
= Arrays
.asList(new String
[] {
261 "children", //$NON-NLS-1$
262 "feature", //$NON-NLS-1$
263 "featureTree", //$NON-NLS-1$
265 Map
<Object
, List
<String
>> propertyPathMap
=
266 new HashMap
<Object
, List
<String
>>();
267 propertyPathMap
.put(FeatureNode
.class,propertyPaths
);
268 return propertyPathMap
;
275 public List
<FeatureTree
> getRootEntities() {
276 List
<FeatureTree
> root
= new ArrayList
<>();
277 root
.add(characterTreeEditorComposite
.getFeatureTree());
282 public ICdmEntitySession
getCdmEntitySession() {
283 return cdmEntitySession
;
287 public void setFocus() {
288 if(conversation
!=null && !conversation
.isBound()){
295 public void selectionChanged(SelectionChangedEvent event
) {
296 //propagate selection
297 selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
));
305 public void modifyText(ModifyEvent e
) {
306 characterTreeEditorComposite
.getFeatureTree().setTitleCache(characterTreeEditorComposite
.getText_title().getText(), true);
311 public void setDirty(boolean isDirty
) {
312 dirty
.setDirty(isDirty
);
319 public IStructuredSelection
getSelection() {
320 return (IStructuredSelection
) characterTreeEditorComposite
.getViewer().getSelection();
324 public void refresh(){
325 structuresTreeEditorComposite
.getViewer().refresh();
326 propertiesTreeEditorComposite
.getViewer().refresh();
327 characterTreeEditorComposite
.getViewer().refresh();
330 private class FeatureTreeChooserListener
extends SelectionAdapter
{
331 FeatureTreeEditorComposite featureTreeEditorComposite
;
333 public FeatureTreeChooserListener(FeatureTreeEditorComposite featureTreeEditorComposite
) {
335 this.featureTreeEditorComposite
= featureTreeEditorComposite
;
339 public void widgetSelected(SelectionEvent e
) {
340 FeatureTree tree
= FeatureTreeSelectionDialog
.select(featureTreeEditorComposite
.getDisplay().getActiveShell(), conversation
, null);
342 featureTreeEditorComposite
.setSelectedTree(tree
, CharacterEditor
.this);
343 featureTreeEditorComposite
.getText_title().setEnabled(false);