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
.util
.ArrayList
;
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
.core
.runtime
.NullProgressMonitor
;
21 import org
.eclipse
.e4
.ui
.di
.Persist
;
22 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
24 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
25 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
28 import org
.eclipse
.jface
.viewers
.CellEditor
;
29 import org
.eclipse
.jface
.viewers
.ICellModifier
;
30 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
31 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
32 import org
.eclipse
.jface
.viewers
.TableViewer
;
33 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
34 import org
.eclipse
.jface
.viewers
.TextCellEditor
;
35 import org
.eclipse
.swt
.SWT
;
36 import org
.eclipse
.swt
.layout
.FillLayout
;
37 import org
.eclipse
.swt
.layout
.GridData
;
38 import org
.eclipse
.swt
.layout
.GridLayout
;
39 import org
.eclipse
.swt
.widgets
.Composite
;
40 import org
.eclipse
.swt
.widgets
.Table
;
42 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
43 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
44 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
45 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
46 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
47 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
48 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
49 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
50 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
51 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
54 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
55 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
56 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
57 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
58 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
66 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
67 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
68 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
{
71 private MDirtyable dirty
;
73 private AbstractBulkEditorInput
<?
> input
;
75 private Composite topComposite
;
77 private TableViewer viewer
;
79 private ConversationHolder conversation
;
82 private ESelectionService selService
;
84 private ISelectionChangedListener selectionChangedListener
;
87 private MPart thisPart
;
90 public BulkEditorE4() {
93 public void init(AbstractBulkEditorInput
<?
> input
){
95 this.conversation
= input
.getConversation();
97 BulkEditorSearchE4 searchBar
= new BulkEditorSearchE4(this, topComposite
, SWT
.NONE
);
98 //layout needed because the search bar is added after @PostConstuct method
99 topComposite
.getParent().layout();
101 thisPart
.setLabel(input
.getName());
104 Table table
= viewer
.getTable();
105 String
[] titles
= {input
.getName(), "Type"};
106 int[] bounds
= {500, 100};
108 CellEditor
[] editors
= new CellEditor
[titles
.length
];
109 for (int i
= 0; i
< titles
.length
; i
++) {
110 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
111 editors
[i
] = new TextCellEditor(table
);
113 column
.getColumn().setText(titles
[i
]);
114 column
.getColumn().setWidth(bounds
[i
]);
115 column
.getColumn().setResizable(true);
116 column
.getColumn().setMoveable(true);
118 table
.setHeaderVisible(true);
119 table
.setLinesVisible(true);
120 viewer
.setCellEditors(editors
);
121 viewer
.setColumnProperties(titles
);
123 //content and label provider (NOTE: has to be set AFTER creating cell editors
124 viewer
.setContentProvider(new ArrayContentProvider());
125 BulkEditorLabelProvider labelProvider
= new BulkEditorLabelProvider(this);
126 viewer
.setLabelProvider(labelProvider
);
129 //allow text selection
130 viewer
.setCellModifier(new ICellModifier() {
132 public void modify(Object element
, String property
, Object value
) {
135 public Object
getValue(Object element
, String property
) {
136 ITableLabelProvider tableLabelProvider
= null;
137 if(viewer
.getLabelProvider() instanceof ITableLabelProvider
){
138 tableLabelProvider
= (ITableLabelProvider
) viewer
.getLabelProvider();
140 Object
[] columnProperties
= viewer
.getColumnProperties();
141 for (int i
=0;i
<columnProperties
.length
;i
++) {
142 if(columnProperties
[i
].equals(property
) && tableLabelProvider
!=null){
143 return tableLabelProvider
.getColumnText(element
, i
);
149 public boolean canModify(Object element
, String property
) {
154 if(input
.getEntityUuid()!=null){
155 performSearch(new BulkEditorQuery(input
.getEntityUuid().toString(), null));
161 public void createPartControl(Composite parent
, EMenuService menuService
) {
162 parent
.setLayout(new GridLayout());
164 topComposite
= new Composite(parent
, SWT
.NONE
);
165 topComposite
.setLayout(new GridLayout());
167 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
168 topComposite
.setLayoutData(gridData
);
170 Composite bottomComposite
= new Composite(parent
, SWT
.NONE
);
171 bottomComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
172 bottomComposite
.setLayout(new FillLayout());
173 viewer
= new TableViewer(bottomComposite
, SWT
.MULTI
| SWT
.H_SCROLL
174 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
176 // createColumns(viewer);
178 //propagate selection
179 selectionChangedListener
= (event
-> selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
)));
180 viewer
.addSelectionChangedListener(selectionChangedListener
);
182 //create context menu
183 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor"); //$NON-NLS-1$
189 public void save(IProgressMonitor monitor
) {
194 public void dispose() {
195 if(conversation
!=null){
196 conversation
.unregisterForDataStoreChanges(this);
197 conversation
.close();
204 public void save(IProgressMonitor monitor
, boolean resetMerge
) {
205 input
.saveModel(resetMerge
);
207 // commit the conversation and start a new transaction immediately
208 conversation
.commit(true);
210 Object object
= viewer
.getInput();
211 List
<CdmBase
> cdmBaseList
= new ArrayList
<>();
212 List
<CdmBase
> saveCandidates
= getEditorInput().getSaveCandidates();
213 // if(saveCandidates != null && !saveCandidates.isEmpty()){
214 // for (CdmBase item: saveCandidates) {
216 // cdmBaseList.add(item);
219 if (!saveCandidates
.isEmpty()){
220 CdmStore
.getService(saveCandidates
.get(0)).merge(saveCandidates
, true);
222 dirty
.setDirty(false);
223 getEditorInput().resetSaveCandidates();
229 public void performSearch(BulkEditorQuery query
) {
232 // TODO check if dirty, prompt save
234 String
[] labels
= {Messages
.BulkEditorE4_SAVE_AND_SEARCH
, Messages
.BulkEditorE4_DONT_SAVE
,Messages
.BulkEditorE4_CANCEL
};
235 MessageDialog dialog
=new MessageDialog(topComposite
.getShell(), Messages
.BulkEditorE4_SAVE_CHANGES_TITLE
, null, Messages
.BulkEditorE4_SAVE_CHANGES_MESSAGE
, MessageDialog
.QUESTION
,labels
, 0);
236 int result
= dialog
.open();
238 save(new NullProgressMonitor());
239 } else if (result
== 2){
243 dirty
.setDirty(false);
244 getEditorInput().dispose();
245 getEditorInput().bind();
246 getEditorInput().performSearch(query
);
248 viewer
.setInput(getEditorInput().getModel());
253 public void refresh() {
257 public void refresh(boolean resetInput
) {
259 viewer
.setInput(getEditorInput().getModel());
264 public TableViewer
getViewer() {
268 public void setDirty(){
269 dirty
.setDirty(true);
272 public boolean isDirty() {
273 return dirty
.isDirty();
276 public AbstractBulkEditorInput
getEditorInput() {
284 public void update(CdmDataChangeMap arg0
) {
291 public boolean canAttachMedia() {
299 public void changed(Object element
) {
300 if (element
instanceof CdmBase
) {
301 getEditorInput().addSaveCandidate((CdmBase
)element
);
303 dirty
.setDirty(true);
310 public void forceDirty() {
311 dirty
.setDirty(true);
318 public boolean postOperation(CdmBase objectAffectedByOperation
) {
326 public boolean onComplete() {
334 public ConversationHolder
getConversationHolder() {