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
;
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
.TaxonDescription
;
41 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonName
;
43 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
47 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
48 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
49 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
50 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
51 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
52 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
56 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
57 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
58 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
66 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
67 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
68 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
, ITaxonEditor
{
71 private MDirtyable dirty
;
73 private AbstractBulkEditorInput input
;
75 private ConversationHolder conversation
;
78 private IEventBroker eventBroker
;
81 IEclipseContext context
;
84 private MPart thisPart
;
86 private BulkEditorQuery lastQuery
= null;
88 private BulkEditorE4Composite bulkEditorComposite
;
91 public BulkEditorE4() {
94 @SuppressWarnings("unused")
95 public void init(AbstractBulkEditorInput
<?
> input
){
97 this.conversation
= input
.getConversation();
98 thisPart
.setLabel(input
.getEditorName());
100 bulkEditorComposite
.init(input
);
105 public void createPartControl(Composite parent
, IEclipseContext context
) {
106 bulkEditorComposite
= new BulkEditorE4Composite(this, parent
, SWT
.NONE
);
107 ContextInjectionFactory
.inject(bulkEditorComposite
, context
);
112 public void save(IProgressMonitor monitor
) {
116 public void save(IProgressMonitor monitor
, boolean resetMerge
) {
117 if (!input
.getCdmEntitySession().isActive()){
118 input
.getCdmEntitySession().bind();
120 input
.saveModel(resetMerge
);
122 IStructuredSelection selection
= getSelection();
124 dirty
.setDirty(false);
127 conversation
.commit(true);
129 if (lastQuery
!= null){
130 bulkEditorComposite
.performSearch(lastQuery
, selection
);
136 public void setFocus() {
137 //make sure to bind again if maybe in another view the conversation was unbound
138 if(conversation
!=null && !conversation
.isBound()){
141 if(input
!=null && input
.getCdmEntitySession()!= null) {
142 input
.getCdmEntitySession().bind();
145 //make sure to bind again if maybe in another view the conversation was unbound
146 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, this);
150 public void dispose() {
151 if(conversation
!=null){
152 conversation
.unregisterForDataStoreChanges(this);
153 conversation
.close();
158 dirty
.setDirty(false);
159 //save table settings
160 if(bulkEditorComposite
.getNatTableState()!=null){
161 try (FileOutputStream tableStateStream
=
162 new FileOutputStream(bulkEditorComposite
.getStatePropertiesFile())) {
163 bulkEditorComposite
.getNatTableState().store(tableStateStream
, null);
164 } catch (IOException ioe
) {
165 ioe
.printStackTrace();
172 private void updateAfterSearch(@UIEventTopic(WorkbenchEventConstants
.BULK_EDITOR_SEARCH_FINISHED
)IStructuredSelection selection
){
176 setSelection(selection
);
180 public void refresh(){
181 bulkEditorComposite
.refresh();
184 public IStructuredSelection
getSelection(){
185 return bulkEditorComposite
.getSelection();
188 public void setSelection(IStructuredSelection selection
){
189 bulkEditorComposite
.setSelection(selection
);
192 public void setDirty(boolean isDirty
){
193 dirty
.setDirty(isDirty
);
196 public void setDirty(){
201 public boolean isDirty() {
202 return dirty
.isDirty();
205 public AbstractBulkEditorInput
getEditorInput() {
209 public void copyDataToClipboard() {
210 bulkEditorComposite
.copyDataToClipboard();
214 public void update(CdmDataChangeMap arg0
) {
218 public boolean canAttachMedia() {
223 public void changed(Object element
) {
224 if(element
instanceof DerivedUnitFacade
){
225 DerivedUnit derivedUnit
= ((DerivedUnitFacade
) element
).innerDerivedUnit();
226 if(derivedUnit
!=null){
227 getEditorInput().addSaveCandidate(derivedUnit
);
229 FieldUnit fieldUnit
= ((DerivedUnitFacade
) element
).innerFieldUnit();
231 getEditorInput().addSaveCandidate(fieldUnit
);
234 else if (element
instanceof CdmBase
) {
235 if (element
instanceof DescriptionBase
){
236 if (element
instanceof TaxonNameDescription
){
237 TaxonName changedName
= ((TaxonNameDescription
)element
).getTaxonName();
238 getEditorInput().addSaveCandidate(changedName
);
239 input
.replaceInModel(changedName
);
240 }else if (element
instanceof TaxonDescription
){
241 Taxon changedTaxon
= ((TaxonDescription
)element
).getTaxon();
242 getEditorInput().addSaveCandidate(changedTaxon
);
243 input
.replaceInModel(changedTaxon
);
245 }else if (element
instanceof DescriptionElementBase
){
246 if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonNameDescription
){
247 TaxonName changedName
= ((TaxonNameDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxonName();
248 getEditorInput().addSaveCandidate(changedName
);
249 input
.replaceInModel(changedName
);
250 }else if (((DescriptionElementBase
)element
).getInDescription() instanceof TaxonDescription
){
251 Taxon changedTaxon
= ((TaxonDescription
)((DescriptionElementBase
)element
).getInDescription()).getTaxon();
252 getEditorInput().addSaveCandidate(changedTaxon
);
253 input
.replaceInModel(changedTaxon
);
256 getEditorInput().addSaveCandidate((CdmBase
)element
);
257 input
.replaceInModel((CdmBase
) element
);
260 dirty
.setDirty(true);
261 setSelection(new StructuredSelection(element
));
265 public void forceDirty() {
266 dirty
.setDirty(true);
270 public boolean postOperation(Object objectAffectedByOperation
) {
275 public boolean onComplete() {
280 public ConversationHolder
getConversationHolder() {
284 public BulkEditorQuery
getLastQuery() {
288 public void setLastQuery(BulkEditorQuery lastQuery
) {
289 this.lastQuery
= lastQuery
;
294 public Taxon
getTaxon() {
295 IStructuredSelection selection
= getSelection();
296 if(selection
.size()==1) {
297 Object object
= selection
.iterator().next();
298 if(object
instanceof Taxon
){
299 return (Taxon
) object
;