Project

General

Profile

Download (30.2 KB) Statistics
| Branch: | Tag: | Revision:
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.Collection;
16
import java.util.Collections;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.SortedSet;
21
import java.util.UUID;
22

    
23
import javax.annotation.PostConstruct;
24
import javax.annotation.PreDestroy;
25
import javax.inject.Inject;
26

    
27
import org.apache.log4j.Logger;
28
import org.eclipse.core.runtime.IProgressMonitor;
29
import org.eclipse.e4.ui.di.Focus;
30
import org.eclipse.e4.ui.di.Persist;
31
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
32
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
33
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
34
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
35
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
36
import org.eclipse.jface.viewers.ISelectionChangedListener;
37
import org.eclipse.jface.viewers.StructuredViewer;
38
import org.eclipse.jface.viewers.TableViewer;
39
import org.eclipse.jface.viewers.TableViewerColumn;
40
import org.eclipse.jface.viewers.TableViewerEditor;
41
import org.eclipse.jface.viewers.TableViewerFocusCellManager;
42
import org.eclipse.jface.wizard.WizardDialog;
43
import org.eclipse.swt.SWT;
44
import org.eclipse.swt.events.ModifyListener;
45
import org.eclipse.swt.events.SelectionAdapter;
46
import org.eclipse.swt.events.SelectionEvent;
47
import org.eclipse.swt.layout.GridData;
48
import org.eclipse.swt.layout.GridLayout;
49
import org.eclipse.swt.widgets.Button;
50
import org.eclipse.swt.widgets.Composite;
51
import org.eclipse.swt.widgets.Display;
52
import org.eclipse.swt.widgets.Label;
53
import org.eclipse.swt.widgets.Table;
54
import org.eclipse.swt.widgets.TableColumn;
55
import org.eclipse.swt.widgets.Text;
56
import org.eclipse.swt.widgets.ToolBar;
57
import org.eclipse.swt.widgets.ToolItem;
58
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
59

    
60
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
61
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
62
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
63
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
64
import eu.etaxonomy.cdm.model.common.Representation;
65
import eu.etaxonomy.cdm.model.reference.Reference;
66
import eu.etaxonomy.cdm.model.taxon.Classification;
67
import eu.etaxonomy.cdm.model.taxon.Taxon;
68
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
69
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
70
import eu.etaxonomy.taxeditor.editor.EditorUtil;
71
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
72
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
73
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
74
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistContentProvider;
75
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorComparator;
76
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
77
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistLabelProvider;
78
import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
79
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
80
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistModifyListener;
81
import eu.etaxonomy.taxeditor.model.AbstractUtility;
82
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
83
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
84
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
85
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
86
import eu.etaxonomy.taxeditor.preference.Resources;
87
import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
88
import eu.etaxonomy.taxeditor.store.CdmStore;
89
import eu.etaxonomy.taxeditor.store.StoreUtil;
90
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
91
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
92
import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
93
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
94

    
95
/**
96
 *
97
 * @author k.luther
98
 *
99
 */
100
public class ChecklistEditorE4 implements  IConversationEnabled,
101
       IDirtyMarkable, IPartContentHasDetails, IE4SavablePart, IDistributionEditor, IPartContentHasSupplementalData{
102

    
103
    private static final String DISTRIBUTION_EDITOR = "Distribution Editor";
104
	private static final String LOADING_TAXA = Messages.ChecklistEditor_LOAD_TAXA;
105
    private static final String UNKNOWN = Messages.ChecklistEditor_UNKNOWN;
106
    private static final String ELEMENT_COUNT = Messages.ChecklistEditor_ELEMENT_COUNT;
107
    public static final String TYPE_FILTER_TEXT = "type filter text"; //$NON-NLS-1$
108

    
109
    private static final Logger logger = Logger.getLogger(ChecklistEditorE4.class);
110

    
111
    /**
112
     * Constant
113
     * <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code>
114
     */
115
    public static String ID = "eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor"; //$NON-NLS-1$
116

    
117
    private TableViewer viewer;
118

    
119
    private ITaxonNodeService taxonNodeService;
120

    
121
    private ChecklistEditorComparator comparator;
122

    
123
    private ChecklistEditorInput checklistEditorInput;
124

    
125
    private ConversationHolder conversation;
126

    
127
    private Integer countNodes;
128

    
129
    private List<TaxonNode> selectedTaxonNodes;
130

    
131
    @Inject
132
    private MPart thisPart;
133

    
134
    @Inject
135
    private MDirtyable dirty;
136

    
137
    @Inject
138
    private ESelectionService selService;
139

    
140
    private ISelectionChangedListener selectionChangedListener;
141

    
142
    private ChecklistEditorFilter filter;
143

    
144
    private Label statusLabel;
145
    private Label statusLabelSourceReference;
146

    
147
    private ChecklistLabelProvider labelProvider;
148

    
149
    private Map<UUID, Integer> areaPosition= new HashMap();
150

    
151
    private ToolItem toolItem;
152
    private ToolItem toolItem2;
153
	private ChecklistDropdownSelectionListenerE4 dropListener;
154
	private Text searchText;
155
	private EntitySelectionElement<Reference> defaultReferenceSelection;
156

    
157
	private Reference defaultSource = null;
158

    
159
	public Reference getDefaultSource() {
160
        return defaultSource;
161
    }
162
    public Map<UUID, Integer> getAreaPosition() {
163
        return areaPosition;
164
    }
165
    /**
166
     * @return the selectedTaxonNodes
167
     */
168
    public List<TaxonNode> getSelectedTaxonNodes() {
169
        return selectedTaxonNodes;
170
    }
171

    
172
    /**
173
     * @param selectedTaxonNodes
174
     *            the selectedTaxonNodes to set
175
     */
176
    public void setSelectedTaxonNodes(List<TaxonNode> selectedTaxonNodes) {
177
        this.selectedTaxonNodes = selectedTaxonNodes;
178
    }
179

    
180
    @Inject
181
    public ChecklistEditorE4() {
182
    }
183

    
184
    /** {@inheritDoc} */
185
    @PostConstruct
186
    public void createPartControl(Composite parent) {
187
    	if (!CdmStore.isActive()){
188
            return;
189
        }
190
        taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
191
        filter = new ChecklistEditorFilter();
192
        createTopComposite(parent);
193
      // createToolbar(parent);
194
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
195

    
196
        //the focuzsCellManager should return the distribution!!!
197
        TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter (viewer));
198
        TableViewerEditor.create(viewer, focusCellManager, new ColumnViewerEditorActivationStrategy(viewer), TableViewerEditor.DEFAULT);
199

    
200
        labelProvider = new ChecklistLabelProvider(this);
201

    
202
        viewer.addFilter(filter);
203
        createTable();
204

    
205
        viewer.setContentProvider(new ChecklistContentProvider());
206
        viewer.setLabelProvider(this.labelProvider);
207

    
208
        comparator = new ChecklistEditorComparator();
209
        viewer.setComparator(comparator);
210
        final ModifyListener modifyListener = new ChecklistModifyListener(viewer, filter, searchText);
211

    
212
        searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
213
        searchText.addModifyListener(modifyListener);
214
        createGridDataForViewerLayout();
215
        createStatusBar(parent);
216

    
217
    }
218

    
219
    /**
220
     * @param parent
221
     */
222
    private void createTopComposite(Composite parent) {
223
        GridLayout gridLayout = new GridLayout(3, false);
224
        gridLayout.marginWidth = 0;
225
        gridLayout.marginHeight = 0;
226
        parent.setLayout(gridLayout);
227

    
228
       searchText = createSearchBar(parent);
229

    
230

    
231
    }
232

    
233

    
234
    /**
235
     * @param parent
236
     * @return
237
     */
238
    private void createSetDefaultReferenceButton(Composite parent) {
239
        CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
240
        defaultReferenceSelection = cdmFormFactory.createSelectionElement(Reference.class,
241
                        null, "Default Source Reference", null,
242
                        EntitySelectionElement.DELETABLE, SWT.NULL);
243

    
244

    
245

    
246

    
247

    
248
        return;
249
    }
250
    private void createGridDataForViewerLayout() {
251
		GridData gridData = new GridData();
252
		gridData.verticalAlignment = GridData.FILL;
253
        gridData.horizontalSpan = 3;
254
        gridData.grabExcessHorizontalSpace = true;
255
        gridData.grabExcessVerticalSpace = true;
256
        gridData.horizontalAlignment = GridData.FILL;
257
        viewer.getControl().setLayoutData(gridData);
258
	}
259

    
260
    /**
261
     * @param parent
262
     * @return
263
     */
264
    private Text createSearchBar(Composite parent) {
265

    
266
        final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
267
        GridData gridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
268

    
269
        searchText.setLayoutData(gridData);
270
        searchText.setForeground(EditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
271
        searchText.setText(TYPE_FILTER_TEXT);
272
        searchText.setToolTipText(Messages.ChecklistEditor_SEARCH_TOOLTIP);
273

    
274
        Button button1 = new Button(parent, SWT.PUSH );
275
        GridData gridData2 = new GridData();
276
        gridData2.horizontalSpan = 1;
277
        gridData2.horizontalAlignment = SWT.RIGHT;
278
        button1.setLayoutData(gridData2);
279

    
280

    
281
        button1.setText(Messages.ChecklistEditor_DIST_STATUS);
282
        button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
283
        button1.addSelectionListener(new SelectionAdapter() {
284
            @Override
285
            public void widgetSelected(SelectionEvent event) {
286
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
287
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
288
                        availableDistributionWizard);
289

    
290
                int open = dialog.open();
291
                if(open == 0){
292
                    reload();
293
                }
294
            }
295
        });
296

    
297
        Button button2 = new Button(parent, SWT.PUSH );
298
        GridData gridData3 = new GridData();
299
        gridData2.horizontalSpan = 1;
300
        button2.setLayoutData(gridData3);
301

    
302

    
303
        button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
304
        button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
305
        button2.addSelectionListener(new SelectionAdapter() {
306

    
307
            @Override
308
            public void widgetSelected(SelectionEvent event) {
309
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
310

    
311
                String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
312
                if (defaultSourceStr.length()> 100){
313
                	defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
314
                }
315
                statusLabelSourceReference.setText(defaultSourceStr);
316

    
317
                button2.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
318
            }
319

    
320
        });
321

    
322
        parent.pack();
323
        return searchText;
324
    }
325

    
326
    /**
327
     * @param parent
328
     */
329
    private void createToolbar(Composite parent) {
330
        ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
331
      //  toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
332

    
333
        toolItem = new ToolItem(toolBar, SWT.PUSH| SWT.BORDER);
334

    
335

    
336
        toolItem.setText(Messages.ChecklistEditor_DIST_STATUS);
337
        toolItem.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
338
        //createToolbarItems();
339
      //  toolItem.addSelectionListener(dropListener);
340
        toolItem.addSelectionListener(new SelectionAdapter() {
341
            @Override
342
            public void widgetSelected(SelectionEvent event) {
343
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
344
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
345
                        availableDistributionWizard);
346

    
347
                int open = dialog.open();
348
                if(open == 0){
349
                    reload();
350
                }
351
            }
352
        });
353
        toolItem2 = new ToolItem(toolBar, SWT.PUSH | SWT.BORDER);
354

    
355

    
356
        toolItem2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
357
        toolItem2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
358
        //createToolbarItems();
359
      //  toolItem.addSelectionListener(dropListener);
360
        toolItem2.addSelectionListener(new SelectionAdapter() {
361
            @Override
362
            public void widgetSelected(SelectionEvent event) {
363
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
364

    
365
            }
366
        });
367

    
368
        toolBar.pack();
369
    }
370

    
371

    
372

    
373
    private void createStatusBar(Composite composite) {
374
        GridData gridData = new GridData();
375
        gridData.horizontalSpan = 1;
376
        gridData.grabExcessHorizontalSpace = true;
377
        gridData.horizontalAlignment = GridData.FILL;
378

    
379
        statusLabel = new Label(composite, SWT.LEFT);
380
        statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
381
        statusLabel.setLayoutData(gridData);
382

    
383
        statusLabelSourceReference = new Label(composite, SWT.RIGHT);
384
//        GridData sourceGrid = new GridData();
385
//        sourceGrid.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;
386
//        //sourceGrid.grabExcessHorizontalSpace = false;
387
//        sourceGrid.horizontalSpan = 1;
388
       statusLabelSourceReference.setLayoutData(gridData);
389

    
390
        if (defaultSource != null){
391
            statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
392

    
393
        }
394
    }
395

    
396
    private void createTable() {
397
        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
398
        List<String> titles = new ArrayList<String>();
399
        List<Integer> bounds = new ArrayList<Integer>();
400
        if (PreferencesUtil.isShowRankInChecklistEditor()){
401
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON,  Messages.ChecklistEditor_RANK);
402
            Collections.addAll(bounds, 300, 200);
403
        } else{
404
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON);
405
            Collections.addAll(bounds, 300);
406
        }
407

    
408

    
409
      //  Map<Integer, Boolean> restoreValuesForColumnWidth =
410
        restoreValuesForColumnWidth(titles, bounds);
411
        createInitalDistributionColumns(table, titles, bounds);
412
        table.setSortDirection(SWT.DOWN);
413
        table.setHeaderVisible(true);
414
        table.setLinesVisible(true);
415
	}
416
    /**
417
     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
418
     *<p>
419
     *
420
     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
421
     *
422
     * @param table
423
     * @param titles
424
     * @param bounds
425
     * @param restoreValuesForColumnWidth
426
     */
427
	private void createInitalDistributionColumns(Table table,
428
			List<String> titles, List<Integer> bounds) {
429
		for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
430
            TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
431
            column.getColumn().setText(titles.get(columnIndex));
432
            column.getColumn().setWidth(bounds.get(columnIndex));
433
            column.getColumn().setResizable(true);
434
            column.getColumn().setMoveable(true);
435
            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
436
            if (columnIndex == 0) {
437
                table.setSortColumn(column.getColumn());
438
            }
439
            if (columnIndex == 1 && PreferencesUtil.isShowRankInChecklistEditor()) {
440
            	/** uncommented it for now because no rank editing is wanted **/
441

    
442
            }
443
            if ((columnIndex == 1 && !PreferencesUtil.isShowRankInChecklistEditor()) || columnIndex >= 2 ) {
444
                column.getColumn().setWidth(50);
445
                column.setEditingSupport(new DistributionEditingSupportE4(viewer, this, columnIndex));
446

    
447
            }
448
        }
449
	}
450

    
451
    /**
452
     * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
453
     *<p>
454
     * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
455
     *
456
     * @param titles
457
     * @param bounds
458
     * @return Map<Integer, Boolean>
459
     */
460
	private void restoreValuesForColumnWidth(List<String> titles,
461
			List<Integer> bounds) {
462
//		Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
463
		if (labelProvider.getNamedAreas(true) != null) {
464
		    int columnIndex;
465
		    if (PreferencesUtil.isShowRankInChecklistEditor()){
466
		        columnIndex = 2;
467
		    } else{
468
		        columnIndex = 1;
469
		    }
470
            for (DefinedTermBase<DefinedTermBase> term : getLabelProvider().getNamedAreas(false)) {
471
                if(term != null){
472
                   // restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
473
                    areaPosition.put(term.getUuid(), columnIndex);
474
                    Representation rep = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
475
                    String label = rep.getLabel();
476
                    if (label == null){
477
                        term.getTitleCache();
478
                    }
479
                    if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
480
                        if (term.getIdInVocabulary() != null){
481
                            titles.add(term.getIdInVocabulary());
482
                        } else{
483
                            titles.add(label);
484
                        }
485
                    }else if (PreferencesUtil.isShowSymbol1InChecklistEditor()){
486
                        if (term.getSymbol() != null){
487
                            titles.add(term.getSymbol());
488
                        } else{
489
                            titles.add(label);
490
                        }
491
                    }else if (PreferencesUtil.isShowSymbol2InChecklistEditor()){
492
                        if (term.getSymbol2() != null){
493
                            titles.add(term.getSymbol2());
494
                        } else{
495
                            titles.add(label);
496
                        }
497
                    }else{
498
                        titles.add(label);
499
                    }
500
                    bounds.add(200);
501
                    columnIndex++;
502
                }
503
            }
504
        }
505

    
506
	}
507

    
508
    /**
509
     * This method adds new DistributionColumns to an existing table.
510
     *
511
     * @param title
512
     * @param bound
513
     * @param colNumber
514
     * @return
515
     */
516
    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber, DefinedTermBase term) {
517

    
518
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.FULL_SELECTION);
519
        final TableColumn column = viewerColumn.getColumn();
520
        column.setText(title);
521
        String[] UuidAndLable = new String[2];
522
        UuidAndLable[0] = term.getUuid().toString();
523
        UuidAndLable[1] = title;
524
        column.setData(UuidAndLable);
525
        column.setWidth(200);
526

    
527
        viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, colNumber));
528
        column.setResizable(true);
529
        column.setMoveable(true);
530
        return viewerColumn;
531
      }
532

    
533

    
534

    
535
    /**
536
     *
537
     * pull data from database and set input for view
538
     *
539
     */
540
    private void loadDataInput() {
541
        Classification classification = checklistEditorInput.getClassification();
542
        TaxonNode taxonNode = checklistEditorInput.getTaxonNode();
543
        if (classification != null && taxonNode == null) {
544
            countNodes = taxonNodeService.countAllNodesForClassification(classification);
545
//            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
546
            // This should not kill the view nor the editor if something goes
547
            // wrong
548
            // TODO: don't load the whole taxonNode Object but rather a small
549
            // and simple Solution
550
            // FIXME: May be don't open classification which are greater than
551
            // 3000 Taxa
552
            selectedTaxonNodes = taxonNodeService.listAllNodesForClassification(classification, 0, countNodes);
553
            countNodes = selectedTaxonNodes.size();
554
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
555
            viewer.setInput(checklistEditorInput.getTaxa());
556
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
557
//            checkListJob.schedule(Job.LONG);
558

    
559
        }
560
        if (taxonNode != null) {
561
        	boolean includeUnpublished = true;
562
            selectedTaxonNodes = taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, NODE_INIT_STRATEGY, true, includeUnpublished, null);
563
            countNodes = selectedTaxonNodes.size();
564
            viewer.setInput(checklistEditorInput.getTaxa());
565
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
566

    
567
        }
568
    }
569

    
570

    
571
    @PreDestroy
572
    public void dispose() {
573
    	if(conversation!=null){
574
    		conversation.unregisterForDataStoreChanges(this);
575
    		conversation.close();
576
    	}
577
    	conversation = null;
578
    	if(checklistEditorInput!=null){
579
    	    checklistEditorInput.dispose();
580
    	}
581
        dirty.setDirty(false);
582

    
583
    }
584

    
585
    /** {@inheritDoc} */
586
    @Focus
587
    public void setFocus() {
588
    	if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
589
            viewer.getControl().setFocus();
590
        }
591
    }
592

    
593
    //FIXME E4 migrate
594
//    /*
595
//     * (non-Javadoc)
596
//     *
597
//     * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
598
//     */
599
//    /** {@inheritDoc} */
600
//    @Override
601
//    public void showBusy(boolean busy) {
602
//        super.showBusy(busy);
603
//        // viewer.getTable().setEnabled(!busy);
604
//        if (busy) {
605
//            partNameCache = getPartName();
606
//            setPartName(String.format(Messages.ChecklistEditor_LOAD_CNT_TAXA, countNodes));
607
//        } else {
608
//            if (partNameCache != null) {
609
//                setPartName(partNameCache);
610
//            }
611
//        }
612
//    }
613

    
614
    /**
615
     * <p>
616
     * Getter for the field <code>viewer</code>.
617
     * </p>
618
     *
619
     * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
620
     */
621
    public StructuredViewer getViewer() {
622
        return viewer;
623
    }
624

    
625
    public void refresh() {
626
        viewer.refresh();
627
    }
628

    
629
    /**
630
     * This method should only be called for adding new Distribution columns and reloading the table.<br>
631
     * It will hide the old distribution column and load the newly added columns.<br>
632
     * <p>
633
     * <b>Notice:</b> for data update please use <b>refresh()</b>
634
     *
635
     */
636
    @SuppressWarnings({ "unchecked", "rawtypes" })
637
	public void reload(){
638
      //create new Items for Dropdown menue
639
        Table table = viewer.getTable();
640
        table.setRedraw(false);
641
        Collection<DefinedTermBase> oldTerms = labelProvider.getNamedAreas(false);
642

    
643
        SortedSet<DefinedTermBase> newTerms = labelProvider.getNamedAreas(true);
644

    
645

    
646
        //check which terms are deleted and which are new.
647
        TableColumn[] columns = viewer.getTable().getColumns() ;
648
        int index = 0;
649

    
650
      for (TableColumn column: columns){
651
          if ((!PreferencesUtil.isShowRankInChecklistEditor() && index > 0) || (index > 1)) {
652
                column.dispose();
653
            }
654
          index++;
655
      }
656

    
657
      areaPosition.clear();
658
      int columnIndex;
659
      if (PreferencesUtil.isShowRankInChecklistEditor()){
660
          columnIndex = 2;
661
      } else{
662
          columnIndex = 1;
663
      }
664
      for (DefinedTermBase term:newTerms){
665
          areaPosition.put(term.getUuid(), Integer.valueOf(columnIndex));
666
          columnIndex++;
667
      }
668

    
669
      for (DefinedTermBase term: newTerms){
670
          Representation rep = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
671

    
672
          String termLable = rep.getAbbreviatedLabel();
673
          if (termLable == null){
674
              termLable = rep.getLabel();
675
          }
676
          if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
677
              if (term.getIdInVocabulary() != null){
678
                termLable = term.getIdInVocabulary();
679
              }
680
          }
681
          TableViewerColumn column= addTableViewerColumn(termLable, 200, areaPosition.get(term.getUuid()), term);
682
          System.err.println(column.getColumn().getText() + " index: " +areaPosition.get(term.getUuid()) );
683

    
684
      }
685

    
686
      viewer.setLabelProvider(labelProvider);
687
      table.setRedraw(true);
688
      viewer.refresh();
689
    }
690

    
691
	/**
692
     *
693
     */
694
    private void updateColumnIndex() {
695
        int columnIndex;
696
        if (PreferencesUtil.isShowRankInChecklistEditor()){
697
            columnIndex = 2;
698
        } else{
699
            columnIndex = 1;
700
        }
701
        for (UUID uuid:areaPosition.keySet()){
702
            areaPosition.put(uuid, columnIndex);
703
            columnIndex++;
704
        }
705

    
706
    }
707

    
708

    
709

    
710

    
711
    /**
712
     * <p>
713
     * Getter for the field <code>service</code>.
714
     * </p>
715
     *
716
     * @return the service
717
     */
718
    public IWorkbenchSiteProgressService getService() {
719
        return TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IWorkbenchSiteProgressService.class);
720
    }
721

    
722
    private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
723
        SelectionAdapter selectionAdapter = new SelectionAdapter() {
724
            @Override
725
            public void widgetSelected(SelectionEvent e) {
726
                comparator.setColumn(index);
727
                int dir = viewer.getTable().getSortDirection();
728
                if (viewer.getTable().getSortColumn() == column) {
729
                    dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
730
                } else {
731
                    dir = SWT.DOWN;
732
                }
733
                viewer.getTable().setSortDirection(dir);
734
                viewer.getTable().setSortColumn(column);
735
                viewer.refresh();
736
            }
737
        };
738
        return selectionAdapter;
739
    }
740

    
741
    public void doSave(IProgressMonitor monitor) {
742
        try {
743
          //  monitor.beginTask(Messages.ChecklistEditor_SAVE_EDITOR, 1);
744
//            if (!conversation.isBound()) {
745
//                conversation.bind();
746
//            }
747

    
748
            this.checklistEditorInput.merge();
749

    
750
            //conversation.commit(true);
751
            setDirty(false);
752
  //          monitor.worked(1);
753
        } finally {
754
   //         monitor.done();
755
        }
756

    
757
    }
758

    
759
    public void init(ChecklistEditorInput input) {
760
        checklistEditorInput = input;
761
        thisPart.setLabel(DISTRIBUTION_EDITOR+": " + checklistEditorInput.getName());
762
        conversation = checklistEditorInput.getConversation();
763
        conversation.registerForDataStoreChanges(this);
764

    
765
        //propagate selection
766
        selectionChangedListener = (event -> selService.setSelection(
767
               DistributionEditorHelper.getDistributionForColumn(event, areaPosition)));
768

    
769
        viewer.addSelectionChangedListener(selectionChangedListener);
770

    
771

    
772
        loadDataInput();
773
    }
774
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
775
            "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$
776
            "childNodes.taxon.name", "taxonNodes", "taxonNodes.*", "taxonNodes.taxon.*", "taxon.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
777
            "taxon.descriptions", "taxon.sec", "taxon.name.*", "terms", "name.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
778
            "name.rank.representations", "name.status.type.representations", "stateData.$" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
779

    
780
    /*
781
     * (non-Javadoc)
782
     *
783
     * @see
784
     * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
785
     * java.lang.Object)
786
     */
787
    @Override
788
    public void changed(Object element) {
789
        if (element != null) {
790
            viewer.update(element, null);
791

    
792
            if (element instanceof Taxon){
793
                checklistEditorInput.addTaxonToSave((Taxon)element);
794
            }
795
            setDirty(true);
796
        }
797
    }
798

    
799
    /* (non-Javadoc)
800
     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
801
     */
802
    @Override
803
    public void forceDirty() {
804
        changed(null);
805
    }
806

    
807
    public void setDirty(boolean dirty) {
808
        this.dirty.setDirty(dirty);
809
    }
810

    
811
    /*
812
     * (non-Javadoc)
813
     *
814
     * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
815
     */
816
    public boolean isDirty() {
817
        return dirty.isDirty();
818
    }
819

    
820

    
821
    /**
822
     * @return the labelProvider
823
     */
824
    public ChecklistLabelProvider getLabelProvider() {
825
        return labelProvider;
826
    }
827

    
828
    /* (non-Javadoc)
829
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
830
     */
831
    @Override
832
    public void update(CdmDataChangeMap arg0) {
833
        // TODO Auto-generated method stub
834

    
835
    }
836

    
837
    /* (non-Javadoc)
838
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
839
     */
840
    @Override
841
    public ConversationHolder getConversationHolder() {
842
        return conversation;
843
    }
844

    
845
    /* (non-Javadoc)
846
     * @see eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart#save(org.eclipse.core.runtime.IProgressMonitor)
847
     */
848
    @Override
849
    @Persist
850
    public void save(IProgressMonitor monitor) {
851
        doSave(monitor);
852
    }
853

    
854

    
855
}
(2-2/5)