Project

General

Profile

Download (3.18 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.bulkeditor.command;
11

    
12
import org.eclipse.core.commands.ExecutionException;
13
import org.eclipse.core.commands.NotEnabledException;
14
import org.eclipse.core.commands.NotHandledException;
15
import org.eclipse.core.commands.common.NotDefinedException;
16
import org.eclipse.jface.action.ContributionItem;
17
import org.eclipse.jface.action.IContributionItem;
18
import org.eclipse.swt.SWT;
19
import org.eclipse.swt.events.SelectionAdapter;
20
import org.eclipse.swt.events.SelectionEvent;
21
import org.eclipse.swt.widgets.Event;
22
import org.eclipse.swt.widgets.Menu;
23
import org.eclipse.swt.widgets.MenuItem;
24
import org.eclipse.ui.PlatformUI;
25
import org.eclipse.ui.actions.CompoundContributionItem;
26
import org.eclipse.ui.handlers.IHandlerService;
27

    
28
import eu.etaxonomy.cdm.model.common.MarkerType;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31

    
32
/**
33
 * @author n.hoffmann
34
 * @created Dec 13, 2010
35
 * @version 1.0
36
 */
37
public class DynamicMarkerTypeEditingMenu extends CompoundContributionItem {
38
	
39
	private IHandlerService handlerService = 
40
		(IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
41
	
42
	/* (non-Javadoc)
43
	 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
44
	 */
45
	@Override
46
	protected IContributionItem[] getContributionItems() {
47

    
48
		return new IContributionItem[] {
49
				new ContributionItem() {
50
					public void fill(Menu menu, int index){
51
						for(MarkerType markerType : CdmStore.getTermManager().getPreferredTerms(MarkerType.class)){
52
							createMenuItem(menu, markerType);
53
						}
54
					}
55
				}
56
		};
57
	}
58
	
59
	private void createMenuItem(Menu menu, final MarkerType markerType) {
60

    
61
		MenuItem subMenuItem = new MenuItem(menu, SWT.CASCADE);
62
		subMenuItem.setText(String.format("Set Flag '%s'", markerType.getLabel()));
63
		
64
		Menu subMenu = new Menu(menu.getShell(), SWT.DROP_DOWN);
65
		subMenuItem.setMenu(subMenu);
66
		
67
		MenuItem trueItem = new MenuItem(subMenu, SWT.PUSH);
68
		trueItem.setText("Yes");
69
		trueItem.addSelectionListener(new SelectionAdapter() {
70
			public void widgetSelected(SelectionEvent e) {
71
				doSetFlagCommand(markerType, true);
72
			}	
73
		});
74
		
75
		MenuItem falseItem = new MenuItem(subMenu, SWT.PUSH);
76
		falseItem.setText("No");
77
		falseItem.addSelectionListener(new SelectionAdapter() {
78
			public void widgetSelected(SelectionEvent e) {
79
				doSetFlagCommand(markerType, false);
80
			}						
81
		});
82
	}
83
	
84
	private void doSetFlagCommand(MarkerType markerType, boolean flag) {
85
		try {
86
			Event event = new Event();
87
			event.data = new Object[]{markerType, flag};
88
			handlerService.executeCommand("taxeditor-bulkeditor.command.setMarkerFlag", event);
89
		} catch (ExecutionException e) {
90
			MessagingUtils.error(getClass(), e);
91
		} catch (NotDefinedException e) {
92
			MessagingUtils.error(getClass(), e);
93
		} catch (NotEnabledException e) {
94
			MessagingUtils.error(getClass(), e);
95
		} catch (NotHandledException e) {
96
			MessagingUtils.error(getClass(), e);
97
		}
98
		
99
	}		
100

    
101
}
(5-5/7)