Project

General

Profile

Download (30.6 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

    
201

    
202

    
203

    
204

    
205
        labelProvider = new ChecklistLabelProvider(this);
206

    
207

    
208

    
209
        viewer.addFilter(filter);
210
        createTable();
211

    
212
        viewer.setContentProvider(new ChecklistContentProvider());
213
        viewer.setLabelProvider(this.labelProvider);
214
//        selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
215
//        viewer.addSelectionChangedListener(selectionChangedListener);
216

    
217
        comparator = new ChecklistEditorComparator();
218
        viewer.setComparator(comparator);
219
        final ModifyListener modifyListener = new ChecklistModifyListener(viewer, filter, searchText);
220

    
221
        searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
222
        searchText.addModifyListener(modifyListener);
223
        createGridDataForViewerLayout();
224
        createStatusBar(parent);
225

    
226
    }
227

    
228
    /**
229
     * @param parent
230
     */
231
    private void createTopComposite(Composite parent) {
232
        GridLayout gridLayout = new GridLayout(3, false);
233
        gridLayout.marginWidth = 0;
234
        gridLayout.marginHeight = 0;
235
        parent.setLayout(gridLayout);
236

    
237
       searchText = createSearchBar(parent);
238

    
239

    
240
    }
241

    
242

    
243
    /**
244
     * @param parent
245
     * @return
246
     */
247
    private void createSetDefaultReferenceButton(Composite parent) {
248
        CdmFormFactory cdmFormFactory = new CdmFormFactory(Display.getCurrent());
249
        defaultReferenceSelection = cdmFormFactory.createSelectionElement(Reference.class,
250
                        null, "Default Source Reference", null,
251
                        EntitySelectionElement.DELETABLE, SWT.NULL);
252

    
253

    
254

    
255

    
256

    
257
        return;
258
    }
259
    private void createGridDataForViewerLayout() {
260
		GridData gridData = new GridData();
261
		gridData.verticalAlignment = GridData.FILL;
262
        gridData.horizontalSpan = 3;
263
        gridData.grabExcessHorizontalSpace = true;
264
        gridData.grabExcessVerticalSpace = true;
265
        gridData.horizontalAlignment = GridData.FILL;
266
        viewer.getControl().setLayoutData(gridData);
267
	}
268

    
269
    /**
270
     * @param parent
271
     * @return
272
     */
273
    private Text createSearchBar(Composite parent) {
274

    
275
        final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
276
        GridData gridData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
277

    
278
        searchText.setLayoutData(gridData);
279
        searchText.setForeground(EditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
280
        searchText.setText(TYPE_FILTER_TEXT);
281
        searchText.setToolTipText(Messages.ChecklistEditor_SEARCH_TOOLTIP);
282

    
283

    
284
      //  Composite buttons = new Composite(parent, SWT.NONE);
285
        //buttons.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL ));
286
        Button button1 = new Button(parent, SWT.PUSH );
287
        GridData gridData2 = new GridData();
288
        gridData2.horizontalSpan = 1;
289
        gridData2.horizontalAlignment = SWT.RIGHT;
290
        button1.setLayoutData(gridData2);
291

    
292

    
293
        button1.setText(Messages.ChecklistEditor_DIST_STATUS);
294
        button1.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
295
        button1.addSelectionListener(new SelectionAdapter() {
296
            @Override
297
            public void widgetSelected(SelectionEvent event) {
298
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
299
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
300
                        availableDistributionWizard);
301

    
302
                int open = dialog.open();
303
                if(open == 0){
304
                    reload();
305
                }
306
            }
307
        });
308

    
309
        Button button2 = new Button(parent, SWT.PUSH );
310
        GridData gridData3 = new GridData();
311
        gridData2.horizontalSpan = 1;
312
        button2.setLayoutData(gridData3);
313

    
314

    
315
        button2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
316
        button2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
317
        button2.addSelectionListener(new SelectionAdapter() {
318

    
319
            @Override
320
            public void widgetSelected(SelectionEvent event) {
321
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
322

    
323
                String defaultSourceStr = (defaultSource == null) ? "" : "Default Source Reference: " + defaultSource.getTitleCache();
324
                if (defaultSourceStr.length()> 100){
325
                	defaultSourceStr = defaultSourceStr.substring(0, 98) + "...";
326
                }
327
                statusLabelSourceReference.setText(defaultSourceStr);
328

    
329
                button2.setBackground(EditorUtil.getColor(Resources.COLOR_CONTROL_SELECTED));
330
            }
331

    
332
        });
333

    
334
        parent.pack();
335
        return searchText;
336
    }
337

    
338
    /**
339
     * @param parent
340
     */
341
    private void createToolbar(Composite parent) {
342
        ToolBar toolBar = new ToolBar(parent, SWT.BORDER);
343
      //  toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
344

    
345
        toolItem = new ToolItem(toolBar, SWT.PUSH| SWT.BORDER);
346

    
347

    
348
        toolItem.setText(Messages.ChecklistEditor_DIST_STATUS);
349
        toolItem.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
350

    
351
        toolItem.addSelectionListener(new SelectionAdapter() {
352
            @Override
353
            public void widgetSelected(SelectionEvent event) {
354
                AvailableDistributionWizard availableDistributionWizard = new AvailableDistributionWizard();
355
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
356
                        availableDistributionWizard);
357

    
358
                int open = dialog.open();
359
                if(open == 0){
360
                    reload();
361
                }
362
            }
363
        });
364
        toolItem2 = new ToolItem(toolBar, SWT.PUSH | SWT.BORDER);
365

    
366

    
367
        toolItem2.setText(Messages.ChecklistEditor_DEFAULT_SOURCE);
368
        toolItem2.setToolTipText(Messages.ChecklistEditor_DEFAULT_SOURCE_TOOLTIP);
369
        //createToolbarItems();
370
      //  toolItem.addSelectionListener(dropListener);
371
        toolItem2.addSelectionListener(new SelectionAdapter() {
372
            @Override
373
            public void widgetSelected(SelectionEvent event) {
374
                defaultSource = ReferenceSelectionDialog.select(AbstractUtility.getShell(), null);
375

    
376
            }
377
        });
378

    
379
        toolBar.pack();
380
    }
381

    
382

    
383

    
384
    private void createStatusBar(Composite composite) {
385
        GridData gridData = new GridData();
386
        gridData.horizontalSpan = 1;
387
        gridData.grabExcessHorizontalSpace = true;
388
        gridData.horizontalAlignment = GridData.FILL;
389

    
390
        statusLabel = new Label(composite, SWT.LEFT);
391
        statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
392
        statusLabel.setLayoutData(gridData);
393

    
394
        statusLabelSourceReference = new Label(composite, SWT.RIGHT);
395

    
396
       statusLabelSourceReference.setLayoutData(gridData);
397

    
398
        if (defaultSource != null){
399
            statusLabelSourceReference.setText("Default Source Reference: " + defaultSource.getAbbrevTitle() != null? defaultSource.getAbbrevTitle() : defaultSource.getAbbrevTitleCache());
400

    
401
        }
402
    }
403

    
404
    private void createTable() {
405
        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
406
        List<String> titles = new ArrayList<String>();
407
        List<Integer> bounds = new ArrayList<Integer>();
408
        if (PreferencesUtil.isShowRankInChecklistEditor()){
409
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON,  Messages.ChecklistEditor_RANK);
410
            Collections.addAll(bounds, 300, 200);
411
        } else{
412
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON);
413
            Collections.addAll(bounds, 300);
414
        }
415

    
416

    
417
      //  Map<Integer, Boolean> restoreValuesForColumnWidth =
418
        restoreValuesForColumnWidth(titles, bounds);
419
        createInitalDistributionColumns(table, titles, bounds);
420
        table.setSortDirection(SWT.DOWN);
421
        table.setHeaderVisible(true);
422
        table.setLinesVisible(true);
423
	}
424
    /**
425
     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
426
     *<p>
427
     *
428
     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
429
     *
430
     * @param table
431
     * @param titles
432
     * @param bounds
433
     * @param restoreValuesForColumnWidth
434
     */
435
	private void createInitalDistributionColumns(Table table,
436
			List<String> titles, List<Integer> bounds) {
437
		for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
438
		    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.FULL_SELECTION);
439
	        final TableColumn column = viewerColumn.getColumn();
440
//            TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
441
            column.setText(titles.get(columnIndex));
442
            column.setWidth(bounds.get(columnIndex));
443
            column.setResizable(true);
444
            column.setMoveable(true);
445
            column.addSelectionListener(getSelectionAdapter(column, columnIndex));
446
            if (columnIndex == 0) {
447
                table.setSortColumn(column);
448
            }
449
            if (columnIndex == 1 && PreferencesUtil.isShowRankInChecklistEditor()) {
450
            	/** uncommented it for now because no rank editing is wanted **/
451
//                column.setEditingSupport(new RankEditingSupport(viewer, this));
452
            }
453
            if ((columnIndex == 1 && !PreferencesUtil.isShowRankInChecklistEditor()) || columnIndex >= 2 ) {
454
                //read PrefrenceStore to setWidth according to last saved state
455
//                if(restoreValuesForColumnWidth.get(columnIndex)){
456
                    column.setWidth(50);
457
//                }else{
458
//                    column.getColumn().setWidth(0);
459
//                }
460
                    viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, columnIndex));
461

    
462
            }
463
        }
464
	}
465

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

    
523
    /**
524
     * This method adds new DistributionColumns to an existing table.
525
     *
526
     * @param title
527
     * @param bound
528
     * @param colNumber
529
     * @return
530
     */
531
    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber, DefinedTermBase term) {
532

    
533
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.FULL_SELECTION, colNumber);
534

    
535
        final TableColumn column = viewerColumn.getColumn();
536
        column.setText(title);
537
        String[] UuidAndLable = new String[2];
538
        UuidAndLable[0] = term.getUuid().toString();
539
        UuidAndLable[1] = title;
540
        column.setData(UuidAndLable);
541
        column.setWidth(200);
542

    
543

    
544
        viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, colNumber));
545
        column.setResizable(true);
546
        column.setMoveable(false);
547
        return viewerColumn;
548
      }
549

    
550

    
551

    
552
    /**
553
     *
554
     * pull data from database and set input for view
555
     *
556
     */
557
    private void loadDataInput() {
558
        Classification classification = checklistEditorInput.getClassification();
559
        TaxonNode taxonNode = checklistEditorInput.getTaxonNode();
560
        if (classification != null && taxonNode == null) {
561
            countNodes = taxonNodeService.countAllNodesForClassification(classification);
562
//            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
563
            // This should not kill the view nor the editor if something goes
564
            // wrong
565
            // TODO: don't load the whole taxonNode Object but rather a small
566
            // and simple Solution
567
            // FIXME: May be don't open classification which are greater than
568
            // 3000 Taxa
569
            selectedTaxonNodes = taxonNodeService.listAllNodesForClassification(classification, 0, countNodes);
570
            countNodes = selectedTaxonNodes.size();
571
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
572
            viewer.setInput(checklistEditorInput.getTaxa());
573
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
574
//            checkListJob.schedule(Job.LONG);
575

    
576
        }
577
        if (taxonNode != null) {
578
        	boolean includeUnpublished = true;
579
            selectedTaxonNodes = taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, NODE_INIT_STRATEGY, true, includeUnpublished, null);
580
            countNodes = selectedTaxonNodes.size();
581
            viewer.setInput(checklistEditorInput.getTaxa());
582
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
583

    
584
        }
585
    }
586

    
587

    
588
    @PreDestroy
589
    public void dispose() {
590
    	if(conversation!=null){
591
    		conversation.unregisterForDataStoreChanges(this);
592
    		conversation.close();
593
    	}
594
    	conversation = null;
595
    	if(checklistEditorInput!=null){
596
    	    checklistEditorInput.dispose();
597
    	}
598
        dirty.setDirty(false);
599

    
600
    }
601

    
602
    /** {@inheritDoc} */
603
    @Focus
604
    public void setFocus() {
605
    	if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
606
            viewer.getControl().setFocus();
607
        }
608
    }
609

    
610
    //FIXME E4 migrate
611
//    /*
612
//     * (non-Javadoc)
613
//     *
614
//     * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
615
//     */
616
//    /** {@inheritDoc} */
617
//    @Override
618
//    public void showBusy(boolean busy) {
619
//        super.showBusy(busy);
620
//        // viewer.getTable().setEnabled(!busy);
621
//        if (busy) {
622
//            partNameCache = getPartName();
623
//            setPartName(String.format(Messages.ChecklistEditor_LOAD_CNT_TAXA, countNodes));
624
//        } else {
625
//            if (partNameCache != null) {
626
//                setPartName(partNameCache);
627
//            }
628
//        }
629
//    }
630

    
631
    /**
632
     * <p>
633
     * Getter for the field <code>viewer</code>.
634
     * </p>
635
     *
636
     * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
637
     */
638
    public StructuredViewer getViewer() {
639
        return viewer;
640
    }
641

    
642
    public void refresh() {
643
        viewer.refresh();
644
    }
645

    
646
    /**
647
     * This method should only be called for adding new Distribution columns and reloading the table.<br>
648
     * It will hide the old distribution column and load the newly added columns.<br>
649
     * <p>
650
     * <b>Notice:</b> for data update please use <b>refresh()</b>
651
     *
652
     */
653
    @SuppressWarnings({ "unchecked", "rawtypes" })
654
	public void reload(){
655
    	//create new Items for Dropdown menue
656
    	Table table = viewer.getTable();
657
    	table.setRedraw(false);
658
    	Collection<DefinedTermBase> oldTerms = labelProvider.getNamedAreas(false);
659

    
660
//    	if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
661
//
662
//    	} else{
663
//    	    terms = (SortedSet<DefinedTermBase>) labelProvider.getTermsOrderedByLabels(labelProvider.getNamedAreas(), CdmStore.getDefaultLanguage());
664
//    	}
665
    	SortedSet<DefinedTermBase> newTerms = labelProvider.getNamedAreas(true);
666

    
667

    
668
    	//check which terms are deleted and which are new.
669
    	TableColumn[] columns = viewer.getTable().getColumns() ;
670
    	int index = 0;
671

    
672
      for (TableColumn column: columns){
673
          if ((!PreferencesUtil.isShowRankInChecklistEditor() && index > 0) || (index > 1)) {
674
                column.dispose();
675
            }
676
          index++;
677
      }
678

    
679
      areaPosition.clear();
680
      int columnIndex;
681
      if (PreferencesUtil.isShowRankInChecklistEditor()){
682
          columnIndex = 2;
683
      } else{
684
          columnIndex = 1;
685
      }
686
      for (DefinedTermBase term:newTerms){
687
          areaPosition.put(term.getUuid(), Integer.valueOf(columnIndex));
688
          columnIndex++;
689
      }
690

    
691
      for (DefinedTermBase term: newTerms){
692
          Representation rep = term.getPreferredRepresentation(CdmStore.getDefaultLanguage());
693

    
694
          String termLable = rep.getAbbreviatedLabel();
695
          if (termLable == null){
696
              termLable = rep.getLabel();
697
          }
698
          if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
699
              if (term.getIdInVocabulary() != null){
700
                termLable = term.getIdInVocabulary();
701
              }
702
          }
703
    	  TableViewerColumn column= addTableViewerColumn(termLable, 200, areaPosition.get(term.getUuid()), term);
704
    	  System.err.println(column.getColumn().getText() + " index: " +areaPosition.get(term.getUuid()) );
705

    
706
      }
707

    
708
      viewer.setLabelProvider(labelProvider);
709
      table.setRedraw(true);
710
      viewer.refresh();
711
    }
712

    
713
	/**
714
     *
715
     */
716
    private void updateColumnIndex() {
717
        int columnIndex;
718
        if (PreferencesUtil.isShowRankInChecklistEditor()){
719
            columnIndex = 2;
720
        } else{
721
            columnIndex = 1;
722
        }
723
        for (UUID uuid:areaPosition.keySet()){
724
            areaPosition.put(uuid, columnIndex);
725
            columnIndex++;
726
        }
727

    
728
    }
729

    
730

    
731

    
732

    
733
    /**
734
     * <p>
735
     * Getter for the field <code>service</code>.
736
     * </p>
737
     *
738
     * @return the service
739
     */
740
    public IWorkbenchSiteProgressService getService() {
741
        return TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IWorkbenchSiteProgressService.class);
742
    }
743

    
744
    private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
745
        SelectionAdapter selectionAdapter = new SelectionAdapter() {
746
            @Override
747
            public void widgetSelected(SelectionEvent e) {
748
                comparator.setColumn(index);
749
                int dir = viewer.getTable().getSortDirection();
750
                if (viewer.getTable().getSortColumn() == column) {
751
                    dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
752
                } else {
753
                    dir = SWT.DOWN;
754
                }
755
                viewer.getTable().setSortDirection(dir);
756
                viewer.getTable().setSortColumn(column);
757
                viewer.refresh();
758
            }
759
        };
760
        return selectionAdapter;
761
    }
762

    
763
    public void doSave(IProgressMonitor monitor) {
764
        try {
765

    
766
            this.checklistEditorInput.merge();
767

    
768

    
769
            setDirty(false);
770

    
771
        } finally {
772

    
773
        }
774

    
775
    }
776

    
777
    public void init(ChecklistEditorInput input) {
778
        checklistEditorInput = input;
779
        thisPart.setLabel(DISTRIBUTION_EDITOR+": " + checklistEditorInput.getName());
780
        conversation = checklistEditorInput.getConversation();
781
        conversation.registerForDataStoreChanges(this);
782

    
783
        //propagate selection
784
        selectionChangedListener = (event -> selService.setSelection(
785
               DistributionEditorHelper.getDistributionForColumn(event, areaPosition)));
786

    
787
        viewer.addSelectionChangedListener(selectionChangedListener);
788

    
789

    
790
        loadDataInput();
791
    }
792
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
793
            "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$
794
            "childNodes.taxon.name", "taxonNodes", "taxonNodes.*", "taxonNodes.taxon.*", "taxon.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
795
            "taxon.descriptions", "taxon.sec", "taxon.name.*", "terms", "name.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
796
            "name.rank.representations", "name.status.type.representations", "stateData.$" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
797

    
798
    /*
799
     * (non-Javadoc)
800
     *
801
     * @see
802
     * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
803
     * java.lang.Object)
804
     */
805
    @Override
806
    public void changed(Object element) {
807
        if (element != null) {
808
            viewer.update(element, null);
809

    
810
            if (element instanceof Taxon){
811
                checklistEditorInput.addTaxonToSave((Taxon)element);
812
            }
813
            setDirty(true);
814
        }
815
    }
816

    
817
    /* (non-Javadoc)
818
     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
819
     */
820
    @Override
821
    public void forceDirty() {
822
        changed(null);
823
    }
824

    
825
    public void setDirty(boolean dirty) {
826
        this.dirty.setDirty(dirty);
827
    }
828

    
829
    /*
830
     * (non-Javadoc)
831
     *
832
     * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
833
     */
834
    public boolean isDirty() {
835
        return dirty.isDirty();
836
    }
837

    
838

    
839
    /**
840
     * @return the labelProvider
841
     */
842
    public ChecklistLabelProvider getLabelProvider() {
843
        return labelProvider;
844
    }
845

    
846
    /* (non-Javadoc)
847
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
848
     */
849
    @Override
850
    public void update(CdmDataChangeMap arg0) {
851
        // TODO Auto-generated method stub
852

    
853
    }
854

    
855
    /* (non-Javadoc)
856
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
857
     */
858
    @Override
859
    public ConversationHolder getConversationHolder() {
860
        return conversation;
861
    }
862

    
863
    /* (non-Javadoc)
864
     * @see eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart#save(org.eclipse.core.runtime.IProgressMonitor)
865
     */
866
    @Override
867
    @Persist
868
    public void save(IProgressMonitor monitor) {
869
        doSave(monitor);
870
    }
871

    
872

    
873
}
(2-2/5)