Project

General

Profile

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

    
377
            }
378
        }
379
	}
380

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

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

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

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

    
446

    
447

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

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

    
485

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

    
497
    }
498

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

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

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

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

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

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

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

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

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

    
593
            }
594
        }
595
    	updateColumnIndex();
596
      }
597

    
598

    
599
    	for (DefinedTermBase term:newTerms){
600
    	    boolean isNew = true;
601

    
602
    	    Integer position = areaPosition.get(term.getUuid());
603

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

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

    
619
           // acitivateNewColumnInDropDownMenu(term);
620
    	}
621

    
622

    
623

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

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

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

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

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

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

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

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

    
731
            this.checklistEditorInput.merge();
732

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

    
740
    }
741

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

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

    
752
        viewer.addSelectionChangedListener(selectionChangedListener);
753

    
754

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

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

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

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

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

    
802

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

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

    
817
    }
818

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

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

    
836

    
837
}
(2-2/4)