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
.bulkeditor
.e4
;
12 import java
.io
.FileOutputStream
;
13 import java
.io
.IOException
;
14 import java
.util
.List
;
16 import javax
.annotation
.PostConstruct
;
17 import javax
.annotation
.PreDestroy
;
18 import javax
.inject
.Inject
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.e4
.core
.contexts
.ContextInjectionFactory
;
22 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
23 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
24 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
25 import org
.eclipse
.e4
.ui
.di
.Focus
;
26 import org
.eclipse
.e4
.ui
.di
.Persist
;
27 import org
.eclipse
.e4
.ui
.di
.UIEventTopic
;
28 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
29 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
32 import org
.eclipse
.swt
.SWT
;
33 import org
.eclipse
.swt
.widgets
.Composite
;
35 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
36 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
37 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
38 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
39 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
40 import eu
.etaxonomy
.cdm
.model
.common
.User
;
41 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
42 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
43 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
44 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
45 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
46 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
47 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
48 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
49 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
50 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
51 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
52 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.GroupEditorInput
;
53 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.TaxonEditorInput
;
54 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
55 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
56 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
57 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
58 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
59 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
60 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
61 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
62 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
63 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
64 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
72 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
73 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
74 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
, ITaxonEditor
{
77 private MDirtyable dirty
;
79 private AbstractBulkEditorInput input
;
81 private ConversationHolder conversation
;
84 private IEventBroker eventBroker
;
87 IEclipseContext context
;
90 private MPart thisPart
;
92 private BulkEditorQuery lastQuery
= null;
94 private BulkEditorE4Composite bulkEditorComposite
;
97 public BulkEditorE4() {
100 @SuppressWarnings("unused")
101 public void init(AbstractBulkEditorInput
<?
> input
){
103 this.conversation
= input
.getConversation();
104 thisPart
.setLabel(input
.getEditorName());
106 bulkEditorComposite
.init(input
);
111 public void createPartControl(Composite parent
, IEclipseContext context
) {
112 bulkEditorComposite
= new BulkEditorE4Composite(this, parent
, SWT
.NONE
);
113 ContextInjectionFactory
.inject(bulkEditorComposite
, context
);
118 public void save(IProgressMonitor monitor
) {
122 public void save(IProgressMonitor monitor
, boolean resetMerge
) {
123 if (!input
.getCdmEntitySession().isActive()){
124 input
.getCdmEntitySession().bind();
126 input
.saveModel(resetMerge
);
128 IStructuredSelection selection
= getSelection();
130 dirty
.setDirty(false);
133 conversation
.commit(true);
135 if (lastQuery
!= null){
136 bulkEditorComposite
.performSearch(lastQuery
, selection
);
142 public void setFocus() {
143 //make sure to bind again if maybe in another view the conversation was unbound
144 if(conversation
!=null && !conversation
.isBound()){
147 if(input
!=null && input
.getCdmEntitySession()!= null) {
148 input
.getCdmEntitySession().bind();
151 //make sure to bind again if maybe in another view the conversation was unbound
152 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, this);
156 public void dispose() {
157 if(conversation
!=null){
158 conversation
.unregisterForDataStoreChanges(this);
159 conversation
.close();
164 dirty
.setDirty(false);
165 //save table settings
166 if(bulkEditorComposite
.getNatTableState()!=null){
167 try (FileOutputStream tableStateStream
=
168 new FileOutputStream(bulkEditorComposite
.getStatePropertiesFile())) {
169 bulkEditorComposite
.getNatTableState().store(tableStateStream
, null);
170 } catch (IOException ioe
) {
171 ioe
.printStackTrace();
178 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
)IStructuredSelection selection
){
182 setSelection(selection
);
186 public void refresh(){
187 bulkEditorComposite
.refresh();
190 public IStructuredSelection
getSelection(){
191 return bulkEditorComposite
.getSelection();
194 public void setSelection(IStructuredSelection selection
){
195 bulkEditorComposite
.setSelection(selection
);
198 public void setDirty(boolean isDirty
){
199 dirty
.setDirty(isDirty
);
202 public void setDirty(){
207 public boolean isDirty() {
208 return dirty
.isDirty();
211 public AbstractBulkEditorInput
getEditorInput() {
215 public void copyDataToClipboard() {
216 bulkEditorComposite
.copyDataToClipboard();
220 public void update(CdmDataChangeMap arg0
) {
224 public boolean canAttachMedia() {
229 public void changed(Object element
) {
230 if(element
instanceof DerivedUnitFacade
){
231 DerivedUnit derivedUnit
= ((DerivedUnitFacade
) element
).innerDerivedUnit();
232 if(derivedUnit
!=null){
233 getEditorInput().addSaveCandidate(derivedUnit
);
235 FieldUnit fieldUnit
= ((DerivedUnitFacade
) element
).innerFieldUnit();
237 getEditorInput().addSaveCandidate(fieldUnit
);
240 else if (element
instanceof CdmBase
) {
241 if (element
instanceof DescriptionBase
){
242 if (element
instanceof TaxonNameDescription
){
243 TaxonName changedName
= ((TaxonNameDescription
)element
).getTaxonName();
244 getEditorInput().addSaveCandidate(changedName
);
245 input
.replaceInModel(changedName
);
246 }else if (element
instanceof TaxonDescription
){
247 Taxon changedTaxon
= ((TaxonDescription
)element
).getTaxon();
248 getEditorInput().addSaveCandidate(changedTaxon
);
249 input
.replaceInModel(changedTaxon
);
251 }else if (element
instanceof DescriptionElementBase
){
252 if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonNameDescription
){
253 TaxonName changedName
= ((TaxonNameDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxonName();
254 getEditorInput().addSaveCandidate(changedName
);
255 input
.replaceInModel(changedName
);
256 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonDescription
){
257 Taxon changedTaxon
= ((TaxonDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxon();
258 getEditorInput().addSaveCandidate(changedTaxon
);
259 input
.replaceInModel(changedTaxon
);
261 }else if(element
instanceof Group
){
262 Group oldGroup
= ((GroupEditorInput
)input
).getEntityFromModel((Group
)element
);
263 ((GroupEditorInput
)input
).getSaveUserCandidates().addAll(oldGroup
.getMembers());
264 getEditorInput().addSaveCandidate((Group
)element
);
265 input
.replaceInModel((CdmBase
) element
);
267 getEditorInput().addSaveCandidate((CdmBase
)element
);
268 input
.replaceInModel((CdmBase
) element
);
271 dirty
.setDirty(true);
272 setSelection(new StructuredSelection(element
));
276 public void forceDirty() {
277 dirty
.setDirty(true);
281 public boolean postOperation(Object objectAffectedByOperation
) {
286 public boolean onComplete() {
291 public ConversationHolder
getConversationHolder() {
295 public BulkEditorQuery
getLastQuery() {
299 public void setLastQuery(BulkEditorQuery lastQuery
) {
300 this.lastQuery
= lastQuery
;
305 public Taxon
getTaxon() {
306 IStructuredSelection selection
= getSelection();
307 if(selection
.size()==1) {
308 Object object
= selection
.iterator().next();
309 if(object
instanceof Taxon
){
310 return (Taxon
) object
;
320 public void update() {
321 input
.performSearch(lastQuery
, getSelection());
325 public void addOperation(AbstractPostOperation operation
) {
326 // operations not yet used for bulk editor
331 private void updateView(@UIEventTopic(WorkbenchEventConstants
.REFRESH_NAME_EDITOR
) List
<CdmBase
> cdmBases
) {
332 if (getEditorInput() instanceof TaxonEditorInput
){
333 for (CdmBase cdmBase
: cdmBases
){
334 if (getEditorInput().getModel().contains(cdmBase
)){
335 input
.performSearch(lastQuery
, getSelection());
344 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants
.REMOVE_USER
) User user
) {
345 if (input
instanceof GroupEditorInput
){
346 ((GroupEditorInput
)input
).getSaveUserCandidates().add(user
);