Project

General

Profile

Download (29.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
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
import org.eclipse.core.runtime.IStatus;
27
import org.eclipse.core.runtime.Status;
28
import org.eclipse.core.runtime.jobs.Job;
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.ISelectionChangedListener;
35
import org.eclipse.jface.viewers.IStructuredSelection;
36
import org.eclipse.jface.viewers.SelectionChangedEvent;
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.swt.SWT;
41
import org.eclipse.swt.events.ModifyListener;
42
import org.eclipse.swt.events.SelectionAdapter;
43
import org.eclipse.swt.events.SelectionEvent;
44
import org.eclipse.swt.layout.GridData;
45
import org.eclipse.swt.layout.GridLayout;
46
import org.eclipse.swt.widgets.Composite;
47
import org.eclipse.swt.widgets.Display;
48
import org.eclipse.swt.widgets.Label;
49
import org.eclipse.swt.widgets.Menu;
50
import org.eclipse.swt.widgets.MenuItem;
51
import org.eclipse.swt.widgets.Table;
52
import org.eclipse.swt.widgets.TableColumn;
53
import org.eclipse.swt.widgets.Text;
54
import org.eclipse.swt.widgets.ToolBar;
55
import org.eclipse.swt.widgets.ToolItem;
56
import org.eclipse.ui.IEditorInput;
57
import org.eclipse.ui.IMemento;
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.api.service.ITaxonService;
64
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
65
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
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.SimpleSelectionProvider;
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.IContextListener;
82
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
83
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
84
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
85
import eu.etaxonomy.taxeditor.model.MessagingUtils;
86
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
87
import eu.etaxonomy.taxeditor.preference.Resources;
88
import eu.etaxonomy.taxeditor.store.CdmStore;
89

    
90
/**
91
 * 
92
 * @author k.luther
93
 *
94
 */
95
public class ChecklistEditorE4 implements IPartContentHasFactualData, IConversationEnabled,
96
        IContextListener, IDirtyMarkable, IPartContentHasDetails{
97

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

    
104
    private static final Logger logger = Logger.getLogger(ChecklistEditorE4.class);
105

    
106

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

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

    
164
    private TableViewer viewer;
165

    
166
    private String partNameCache;
167

    
168
    //FIXME E4 migrate/delete ater full migration
169
    
170
    private IWorkbenchSiteProgressService service;
171

    
172
    private ITaxonNodeService taxonNodeService;
173

    
174
    private ChecklistEditorComparator comparator;
175

    
176
    private ChecklistEditorInput checklistEditorInput;
177

    
178
    private SimpleSelectionProvider simpleSelectionProvider;
179

    
180
    private ConversationHolder conversation;
181

    
182
    private Integer countNodes;
183

    
184
    private List<TaxonNode> selectedTaxonNodes;
185

    
186
    @Inject
187
    private MPart thisPart;
188
    
189
    @Inject
190
    private MDirtyable dirty;
191
    
192
    @Inject
193
    private ESelectionService selService;
194

    
195
    private ISelectionChangedListener selectionChangedListener;
196

    
197
    private ChecklistEditorFilter filter;
198

    
199
    private Menu headerMenu;
200

    
201
    private Label statusLabel;
202

    
203
   // private final ICdmEntitySession cdmEntitySession;
204

    
205
    private ChecklistLabelProvider labelProvider;
206

    
207
    private SortedSet<DefinedTermBase> terms = null;
208
	private ToolItem toolItem;
209
	private ChecklistDropdownSelectionListenerE4 dropListener;
210

    
211
    /**
212
     * @return the selectedTaxonNodes
213
     */
214
    public List<TaxonNode> getSelectedTaxonNodes() {
215
        return selectedTaxonNodes;
216
    }
217

    
218
    /**
219
     * @param selectedTaxonNodes
220
     *            the selectedTaxonNodes to set
221
     */
222
    public void setSelectedTaxonNodes(List<TaxonNode> selectedTaxonNodes) {
223
        this.selectedTaxonNodes = selectedTaxonNodes;
224
    }
225

    
226
    @Inject
227
    public ChecklistEditorE4() {
228
    }
229

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

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

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

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

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

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

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

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

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

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

    
289
        createStatusBar(parent);
290
    }
291

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

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

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

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

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

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

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

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

    
362

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

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

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

    
466

    
467

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
671
    }
672

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

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

    
687
        loadDataInput();
688
    }
689

    
690

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

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

    
702
    }
703

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

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

    
735
    }
736

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

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

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

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

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

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

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

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

    
808

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

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

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

    
841
    }
842

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

    
849
        return conversation;
850
    }
851

    
852

    
853
}
(2-2/3)