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
.EventObject
;
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
.core
.services
.events
.IEventBroker
;
22 import org
.eclipse
.e4
.ui
.di
.Focus
;
23 import org
.eclipse
.e4
.ui
.di
.Persist
;
24 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
25 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
26 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
27 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
30 import org
.eclipse
.jface
.viewers
.CellEditor
;
31 import org
.eclipse
.jface
.viewers
.ColumnViewerEditor
;
32 import org
.eclipse
.jface
.viewers
.ColumnViewerEditorActivationEvent
;
33 import org
.eclipse
.jface
.viewers
.ColumnViewerEditorActivationStrategy
;
34 import org
.eclipse
.jface
.viewers
.ICellModifier
;
35 import org
.eclipse
.jface
.viewers
.ISelection
;
36 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
37 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
38 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
39 import org
.eclipse
.jface
.viewers
.TableViewer
;
40 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
41 import org
.eclipse
.jface
.viewers
.TableViewerEditor
;
42 import org
.eclipse
.jface
.viewers
.TableViewerFocusCellManager
;
43 import org
.eclipse
.jface
.viewers
.TextCellEditor
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.MouseEvent
;
46 import org
.eclipse
.swt
.layout
.FillLayout
;
47 import org
.eclipse
.swt
.layout
.GridData
;
48 import org
.eclipse
.swt
.layout
.GridLayout
;
49 import org
.eclipse
.swt
.widgets
.Composite
;
50 import org
.eclipse
.swt
.widgets
.Table
;
52 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
53 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
54 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
55 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
56 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
57 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
58 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
59 import eu
.etaxonomy
.taxeditor
.editor
.ITaxonEditor
;
60 import eu
.etaxonomy
.taxeditor
.event
.WorkbenchEventConstants
;
61 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
62 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
63 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
64 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
65 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
66 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
67 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
68 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
69 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
70 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
78 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
79 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
80 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
, ITaxonEditor
{
83 private MDirtyable dirty
;
85 private AbstractBulkEditorInput
<?
> input
;
87 private Composite topComposite
;
89 private TableViewer viewer
;
91 private ConversationHolder conversation
;
94 private ESelectionService selService
;
97 private IEventBroker eventBroker
;
99 private ISelectionChangedListener selectionChangedListener
;
102 private MPart thisPart
;
104 private BulkEditorQuery lastQuery
= null;
107 public BulkEditorE4() {
110 public void init(AbstractBulkEditorInput
<?
> input
){
112 this.conversation
= input
.getConversation();
114 BulkEditorSearchE4 searchBar
= new BulkEditorSearchE4(this, topComposite
, SWT
.NONE
);
115 //layout needed because the search bar is added after @PostConstuct method
116 topComposite
.getParent().layout();
118 thisPart
.setLabel(input
.getName());
121 Table table
= viewer
.getTable();
122 String
[] titles
= {input
.getName(), "Type"};
123 int[] bounds
= {500, 100};
125 ColumnViewerEditorActivationStrategy activationSupport
= new ColumnViewerEditorActivationStrategy(viewer
) {
127 protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event
) {
128 // Enable editor only with mouse double click
129 if (event
.eventType
== ColumnViewerEditorActivationEvent
.MOUSE_DOUBLE_CLICK_SELECTION
) {
130 EventObject source
= event
.sourceEvent
;
131 if (source
instanceof MouseEvent
&& ((MouseEvent
)source
).button
== 3) {
141 TableViewerFocusCellManager focusCellManager
= new TableViewerFocusCellManager(viewer
, new BulkEditorFocusCellOwnerDrawHighLighter(viewer
));
143 TableViewerEditor
.create(viewer
, focusCellManager
, activationSupport
, ColumnViewerEditor
.TABBING_HORIZONTAL
|
144 ColumnViewerEditor
.TABBING_MOVE_TO_ROW_NEIGHBOR
|
145 ColumnViewerEditor
.TABBING_VERTICAL
|
146 ColumnViewerEditor
.KEYBOARD_ACTIVATION
);
148 CellEditor
[] editors
= new CellEditor
[titles
.length
];
149 for (int i
= 0; i
< titles
.length
; i
++) {
150 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
151 editors
[i
] = new TextCellEditor(table
);
153 column
.getColumn().setText(titles
[i
]);
154 column
.getColumn().setWidth(bounds
[i
]);
155 column
.getColumn().setResizable(true);
156 column
.getColumn().setMoveable(true);
160 table
.setHeaderVisible(true);
161 table
.setLinesVisible(true);
163 viewer
.setCellEditors(editors
);
164 viewer
.setColumnProperties(titles
);
166 //content and label provider (NOTE: has to be set AFTER creating cell editors
167 viewer
.setContentProvider(new ArrayContentProvider());
168 BulkEditorLabelProvider labelProvider
= new BulkEditorLabelProvider(this);
169 viewer
.setLabelProvider(labelProvider
);
172 //allow text selection
173 viewer
.setCellModifier(new ICellModifier() {
175 public void modify(Object element
, String property
, Object value
) {
178 public Object
getValue(Object element
, String property
) {
179 ITableLabelProvider tableLabelProvider
= null;
180 if(viewer
.getLabelProvider() instanceof ITableLabelProvider
){
181 tableLabelProvider
= (ITableLabelProvider
) viewer
.getLabelProvider();
183 Object
[] columnProperties
= viewer
.getColumnProperties();
184 for (int i
=0;i
<columnProperties
.length
;i
++) {
185 if(columnProperties
[i
].equals(property
) && tableLabelProvider
!=null){
186 return tableLabelProvider
.getColumnText(element
, i
);
192 public boolean canModify(Object element
, String property
) {
199 if(input
.getEntityUuid()!=null){
200 performSearch(new BulkEditorQuery(input
.getEntityUuid().toString(), null));
206 public void createPartControl(Composite parent
, EMenuService menuService
) {
207 parent
.setLayout(new GridLayout());
209 topComposite
= new Composite(parent
, SWT
.NONE
);
210 topComposite
.setLayout(new GridLayout());
212 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
213 topComposite
.setLayoutData(gridData
);
215 Composite bottomComposite
= new Composite(parent
, SWT
.NONE
);
216 bottomComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
217 bottomComposite
.setLayout(new FillLayout());
218 viewer
= new TableViewer(bottomComposite
, SWT
.MULTI
| SWT
.FULL_SELECTION
);
220 // createColumns(viewer);
222 //propagate selection
223 selectionChangedListener
= (event
-> selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
)));
224 viewer
.addSelectionChangedListener(selectionChangedListener
);
226 //create context menu
227 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor"); //$NON-NLS-1$
233 public void save(IProgressMonitor monitor
) {
238 public void setFocus() {
239 //make sure to bind again if maybe in another view the conversation was unbound
240 eventBroker
.post(WorkbenchEventConstants
.CURRENT_ACTIVE_EDITOR
, this);
244 public void dispose() {
245 if(conversation
!=null){
246 conversation
.unregisterForDataStoreChanges(this);
247 conversation
.close();
254 public void save(IProgressMonitor monitor
, boolean resetMerge
) {
255 if (!input
.getCdmEntitySession().isActive()){
256 input
.getCdmEntitySession().bind();
258 input
.saveModel(resetMerge
);
260 List
<CdmBase
> saveCandidates
= getEditorInput().getSaveCandidates();
263 dirty
.setDirty(false);
264 getEditorInput().resetSaveCandidates();
265 getEditorInput().dispose();
266 getEditorInput().bind();
267 conversation
.commit(true);
269 if (lastQuery
!= null){
270 performSearch(lastQuery
);
278 public void performSearch(BulkEditorQuery query
) {
281 // TODO check if dirty, prompt save
283 String
[] labels
= {Messages
.BulkEditorE4_SAVE_AND_SEARCH
, Messages
.BulkEditorE4_DONT_SAVE
,Messages
.BulkEditorE4_CANCEL
};
284 MessageDialog dialog
=new MessageDialog(topComposite
.getShell(), Messages
.BulkEditorE4_SAVE_CHANGES_TITLE
, null, Messages
.BulkEditorE4_SAVE_CHANGES_MESSAGE
, MessageDialog
.QUESTION
,labels
, 0);
285 int result
= dialog
.open();
287 save(new NullProgressMonitor());
288 } else if (result
== 2){
292 dirty
.setDirty(false);
293 // getEditorInput().dispose();
294 // getEditorInput().bind();
295 getEditorInput().performSearch(query
);
297 viewer
.setInput(getEditorInput().getModel());
302 public void refresh() {
306 public void refresh(boolean resetInput
) {
308 viewer
.setInput(getEditorInput().getModel());
313 public TableViewer
getViewer() {
317 public void setDirty(){
318 dirty
.setDirty(true);
321 public boolean isDirty() {
322 return dirty
.isDirty();
325 public AbstractBulkEditorInput
getEditorInput() {
333 public void update(CdmDataChangeMap arg0
) {
340 public boolean canAttachMedia() {
348 public void changed(Object element
) {
349 if (element
instanceof CdmBase
) {
350 getEditorInput().addSaveCandidate((CdmBase
)element
);
352 dirty
.setDirty(true);
359 public void forceDirty() {
360 dirty
.setDirty(true);
367 public boolean postOperation(CdmBase objectAffectedByOperation
) {
375 public boolean onComplete() {
383 public ConversationHolder
getConversationHolder() {
390 public BulkEditorQuery
getLastQuery() {
397 public void setLastQuery(BulkEditorQuery lastQuery
) {
398 this.lastQuery
= lastQuery
;
406 public Taxon
getTaxon() {
407 ISelection selection
= viewer
.getSelection();
408 if(selection
instanceof IStructuredSelection
409 && ((IStructuredSelection
) selection
).size()==1) {
410 Object object
= ((IStructuredSelection
) selection
).iterator().next();
411 if(object
instanceof Taxon
){
412 return (Taxon
) object
;