Project

General

Profile

Download (29.5 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
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collection;
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.Composite;
49
import org.eclipse.swt.widgets.Label;
50
import org.eclipse.swt.widgets.Menu;
51
import org.eclipse.swt.widgets.MenuItem;
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.taxon.Classification;
64
import eu.etaxonomy.cdm.model.taxon.Taxon;
65
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
66
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
67
import eu.etaxonomy.taxeditor.editor.EditorUtil;
68
import eu.etaxonomy.taxeditor.editor.IDistributionEditor;
69
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
70
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
71
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistContentProvider;
72
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorComparator;
73
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
74
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistLabelProvider;
75
import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
76
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
77
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistModifyListener;
78
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
79
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
80
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
81
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
82
import eu.etaxonomy.taxeditor.preference.Resources;
83
import eu.etaxonomy.taxeditor.preference.wizard.AvailableDistributionWizard;
84
import eu.etaxonomy.taxeditor.store.CdmStore;
85
import eu.etaxonomy.taxeditor.store.StoreUtil;
86
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
87

    
88
/**
89
 *
90
 * @author k.luther
91
 *
92
 */
93
public class ChecklistEditorE4 implements IPartContentHasFactualData, IConversationEnabled,
94
       IDirtyMarkable, IPartContentHasDetails, IE4SavablePart, IDistributionEditor{
95

    
96
    private static final String DISTRIBUTION_EDITOR = "Distribution Editor";
97
	private static final String LOADING_TAXA = Messages.ChecklistEditor_LOAD_TAXA;
98
    private static final String UNKNOWN = Messages.ChecklistEditor_UNKNOWN;
99
    private static final String ELEMENT_COUNT = Messages.ChecklistEditor_ELEMENT_COUNT;
100
    public static final String TYPE_FILTER_TEXT = "type filter text"; //$NON-NLS-1$
101

    
102
    private static final Logger logger = Logger.getLogger(ChecklistEditorE4.class);
103

    
104
    /**
105
     * Constant
106
     * <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code>
107
     */
108
    public static String ID = "eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor"; //$NON-NLS-1$
109

    
110
    private TableViewer viewer;
111

    
112
    private ITaxonNodeService taxonNodeService;
113

    
114
    private ChecklistEditorComparator comparator;
115

    
116
    private ChecklistEditorInput checklistEditorInput;
117

    
118
    private ConversationHolder conversation;
119

    
120
    private Integer countNodes;
121

    
122
    private List<TaxonNode> selectedTaxonNodes;
123

    
124
    @Inject
125
    private MPart thisPart;
126

    
127
    @Inject
128
    private MDirtyable dirty;
129

    
130
    @Inject
131
    private ESelectionService selService;
132

    
133
    private ISelectionChangedListener selectionChangedListener;
134

    
135
    private ChecklistEditorFilter filter;
136

    
137
    private Label statusLabel;
138

    
139
    private ChecklistLabelProvider labelProvider;
140
//TODO: maybe it is better to use a hashMap for better handling of adding and removing terms??
141
    private Map<UUID, Integer> areaPosition= new HashMap();
142

    
143
    private ToolItem toolItem;
144
	private ChecklistDropdownSelectionListenerE4 dropListener;
145
	private Text searchText;
146

    
147
	public Map<UUID, Integer> getAreaPosition() {
148
        return areaPosition;
149
    }
150
    /**
151
     * @return the selectedTaxonNodes
152
     */
153
    public List<TaxonNode> getSelectedTaxonNodes() {
154
        return selectedTaxonNodes;
155
    }
156

    
157
    /**
158
     * @param selectedTaxonNodes
159
     *            the selectedTaxonNodes to set
160
     */
161
    public void setSelectedTaxonNodes(List<TaxonNode> selectedTaxonNodes) {
162
        this.selectedTaxonNodes = selectedTaxonNodes;
163
    }
164

    
165
    @Inject
166
    public ChecklistEditorE4() {
167
    }
168

    
169
    /** {@inheritDoc} */
170
    @PostConstruct
171
    public void createPartControl(Composite parent) {
172
    	if (!CdmStore.isActive()){
173
            return;
174
        }
175
        taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
176
        filter = new ChecklistEditorFilter();
177
        createTopComposite(parent);
178
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
179

    
180
        //the focuzsCellManager should return the distribution!!!
181
        TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(viewer, new FocusCellOwnerDrawHighlighter (viewer));
182
        TableViewerEditor.create(viewer, focusCellManager, new ColumnViewerEditorActivationStrategy(viewer), TableViewerEditor.KEYBOARD_ACTIVATION);
183

    
184

    
185
//        viewer.addSelectionChangedListener(new ISelectionChangedListener() {
186
//        	  @Override
187
//        	  public void selectionChanged(SelectionChangedEvent event) {
188
//        	    IStructuredSelection selection = viewer.getStructuredSelection();
189
//        	    Object firstElement = selection.getFirstElement();
190
//        	    // do something with it
191
//        	  }
192
//
193
//
194
//        	});
195

    
196

    
197

    
198
        labelProvider = new ChecklistLabelProvider(this.viewer);
199
       // SortedSet<DefinedTermBase> termSet = labelProvider.getNamedAreas(true);
200
//        terms = new HashMap();
201
//        for (DefinedTermBase term : termSet){
202
//                terms.put(term.getUuid(), term);
203
//        }
204
//        if (terms == null){
205
//			MessagingUtils.informationDialog(Messages.ChecklistEditor_NO_AREAS, Messages.ChecklistEditor_NO_AREAS_MESSAGE);
206
//			this.dispose();
207
//			return;
208
//		}
209

    
210

    
211
        viewer.addFilter(filter);
212
        createTable();
213

    
214
        viewer.setContentProvider(new ChecklistContentProvider());
215
        viewer.setLabelProvider(this.labelProvider);
216
        comparator = new ChecklistEditorComparator();
217
        viewer.setComparator(comparator);
218
        final ModifyListener modifyListener = new ChecklistModifyListener(viewer, filter, searchText);
219

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

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

    
236
       searchText = createSearchBar(parent);
237

    
238

    
239
       // getSite().setSelectionProvider(viewer);
240

    
241

    
242

    
243

    
244

    
245
    }
246

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

    
257
    /**
258
     * @param parent
259
     * @return
260
     */
261
    private Text createSearchBar(Composite parent) {
262
        Label searchLabel = new Label(parent, SWT.NONE);
263
        searchLabel.setText(Messages.ChecklistEditor_SEARCH);
264
        final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
265
        searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
266
        searchText.setForeground(EditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
267
        searchText.setText(TYPE_FILTER_TEXT);
268
        return searchText;
269
    }
270

    
271
    /**
272
     * @param parent
273
     */
274
    private void createToolbar(Composite parent) {
275
        ToolBar toolBar = new ToolBar(parent, SWT.NONE);
276
      //  toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
277
        toolItem = new ToolItem(toolBar, SWT.BUTTON1);
278

    
279
        toolItem.setText(Messages.ChecklistEditor_DIST_STATUS);
280
        toolItem.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
281
        //createToolbarItems();
282
      //  toolItem.addSelectionListener(dropListener);
283
        toolItem.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
        toolBar.pack();
297
    }
298

    
299
	private void createToolbarItems() {
300
	    SortedSet<DefinedTermBase> termsList = getLabelProvider().getNamedAreas(false);
301
		dropListener = new ChecklistDropdownSelectionListenerE4(toolItem, this, termsList);
302

    
303
        for (DefinedTermBase<DefinedTermBase> term : termsList) {
304
            if(term!=null){
305
                dropListener.add(term);
306
            }
307
        }
308
	}
309

    
310
    private void createStatusBar(Composite composite) {
311
        GridData gridData = new GridData();
312
        gridData.horizontalSpan = 3;
313
        gridData.grabExcessHorizontalSpace = true;
314
        gridData.horizontalAlignment = GridData.FILL;
315

    
316
        statusLabel = new Label(composite, SWT.LEFT);
317
        statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
318
        statusLabel.setLayoutData(gridData);
319
    }
320

    
321
    private void createTable() {
322
        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
323
        List<String> titles = new ArrayList<String>();
324
        List<Integer> bounds = new ArrayList<Integer>();
325
        if (PreferencesUtil.isShowRankInChecklistEditor()){
326
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON,  Messages.ChecklistEditor_RANK);
327
            Collections.addAll(bounds, 300, 200);
328
        } else{
329
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON);
330
            Collections.addAll(bounds, 300);
331
        }
332

    
333

    
334
        Map<Integer, Boolean> restoreValuesForColumnWidth = restoreValuesForColumnWidth(titles, bounds);
335
        createInitalDistributionColumns(table, titles, bounds, restoreValuesForColumnWidth);
336
        table.setSortDirection(SWT.DOWN);
337
        table.setHeaderVisible(true);
338
        table.setLinesVisible(true);
339
	}
340
    /**
341
     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
342
     *<p>
343
     *
344
     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
345
     *
346
     * @param table
347
     * @param titles
348
     * @param bounds
349
     * @param restoreValuesForColumnWidth
350
     */
351
	private void createInitalDistributionColumns(Table table,
352
			List<String> titles, List<Integer> bounds, Map<Integer, Boolean> restoreValuesForColumnWidth) {
353
		for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
354
            TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
355
            column.getColumn().setText(titles.get(columnIndex));
356
            column.getColumn().setWidth(bounds.get(columnIndex));
357
            column.getColumn().setResizable(true);
358
            column.getColumn().setMoveable(true);
359
            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
360
            if (columnIndex == 0) {
361
                table.setSortColumn(column.getColumn());
362
            }
363
            if (columnIndex == 1 && PreferencesUtil.isShowRankInChecklistEditor()) {
364
            	/** uncommented it for now because no rank editing is wanted **/
365
//                column.setEditingSupport(new RankEditingSupport(viewer, this));
366
            }
367
            if ((columnIndex == 1 && !PreferencesUtil.isShowRankInChecklistEditor()) || columnIndex >= 2 ) {
368
                //read PrefrenceStore to setWidth according to last saved state
369
                if(restoreValuesForColumnWidth.get(columnIndex)){
370
                    column.getColumn().setWidth(50);
371
                }else{
372
                    column.getColumn().setWidth(0);
373
                }
374
                column.setEditingSupport(new DistributionEditingSupportE4(viewer, this, columnIndex));
375

    
376
            }
377
        }
378
	}
379

    
380
    /**
381
     * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
382
     *<p>
383
     * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
384
     *
385
     * @param titles
386
     * @param bounds
387
     * @return Map<Integer, Boolean>
388
     */
389
	private Map<Integer, Boolean> restoreValuesForColumnWidth(List<String> titles,
390
			List<Integer> bounds) {
391
		Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
392
		if (labelProvider.getNamedAreas(true) != null) {
393
		    int columnIndex;
394
		    if (PreferencesUtil.isShowRankInChecklistEditor()){
395
		        columnIndex = 2;
396
		    } else{
397
		        columnIndex = 1;
398
		    }
399
            for (DefinedTermBase<DefinedTermBase> term : getLabelProvider().getNamedAreas(false)) {
400
                if(term != null){
401
                    restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
402
                    areaPosition.put(term.getUuid(), columnIndex);
403
                    if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
404
                        if (term.getIdInVocabulary() != null){
405
                            titles.add(term.getIdInVocabulary());
406
                        } else{
407
                            titles.add(term.getTitleCache());
408
                        }
409
                    }else{
410
                        titles.add(term.getTitleCache());
411
                    }
412
                    bounds.add(200);
413
                    columnIndex++;
414
                }
415
            }
416
        }
417
		return restoreColumnWidth;
418
	}
419

    
420
    /**
421
     * This method adds new DistributionColumns to an existing table.
422
     *
423
     * @param title
424
     * @param bound
425
     * @param colNumber
426
     * @return
427
     */
428
    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber, DefinedTermBase term) {
429

    
430
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.FULL_SELECTION);
431
        final TableColumn column = viewerColumn.getColumn();
432
        column.setText(title);
433
        String[] UuidAndLable = new String[2];
434
        UuidAndLable[0] = term.getUuid().toString();
435
        UuidAndLable[1] = title;
436
        column.setData(UuidAndLable);
437
        column.setWidth(200);
438

    
439
        viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, colNumber));
440
        column.setResizable(true);
441
        column.setMoveable(true);
442
        return viewerColumn;
443
      }
444

    
445

    
446

    
447
    /**
448
     *
449
     * pull data from database and set input for view
450
     *
451
     */
452
    private void loadDataInput() {
453
        Classification classification = checklistEditorInput.getClassification();
454
        TaxonNode taxonNode = checklistEditorInput.getTaxonNode();
455
        if (classification != null && taxonNode == null) {
456
            countNodes = taxonNodeService.countAllNodesForClassification(classification);
457
//            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
458
            // This should not kill the view nor the editor if something goes
459
            // wrong
460
            // TODO: don't load the whole taxonNode Object but rather a small
461
            // and simple Solution
462
            // FIXME: May be don't open classification which are greater than
463
            // 3000 Taxa
464
            selectedTaxonNodes = taxonNodeService.listAllNodesForClassification(classification, 0, countNodes);
465
            countNodes = selectedTaxonNodes.size();
466
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
467
            viewer.setInput(checklistEditorInput.getTaxa());
468
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
469
//            checkListJob.schedule(Job.LONG);
470

    
471
        }
472
        if (taxonNode != null) {
473
        	boolean includeUnpublished = true;
474
            selectedTaxonNodes = taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, NODE_INIT_STRATEGY, true, includeUnpublished, null);
475
            countNodes = selectedTaxonNodes.size();
476
            viewer.setInput(checklistEditorInput.getTaxa());
477
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
478
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
479
//            checkListJob.schedule(Job.LONG);
480
//          getService().schedule(new ChecklistJob(LOADING_TAXA, selectedTaxonNodes), Job.LONG);
481
        }
482
    }
483

    
484

    
485
    @PreDestroy
486
    public void dispose() {
487
    	if(conversation!=null){
488
    		conversation.unregisterForDataStoreChanges(this);
489
    		conversation.close();
490
    	}
491
    	conversation = null;
492
    	if(checklistEditorInput!=null){
493
    	    checklistEditorInput.dispose();
494
    	}
495

    
496
    }
497

    
498
    /** {@inheritDoc} */
499
    @Focus
500
    public void setFocus() {
501
    	if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()) {
502
            viewer.getControl().setFocus();
503
        }
504
    }
505

    
506
    //FIXME E4 migrate
507
//    /*
508
//     * (non-Javadoc)
509
//     *
510
//     * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
511
//     */
512
//    /** {@inheritDoc} */
513
//    @Override
514
//    public void showBusy(boolean busy) {
515
//        super.showBusy(busy);
516
//        // viewer.getTable().setEnabled(!busy);
517
//        if (busy) {
518
//            partNameCache = getPartName();
519
//            setPartName(String.format(Messages.ChecklistEditor_LOAD_CNT_TAXA, countNodes));
520
//        } else {
521
//            if (partNameCache != null) {
522
//                setPartName(partNameCache);
523
//            }
524
//        }
525
//    }
526

    
527
    /**
528
     * <p>
529
     * Getter for the field <code>viewer</code>.
530
     * </p>
531
     *
532
     * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
533
     */
534
    public StructuredViewer getViewer() {
535
        return viewer;
536
    }
537

    
538
    public void refresh() {
539
        viewer.refresh();
540
    }
541

    
542
    /**
543
     * This method should only be called for adding new Distribution columns and reloading the table.<br>
544
     * It will hide the old distribution column and load the newly added columns.<br>
545
     * <p>
546
     * <b>Notice:</b> for data update please use <b>refresh()</b>
547
     *
548
     */
549
    @SuppressWarnings({ "unchecked", "rawtypes" })
550
	public void reload(){
551
    	//create new Items for Dropdown menue
552
    	Table table = viewer.getTable();
553
    	table.setRedraw(false);
554
    	Collection<DefinedTermBase> oldTerms = labelProvider.getNamedAreas(false);
555

    
556
//    	if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
557
//
558
//    	} else{
559
//    	    terms = (SortedSet<DefinedTermBase>) labelProvider.getTermsOrderedByLabels(labelProvider.getNamedAreas(), CdmStore.getDefaultLanguage());
560
//    	}
561
    	SortedSet<DefinedTermBase> newTerms = labelProvider.getNamedAreas(true);
562
    	//terms = newTerms;
563
//    	toolItem.removeSelectionListener(dropListener);
564
////    	hideDistributionColumns(oldTerms);
565
//    	createToolbarItems();
566
//    	toolItem.addSelectionListener(dropListener);
567

    
568
    	//check which terms are deleted and which are new.
569
    	TableColumn[] columns = viewer.getTable().getColumns() ;
570
    	int index = 0;
571

    
572
      for (TableColumn column: columns){
573
          if ((!PreferencesUtil.isShowRankInChecklistEditor() && index > 0) || (index > 1)) {
574
                column.dispose();
575
            }
576
          index++;
577
      }
578
    	for (DefinedTermBase oldTerm:oldTerms){
579
            boolean delete = true;
580
            for (DefinedTermBase term: newTerms){
581
                   if(oldTerm.getUuid().equals(term.getUuid())){
582
                       delete = false;
583
                       break;
584
                   }
585

    
586
            }
587
            if (delete){
588
               // viewer.getTable().remove(areaPosition.get(oldTerm.getUuid()));
589
                areaPosition.remove(oldTerm.getUuid());
590

    
591
            }
592
        }
593
    	updateColumnIndex();
594

    
595

    
596

    
597
    	for (DefinedTermBase term:newTerms){
598
    	    boolean isNew = true;
599

    
600
    	    Integer position = areaPosition.get(term.getUuid());
601

    
602
    	    String termLable = term.getTitleCache();;
603
            if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
604
                if (term.getIdInVocabulary() != null){
605
                    termLable = term.getIdInVocabulary();
606
                }
607
            }
608

    
609
            if (position == null){
610
                int count = viewer.getTable().getColumnCount();
611
                TableViewerColumn column= addTableViewerColumn(termLable, 200, count, term);
612
                areaPosition.put(term.getUuid(), Integer.valueOf(count));
613
            }else{
614
                TableViewerColumn column= addTableViewerColumn(termLable, 200, position, term);
615
            }
616

    
617
           // acitivateNewColumnInDropDownMenu(term);
618
    	}
619

    
620

    
621

    
622
//    	for(DefinedTermBase term:terms.values()){
623
//    		int count = viewer.getTable().getColumnCount();
624
//
625
//    		//add new terms
626
//    		for (TableColumn column: columns){
627
//    		    if (terms.){
628
//    		        break;
629
//    		    }
630
//    		}
631
//
632
////    		String termLable = term.getTitleCache();;
633
////    		if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
634
////    			if (term.getIdInVocabulary() != null){
635
////    				termLable = term.getIdInVocabulary();
636
////    	        }
637
////    		}
638
////    		addTableViewerColumn(termLable, 200, count);
639
////    		acitivateNewColumnInDropDownMenu(term);
640
    //	}
641

    
642
        viewer.setLabelProvider(labelProvider);
643
//        getService().schedule(new ChecklistJob(LOADING_TAXA, selectedTaxonNodes), Job.LONG);
644
        table.setRedraw(true);
645
    	viewer.refresh();
646
    }
647

    
648
	/**
649
     *
650
     */
651
    private void updateColumnIndex() {
652
        int columnIndex;
653
        if (PreferencesUtil.isShowRankInChecklistEditor()){
654
            columnIndex = 2;
655
        } else{
656
            columnIndex = 1;
657
        }
658
        for (UUID uuid:areaPosition.keySet()){
659
            areaPosition.put(uuid, columnIndex);
660
            columnIndex++;
661
        }
662

    
663
    }
664
    private void acitivateNewColumnInDropDownMenu(DefinedTermBase term) {
665
		Menu menu = dropListener.getMenu();
666
		MenuItem[] items = menu.getItems();
667
		for(MenuItem item: items){
668
			if(item.getText().equalsIgnoreCase(term.getTitleCache())){
669
				item.setSelection(true);
670
				PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), true);
671
			}
672
		}
673
	}
674

    
675
	private void hideDistributionColumns(Collection<DefinedTermBase> oldTerms) {
676
		TableColumn[] columns = viewer.getTable().getColumns();
677
    	for(int i=4; i<columns.length; i++){
678
    		//remove all columns
679
    		columns[i].dispose();
680
    		Menu menu = dropListener.getMenu();
681
    		int itemCount = menu.getItemCount();
682
    		MenuItem item = menu.getItem(i-3);
683
    		item.setSelection(false);
684
    	}
685
    	if(oldTerms != null){
686
    		for(DefinedTermBase term : oldTerms){
687
    			PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), false);
688
    		}
689
    	}
690
	}
691

    
692
    /**
693
     * <p>
694
     * Getter for the field <code>service</code>.
695
     * </p>
696
     *
697
     * @return the service
698
     */
699
    public IWorkbenchSiteProgressService getService() {
700
        return TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IWorkbenchSiteProgressService.class);
701
    }
702

    
703
    private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
704
        SelectionAdapter selectionAdapter = new SelectionAdapter() {
705
            @Override
706
            public void widgetSelected(SelectionEvent e) {
707
                comparator.setColumn(index);
708
                int dir = viewer.getTable().getSortDirection();
709
                if (viewer.getTable().getSortColumn() == column) {
710
                    dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
711
                } else {
712
                    dir = SWT.DOWN;
713
                }
714
                viewer.getTable().setSortDirection(dir);
715
                viewer.getTable().setSortColumn(column);
716
                viewer.refresh();
717
            }
718
        };
719
        return selectionAdapter;
720
    }
721

    
722
    public void doSave(IProgressMonitor monitor) {
723
        try {
724
            monitor.beginTask(Messages.ChecklistEditor_SAVE_EDITOR, 1);
725
            if (!conversation.isBound()) {
726
                conversation.bind();
727
            }
728

    
729
            this.checklistEditorInput.merge();
730

    
731
            conversation.commit(true);
732
            setDirty(false);
733
            monitor.worked(1);
734
        } finally {
735
            monitor.done();
736
        }
737

    
738
    }
739

    
740
    public void init(ChecklistEditorInput input) {
741
        checklistEditorInput = input;
742
        thisPart.setLabel(DISTRIBUTION_EDITOR+": " + checklistEditorInput.getName());
743
        conversation = checklistEditorInput.getConversation();
744
        conversation.registerForDataStoreChanges(this);
745

    
746
        //propagate selection
747
        selectionChangedListener = (event -> selService.setSelection(
748
               DistributionEditorHelper.getDistributionForColumn(event, areaPosition)));
749

    
750
        viewer.addSelectionChangedListener(selectionChangedListener);
751

    
752

    
753
        loadDataInput();
754
    }
755
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
756
            "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$
757
            "childNodes.taxon.name", "taxonNodes", "taxonNodes.*", "taxonNodes.taxon.*", "taxon.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
758
            "taxon.descriptions", "taxon.sec", "taxon.name.*", "terms", "name.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
759
            "name.rank.representations", "name.status.type.representations", "stateData.$" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
760

    
761
    /*
762
     * (non-Javadoc)
763
     *
764
     * @see
765
     * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
766
     * java.lang.Object)
767
     */
768
    @Override
769
    public void changed(Object element) {
770
        if (element != null) {
771
            viewer.update(element, null);
772
            if (element instanceof Taxon){
773
                checklistEditorInput.addTaxonToSave((Taxon)element);
774
            }
775
            setDirty(true);
776
        }
777
    }
778

    
779
    /* (non-Javadoc)
780
     * @see eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#forceDirty()
781
     */
782
    @Override
783
    public void forceDirty() {
784
        changed(null);
785
    }
786

    
787
    public void setDirty(boolean dirty) {
788
        this.dirty.setDirty(dirty);
789
    }
790

    
791
    /*
792
     * (non-Javadoc)
793
     *
794
     * @see org.eclipse.ui.forms.editor.FormEditor#isDirty()
795
     */
796
    public boolean isDirty() {
797
        return dirty.isDirty();
798
    }
799

    
800

    
801
    /**
802
     * @return the labelProvider
803
     */
804
    public ChecklistLabelProvider getLabelProvider() {
805
        return labelProvider;
806
    }
807

    
808
    /* (non-Javadoc)
809
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
810
     */
811
    @Override
812
    public void update(CdmDataChangeMap arg0) {
813
        // TODO Auto-generated method stub
814

    
815
    }
816

    
817
    /* (non-Javadoc)
818
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
819
     */
820
    @Override
821
    public ConversationHolder getConversationHolder() {
822
        return conversation;
823
    }
824

    
825
    /* (non-Javadoc)
826
     * @see eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart#save(org.eclipse.core.runtime.IProgressMonitor)
827
     */
828
    @Override
829
    @Persist
830
    public void save(IProgressMonitor monitor) {
831
        doSave(monitor);
832
    }
833

    
834

    
835
}
(2-2/5)