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.
9 package eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
;
11 import java
.io
.FileOutputStream
;
12 import java
.io
.IOException
;
13 import java
.util
.List
;
15 import javax
.annotation
.PostConstruct
;
16 import javax
.annotation
.PreDestroy
;
17 import javax
.inject
.Inject
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.e4
.core
.contexts
.ContextInjectionFactory
;
21 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
22 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
23 import org
.eclipse
.e4
.core
.services
.events
.IEventBroker
;
24 import org
.eclipse
.e4
.ui
.di
.Focus
;
25 import org
.eclipse
.e4
.ui
.di
.Persist
;
26 import org
.eclipse
.e4
.ui
.di
.UIEventTopic
;
27 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
28 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
34 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
35 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
36 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
37 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
39 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
40 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
42 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
43 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
44 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
46 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
47 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
48 import eu
.etaxonomy
.cdm
.model
.permission
.Group
;
49 import eu
.etaxonomy
.cdm
.model
.permission
.User
;
50 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
51 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
53 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
54 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
55 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
56 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.GroupEditorInput
;
57 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.TaxonEditorInput
;
58 import eu
.etaxonomy
.taxeditor
.editor
.IBulkEditor
;
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 BulkEditor
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
76 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
77 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
, ITaxonEditor
, IBulkEditor
{
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 BulkEditorComposite bulkEditorComposite
;
100 public BulkEditor() {
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 BulkEditorComposite(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
){
181 setSelection(selection
);
185 public void refresh(){
186 bulkEditorComposite
.refresh();
189 public IStructuredSelection
getSelection(){
190 return bulkEditorComposite
.getSelection();
193 public IStructuredSelection
getCellSelection(){
194 return bulkEditorComposite
.getCellSelection();
197 public void setSelection(IStructuredSelection selection
){
198 bulkEditorComposite
.setSelection(selection
);
201 public void setDirty(boolean isDirty
){
202 dirty
.setDirty(isDirty
);
205 public void setDirty(){
210 public boolean isDirty() {
211 return dirty
.isDirty();
214 public AbstractBulkEditorInput
getEditorInput() {
218 public void copyDataToClipboard() {
219 bulkEditorComposite
.copyDataToClipboard();
223 public void update(CdmDataChangeMap arg0
) {
227 public boolean canAttachMedia() {
232 public void changed(Object element
) {
233 if(element
instanceof DerivedUnitFacade
){
234 DerivedUnit derivedUnit
= ((DerivedUnitFacade
) element
).innerDerivedUnit();
235 if(derivedUnit
!=null){
236 getEditorInput().addSaveCandidate(derivedUnit
);
238 FieldUnit fieldUnit
= ((DerivedUnitFacade
) element
).innerFieldUnit();
240 getEditorInput().addSaveCandidate(fieldUnit
);
243 else if (element
instanceof CdmBase
) {
244 if (element
instanceof DescriptionBase
){
245 if (element
instanceof TaxonNameDescription
){
246 TaxonName changedName
= ((TaxonNameDescription
)element
).getTaxonName();
247 if (getEditorInput() instanceof TaxonEditorInput
){
248 IStructuredSelection sel
= getSelection();
249 Object firstElement
= sel
.getFirstElement();
250 if (firstElement
instanceof TaxonBase
){
251 getEditorInput().addSaveCandidate((TaxonBase
)firstElement
);
254 getEditorInput().addSaveCandidate(changedName
);
257 input
.replaceInModel(changedName
);
258 }else if (element
instanceof TaxonDescription
){
259 Taxon changedTaxon
= ((TaxonDescription
)element
).getTaxon();
260 getEditorInput().addSaveCandidate(changedTaxon
);
261 input
.replaceInModel(changedTaxon
);
263 }else if (element
instanceof DescriptionElementBase
){
264 if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonNameDescription
){
265 TaxonName changedName
= ((TaxonNameDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxonName();
266 //check whether the bulk editor is taxon or name bulk editor
267 if (getEditorInput() instanceof TaxonEditorInput
){
268 IStructuredSelection sel
= getSelection();
269 Object firstElement
= sel
.getFirstElement();
270 if (firstElement
instanceof TaxonBase
){
271 getEditorInput().addSaveCandidate((TaxonBase
)firstElement
);
274 getEditorInput().addSaveCandidate(changedName
);
276 input
.replaceInModel(changedName
);
277 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonDescription
){
278 Taxon changedTaxon
= ((TaxonDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxon();
279 getEditorInput().addSaveCandidate(changedTaxon
);
280 input
.replaceInModel(changedTaxon
);
281 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof SpecimenDescription
){
282 SpecimenOrObservationBase changedSpecimen
= ((SpecimenDescription
)((DescriptionElementBase
)element
).getInDescription()).getDescribedSpecimenOrObservation();
283 getEditorInput().addSaveCandidate(changedSpecimen
);
284 input
.replaceInModel(changedSpecimen
);
286 }else if (element
instanceof Media
){
287 IStructuredSelection sel
= getSelection();
288 Object firstElement
= sel
.getFirstElement();
289 if (firstElement
instanceof TaxonBase
){
290 TaxonBase changedTaxon
= (TaxonBase
)sel
.getFirstElement();
291 getEditorInput().addSaveCandidate(changedTaxon
);
292 input
.replaceInModel(changedTaxon
);
293 }else if (firstElement
instanceof SpecimenOrObservationBase
){
294 SpecimenOrObservationBase changedSpecimen
= (SpecimenOrObservationBase
)sel
.getFirstElement();
295 getEditorInput().addSaveCandidate(changedSpecimen
);
296 input
.replaceInModel(changedSpecimen
);
297 } else if (firstElement
instanceof Media
){
298 getEditorInput().addSaveCandidate((Media
)element
);
299 input
.replaceInModel((Media
)element
);
301 }else if(element
instanceof Group
){
302 Group oldGroup
= ((GroupEditorInput
)input
).getEntityFromModel((Group
)element
);
303 ((GroupEditorInput
)input
).getSaveUserCandidates().addAll(oldGroup
.getMembers());
304 getEditorInput().addSaveCandidate((Group
)element
);
305 input
.replaceInModel((CdmBase
) element
);
307 getEditorInput().addSaveCandidate((CdmBase
)element
);
308 input
.replaceInModel((CdmBase
) element
);
311 dirty
.setDirty(true);
312 setSelection(new StructuredSelection(element
));
316 public void forceDirty() {
317 dirty
.setDirty(true);
321 public boolean postOperation(Object objectAffectedByOperation
) {
326 public boolean onComplete() {
331 public ConversationHolder
getConversationHolder() {
335 public BulkEditorQuery
getLastQuery() {
339 public void setLastQuery(BulkEditorQuery lastQuery
) {
340 this.lastQuery
= lastQuery
;
345 public Taxon
getTaxon() {
346 IStructuredSelection selection
= getSelection();
347 if(selection
.size()==1) {
348 Object object
= selection
.iterator().next();
349 if(object
instanceof Taxon
){
350 return (Taxon
) object
;
357 public void update() {
358 input
.performSearch(lastQuery
, getSelection());
362 public void addOperation(AbstractPostOperation operation
) {
363 // operations not yet used for bulk editor
368 private void updateView(@UIEventTopic(WorkbenchEventConstants
.REFRESH_NAME_EDITOR
) List
<CdmBase
> cdmBases
) {
369 if (getEditorInput() instanceof TaxonEditorInput
){
370 for (CdmBase cdmBase
: cdmBases
){
371 if (getEditorInput().getModel().contains(cdmBase
)){
372 input
.performSearch(lastQuery
, getSelection());
381 private void updatefromDelete(@UIEventTopic(WorkbenchEventConstants
.REMOVE_USER
) User user
) {
382 if (input
instanceof GroupEditorInput
){
383 ((GroupEditorInput
)input
).getSaveUserCandidates().add(user
);
388 public TaxonNode
getTaxonNode() {