ref #7854 Implement NatTable for distribution editor
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / checklist / e4 / ChecklistEditorE4.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.view.checklist.e4;
11
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.SortedSet;
20 import java.util.UUID;
21
22 import javax.annotation.PostConstruct;
23 import javax.annotation.PreDestroy;
24 import javax.inject.Inject;
25
26 import org.apache.log4j.Logger;
27 import org.eclipse.core.runtime.IProgressMonitor;
28 import org.eclipse.e4.ui.di.Focus;
29 import org.eclipse.e4.ui.di.Persist;
30 import org.eclipse.e4.ui.model.application.ui.MDirtyable;
31 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
32 import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
33 import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
34 import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
35 import org.eclipse.jface.viewers.ISelectionChangedListener;
36 import org.eclipse.jface.viewers.StructuredViewer;
37 import org.eclipse.jface.viewers.TableViewer;
38 import org.eclipse.jface.viewers.TableViewerColumn;
39 import org.eclipse.jface.viewers.TableViewerEditor;
40 import org.eclipse.jface.viewers.TableViewerFocusCellManager;
41 import org.eclipse.jface.wizard.WizardDialog;
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.events.ModifyListener;
44 import org.eclipse.swt.events.SelectionAdapter;
45 import org.eclipse.swt.events.SelectionEvent;
46 import org.eclipse.swt.layout.GridData;
47 import org.eclipse.swt.layout.GridLayout;
48 import org.eclipse.swt.widgets.Button;
49 import org.eclipse.swt.widgets.Composite;
50 import org.eclipse.swt.widgets.Display;
51 import org.eclipse.swt.widgets.Label;
52 import org.eclipse.swt.widgets.Table;
53 import org.eclipse.swt.widgets.TableColumn;
54 import org.eclipse.swt.widgets.Text;
55 import org.eclipse.swt.widgets.ToolBar;
56 import org.eclipse.swt.widgets.ToolItem;
57 import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
58
59 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
60 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
61 import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
62 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
63 import eu.etaxonomy.cdm.model.common.Representation;
64 import eu.etaxonomy.cdm.model.reference.Reference;
65 import eu.etaxonomy.cdm.model.taxon.Classification;
66 import eu.etaxonomy.cdm.model.taxon.Taxon;
67 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
68 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
69 import eu.etaxonomy.taxeditor.editor.EditorUtil;
70 import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
71 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
72 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
73 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistContentProvider;
74 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorComparator;
75 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
76 import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistLabelProvider;
77 import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
78 import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
79 import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistModifyListener;
80 import eu.etaxonomy.taxeditor.model.AbstractUtility;
81 import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
82 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
83 import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
84 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
85 import eu.etaxonomy.taxeditor.preference.Resources;
86 import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
87 import eu.etaxonomy.taxeditor.store.CdmStore;
88 import eu.etaxonomy.taxeditor.store.StoreUtil;
89 import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
90 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
91 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
92 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
93
94 /**
95 *
96 * @author k.luther
97 *
98 */
99 public class ChecklistEditorE4 implements IConversationEnabled,
100 IDirtyMarkable, IPartContentHasDetails, IE4SavablePart, IDistributionEditor, IPartContentHasSupplementalData{
101
102 private static final String DISTRIBUTION_EDITOR = "Distribution Editor";
103 private static final String LOADING_TAXA = Messages.ChecklistEditor_LOAD_TAXA;
104 private static final String UNKNOWN = Messages.ChecklistEditor_UNKNOWN;
105 private static final String ELEMENT_COUNT = Messages.ChecklistEditor_ELEMENT_COUNT;
106 public static final String TYPE_FILTER_TEXT = "type filter text"; //$NON-NLS-1$
107
108 private static final Logger logger = Logger.getLogger(ChecklistEditorE4.class);
109
110 /**
111 * Constant
112 * <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code>
113 */
114 public static String ID = "eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor"; //$NON-NLS-1$
115
116 private TableViewer viewer;
117
118 private ITaxonNodeService taxonNodeService;
119
120 private ChecklistEditorComparator comparator;
121
122 private ChecklistEditorInput checklistEditorInput;
123
124 private ConversationHolder conversation;
125
126 private Integer countNodes;
127
128 private List<TaxonNode> selectedTaxonNodes;
129
130 @Inject
131 private MPart thisPart;
132
133 @Inject
134 private MDirtyable dirty;
135
136 @Inject
137 private ESelectionService selService;
138
139 private ISelectionChangedListener selectionChangedListener;
140
141 private ChecklistEditorFilter filter;
142
143 private Label statusLabel;
144 private Label statusLabelSourceReference;
145
146 private ChecklistLabelProvider labelProvider;
147
148 private Map<UUID, Integer> areaPosition= new HashMap();
149
150 private ToolItem toolItem;
151 private ToolItem toolItem2;
152 private ChecklistDropdownSelectionListenerE4 dropListener;
153 private Text searchText;
154 private EntitySelectionElement<Reference> defaultReferenceSelection;
155
156 private Reference defaultSource = null;
157
158 public Reference getDefaultSource() {
159 return defaultSource;
160 }
161 public Map<UUID, Integer> getAreaPosition() {
162 return areaPosition;
163 }
164 /**
165 * @return the selectedTaxonNodes
166 */
167 public List<TaxonNode> getSelectedTaxonNodes() {
168 return selectedTaxonNodes;
169 }
170
171 /**
172 * @param selectedTaxonNodes
173 * the selectedTaxonNodes to set
174 */
175 public void setSelectedTaxonNodes(List<TaxonNode> selectedTaxonNodes) {
176 this.selectedTaxonNodes = selectedTaxonNodes;
177 }
178
179 @Inject
180 public ChecklistEditorE4() {
181 }
182
183 /** {@inheritDoc} */
184 @PostConstruct
185 public void createPartControl(Composite parent) {
186 if (!CdmStore.isActive()){
187 return;
188 }
189 taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
190 filter = new ChecklistEditorFilter();
191 createTopComposite(parent);
192 // createToolbar(parent);
193 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
194
195 //the focuzsCellManager should return the distribution!!!
196 TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter (viewer));
197 TableViewerEditor.create(viewer, focusCellManager, new ColumnViewerEditorActivationStrategy(viewer), TableViewerEditor.DEFAULT);
198
199 labelProvider = new ChecklistLabelProvider(this);
200
201 viewer.addFilter(filter);
202 createTable();
203
204 viewer.setContentProvider(new ChecklistContentProvider());
205 viewer.setLabelProvider(this.labelProvider);
206
207 comparator = new ChecklistEditorComparator();
208 viewer.setComparator(comparator);
209 final ModifyListener modifyListener = new ChecklistModifyListener(viewer, filter, searchText);
210
211 searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
212 searchText.addModifyListener(modifyListener);
213 createGridDataForViewerLayout();
214 createStatusBar(parent);
215
216 }
217
218 /**
219 * @param parent
220 */
221 private void createTopComposite(Composite parent) {
222 GridLayout gridLayout = new GridLayout(3, false);
223 gridLayout.marginWidth = 0;
224 gridLayout.marginHeight = 0;
225 parent.setLayout(gridLayout);
226
227 searchText = createSearchBar(parent);
228
229
230 }
231
232
233 /**
234 * @param parent
235 * @return
236 */
237 private void createSetDefaultReferenceButton(Composite parent) {
238 CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
239 defaultReferenceSelection = cdmFormFactory.createSelectionElement(Reference.class,
240 null, "Default Source Reference", null,
241 EntitySelectionElement.DELETABLE, SWT.NULL);
242
243
244
245
246
247 return;
248 }
249 private void createGridDataForViewerLayout() {
250 GridData gridData = new GridData();
251 gridData.verticalAlignment = GridData.FILL;
252 gridData.horizontalSpan = 3;
253 gridData.grabExcessHorizontalSpace = true;
254 gridData.grabExcessVerticalSpace = true;
255 gridData.horizontalAlignment = GridData.FILL;
256 viewer.getControl().setLayoutData(gridData);
257 }
258
259 /**
260 * @param parent
261 * @return
262 */
263 private Text createSearchBar(Composite parent) {
264
265 final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
266 GridData gridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
267
268 searchText.setLayoutData(gridData);
269 searchText.setForeground(EditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
270 searchText.setText(TYPE_FILTER_TEXT);
271 searchText.setToolTipText(Messages.ChecklistEditor_SEARCH_TOOLTIP);
272
273 Button button1 = new Button(parent, SWT.PUSH );
274 GridData gridData2 = new GridData();
275 gridData2.horizontalSpan = 1;
276 gridData2.horizontalAlignment = SWT.RIGHT;
277 button1.setLayoutData(gridData2);
278
279
280 button1.setText(Messages.ChecklistEditor_DIST_STATUS);
281 button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
282 button1.addSelectionListener(new SelectionAdapter() {
283 @Override
284 public void widgetSelected(SelectionEvent event) {
285 AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
286 WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
287 availableDistributionWizard);
288
289 int open = dialog.open();
290 if(open == 0){
291 reload();
292 }
293 }
294 });
295
296 Button button2 = new Button(parent, SWT.PUSH );
297 GridData gridData3 = new GridData();
298 gridData2.horizontalSpan = 1;
299 button2.setLayoutData(gridData3);
300
301
302 button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
303 button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
304 button2.addSelectionListener(new SelectionAdapter() {
305
306 @Override
307 public void widgetSelected(SelectionEvent event) {
308 defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
309
310 String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
311 if (defaultSourceStr.length()> 100){
312 defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
313 }
314 statusLabelSourceReference.setText(defaultSourceStr);
315
316 button2.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
317 }
318
319 });
320
321 parent.pack();
322 return searchText;
323 }
324
325 /**
326 * @param parent
327 */
328 private void createToolbar(Composite parent) {
329 ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
330 // toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
331
332 toolItem = new ToolItem(toolBar, SWT.PUSH| SWT.BORDER);
333
334
335 toolItem.setText(Messages.ChecklistEditor_DIST_STATUS);
336 toolItem.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
337 //createToolbarItems();
338 // toolItem.addSelectionListener(dropListener);
339 toolItem.addSelectionListener(new SelectionAdapter() {
340 @Override
341 public void widgetSelected(SelectionEvent event) {
342 AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
343 WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
344 availableDistributionWizard);
345
346 int open = dialog.open();
347 if(open == 0){
348 reload();
349 }
350 }
351 });
352 toolItem2 = new ToolItem(toolBar, SWT.PUSH | SWT.BORDER);
353
354
355 toolItem2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
356 toolItem2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
357 //createToolbarItems();
358 // toolItem.addSelectionListener(dropListener);
359 toolItem2.addSelectionListener(new SelectionAdapter() {
360 @Override
361 public void widgetSelected(SelectionEvent event) {
362 defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
363
364 }
365 });
366
367 toolBar.pack();
368 }
369
370
371
372 private void createStatusBar(Composite composite) {
373 GridData gridData = new GridData();
374 gridData.horizontalSpan = 1;
375 gridData.grabExcessHorizontalSpace = true;
376 gridData.horizontalAlignment = GridData.FILL;
377
378 statusLabel = new Label(composite, SWT.LEFT);
379 statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
380 statusLabel.setLayoutData(gridData);
381
382 statusLabelSourceReference = new Label(composite, SWT.RIGHT);
383 // GridData sourceGrid = new GridData();
384 // sourceGrid.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;
385 // //sourceGrid.grabExcessHorizontalSpace = false;
386 // sourceGrid.horizontalSpan = 1;
387 statusLabelSourceReference.setLayoutData(gridData);
388
389 if (defaultSource != null){
390 statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
391
392 }
393 }
394
395 private void createTable() {
396 Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
397 List<String> titles = new ArrayList<String>();
398 List<Integer> bounds = new ArrayList<Integer>();
399 if (PreferencesUtil.isShowRankInChecklistEditor()){
400 Collections.addAll(titles, Messages.ChecklistEditor_TAXON, Messages.ChecklistEditor_RANK);
401 Collections.addAll(bounds, 300, 200);
402 } else{
403 Collections.addAll(titles, Messages.ChecklistEditor_TAXON);
404 Collections.addAll(bounds, 300);
405 }
406
407
408 // Map<Integer, Boolean> restoreValuesForColumnWidth =
409 restoreValuesForColumnWidth(titles, bounds);
410 createInitalDistributionColumns(table, titles, bounds);
411 table.setSortDirection(SWT.DOWN);
412 table.setHeaderVisible(true);
413 table.setLinesVisible(true);
414 }
415 /**
416 * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
417 *<p>
418 *
419 *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
420 *
421 * @param table
422 * @param titles
423 * @param bounds
424 * @param restoreValuesForColumnWidth
425 */
426 private void createInitalDistributionColumns(Table table,
427 List<String> titles, List<Integer> bounds) {
428 for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
429 TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
430 column.getColumn().setText(titles.get(columnIndex));
431 column.getColumn().setWidth(bounds.get(columnIndex));
432 column.getColumn().setResizable(true);
433 column.getColumn().setMoveable(true);
434 column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
435 if (columnIndex == 0) {
436 table.setSortColumn(column.getColumn());
437 }
438 if (columnIndex == 1 && PreferencesUtil.isShowRankInChecklistEditor()) {
439 /** uncommented it for now because no rank editing is wanted **/
440
441 }
442 if ((columnIndex == 1 && !PreferencesUtil.isShowRankInChecklistEditor()) || columnIndex >= 2 ) {
443 column.getColumn().setWidth(50);
444 column.setEditingSupport(new DistributionEditingSupportE4(viewer, this, columnIndex));
445
446 }
447 }
448 }
449
450 /**
451 * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
452 *<p>
453 * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
454 *
455 * @param titles
456 * @param bounds
457 * @return Map<Integer, Boolean>
458 */
459 private void restoreValuesForColumnWidth(List<String> titles,
460 List<Integer> bounds) {
461 // Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
462 if (labelProvider.getNamedAreas(true) != null) {
463 int columnIndex;
464 if (PreferencesUtil.isShowRankInChecklistEditor()){
465 columnIndex = 2;
466 } else{
467 columnIndex = 1;
468 }
469 for (DefinedTermBase<DefinedTermBase> term : getLabelProvider().getNamedAreas(false)) {
470 if(term != null){
471 // restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
472 areaPosition.put(term.getUuid(), columnIndex);
473 Representation rep = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
474 String label = rep.getLabel();
475 if (label == null){
476 term.getTitleCache();
477 }
478 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
479 if (term.getIdInVocabulary() != null){
480 titles.add(term.getIdInVocabulary());
481 } else{
482 titles.add(label);
483 }
484 }else if (PreferencesUtil.isShowSymbol1InChecklistEditor()){
485 if (term.getSymbol() != null){
486 titles.add(term.getSymbol());
487 } else{
488 titles.add(label);
489 }
490 }else if (PreferencesUtil.isShowSymbol2InChecklistEditor()){
491 if (term.getSymbol2() != null){
492 titles.add(term.getSymbol2());
493 } else{
494 titles.add(label);
495 }
496 }else{
497 titles.add(label);
498 }
499 bounds.add(200);
500 columnIndex++;
501 }
502 }
503 }
504
505 }
506
507 /**
508 * This method adds new DistributionColumns to an existing table.
509 *
510 * @param title
511 * @param bound
512 * @param colNumber
513 * @return
514 */
515 private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber, DefinedTermBase term) {
516
517 final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.FULL_SELECTION);
518 final TableColumn column = viewerColumn.getColumn();
519 column.setText(title);
520 String[] UuidAndLable = new String[2];
521 UuidAndLable[0] = term.getUuid().toString();
522 UuidAndLable[1] = title;
523 column.setData(UuidAndLable);
524 column.setWidth(200);
525
526 viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, colNumber));
527 column.setResizable(true);
528 column.setMoveable(true);
529 return viewerColumn;
530 }
531
532
533
534 /**
535 *
536 * pull data from database and set input for view
537 *
538 */
539 private void loadDataInput() {
540 Classification classification = checklistEditorInput.getClassification();
541 TaxonNode taxonNode = checklistEditorInput.getTaxonNode();
542 if (classification != null && taxonNode == null) {
543 countNodes = taxonNodeService.countAllNodesForClassification(classification);
544 // statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
545 // This should not kill the view nor the editor if something goes
546 // wrong
547 // TODO: don't load the whole taxonNode Object but rather a small
548 // and simple Solution
549 // FIXME: May be don't open classification which are greater than
550 // 3000 Taxa
551 selectedTaxonNodes = taxonNodeService.listAllNodesForClassification(classification, 0, countNodes);
552 countNodes = selectedTaxonNodes.size();
553 statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
554 viewer.setInput(checklistEditorInput.getTaxa());
555 // Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
556 // checkListJob.schedule(Job.LONG);
557
558 }
559 if (taxonNode != null) {
560 boolean includeUnpublished = true;
561 selectedTaxonNodes = taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, NODE_INIT_STRATEGY, true, includeUnpublished, null);
562 countNodes = selectedTaxonNodes.size();
563 viewer.setInput(checklistEditorInput.getTaxa());
564 statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
565
566 }
567 }
568
569
570 @PreDestroy
571 public void dispose() {
572 if(conversation!=null){
573 conversation.unregisterForDataStoreChanges(this);
574 conversation.close();
575 }
576 conversation = null;
577 if(checklistEditorInput!=null){
578 checklistEditorInput.dispose();
579 }
580 dirty.setDirty(false);
581
582 }
583
584 /** {@inheritDoc} */
585 @Focus
586 public void setFocus() {
587 if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
588 viewer.getControl().setFocus();
589 }
590 }
591
592 //FIXME E4 migrate
593 // /*
594 // * (non-Javadoc)
595 // *
596 // * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
597 // */
598 // /** {@inheritDoc} */
599 // @Override
600 // public void showBusy(boolean busy) {
601 // super.showBusy(busy);
602 // // viewer.getTable().setEnabled(!busy);
603 // if (busy) {
604 // partNameCache = getPartName();
605 // setPartName(String.format(Messages.ChecklistEditor_LOAD_CNT_TAXA, countNodes));
606 // } else {
607 // if (partNameCache != null) {
608 // setPartName(partNameCache);
609 // }
610 // }
611 // }
612
613 /**
614 * <p>
615 * Getter for the field <code>viewer</code>.
616 * </p>
617 *
618 * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
619 */
620 public StructuredViewer getViewer() {
621 return viewer;
622 }
623
624 public void refresh() {
625 viewer.refresh();
626 }
627
628 /**
629 * This method should only be called for adding new Distribution columns and reloading the table.<br>
630 * It will hide the old distribution column and load the newly added columns.<br>
631 * <p>
632 * <b>Notice:</b> for data update please use <b>refresh()</b>
633 *
634 */
635 @SuppressWarnings({ "unchecked", "rawtypes" })
636 public void reload(){
637 //create new Items for Dropdown menue
638 Table table = viewer.getTable();
639 table.setRedraw(false);
640 SortedSet<DefinedTermBase> newTerms = labelProvider.getNamedAreas(true);
641
642
643 //check which terms are deleted and which are new.
644 TableColumn[] columns = viewer.getTable().getColumns() ;
645 int index = 0;
646
647 for (TableColumn column: columns){
648 if ((!PreferencesUtil.isShowRankInChecklistEditor() && index > 0) || (index > 1)) {
649 column.dispose();
650 }
651 index++;
652 }
653
654 areaPosition.clear();
655 int columnIndex;
656 if (PreferencesUtil.isShowRankInChecklistEditor()){
657 columnIndex = 2;
658 } else{
659 columnIndex = 1;
660 }
661 for (DefinedTermBase term:newTerms){
662 areaPosition.put(term.getUuid(), Integer.valueOf(columnIndex));
663 columnIndex++;
664 }
665
666 for (DefinedTermBase term: newTerms){
667 Representation rep = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
668
669 String termLable = rep.getAbbreviatedLabel();
670 if (termLable == null){
671 termLable = rep.getLabel();
672 }
673 if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
674 if (term.getIdInVocabulary() != null){
675 termLable = term.getIdInVocabulary();
676 }
677 }
678 TableViewerColumn column= addTableViewerColumn(termLable, 200, areaPosition.get(term.getUuid()), term);
679
680
681 }
682
683 viewer.setLabelProvider(labelProvider);
684 table.setRedraw(true);
685 viewer.refresh();
686 }
687
688 /**
689 *
690 */
691 private void updateColumnIndex() {
692 int columnIndex;
693 if (PreferencesUtil.isShowRankInChecklistEditor()){
694 columnIndex = 2;
695 } else{
696 columnIndex = 1;
697 }
698 for (UUID uuid:areaPosition.keySet()){
699 areaPosition.put(uuid, columnIndex);
700 columnIndex++;
701 }
702
703 }
704
705
706
707
708 /**
709 * <p>
710 * Getter for the field <code>service</code>.
711 * </p>
712 *
713 * @return the service
714 */
715 public IWorkbenchSiteProgressService getService() {
716 return TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IWorkbenchSiteProgressService.class);
717 }
718
719 private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
720 SelectionAdapter selectionAdapter = new SelectionAdapter() {
721 @Override
722 public void widgetSelected(SelectionEvent e) {
723 comparator.setColumn(index);
724 int dir = viewer.getTable().getSortDirection();
725 if (viewer.getTable().getSortColumn() == column) {
726 dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
727 } else {
728 dir = SWT.DOWN;
729 }
730 viewer.getTable().setSortDirection(dir);
731 viewer.getTable().setSortColumn(column);
732 viewer.refresh();
733 }
734 };
735 return selectionAdapter;
736 }
737
738 public void doSave(IProgressMonitor monitor) {
739 try {
740 // monitor.beginTask(Messages.ChecklistEditor_SAVE_EDITOR, 1);
741 // if (!conversation.isBound()) {
742 // conversation.bind();
743 // }
744
745 this.checklistEditorInput.merge();
746
747 //conversation.commit(true);
748 setDirty(false);
749 // monitor.worked(1);
750 } finally {
751 // monitor.done();
752 }
753
754 }
755
756 public void init(ChecklistEditorInput input) {
757 checklistEditorInput = input;
758 thisPart.setLabel(DISTRIBUTION_EDITOR+": " + checklistEditorInput.getName());
759 conversation = checklistEditorInput.getConversation();
760 conversation.registerForDataStoreChanges(this);
761
762 //propagate selection
763 selectionChangedListener = (event -> selService.setSelection(
764 DistributionEditorHelper.getDistributionForColumn(event, areaPosition)));
765
766 viewer.addSelectionChangedListener(selectionChangedListener);
767
768
769 loadDataInput();
770 }
771 private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
772 "descriptions.*", "description.state", "feature", "feature.*", "childNodes", "childNodes.taxon", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
773 "childNodes.taxon.name", "taxonNodes", "taxonNodes.*", "taxonNodes.taxon.*", "taxon.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
774 "taxon.descriptions", "taxon.sec", "taxon.name.*", "terms", "name.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
775 "name.rank.representations", "name.status.type.representations", "stateData.$" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
776
777 /*
778 * (non-Javadoc)
779 *
780 * @see
781 * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
782 * java.lang.Object)
783 */
784 @Override
785 public void changed(Object element) {
786 if (element != null) {
787 viewer.update(element, null);
788
789 if (element instanceof Taxon){
790 checklistEditorInput.addTaxonToSave((Taxon)element);
791 }
792 setDirty(true);
793 }
794 }
795
796 /* (non-Javadoc)
797 * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
798 */
799 @Override
800 public void forceDirty() {
801 changed(null);
802 }
803
804 public void setDirty(boolean dirty) {
805 this.dirty.setDirty(dirty);
806 }
807
808 /*
809 * (non-Javadoc)
810 *
811 * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
812 */
813 @Override
814 public boolean isDirty() {
815 return dirty.isDirty();
816 }
817
818
819 /**
820 * @return the labelProvider
821 */
822 public ChecklistLabelProvider getLabelProvider() {
823 return labelProvider;
824 }
825
826 /* (non-Javadoc)
827 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
828 */
829 @Override
830 public void update(CdmDataChangeMap arg0) {
831 // TODO Auto-generated method stub
832
833 }
834
835 /* (non-Javadoc)
836 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
837 */
838 @Override
839 public ConversationHolder getConversationHolder() {
840 return conversation;
841 }
842
843 /* (non-Javadoc)
844 * @see eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart#save(org.eclipse.core.runtime.IProgressMonitor)
845 */
846 @Override
847 @Persist
848 public void save(IProgressMonitor monitor) {
849 doSave(monitor);
850 }
851
852
853 }