had to rename the packages to make them compliant with buckminster
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / command / DynamicNewObjectMenu.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.bulkeditor.command;
12
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 * <p>DynamicNewObjectMenu class.</p>
36 *
37 * @author n.hoffmann
38 * @created 17.04.2009
39 * @version 1.0
40 */
41 public class DynamicNewObjectMenu extends CompoundContributionItem {
42 private static final Logger logger = Logger
43 .getLogger(DynamicNewObjectMenu.class);
44
45 private IHandlerService handlerService = (IHandlerService) BulkEditorUtil.getService(IHandlerService.class);
46 private Map<Object, String> classLabelPairs;
47 /* (non-Javadoc)
48 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
49 */
50 /** {@inheritDoc} */
51 @Override
52 protected IContributionItem[] getContributionItems() {
53
54 classLabelPairs = getClassLabelPairs();
55
56 return new IContributionItem[] {
57 new ContributionItem() {
58 public void fill(Menu menu, int index){
59 for(final Object key : classLabelPairs.keySet()){
60 createMenuItem(menu, key);
61 }
62 }
63 }
64 };
65 }
66
67 private void createMenuItem(Menu menu, final Object key){
68 MenuItem menuItem = new MenuItem(menu, -1);
69 menuItem.setText(classLabelPairs.get(key));
70 menuItem.addSelectionListener(new SelectionListener(){
71
72 public void widgetDefaultSelected(SelectionEvent e) {}
73
74 public void widgetSelected(SelectionEvent ev) {
75 Event event = new Event();
76 event.data = key;
77 event.text = classLabelPairs.get(key);
78 try {
79 handlerService.executeCommand(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID, event);
80 } catch (Exception e) {
81 logger.error("Error executing command", e);
82 throw new RuntimeException("Error executing command", e);
83 }
84 }
85 });
86 }
87
88 /**
89 * Get class label pairs from Annotated Line Editor's entity creator.
90 * @return
91 */
92 private Map<Object, String> getClassLabelPairs() {
93 IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
94 if (editor != null){
95 IEditorInput input = editor.getEditorInput();
96
97 if(input instanceof AbstractBulkEditorInput){
98 IEntityCreator<?> entityCreator = ((AbstractBulkEditorInput) input).getEntityCreator();
99 return entityCreator.getKeyLabelPairs();
100 }
101 }
102
103 return null;
104 }
105 }