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
.List
;
14 import javax
.annotation
.PostConstruct
;
15 import javax
.inject
.Inject
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
19 import org
.eclipse
.e4
.ui
.di
.Persist
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.MDirtyable
;
21 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
22 import org
.eclipse
.e4
.ui
.services
.EMenuService
;
23 import org
.eclipse
.e4
.ui
.workbench
.modeling
.ESelectionService
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
26 import org
.eclipse
.jface
.viewers
.CellEditor
;
27 import org
.eclipse
.jface
.viewers
.ICellModifier
;
28 import org
.eclipse
.jface
.viewers
.ISelectionChangedListener
;
29 import org
.eclipse
.jface
.viewers
.ITableLabelProvider
;
30 import org
.eclipse
.jface
.viewers
.TableViewer
;
31 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
32 import org
.eclipse
.jface
.viewers
.TextCellEditor
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.layout
.FillLayout
;
35 import org
.eclipse
.swt
.layout
.GridData
;
36 import org
.eclipse
.swt
.layout
.GridLayout
;
37 import org
.eclipse
.swt
.widgets
.Composite
;
38 import org
.eclipse
.swt
.widgets
.Table
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
41 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
42 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
43 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.CdmDataChangeMap
;
44 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditorQuery
;
45 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
46 import eu
.etaxonomy
.taxeditor
.l10n
.Messages
;
47 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
48 import eu
.etaxonomy
.taxeditor
.model
.IDerivedUnitFacadePart
;
49 import eu
.etaxonomy
.taxeditor
.model
.IDirtyMarkable
;
50 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasDetails
;
51 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasFactualData
;
52 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasMedia
;
53 import eu
.etaxonomy
.taxeditor
.model
.IPartContentHasSupplementalData
;
54 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
56 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
64 public class BulkEditorE4
implements IPartContentHasDetails
, IConversationEnabled
, IPostOperationEnabled
,
65 IDirtyMarkable
, IDerivedUnitFacadePart
, IPartContentHasFactualData
,
66 IPartContentHasSupplementalData
, IPartContentHasMedia
, IE4SavablePart
{
69 private MDirtyable dirty
;
71 private AbstractBulkEditorInput
<?
> input
;
73 private Composite topComposite
;
75 private TableViewer viewer
;
77 private ConversationHolder conversation
;
80 private ESelectionService selService
;
82 private ISelectionChangedListener selectionChangedListener
;
85 private MPart thisPart
;
88 public BulkEditorE4() {
91 public void init(AbstractBulkEditorInput
<?
> input
){
93 AbstractBulkEditorInput
<?
> bulkEditorInput
= input
;
94 BulkEditorSearchE4 searchBar
= new BulkEditorSearchE4(this, topComposite
, SWT
.NONE
);
95 //layout needed because the search bar is added after @PostConstuct method
96 topComposite
.getParent().layout();
98 thisPart
.setLabel(input
.getName());
101 Table table
= viewer
.getTable();
102 String
[] titles
= {input
.getName(), "Type"};
103 int[] bounds
= {500, 100};
105 CellEditor
[] editors
= new CellEditor
[titles
.length
];
106 for (int i
= 0; i
< titles
.length
; i
++) {
107 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
108 editors
[i
] = new TextCellEditor(table
);
109 column
.getColumn().setText(titles
[i
]);
110 column
.getColumn().setWidth(bounds
[i
]);
111 column
.getColumn().setResizable(true);
112 column
.getColumn().setMoveable(true);
114 table
.setHeaderVisible(true);
115 table
.setLinesVisible(true);
116 viewer
.setCellEditors(editors
);
117 viewer
.setColumnProperties(titles
);
119 //content and label provider (NOTE: has to be set AFTER creating cell editors
120 viewer
.setContentProvider(new ArrayContentProvider());
121 BulkEditorLabelProvider labelProvider
= new BulkEditorLabelProvider(this);
122 viewer
.setLabelProvider(labelProvider
);
125 //allow text selection
126 viewer
.setCellModifier(new ICellModifier() {
128 public void modify(Object element
, String property
, Object value
) {
131 public Object
getValue(Object element
, String property
) {
132 ITableLabelProvider tableLabelProvider
= null;
133 if(viewer
.getLabelProvider() instanceof ITableLabelProvider
){
134 tableLabelProvider
= (ITableLabelProvider
) viewer
.getLabelProvider();
136 Object
[] columnProperties
= viewer
.getColumnProperties();
137 for (int i
=0;i
<columnProperties
.length
;i
++) {
138 if(columnProperties
[i
].equals(property
) && tableLabelProvider
!=null){
139 return tableLabelProvider
.getColumnText(element
, i
);
145 public boolean canModify(Object element
, String property
) {
150 if(bulkEditorInput
.getEntityUuid()!=null){
151 performSearch(new BulkEditorQuery(bulkEditorInput
.getEntityUuid().toString(), null));
157 public void createPartControl(Composite parent
, EMenuService menuService
) {
158 if (CdmStore
.isActive()){
159 if(conversation
== null){
160 conversation
= CdmStore
.createConversation();
167 parent
.setLayout(new GridLayout());
169 topComposite
= new Composite(parent
, SWT
.NONE
);
170 topComposite
.setLayout(new GridLayout());
172 GridData gridData
= new GridData(SWT
.FILL
, SWT
.TOP
, true, false);
173 topComposite
.setLayoutData(gridData
);
175 Composite bottomComposite
= new Composite(parent
, SWT
.NONE
);
176 bottomComposite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
177 bottomComposite
.setLayout(new FillLayout());
178 viewer
= new TableViewer(bottomComposite
, SWT
.MULTI
| SWT
.H_SCROLL
179 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
181 // createColumns(viewer);
183 //propagate selection
184 selectionChangedListener
= (event
-> selService
.setSelection(AbstractUtility
.getElementsFromSelectionChangedEvent(event
)));
185 viewer
.addSelectionChangedListener(selectionChangedListener
);
187 //create context menu
188 menuService
.registerContextMenu(viewer
.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.bulkeditor"); //$NON-NLS-1$
194 public void save(IProgressMonitor monitor
) {
197 // commit the conversation and start a new transaction immediately
198 conversation
.commit(true);
200 Object object
= viewer
.getInput();
201 if(object
instanceof List
){
202 for (Object item
: (List
)object
) {
203 CdmBase cdmBase
= (CdmBase
) item
;
204 CdmStore
.getService(cdmBase
).merge(cdmBase
, true);
207 dirty
.setDirty(false);
213 public void performSearch(BulkEditorQuery query
) {
216 // TODO check if dirty, prompt save
218 String
[] labels
= {Messages
.BulkEditorE4_SAVE_AND_SEARCH
, Messages
.BulkEditorE4_DONT_SAVE
,Messages
.BulkEditorE4_CANCEL
};
219 MessageDialog dialog
=new MessageDialog(topComposite
.getShell(), Messages
.BulkEditorE4_SAVE_CHANGES_TITLE
, null, Messages
.BulkEditorE4_SAVE_CHANGES_MESSAGE
, MessageDialog
.QUESTION
,labels
, 0);
220 int result
= dialog
.open();
222 save(new NullProgressMonitor());
223 } else if (result
== 2){
228 getEditorInput().performSearch(query
);
230 viewer
.setInput(getEditorInput().getModel());
235 public void refresh() {
239 public void refresh(boolean resetInput
) {
241 viewer
.setInput(getEditorInput().getModel());
246 public TableViewer
getViewer() {
250 public void setDirty(){
251 dirty
.setDirty(true);
254 public boolean isDirty() {
255 return dirty
.isDirty();
258 public AbstractBulkEditorInput
getEditorInput() {
266 public void update(CdmDataChangeMap arg0
) {
273 public boolean canAttachMedia() {
281 public void changed(Object element
) {
282 dirty
.setDirty(true);
289 public void forceDirty() {
290 dirty
.setDirty(true);
297 public boolean postOperation(CdmBase objectAffectedByOperation
) {
305 public boolean onComplete() {
313 public ConversationHolder
getConversationHolder() {