Project

General

Profile

Download (3.21 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 java.util.Collections;
13
import java.util.Map;
14

    
15
import org.apache.log4j.Logger;
16
import org.eclipse.jface.action.ContributionItem;
17
import org.eclipse.jface.action.IContributionItem;
18
import org.eclipse.swt.events.SelectionEvent;
19
import org.eclipse.swt.events.SelectionListener;
20
import org.eclipse.swt.widgets.Event;
21
import org.eclipse.swt.widgets.Menu;
22
import org.eclipse.swt.widgets.MenuItem;
23
import org.eclipse.ui.IEditorInput;
24
import org.eclipse.ui.IEditorPart;
25
import org.eclipse.ui.PlatformUI;
26
import org.eclipse.ui.actions.CompoundContributionItem;
27
import org.eclipse.ui.handlers.IHandlerService;
28

    
29
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
30
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
31
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
32
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
33

    
34
/**
35
 * @author n.hoffmann
36
 * @created 17.04.2009
37
 * @version 1.0
38
 */
39
public class DynamicNewObjectMenu extends CompoundContributionItem {
40
	private static final Logger logger = Logger
41
			.getLogger(DynamicNewObjectMenu.class);
42

    
43
	private final IHandlerService handlerService = (IHandlerService) BulkEditorUtil.getService(IHandlerService.class);
44
	private Map<Object, String> classLabelPairs;
45

    
46
	/** {@inheritDoc} */
47
	@Override
48
	protected IContributionItem[] getContributionItems() {
49

    
50
		classLabelPairs = getClassLabelPairs();
51

    
52
		return new IContributionItem[] {
53
				new ContributionItem() {
54
					@Override
55
                    public void fill(Menu menu, int index){
56
						for(final Object key : classLabelPairs.keySet()){
57
							createMenuItem(menu, key);
58
						}
59
					}
60
				}
61
		};
62
	}
63

    
64
	private void createMenuItem(Menu menu, final Object key){
65
		MenuItem menuItem = new MenuItem(menu, -1);
66
		menuItem.setText(classLabelPairs.get(key));
67
		menuItem.addSelectionListener(new SelectionListener(){
68

    
69
			@Override
70
            public void widgetDefaultSelected(SelectionEvent e) {}
71

    
72
			@Override
73
            public void widgetSelected(SelectionEvent ev) {
74
				Event event = new Event();
75
				event.data = key;
76
				event.text = classLabelPairs.get(key);
77
				try {
78
					handlerService.executeCommand(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID, event);
79
				} catch (Exception e) {
80
					logger.error("Error executing command", e);
81
					throw new RuntimeException("Error executing command", e);
82
				}
83
			}
84
		});
85
	}
86

    
87
	/**
88
	 * Get class label pairs from Annotated Line Editor's entity creator.
89
	 * @return
90
	 */
91
	private Map<Object, String> getClassLabelPairs() {
92
		IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
93
		if (editor != null){
94
			IEditorInput input = editor.getEditorInput();
95

    
96
			if(input instanceof AbstractBulkEditorInput){
97
				IEntityCreator<?> entityCreator = ((AbstractBulkEditorInput) input).getEntityCreator();
98
				if(entityCreator!=null){
99
				    return entityCreator.getKeyLabelPairs();
100
				}
101
			}
102
		}
103

    
104
		return Collections.EMPTY_MAP;
105
	}
106
}
(6-6/7)