Project

General

Profile

Download (6.38 KB) Statistics
| Branch: | Tag: | Revision:
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.Collection;
13

    
14
import javax.annotation.PostConstruct;
15
import javax.inject.Inject;
16
import javax.inject.Named;
17

    
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.e4.ui.di.Focus;
20
import org.eclipse.e4.ui.di.Persist;
21
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
24
import org.eclipse.jface.viewers.ISelectionChangedListener;
25
import org.eclipse.jface.viewers.IStructuredSelection;
26
import org.eclipse.jface.viewers.SelectionChangedEvent;
27
import org.eclipse.jface.wizard.WizardDialog;
28
import org.eclipse.swt.SWT;
29
import org.eclipse.swt.events.ModifyEvent;
30
import org.eclipse.swt.events.ModifyListener;
31
import org.eclipse.swt.events.SelectionAdapter;
32
import org.eclipse.swt.events.SelectionEvent;
33
import org.eclipse.swt.widgets.Composite;
34
import org.eclipse.swt.widgets.Shell;
35

    
36
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
37
import eu.etaxonomy.cdm.api.service.IFeatureNodeService;
38
import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
39
import eu.etaxonomy.cdm.api.service.config.FeatureNodeDeletionConfigurator;
40
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
41
import eu.etaxonomy.cdm.model.description.Feature;
42
import eu.etaxonomy.cdm.model.description.FeatureNode;
43
import eu.etaxonomy.cdm.model.description.FeatureTree;
44
import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.ui.dialog.selection.FeatureTreeSelectionDialog;
47

    
48
/**
49
 *
50
 * @author pplitzner
51
 * @date 06.06.2017
52
 *
53
 */
54
public class FeatureTreeEditor implements
55
		ModifyListener, ISelectionChangedListener {
56

    
57
    private ConversationHolder conversation;
58

    
59
    @Inject
60
    private ESelectionService selService;
61

    
62
    @Inject
63
    private MDirtyable dirty;
64

    
65
    private Shell shell;
66

    
67
    private FeatureTree featureTree;
68

    
69
    private FeatureTreeEditorComposite composite;
70

    
71
    @Inject
72
    public FeatureTreeEditor(@Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
73
        this.shell = shell;
74
        if(conversation==null){
75
            conversation = CdmStore.createConversation();
76
        }
77
    }
78

    
79
	/** {@inheritDoc} */
80
	@PostConstruct
81
	public void createControl(Composite parent, @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
82
	    composite = new FeatureTreeEditorComposite(parent, SWT.NULL);
83
		composite.init(new FeatureNodeDragListener(composite.getViewer()), new FeatureNodeDropAdapter(this, composite.getViewer()), this, new SelectionAdapter() {
84
            @Override
85
            public void widgetSelected(SelectionEvent e) {
86
                FeatureTree tree = FeatureTreeSelectionDialog.select(shell, conversation, null);
87
                if(tree!=null){
88
                    setSelectedTree(tree);
89
                }
90
            }
91
        }, new AddButtonListener(), new RemoveSelectionListener());
92
        composite.getText_title().setEnabled(false);
93
	}
94

    
95
	public void setDirty(boolean isDirty){
96
	    this.dirty.setDirty(isDirty);
97
	}
98

    
99
	public boolean isDirty(){
100
	    return dirty.isDirty();
101
	}
102

    
103
    public void setSelectedTree(FeatureTree featureTree) {
104
		this.featureTree = HibernateProxyHelper.deproxy(featureTree, FeatureTree.class);
105
		this.featureTree.setRoot(HibernateProxyHelper.deproxy(featureTree.getRoot(), FeatureNode.class));
106
		composite.getViewer().setInput(featureTree);
107

    
108
		composite.getText_title().setEnabled(true);
109
		composite.getText_title().removeModifyListener(this);
110
		composite.getText_title().setText(featureTree.getTitleCache());
111
		composite.getText_title().addModifyListener(this);
112
	}
113

    
114
	/** {@inheritDoc} */
115
	@Override
116
	public void modifyText(ModifyEvent e) {
117
		featureTree.setTitleCache(composite.getText_title().getText(), true);
118
		setDirty(true);
119
	}
120

    
121
	/** {@inheritDoc} */
122
	@Override
123
	public void selectionChanged(SelectionChangedEvent event) {
124
		IStructuredSelection selection = (IStructuredSelection) event
125
				.getSelection();
126

    
127
		composite.getButton_add().setEnabled(selection.size() <= 1);
128
		composite.getButton_remove().setEnabled(selection.size() > 0);
129
		//propagate selection
130
		IStructuredSelection isel = (IStructuredSelection) event.getSelection();
131
		selService.setSelection((isel.size() == 1 ? isel.getFirstElement() : isel.toArray()));
132
	}
133

    
134
	@Focus
135
	public void focus(){
136
	    composite.getViewer().getControl().setFocus();
137
        if(conversation!=null && !conversation.isBound()){
138
            conversation.bind();
139
        }
140
	}
141

    
142
	@Persist
143
	public void save(){
144
	    CdmStore.getService(IFeatureTreeService.class).saveOrUpdate(featureTree);
145
	    setDirty(false);
146
	}
147

    
148
	private class AddButtonListener extends SelectionAdapter {
149
		@Override
150
		public void widgetSelected(SelectionEvent e) {
151
			AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(
152
					featureTree);
153
			WizardDialog dialog = new WizardDialog(shell, wizard);
154

    
155
			if (dialog.open() == IStatus.OK) {
156
				IStructuredSelection selection = (IStructuredSelection) composite.getViewer()
157
						.getSelection();
158
				FeatureNode parent = (FeatureNode) (selection.getFirstElement() != null ? selection
159
						.getFirstElement() : ((FeatureTree) composite.getViewer().getInput())
160
						.getRoot());
161
				Collection<Feature> additionalFeatures = wizard
162
						.getAdditionalFeatures();
163
				for (Feature feature : additionalFeatures) {
164
					FeatureNode child = FeatureNode.NewInstance(feature);
165
					parent.addChild(child);
166
					CdmStore.getService(IFeatureNodeService.class).save(child);
167
				}
168
				setDirty(true);
169
				composite.getViewer().refresh();
170
			}
171
		}
172

    
173
	}
174

    
175
	private class RemoveSelectionListener extends SelectionAdapter {
176
		@Override
177
		public void widgetSelected(SelectionEvent e) {
178
			IStructuredSelection selection = (IStructuredSelection) composite.getViewer()
179
					.getSelection();
180

    
181
			for (Object selectedObject : selection.toArray()) {
182
				FeatureNode featureNode = (FeatureNode) selectedObject;
183
				FeatureNode parent = featureNode.getParent();
184
				parent.removeChild(featureNode);
185

    
186
				CdmStore.getService(IFeatureNodeService.class).deleteFeatureNode(featureNode.getUuid(), new FeatureNodeDeletionConfigurator());
187

    
188
			}
189
			setDirty(true);
190
			composite.getViewer().refresh();
191
		}
192
	}
193
}
(3-3/4)