Project

General

Profile

« Previous | Next » 

Revision 4561dd9c

Added by Alexander Oppermann over 7 years ago

Added a reload function so that the checklist editor doesn't need to be relaunched after adding terms.

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/ChecklistEditor.java
37 37
import org.eclipse.swt.widgets.Display;
38 38
import org.eclipse.swt.widgets.Label;
39 39
import org.eclipse.swt.widgets.Menu;
40
import org.eclipse.swt.widgets.MenuItem;
40 41
import org.eclipse.swt.widgets.Table;
41 42
import org.eclipse.swt.widgets.TableColumn;
42 43
import org.eclipse.swt.widgets.Text;
......
64 65
import eu.etaxonomy.taxeditor.editor.EditorUtil;
65 66
import eu.etaxonomy.taxeditor.editor.SimpleSelectionProvider;
66 67
import eu.etaxonomy.taxeditor.editor.view.checklist.edit.DistributionEditingSupport;
67
import eu.etaxonomy.taxeditor.editor.view.checklist.edit.RankEditingSupport;
68 68
import eu.etaxonomy.taxeditor.editor.view.checklist.filter.ChecklistEditorFilter;
69 69
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistDropdownSelectionListener;
70 70
import eu.etaxonomy.taxeditor.editor.view.checklist.listener.ChecklistFocusListener;
......
132 132

  
133 133
                    @Override
134 134
                    public void run() {
135
                        viewer.setInput(taxonList);
135
                    	viewer.setInput(taxonList);
136 136
                    }
137 137
                });
138 138
                monitor.worked(1);
......
182 182
    private Label statusLabel;
183 183

  
184 184
    private SortedSet<DefinedTermBase> terms = null;
185
	private ToolItem toolItem;
186
	private ChecklistDropdownSelectionListener dropListener;
185 187

  
186 188
    /**
187 189
     * @return the selectedTaxonNodes
......
231 233
        parent.setLayout(gridLayout);
232 234

  
233 235
        final Text searchText = createSearchBar(parent);
234

  
235
//        if (terms != null && !terms.isEmpty()) {
236
            createToolbar(parent);
237
//        }
236
        createToolbar(parent);
238 237
        viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
239 238
        getSite().setSelectionProvider(viewer);
240 239

  
......
242 241

  
243 242
        searchText.addFocusListener(new ChecklistFocusListener(searchText, modifyListener));
244 243
        searchText.addModifyListener(modifyListener);
245

  
246
        // Layout the viewer
247
        GridData gridData = new GridData();
248
        gridData.verticalAlignment = GridData.FILL;
249
        gridData.horizontalSpan = 3;
250
        gridData.grabExcessHorizontalSpace = true;
251
        gridData.grabExcessVerticalSpace = true;
252
        gridData.horizontalAlignment = GridData.FILL;
253
        viewer.getControl().setLayoutData(gridData);
244
        createGridDataForViewerLayout();
254 245

  
255 246
        viewer.addFilter(filter);
256
        createColumns(viewer);
247
        createTable();
257 248

  
258 249
        viewer.setContentProvider(new ChecklistContentProvider());
259
        viewer.setLabelProvider(new ChecklistLabelProvider());
250
        viewer.setLabelProvider(new ChecklistLabelProvider(viewer));
260 251
        comparator = new ChecklistEditorComparator();
261 252
        viewer.setComparator(comparator);
262 253

  
263 254
        createStatusBar(parent);
264 255
    }
265 256

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

  
266 267
    /**
267 268
     * @param parent
268 269
     * @return
......
282 283
     */
283 284
    private void createToolbar(Composite parent) {
284 285
        ToolBar toolBar = new ToolBar(parent, SWT.NONE);
285
        ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
286
        toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
286 287
        toolItem.setText("Distribution Status");
287 288
        toolItem.setToolTipText("Show Distribution Status for selected Areas");
288
        ChecklistDropdownSelectionListener dropListener = new ChecklistDropdownSelectionListener(toolItem, this, terms);
289
        createToolbarItems();
290
        toolItem.addSelectionListener(dropListener);
291
        toolBar.pack();
292
    }
293

  
294
	private void createToolbarItems() {
295
		dropListener = new ChecklistDropdownSelectionListener(toolItem, this, terms);
289 296
        for (DefinedTermBase<DefinedTermBase> term : terms) {
290 297
            if(term!=null){
291 298
                dropListener.add(term);
292 299
            }
293 300
        }
294
        toolItem.addSelectionListener(dropListener);
295
        toolBar.pack();
296
    }
301
	}
297 302

  
298 303
    private void createStatusBar(Composite composite) {
299 304
        GridData gridData = new GridData();
......
306 311
        statusLabel.setLayoutData(gridData);
307 312
    }
308 313

  
309
    // This will create the columns for the table
310
    private void createColumns(TableViewer viewer) {
311
        Table table = viewer.getTable();
314
    private void createTable() {
315
        Table table = viewer.getTable();//new Table(parent, viewer.getTable().getStyle());
312 316
        List<String> titles = new ArrayList<String>();
313 317
        Collections.addAll(titles, "Taxon", "Author", "Reference", "Rank");
314 318
        List<Integer> bounds = new ArrayList<Integer>();
315 319
        Collections.addAll(bounds, 300, 200, 200, 200);
316
        Map<Integer, Boolean>restoreColumnWidth = new HashMap<Integer, Boolean>();
317
        if (terms != null) {
318
            int i = 4;
319
            for (DefinedTermBase<DefinedTermBase> term : terms) {
320
                if(term != null){
321
                    restoreColumnWidth.put(i, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
322
                    titles.add(term.getTitleCache());
323
                    bounds.add(200);
324
                    i++;
325
                }
326
            }
327
        }
328

  
329
        for (int i = 0; i < titles.size(); i++) {
320
        Map<Integer, Boolean> restoreValuesForColumnWidth = restoreValuesForColumnWidth(titles, bounds);
321
        createInitalDistributionColumns(table, titles, bounds, restoreValuesForColumnWidth);
322
        table.setSortDirection(SWT.DOWN);
323
        table.setHeaderVisible(true);
324
        table.setLinesVisible(true);
325
	}
326
    /**
327
     * This method creates initially the distribution columns for a table. It should only be called for creation.<br>
328
     *<p> 
329
     *
330
     *<b>Notice:</b> If you want to add additional columns later please use <b>addTableViewerColumn()</b>
331
     *
332
     * @param table
333
     * @param titles
334
     * @param bounds
335
     * @param restoreValuesForColumnWidth
336
     */
337
	private void createInitalDistributionColumns(Table table,
338
			List<String> titles, List<Integer> bounds,
339
			Map<Integer, Boolean> restoreValuesForColumnWidth) {
340
		for (int columnIndex = 0; columnIndex < titles.size(); columnIndex++) {
330 341
            TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
331
            column.getColumn().setText(titles.get(i));
332
            column.getColumn().setWidth(bounds.get(i));
342
            column.getColumn().setText(titles.get(columnIndex));
343
            column.getColumn().setWidth(bounds.get(columnIndex));
333 344
            column.getColumn().setResizable(true);
334 345
            column.getColumn().setMoveable(true);
335
            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), i));
336
            if (i == 1) {
346
            column.getColumn().addSelectionListener(getSelectionAdapter(column.getColumn(), columnIndex));
347
            if (columnIndex == 1) {
337 348
                table.setSortColumn(column.getColumn());
338 349
            }
339
            if (i == 3) {
350
            if (columnIndex == 3) {
340 351
            	/** uncommented it for now because no rank editing is wanted **/
341 352
//                column.setEditingSupport(new RankEditingSupport(viewer, this));
342 353
            }
343
            if (i >= 4) {
354
            if (columnIndex >= 4) {
344 355
                //read PrefrenceStore to setWidth according to last saved state
345
                if(restoreColumnWidth.get(i)){
356
                if(restoreValuesForColumnWidth.get(columnIndex)){
346 357
                    column.getColumn().setWidth(100);
347 358
                }else{
348 359
                    column.getColumn().setWidth(0);
349 360
                }
350
                column.setEditingSupport(new DistributionEditingSupport(viewer, this, i));
361
                column.setEditingSupport(new DistributionEditingSupport(viewer, this, columnIndex));
351 362
            }
352 363
        }
353
        table.setSortDirection(SWT.DOWN);
354
        table.setHeaderVisible(true);
355
        table.setLinesVisible(true);
356
    }
364
	}
365

  
366
    /**
367
     * This methods loads the last opened distribution columns for the table viewer from the prefrence store.<br>
368
     *<p> 
369
     * <b>Notice:</b> It adds also the TitleCache to the titles list for the header of each column.<p>
370
     * 
371
     * @param titles
372
     * @param bounds
373
     * @return Map<Integer, Boolean>
374
     */
375
	private Map<Integer, Boolean> restoreValuesForColumnWidth(List<String> titles,
376
			List<Integer> bounds) {
377
		Map<Integer, Boolean> restoreColumnWidth = new HashMap<Integer, Boolean>();
378
		if (terms != null) {
379
            int columnIndex = 4;
380
            for (DefinedTermBase<DefinedTermBase> term : terms) {
381
                if(term != null){
382
                    restoreColumnWidth.put(columnIndex, PreferencesUtil.getPreferenceStore().getBoolean(term.getUuid().toString()));
383
                    titles.add(term.getTitleCache());
384
                    bounds.add(200);
385
                    columnIndex++;
386
                }
387
            }
388
        }
389
		return restoreColumnWidth;
390
	}
357 391

  
392
    /**
393
     * This method adds new DistributionColumns to an existing table. 
394
     * 
395
     * @param title
396
     * @param bound
397
     * @param colNumber
398
     * @return
399
     */
400
    private TableViewerColumn addTableViewerColumn(String title, int bound, final int colNumber) {
401
        final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
402
        final TableColumn column = viewerColumn.getColumn();
403
        column.setText(title);
404
        column.setWidth(200);
405
        viewerColumn.setEditingSupport(new DistributionEditingSupport(viewer, this, colNumber));
406
        column.setResizable(true);
407
        column.setMoveable(true);
408
        return viewerColumn;
409
      }
410
    
411
    
412
    
358 413
    /**
359 414
     *
360 415
     * pull data from database and set input for view
......
428 483
        viewer.refresh();
429 484
    }
430 485

  
486
    /**
487
     * This method should only be called for adding new Distribution columns and reloading the table.<br>
488
     * It will hide the old distribution column and load the newly added columns.<br>
489
     * <p>
490
     * <b>Notice:</b> for data update please use <b>refresh()</b>
491
     * 
492
     */
493
    @SuppressWarnings({ "unchecked", "rawtypes" })
494
	public void reload(){
495
    	//create new Items for Dropdown menue
496
    	SortedSet<DefinedTermBase> oldTerms = terms;
497
    	ChecklistLabelProvider labelProvider = new ChecklistLabelProvider();
498
    	terms = (SortedSet<DefinedTermBase>) labelProvider.getTermsOrderedByLabels(labelProvider.getNamedAreas(), CdmStore.getDefaultLanguage());
499
    	SortedSet<DefinedTermBase> newTerms = terms;
500
    	toolItem.removeSelectionListener(dropListener);
501
    	hideDistributionColumns(oldTerms);
502
    	createToolbarItems();
503
    	toolItem.addSelectionListener(dropListener);
504
        newTerms.removeAll(oldTerms);
505
        if(newTerms.size() > 0){
506
        	for(DefinedTermBase term:newTerms){
507
        		int count = viewer.getTable().getColumnCount();
508
        		addTableViewerColumn(term.getTitleCache(), 200, count);
509
        		acitivateNewColumnInDropDownMenu(term);
510
        	}
511
        }
512
        viewer.setLabelProvider(new ChecklistLabelProvider(viewer));
513
        getService().schedule(new ChecklistJob("loading Taxa", selectedTaxonNodes), Job.LONG);
514
    	viewer.refresh();
515
    }
516

  
517
	private void acitivateNewColumnInDropDownMenu(DefinedTermBase term) {
518
		Menu menu = dropListener.getMenu();
519
		MenuItem[] items = menu.getItems();
520
		for(MenuItem item: items){
521
			if(item.getText().equalsIgnoreCase(term.getTitleCache())){
522
				item.setSelection(true);
523
				PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), true);
524
			}
525
		}
526
	}
527

  
528
	private void hideDistributionColumns(SortedSet<DefinedTermBase> oldTerms) {
529
		//FIXME: THERE IS A BUG WITH THE getColumns() functin.
530
		TableColumn[] columns = viewer.getTable().getColumns();
531
    	for(int i=4; i<columns.length; i++){
532
    		columns[i].setWidth(0);
533
    		Menu menu = dropListener.getMenu();
534
    		int itemCount = menu.getItemCount();
535
    		MenuItem item = menu.getItem(i-3);
536
    		item.setSelection(false);
537
    	}
538
    	if(oldTerms != null){
539
    		for(DefinedTermBase term : oldTerms){
540
    			PreferencesUtil.getPreferenceStore().setValue(term.getUuid().toString(), false);
541
    		}
542
    	}
543
	}
544
    
431 545
    /**
432 546
     * <p>
433 547
     * Getter for the field <code>service</code>.
......
474 588

  
475 589
    @Override
476 590
    public void doSaveAs() {
477
        // TODO Auto-generated method stub
478

  
479 591
    }
480 592

  
481 593
    @Override
......
493 605

  
494 606
    @Override
495 607
    public boolean isSaveAsAllowed() {
496
        // TODO Auto-generated method stub
497 608
        return false;
498 609
    }
499 610

  
......
505 616
     */
506 617
    @Override
507 618
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
508
        // TODO Auto-generated method stub
509

  
510 619
    }
511 620

  
512 621
    /*
......
555 664
     */
556 665
    @Override
557 666
    public void contextStop(IMemento memento, IProgressMonitor monitor) {
558
        // TODO Auto-generated method stub
559 667
//        IStructuredSelection sel = (IStructuredSelection) this.viewer.getSelection();
560 668
//        if (sel.isEmpty()) {
561 669
//            return;
......
589 697
     */
590 698
    @Override
591 699
    public void contextRefresh(IProgressMonitor monitor) {
592
        // TODO Auto-generated method stub
593

  
594 700
    }
595 701

  
596 702
    /*
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/ChecklistLabelProvider.java
20 20
import org.eclipse.jface.preference.IPreferenceStore;
21 21
import org.eclipse.jface.viewers.ITableLabelProvider;
22 22
import org.eclipse.jface.viewers.LabelProvider;
23
import org.eclipse.jface.viewers.TableViewer;
23 24
import org.eclipse.swt.graphics.Image;
25
import org.eclipse.swt.widgets.TableColumn;
24 26

  
25 27
import eu.etaxonomy.cdm.api.service.IDescriptionService;
26 28
import eu.etaxonomy.cdm.api.service.ITermService;
......
49 51
    public static final String DEFAULT_ENTRY = "";
50 52
    private IDescriptionService descriptionService;
51 53
    private final List<DefinedTermBase<?>> namedAreas;
52

  
53

  
54
    TableViewer viewer = null;
54 55

  
55 56
    /**
56 57
     *
......
59 60
        namedAreas = loadNamedAreas();
60 61
    }
61 62

  
63
    public ChecklistLabelProvider(TableViewer viewer) {
64
        namedAreas = loadNamedAreas();
65
        this.viewer = viewer;
66
    }
67
    
62 68
    /*
63 69
     * (non-Javadoc)
64 70
     *
......
81 87
     * .Object, int)
82 88
     */
83 89
    /** {@inheritDoc} */
84
    @Override
90
	@Override
85 91
    public String getColumnText(Object element, int columnIndex) {
86 92
        descriptionService = CdmStore.getService(IDescriptionService.class);
87 93
        Taxon taxon = (Taxon) element;
88 94
        //TODO load areas by this list
89
        SortedSet<DefinedTermBase<?>> sortedNamedAreas = (SortedSet<DefinedTermBase<?>>) getTermsOrderedByLabels(namedAreas, CdmStore.getDefaultLanguage());
90 95

  
91 96
        List<TaxonDescription> listTaxonDescriptions = descriptionService.listTaxonDescriptions(taxon, null, null, null, null, null, DESC_INIT_STRATEGY);;
92 97
        NonViralName<?> nonVirlaName = HibernateProxyHelper.deproxy(taxon.getName(), NonViralName.class);
......
124 129
                    if (deb instanceof Distribution) {
125 130
                        Distribution distribution = (Distribution) deb;
126 131
                        String area = distribution.toString();
127
                        if(sortedNamedAreas != null){
128
                            Object[] array = sortedNamedAreas.toArray();
129
                            DefinedTermBase<DefinedTermBase> term = (DefinedTermBase<DefinedTermBase>) array[columnIndex-4];
130
                            if(term != null){
131
                                if (area.equalsIgnoreCase(term.getTitleCache())) {
132
                                    return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
133
                                }
134
                            }
132
                        if(viewer != null){
133
                        	TableColumn column = viewer.getTable().getColumn(columnIndex);
134
                        	if (area.equalsIgnoreCase(column.getText())) {
135
                        		return (distribution.getStatus().getTitleCache() != null)?distribution.getStatus().getTitleCache():DEFAULT_ENTRY;
136
                        	}
135 137
                        }
136 138
                    }
137 139
                }
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/checklist/listener/ChecklistDropdownSelectionListener.java
2 2

  
3 3
import java.util.SortedSet;
4 4

  
5
import org.apache.log4j.Logger;
5 6
import org.eclipse.jface.viewers.TableViewer;
6 7
import org.eclipse.jface.wizard.WizardDialog;
7 8
import org.eclipse.swt.SWT;
......
32 33
public class ChecklistDropdownSelectionListener extends SelectionAdapter {
33 34

  
34 35
    private final Menu menu;
35
    private final ToolItem dropdown;
36 36
    private final ChecklistEditor editor;
37 37
    private final SortedSet<DefinedTermBase> termSet;
38

  
38
    private Logger logger = Logger.getLogger(ChecklistDropdownSelectionListener.class);
39
    
39 40
    public ChecklistDropdownSelectionListener(ToolItem dropdown, ChecklistEditor editor,
40 41
            SortedSet<DefinedTermBase> termSet) {
41
        this.dropdown = dropdown;
42 42
        this.editor = editor;
43 43
        this.termSet = termSet;
44 44
        menu = new Menu(dropdown.getParent().getShell());
......
65 65
                TableColumn column = null;
66 66
                DefinedTermBase<DefinedTermBase> selectedTerm = null;
67 67
                if (termSet != null) {
68
                    int i = 0;
69
                    for (DefinedTermBase<DefinedTermBase> term : termSet) {
70
                        if (term != null) {
71
                            if (selected.getText().equalsIgnoreCase(term.getTitleCache())) {
72
                                selectedTerm = term;
73
                                column = table.getColumn(4 + i);
74
                            }
75
                            i++;
76
                        }
77
                    }
68
                	TableColumn[] columns = viewer.getTable().getColumns();
69
                	for(int i=0; i<columns.length; i++){
70
                		if(selected.getText().equalsIgnoreCase(columns[i].getText())){
71
                			column = columns[i];
72
                			logger.info("Column no " +i +" Column Header "+ column.getText() );
73
                			for (DefinedTermBase<DefinedTermBase> term : termSet) {
74
                				if(column.getText().equalsIgnoreCase(term.getTitleCache())){
75
                					selectedTerm = term;
76
                					break;
77
                				}
78
                			}
79
                		}
80
                	}
78 81
                }
79 82
                if (column != null && column.getWidth() == 0) {
80 83
                    // save column status if shown or not
......
88 91
                    }
89 92
                    column.setWidth(0);
90 93
                }
91
                viewer.refresh();
92 94
            }
93 95
        });
94 96
    }
......
103 105
                WizardDialog dialog = new WizardDialog(StoreUtil.getShell(),
104 106
                        availableDistributionWizard);
105 107

  
106
                dialog.open();
108
                int open = dialog.open();
109
                if(open == 0){
110
                	editor.reload();
111
                }
107 112
            }
108 113
        });
109 114
        new MenuItem(menu, SWT.SEPARATOR);
......
121 126
            menu.setVisible(true);
122 127
        }
123 128
    }
129
    
130
    public Menu getMenu(){
131
    	return menu;
132
    }
124 133
}

Also available in: Unified diff