Project

General

Profile

Download (29.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
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collections;
15
import java.util.HashMap;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.SortedSet;
19

    
20
import javax.annotation.PostConstruct;
21
import javax.annotation.PreDestroy;
22
import javax.inject.Inject;
23

    
24
import org.apache.log4j.Logger;
25
import org.eclipse.core.runtime.IProgressMonitor;
26

    
27
import org.eclipse.e4.ui.di.Focus;
28
import org.eclipse.e4.ui.di.Persist;
29
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
30
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
31
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
32
import org.eclipse.jface.viewers.ISelectionChangedListener;
33

    
34
import org.eclipse.jface.viewers.StructuredViewer;
35
import org.eclipse.jface.viewers.TableViewer;
36
import org.eclipse.jface.viewers.TableViewerColumn;
37
import org.eclipse.swt.SWT;
38
import org.eclipse.swt.events.ModifyListener;
39
import org.eclipse.swt.events.SelectionAdapter;
40
import org.eclipse.swt.events.SelectionEvent;
41
import org.eclipse.swt.layout.GridData;
42
import org.eclipse.swt.layout.GridLayout;
43
import org.eclipse.swt.widgets.Composite;
44

    
45
import org.eclipse.swt.widgets.Label;
46
import org.eclipse.swt.widgets.Menu;
47
import org.eclipse.swt.widgets.MenuItem;
48
import org.eclipse.swt.widgets.Table;
49
import org.eclipse.swt.widgets.TableColumn;
50
import org.eclipse.swt.widgets.Text;
51
import org.eclipse.swt.widgets.ToolBar;
52
import org.eclipse.swt.widgets.ToolItem;
53
import org.eclipse.ui.IEditorInput;
54
import org.eclipse.ui.IMemento;
55
import org.eclipse.ui.progress.IWorkbenchSiteProgressService;
56

    
57
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
58
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
59
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
60

    
61
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
62
import eu.etaxonomy.cdm.model.taxon.Classification;
63

    
64
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
65
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
66
import eu.etaxonomy.taxeditor.editor.EditorUtil;
67
import eu.etaxonomy.taxeditor.editor.SimpleSelectionProvider;
68
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
69
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
70
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistContentProvider;
71
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorComparator;
72
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditorInput;
73
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistLabelProvider;
74
import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
75
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
76
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistModifyListener;
77
import eu.etaxonomy.taxeditor.model.IContextListener;
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.model.MessagingUtils;
82
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
83
import eu.etaxonomy.taxeditor.preference.Resources;
84
import eu.etaxonomy.taxeditor.store.CdmStore;
85

    
86
/**
87
 * 
88
 * @author k.luther
89
 *
90
 */
91
public class ChecklistEditorE4 implements IPartContentHasFactualData, IConversationEnabled,
92
        IContextListener, IDirtyMarkable, IPartContentHasDetails{
93

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

    
100
    private static final Logger logger = Logger.getLogger(ChecklistEditorE4.class);
101

    
102

    
103
//    private class ChecklistJob extends Job {
104
//
105
//        private List<TaxonNode> taxonNodes;
106
//
107
//        /**
108
//         * @param name
109
//         */
110
//        public ChecklistJob(String title, List<TaxonNode> listTaxonNodes) {
111
//            super(title);
112
//            this.taxonNodes = listTaxonNodes;
113
//
114
//
115
//        }
116
//
117
//        /*
118
//         * (non-Javadoc)
119
//         *
120
//         * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.
121
//         * IProgressMonitor)
122
//         */
123
//        @Override
124
//        public IStatus run(final IProgressMonitor monitor) {
125
//            try {
126
//                logger.debug("Begin of eclipse core runtime Job to Retrieve datasources"); //$NON-NLS-1$
127
//                monitor.beginTask(Messages.ChecklistEditor_RETRIEVE_NODES, taxonNodes.size() + 1);
128
//
129
//                final List<Taxon> taxonList = new ArrayList<Taxon>();
130
//
131
//                for (TaxonNode taxonNode : taxonNodes) {
132
//                    HibernateProxyHelper hh = new HibernateProxyHelper();
133
//                    Taxon taxon = hh.deproxy(taxonNode.getTaxon(), Taxon.class);
134
//                    taxonList.add(taxon);
135
//                }
136
//                // set View input
137
//                Display.getDefault().asyncExec(new Runnable() {
138
//
139
//                    @Override
140
//                    public void run() {
141
//                    	viewer.setInput(taxonList);
142
//                    }
143
//                });
144
//                monitor.worked(1);
145
//
146
//            } finally {
147
//                monitor.done();
148
//                taxonNodes = null;
149
//            }
150
//            return Status.OK_STATUS;
151
//        }
152
//    }
153

    
154
    /**
155
     * Constant
156
     * <code>ID="eu.etaxonomy.taxeditor.store.datasource"{trunked}</code>
157
     */
158
    public static String ID = "eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor"; //$NON-NLS-1$
159

    
160
    private TableViewer viewer;
161

    
162
    private String partNameCache;
163

    
164
    //FIXME E4 migrate/delete ater full migration
165
    
166
    private IWorkbenchSiteProgressService service;
167

    
168
    private ITaxonNodeService taxonNodeService;
169

    
170
    private ChecklistEditorComparator comparator;
171

    
172
    private ChecklistEditorInput checklistEditorInput;
173

    
174
    private SimpleSelectionProvider simpleSelectionProvider;
175

    
176
    private ConversationHolder conversation;
177

    
178
    private Integer countNodes;
179

    
180
    private List<TaxonNode> selectedTaxonNodes;
181

    
182
    @Inject
183
    private MPart thisPart;
184
    
185
    @Inject
186
    private MDirtyable dirty;
187
    
188
    @Inject
189
    private ESelectionService selService;
190

    
191
    private ISelectionChangedListener selectionChangedListener;
192

    
193
    private ChecklistEditorFilter filter;
194

    
195
    private Menu headerMenu;
196

    
197
    private Label statusLabel;
198

    
199
   // private final ICdmEntitySession cdmEntitySession;
200

    
201
    private ChecklistLabelProvider labelProvider;
202

    
203
    private SortedSet<DefinedTermBase> terms = null;
204
	private ToolItem toolItem;
205
	private ChecklistDropdownSelectionListenerE4 dropListener;
206

    
207
    /**
208
     * @return the selectedTaxonNodes
209
     */
210
    public List<TaxonNode> getSelectedTaxonNodes() {
211
        return selectedTaxonNodes;
212
    }
213

    
214
    /**
215
     * @param selectedTaxonNodes
216
     *            the selectedTaxonNodes to set
217
     */
218
    public void setSelectedTaxonNodes(List<TaxonNode> selectedTaxonNodes) {
219
        this.selectedTaxonNodes = selectedTaxonNodes;
220
    }
221

    
222
    @Inject
223
    public ChecklistEditorE4() {
224
    }
225

    
226
    /** {@inheritDoc} */
227
    @PostConstruct
228
    public void createPartControl(Composite parent) {
229
    	if (!CdmStore.isActive()){
230
            return;
231
        }
232
        taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
233
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
234
      
235
//        viewer.addSelectionChangedListener(new ISelectionChangedListener() {
236
//        	  @Override
237
//        	  public void selectionChanged(SelectionChangedEvent event) {
238
//        	    IStructuredSelection selection = viewer.getStructuredSelection();
239
//        	    Object firstElement = selection.getFirstElement();
240
//        	    // do something with it
241
//        	  }
242
//
243
//			
244
//        	}); 
245
      
246

    
247
        
248
        labelProvider = new ChecklistLabelProvider(this.viewer);
249

    
250
        terms = labelProvider.getNamedAreas();
251
        if (terms == null){
252
			MessagingUtils.informationDialog(Messages.ChecklistEditor_NO_AREAS, Messages.ChecklistEditor_NO_AREAS_MESSAGE);
253
			this.dispose();
254
			return;
255
		}
256
        filter = new ChecklistEditorFilter();
257
        createTopComposite(parent);
258
    }
259

    
260
    /**
261
     * @param parent
262
     */
263
    private void createTopComposite(Composite parent) {
264
        GridLayout gridLayout = new GridLayout(3, false);
265
        gridLayout.marginWidth = 0;
266
        gridLayout.marginHeight = 0;
267
        parent.setLayout(gridLayout);
268

    
269
        final Text searchText = createSearchBar(parent);
270
        createToolbar(parent);
271

    
272
       // getSite().setSelectionProvider(viewer);
273

    
274
        final ModifyListener modifyListener = new ChecklistModifyListener(viewer, filter, searchText);
275

    
276
        searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
277
        searchText.addModifyListener(modifyListener);
278
        createGridDataForViewerLayout();
279

    
280
        viewer.addFilter(filter);
281
        createTable();
282

    
283
        viewer.setContentProvider(new ChecklistContentProvider());
284
        viewer.setLabelProvider(this.labelProvider);
285
        comparator = new ChecklistEditorComparator();
286
        viewer.setComparator(comparator);
287

    
288
        createStatusBar(parent);
289
    }
290

    
291
	private void createGridDataForViewerLayout() {
292
		GridData gridData = new GridData();
293
		gridData.verticalAlignment = GridData.FILL;
294
        gridData.horizontalSpan = 3;
295
        gridData.grabExcessHorizontalSpace = true;
296
        gridData.grabExcessVerticalSpace = true;
297
        gridData.horizontalAlignment = GridData.FILL;
298
        viewer.getControl().setLayoutData(gridData);
299
	}
300

    
301
    /**
302
     * @param parent
303
     * @return
304
     */
305
    private Text createSearchBar(Composite parent) {
306
        Label searchLabel = new Label(parent, SWT.NONE);
307
        searchLabel.setText(Messages.ChecklistEditor_SEARCH);
308
        final Text searchText = new Text(parent, SWT.BORDER | SWT.SEARCH | SWT.ICON_SEARCH | SWT.CANCEL);
309
        searchText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL));
310
        searchText.setForeground(EditorUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
311
        searchText.setText(TYPE_FILTER_TEXT);
312
        return searchText;
313
    }
314

    
315
    /**
316
     * @param parent
317
     */
318
    private void createToolbar(Composite parent) {
319
        ToolBar toolBar = new ToolBar(parent, SWT.NONE);
320
        toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
321
        toolItem.setText(Messages.ChecklistEditor_DIST_STATUS);
322
        toolItem.setToolTipText(Messages.ChecklistEditor_DIST_STATUS_TOOLTIP);
323
        createToolbarItems();
324
        toolItem.addSelectionListener(dropListener);
325
        toolBar.pack();
326
    }
327

    
328
	private void createToolbarItems() {
329
		dropListener = new ChecklistDropdownSelectionListenerE4(toolItem, this, terms);
330

    
331
        for (DefinedTermBase<DefinedTermBase> term : terms) {
332
            if(term!=null){
333
                dropListener.add(term);
334
            }
335
        }
336
	}
337

    
338
    private void createStatusBar(Composite composite) {
339
        GridData gridData = new GridData();
340
        gridData.horizontalSpan = 3;
341
        gridData.grabExcessHorizontalSpace = true;
342
        gridData.horizontalAlignment = GridData.FILL;
343

    
344
        statusLabel = new Label(composite, SWT.LEFT);
345
        statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
346
        statusLabel.setLayoutData(gridData);
347
    }
348

    
349
    private void createTable() {
350
        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
351
        List<String> titles = new ArrayList<String>();
352
        List<Integer> bounds = new ArrayList<Integer>();
353
        if (PreferencesUtil.isShowRankInChecklistEditor()){
354
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON,  Messages.ChecklistEditor_RANK);
355
            Collections.addAll(bounds, 300, 200);
356
        } else{
357
            Collections.addAll(titles, Messages.ChecklistEditor_TAXON);
358
            Collections.addAll(bounds, 300);
359
        }
360

    
361

    
362
        Map<Integer, Boolean> restoreValuesForColumnWidth = restoreValuesForColumnWidth(titles, bounds);
363
        createInitalDistributionColumns(table, titles, bounds, restoreValuesForColumnWidth);
364
        table.setSortDirection(SWT.DOWN);
365
        table.setHeaderVisible(true);
366
        table.setLinesVisible(true);
367
	}
368
    /**
369
     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
370
     *<p>
371
     *
372
     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
373
     *
374
     * @param table
375
     * @param titles
376
     * @param bounds
377
     * @param restoreValuesForColumnWidth
378
     */
379
	private void createInitalDistributionColumns(Table table,
380
			List<String> titles, List<Integer> bounds, Map<Integer, Boolean> restoreValuesForColumnWidth) {
381
		for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
382
            TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
383
            column.getColumn().setText(titles.get(columnIndex));
384
            column.getColumn().setWidth(bounds.get(columnIndex));
385
            column.getColumn().setResizable(true);
386
            column.getColumn().setMoveable(true);
387
            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
388
            if (columnIndex == 0) {
389
                table.setSortColumn(column.getColumn());
390
            }
391
            if (columnIndex == 1 && PreferencesUtil.isShowRankInChecklistEditor()) {
392
            	/** uncommented it for now because no rank editing is wanted **/
393
//                column.setEditingSupport(new RankEditingSupport(viewer, this));
394
            }
395
            if ((columnIndex == 1 && !PreferencesUtil.isShowRankInChecklistEditor()) || columnIndex >= 2 ) {
396
                //read PrefrenceStore to setWidth according to last saved state
397
                if(restoreValuesForColumnWidth.get(columnIndex)){
398
                    column.getColumn().setWidth(50);
399
                }else{
400
                    column.getColumn().setWidth(50);
401
                }
402
                column.setEditingSupport(new DistributionEditingSupportE4(viewer, this, columnIndex));
403
            }
404
        }
405
	}
406

    
407
    /**
408
     * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
409
     *<p>
410
     * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
411
     *
412
     * @param titles
413
     * @param bounds
414
     * @return Map<Integer, Boolean>
415
     */
416
	private Map<Integer, Boolean> restoreValuesForColumnWidth(List<String> titles,
417
			List<Integer> bounds) {
418
		Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
419
		if (terms != null) {
420
		    int columnIndex;
421
		    if (PreferencesUtil.isShowRankInChecklistEditor()){
422
		        columnIndex = 2;
423
		    } else{
424
		        columnIndex = 1;
425
		    }
426
            for (DefinedTermBase<DefinedTermBase> term : terms) {
427
                if(term != null){
428
                    restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
429
                    if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
430
                        if (term.getIdInVocabulary() != null){
431
                            titles.add(term.getIdInVocabulary());
432
                        } else{
433
                            titles.add(term.getTitleCache());
434
                        }
435
                    }else{
436
                        titles.add(term.getTitleCache());
437
                    }
438
                    bounds.add(200);
439
                    columnIndex++;
440
                }
441
            }
442
        }
443
		return restoreColumnWidth;
444
	}
445

    
446
    /**
447
     * This method adds new DistributionColumns to an existing table.
448
     *
449
     * @param title
450
     * @param bound
451
     * @param colNumber
452
     * @return
453
     */
454
    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber) {
455
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
456
        final TableColumn column = viewerColumn.getColumn();
457
        column.setText(title);
458
        column.setWidth(200);
459
        viewerColumn.setEditingSupport(new DistributionEditingSupportE4(viewer, this, colNumber));
460
        column.setResizable(true);
461
        column.setMoveable(true);
462
        return viewerColumn;
463
      }
464

    
465

    
466

    
467
    /**
468
     *
469
     * pull data from database and set input for view
470
     *
471
     */
472
    private void loadDataInput() {
473
        Classification classification = checklistEditorInput.getClassification();
474
        TaxonNode taxonNode = checklistEditorInput.getTaxonNode();
475
        if (classification != null && taxonNode == null) {
476
            countNodes = taxonNodeService.countAllNodesForClassification(classification);
477
//            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
478
            // This should not kill the view nor the editor if something goes
479
            // wrong
480
            // TODO: don't load the whole taxonNode Object but rather a small
481
            // and simple Solution
482
            // FIXME: May be don't open classification which are greater than
483
            // 3000 Taxa
484
            selectedTaxonNodes = taxonNodeService.listAllNodesForClassification(classification, 0, countNodes);
485
            countNodes = selectedTaxonNodes.size();
486
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
487
            viewer.setInput(checklistEditorInput.getTaxa());
488
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
489
//            checkListJob.schedule(Job.LONG);
490
            
491
        }
492
        if (taxonNode != null) {
493
            selectedTaxonNodes = taxonNodeService.loadChildNodesOfTaxonNode(taxonNode, NODE_INIT_STRATEGY, true, null);
494
            countNodes = selectedTaxonNodes.size();
495
            viewer.setInput(checklistEditorInput.getTaxa());
496
            statusLabel.setText(ELEMENT_COUNT + (countNodes != null ? countNodes : UNKNOWN));
497
//            Job checkListJob = new ChecklistJob(LOADING_TAXA, selectedTaxonNodes);
498
//            checkListJob.schedule(Job.LONG);
499
//          getService().schedule(new ChecklistJob(LOADING_TAXA, selectedTaxonNodes), Job.LONG);
500
        }
501
    }
502

    
503
    /** {@inheritDoc} */
504
    @PreDestroy
505
    public void dispose() {
506
//    	if(checklistEditorInput!=null){
507
//    		this.checklistEditorInput.getConversation().unregisterForDataStoreChanges(this.checklistEditorInput);
508
//    	}
509

    
510
    	if(conversation!=null){
511
    		conversation.unregisterForDataStoreChanges(this);
512
    		conversation.unbind();
513
    	}
514
        //conversation.close();
515
		conversation = null;
516
    }
517

    
518
    /** {@inheritDoc} */
519
    @Focus
520
    public void setFocus() {
521
    	if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed())
522
        viewer.getControl().setFocus();
523
    }
524

    
525
    //FIXME E4 migrate
526
//    /*
527
//     * (non-Javadoc)
528
//     *
529
//     * @see org.eclipse.ui.part.WorkbenchPart#showBusy(boolean)
530
//     */
531
//    /** {@inheritDoc} */
532
//    @Override
533
//    public void showBusy(boolean busy) {
534
//        super.showBusy(busy);
535
//        // viewer.getTable().setEnabled(!busy);
536
//        if (busy) {
537
//            partNameCache = getPartName();
538
//            setPartName(String.format(Messages.ChecklistEditor_LOAD_CNT_TAXA, countNodes));
539
//        } else {
540
//            if (partNameCache != null) {
541
//                setPartName(partNameCache);
542
//            }
543
//        }
544
//    }
545

    
546
    /**
547
     * <p>
548
     * Getter for the field <code>viewer</code>.
549
     * </p>
550
     *
551
     * @return a {@link org.eclipse.jface.viewers.StructuredViewer} object.
552
     */
553
    public StructuredViewer getViewer() {
554
        return viewer;
555
    }
556

    
557
    public void refresh() {
558
        viewer.refresh();
559
    }
560

    
561
    /**
562
     * This method should only be called for adding new Distribution columns and reloading the table.<br>
563
     * It will hide the old distribution column and load the newly added columns.<br>
564
     * <p>
565
     * <b>Notice:</b> for data update please use <b>refresh()</b>
566
     *
567
     */
568
    @SuppressWarnings({ "unchecked", "rawtypes" })
569
	public void reload(){
570
    	//create new Items for Dropdown menue
571
    	Table table = viewer.getTable();
572
    	table.setRedraw(false);
573
    	SortedSet<DefinedTermBase> oldTerms = terms;
574

    
575
//    	if (PreferencesUtil.isShowIdInVocabularyInChecklistEditor()){
576
//
577
//    	} else{
578
//    	    terms = (SortedSet<DefinedTermBase>) labelProvider.getTermsOrderedByLabels(labelProvider.getNamedAreas(), CdmStore.getDefaultLanguage());
579
//    	}
580
    	SortedSet<DefinedTermBase> newTerms = labelProvider.getNamedAreas();
581
    	toolItem.removeSelectionListener(dropListener);
582
    	hideDistributionColumns(oldTerms);
583
    	createToolbarItems();
584
    	toolItem.addSelectionListener(dropListener);
585
    	for(DefinedTermBase term:newTerms){
586
    		int count = viewer.getTable().getColumnCount();
587
    		addTableViewerColumn(term.getTitleCache(), 200, count);
588
    		acitivateNewColumnInDropDownMenu(term);
589
    	}
590
        viewer.setLabelProvider(labelProvider);
591
//        getService().schedule(new ChecklistJob(LOADING_TAXA, selectedTaxonNodes), Job.LONG);
592
        table.setRedraw(true);
593
    	viewer.refresh();
594
    }
595

    
596
	private void acitivateNewColumnInDropDownMenu(DefinedTermBase term) {
597
		Menu menu = dropListener.getMenu();
598
		MenuItem[] items = menu.getItems();
599
		for(MenuItem item: items){
600
			if(item.getText().equalsIgnoreCase(term.getTitleCache())){
601
				item.setSelection(true);
602
				PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), true);
603
			}
604
		}
605
	}
606

    
607
	private void hideDistributionColumns(SortedSet<DefinedTermBase> oldTerms) {
608
		TableColumn[] columns = viewer.getTable().getColumns();
609
    	for(int i=4; i<columns.length; i++){
610
    		//remove all columns
611
    		columns[i].dispose();
612
    		Menu menu = dropListener.getMenu();
613
    		int itemCount = menu.getItemCount();
614
    		MenuItem item = menu.getItem(i-3);
615
    		item.setSelection(false);
616
    	}
617
    	if(oldTerms != null){
618
    		for(DefinedTermBase term : oldTerms){
619
    			PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), false);
620
    		}
621
    	}
622
	}
623

    
624
    /**
625
     * <p>
626
     * Getter for the field <code>service</code>.
627
     * </p>
628
     *
629
     * @return the service
630
     */
631
    public IWorkbenchSiteProgressService getService() {
632
        return TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IWorkbenchSiteProgressService.class);
633
    }
634

    
635
    private SelectionAdapter getSelectionAdapter(final TableColumn column, final int index) {
636
        SelectionAdapter selectionAdapter = new SelectionAdapter() {
637
            @Override
638
            public void widgetSelected(SelectionEvent e) {
639
                comparator.setColumn(index);
640
                int dir = viewer.getTable().getSortDirection();
641
                if (viewer.getTable().getSortColumn() == column) {
642
                    dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
643
                } else {
644
                    dir = SWT.DOWN;
645
                }
646
                viewer.getTable().setSortDirection(dir);
647
                viewer.getTable().setSortColumn(column);
648
                viewer.refresh();
649
            }
650
        };
651
        return selectionAdapter;
652
    }
653

    
654
    @Persist
655
    public void doSave(IProgressMonitor monitor) {
656
        try {
657
            monitor.beginTask(Messages.ChecklistEditor_SAVE_EDITOR, 1);
658
            if (!conversation.isBound()) {
659
                conversation.bind();
660
            }
661
           this.checklistEditorInput.merge();
662
            //this.checklistEditorInput.merge();
663
            conversation.commit(true);
664
            setDirty(false);
665
            monitor.worked(1);
666
        } finally {
667
            monitor.done();
668
        }
669

    
670
    }
671

    
672
    public void init(IEditorInput input) {
673
        if (input instanceof ChecklistEditorInput) {
674
            checklistEditorInput = (ChecklistEditorInput) input;
675
            thisPart.setLabel(DISTRIBUTION_EDITOR+": " + checklistEditorInput.getName());
676
            conversation = checklistEditorInput.getConversation();
677
            conversation.registerForDataStoreChanges(this);
678
        }
679
        simpleSelectionProvider = new SimpleSelectionProvider();
680

    
681
        //propagate selection
682
        selectionChangedListener = (event -> selService.setSelection(event));
683
        viewer.addSelectionChangedListener(selectionChangedListener);
684
        
685

    
686
        loadDataInput();
687
    }
688

    
689

    
690
    /*
691
     * (non-Javadoc)
692
     *
693
     * @see
694
     * eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.
695
     * eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
696
     */
697

    
698
    @Override
699
    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
700

    
701
    }
702

    
703
    /*
704
     * (non-Javadoc)
705
     *
706
     * @see
707
     * eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse
708
     * .ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
709
     */
710
    @Override
711
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
712
//        IStructuredSelection sel = (IStructuredSelection) this.viewer.getSelection();
713
//        if (sel.isEmpty()) {
714
//            return;
715
//        }
716
//        memento = memento.createChild("tree-selections");
717
//        Iterator iter = sel.iterator();
718
//        while (iter.hasNext()) {
719
//            String nodeName = (String) iter.next();
720
//            memento.createChild("selected-nodes", nodeName);
721
//        }
722
    }
723

    
724
    /*
725
     * (non-Javadoc)
726
     *
727
     * @see
728
     * eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse
729
     * .ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
730
     */
731
    @Override
732
    public void contextStart(IMemento memento, IProgressMonitor monitor) {
733

    
734
    }
735

    
736
    /*
737
     * (non-Javadoc)
738
     *
739
     * @see
740
     * eu.etaxonomy.taxeditor.model.IContextListener#contextRefresh(org.eclipse
741
     * .core.runtime.IProgressMonitor)
742
     */
743
    @Override
744
    public void contextRefresh(IProgressMonitor monitor) {
745
    }
746

    
747
    /*
748
     * (non-Javadoc)
749
     *
750
     * @see
751
     * eu.etaxonomy.taxeditor.model.IContextListener#workbenchShutdown(org.eclipse
752
     * .ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
753
     */
754
    @Override
755
    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
756
        conversation.clear();
757
        conversation.close();
758
        conversation = null;
759
    }
760

    
761
    private static final List<String> NODE_INIT_STRATEGY = Arrays.asList(new String[] { "descriptions", //$NON-NLS-1$
762
            "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$
763
            "childNodes.taxon.name", "taxonNodes", "taxonNodes.*", "taxonNodes.taxon.*", "taxon.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
764
            "taxon.descriptions", "taxon.sec", "taxon.name.*", "terms", "name.*", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
765
            "name.rank.representations", "name.status.type.representations", "stateData.$" }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
766

    
767
    /*
768
     * (non-Javadoc)
769
     *
770
     * @see
771
     * eu.etaxonomy.taxeditor.model.IDirtyMarkableSelectionProvider#changed(
772
     * java.lang.Object)
773
     */
774
    @Override
775
    public void changed(Object element) {
776
        if (element != null) {
777
            viewer.update(element, null);
778
            setDirty(true);
779
        }
780
    }
781

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

    
790
    public void setDirty(boolean dirty) {
791
        this.dirty.setDirty(dirty);
792
    }
793

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

    
803
    /* (non-Javadoc)
804
     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getCdmEntitySession()
805
     */
806

    
807

    
808
//    /* (non-Javadoc)
809
//     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getRootEntities()
810
//     */
811
//    @Override
812
//    public <T extends CdmBase> Collection<T> getRootEntities() {
813
//        // TODO Auto-generated method stub
814
//        return null;
815
//    }
816
//
817
//    /* (non-Javadoc)
818
//     * @see eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled#getPropertyPathsMap()
819
//     */
820
//    @Override
821
//    public Map<Object, List<String>> getPropertyPathsMap() {
822
//        // TODO Auto-generated method stub
823
//        return null;
824
//    }
825

    
826
    /**
827
     * @return the labelProvider
828
     */
829
    public ChecklistLabelProvider getLabelProvider() {
830
        return labelProvider;
831
    }
832

    
833
    /* (non-Javadoc)
834
     * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
835
     */
836
    @Override
837
    public void update(CdmDataChangeMap arg0) {
838
        // TODO Auto-generated method stub
839

    
840
    }
841

    
842
    /* (non-Javadoc)
843
     * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
844
     */
845
    @Override
846
    public ConversationHolder getConversationHolder() {
847

    
848
        return conversation;
849
    }
850

    
851

    
852
}
(2-2/3)