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
.description
.DescriptionBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
41 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
43 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
44 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
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
.occurrence
.SpecimenOrObservationBase
;
49 import eu
.etaxonomy
.cdm
.model
.permission
.Group
;
50 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
53 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
54 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
56 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
57 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.GroupEditorInput
;
58 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.TaxonEditorInput
;
59 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
60 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
61 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
62 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
63 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
64 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
66 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
67 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
68 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
69 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
75 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
76 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
77 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
, ITaxonEditor
{
80 private MDirtyable dirty
;
82 private AbstractBulkEditorInput input
;
84 private ConversationHolder conversation
;
87 private IEventBroker eventBroker
;
90 IEclipseContext context
;
93 private MPart thisPart
;
95 private BulkEditorQuery lastQuery
= null;
97 private BulkEditorE4Composite bulkEditorComposite
;
100 public BulkEditorE4() {
103 public void init(AbstractBulkEditorInput
<?
> input
){
105 this.conversation
= input
.getConversation();
106 thisPart
.setLabel(input
.getEditorName());
108 bulkEditorComposite
.init(input
);
112 public void createPartControl(Composite parent
, IEclipseContext context
) {
113 bulkEditorComposite
= new BulkEditorE4Composite(this, parent
, SWT
.NONE
);
114 ContextInjectionFactory
.inject(bulkEditorComposite
, context
);
119 public void save(IProgressMonitor monitor
) {
123 public void save(IProgressMonitor monitor
, boolean resetMerge
) {
124 if (!input
.getCdmEntitySession().isActive()){
125 input
.getCdmEntitySession().bind();
127 input
.saveModel(resetMerge
);
129 IStructuredSelection selection
= getSelection();
131 dirty
.setDirty(false);
134 conversation
.commit(true);
136 if (lastQuery
!= null){
137 bulkEditorComposite
.performSearch(lastQuery
, selection
);
143 public void setFocus() {
144 //make sure to bind again if maybe in another view the conversation was unbound
145 if(conversation
!=null && !conversation
.isBound()){
148 if(input
!=null && input
.getCdmEntitySession()!= null) {
149 input
.getCdmEntitySession().bind();
152 //make sure to bind again if maybe in another view the conversation was unbound
153 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, this);
157 public void dispose() {
158 if(conversation
!=null){
159 conversation
.unregisterForDataStoreChanges(this);
160 conversation
.close();
165 dirty
.setDirty(false);
166 //save table settings
167 if(bulkEditorComposite
.getNatTableState()!=null){
168 try (FileOutputStream tableStateStream
=
169 new FileOutputStream(bulkEditorComposite
.getStatePropertiesFile())) {
170 bulkEditorComposite
.getNatTableState().store(tableStateStream
, null);
171 } catch (IOException ioe
) {
172 ioe
.printStackTrace();
179 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
)IStructuredSelection selection
){
183 setSelection(selection
);
187 public void refresh(){
188 bulkEditorComposite
.refresh();
191 public IStructuredSelection
getSelection(){
192 return bulkEditorComposite
.getSelection();
195 public void setSelection(IStructuredSelection selection
){
196 bulkEditorComposite
.setSelection(selection
);
199 public void setDirty(boolean isDirty
){
200 dirty
.setDirty(isDirty
);
203 public void setDirty(){
208 public boolean isDirty() {
209 return dirty
.isDirty();
212 public AbstractBulkEditorInput
getEditorInput() {
216 public void copyDataToClipboard() {
217 bulkEditorComposite
.copyDataToClipboard();
221 public void update(CdmDataChangeMap arg0
) {
225 public boolean canAttachMedia() {
230 public void changed(Object element
) {
231 if(element
instanceof DerivedUnitFacade
){
232 DerivedUnit derivedUnit
= ((DerivedUnitFacade
) element
).innerDerivedUnit();
233 if(derivedUnit
!=null){
234 getEditorInput().addSaveCandidate(derivedUnit
);
236 FieldUnit fieldUnit
= ((DerivedUnitFacade
) element
).innerFieldUnit();
238 getEditorInput().addSaveCandidate(fieldUnit
);
241 else if (element
instanceof CdmBase
) {
242 if (element
instanceof DescriptionBase
){
243 if (element
instanceof TaxonNameDescription
){
244 TaxonName changedName
= ((TaxonNameDescription
)element
).getTaxonName();
245 if (getEditorInput() instanceof TaxonEditorInput
){
246 IStructuredSelection sel
= getSelection();
247 Object firstElement
= sel
.getFirstElement();
248 if (firstElement
instanceof TaxonBase
){
249 getEditorInput().addSaveCandidate((TaxonBase
)firstElement
);
252 getEditorInput().addSaveCandidate(changedName
);
255 input
.replaceInModel(changedName
);
256 }else if (element
instanceof TaxonDescription
){
257 Taxon changedTaxon
= ((TaxonDescription
)element
).getTaxon();
258 getEditorInput().addSaveCandidate(changedTaxon
);
259 input
.replaceInModel(changedTaxon
);
261 }else if (element
instanceof DescriptionElementBase
){
262 if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonNameDescription
){
263 TaxonName changedName
= ((TaxonNameDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxonName();
264 //check whether the bulk editor is taxon or name bulk editor
265 if (getEditorInput() instanceof TaxonEditorInput
){
266 IStructuredSelection sel
= getSelection();
267 Object firstElement
= sel
.getFirstElement();
268 if (firstElement
instanceof TaxonBase
){
269 getEditorInput().addSaveCandidate((TaxonBase
)firstElement
);
272 getEditorInput().addSaveCandidate(changedName
);
274 input
.replaceInModel(changedName
);
275 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonDescription
){
276 Taxon changedTaxon
= ((TaxonDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxon();
277 getEditorInput().addSaveCandidate(changedTaxon
);
278 input
.replaceInModel(changedTaxon
);
279 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof SpecimenDescription
){
280 SpecimenOrObservationBase changedSpecimen
= ((SpecimenDescription
)((DescriptionElementBase
)element
).getInDescription()).getDescribedSpecimenOrObservation();
281 getEditorInput().addSaveCandidate(changedSpecimen
);
282 input
.replaceInModel(changedSpecimen
);
284 }else if (element
instanceof Media
){
285 IStructuredSelection sel
= getSelection();
286 Object firstElement
= sel
.getFirstElement();
287 if (firstElement
instanceof TaxonBase
){
288 TaxonBase changedTaxon
= (TaxonBase
)sel
.getFirstElement();
289 getEditorInput().addSaveCandidate(changedTaxon
);
290 input
.replaceInModel(changedTaxon
);
291 }else if (firstElement
instanceof SpecimenOrObservationBase
){
292 SpecimenOrObservationBase changedSpecimen
= (SpecimenOrObservationBase
)sel
.getFirstElement();
293 getEditorInput().addSaveCandidate(changedSpecimen
);
294 input
.replaceInModel(changedSpecimen
);
296 }else if(element
instanceof Group
){
297 Group oldGroup
= ((GroupEditorInput
)input
).getEntityFromModel((Group
)element
);
298 ((GroupEditorInput
)input
).getSaveUserCandidates().addAll(oldGroup
.getMembers());
299 getEditorInput().addSaveCandidate((Group
)element
);
300 input
.replaceInModel((CdmBase
) element
);
302 getEditorInput().addSaveCandidate((CdmBase
)element
);
303 input
.replaceInModel((CdmBase
) element
);
306 dirty
.setDirty(true);
307 setSelection(new StructuredSelection(element
));
311 public void forceDirty() {
312 dirty
.setDirty(true);
316 public boolean postOperation(Object objectAffectedByOperation
) {
321 public boolean onComplete() {
326 public ConversationHolder
getConversationHolder() {
330 public BulkEditorQuery
getLastQuery() {
334 public void setLastQuery(BulkEditorQuery lastQuery
) {
335 this.lastQuery
= lastQuery
;
340 public Taxon
getTaxon() {
341 IStructuredSelection selection
= getSelection();
342 if(selection
.size()==1) {
343 Object object
= selection
.iterator().next();
344 if(object
instanceof Taxon
){
345 return (Taxon
) object
;
352 public void update() {
353 input
.performSearch(lastQuery
, getSelection());
357 public void addOperation(AbstractPostOperation operation
) {
358 // operations not yet used for bulk editor
363 private void updateView(@UIEventTopic(WorkbenchEventConstants
.REFRESH_NAME_EDITOR
) List
<CdmBase
> cdmBases
) {
364 if (getEditorInput() instanceof TaxonEditorInput
){
365 for (CdmBase cdmBase
: cdmBases
){
366 if (getEditorInput().getModel().contains(cdmBase
)){
367 input
.performSearch(lastQuery
, getSelection());
376 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants
.REMOVE_USER
) User user
) {
377 if (input
instanceof GroupEditorInput
){
378 ((GroupEditorInput
)input
).getSaveUserCandidates().add(user
);
383 public TaxonNode
getTaxonNode() {