2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
.command
;
12 import java
.util
.Collections
;
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
;
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
;
39 public class DynamicNewObjectMenu
extends CompoundContributionItem
{
40 private static final Logger logger
= Logger
41 .getLogger(DynamicNewObjectMenu
.class);
43 private final IHandlerService handlerService
= (IHandlerService
) BulkEditorUtil
.getService(IHandlerService
.class);
44 private Map
<?
extends Object
, String
> classLabelPairs
;
48 protected IContributionItem
[] getContributionItems() {
50 classLabelPairs
= getClassLabelPairs();
52 return new IContributionItem
[] {
53 new ContributionItem() {
55 public void fill(Menu menu
, int index
){
56 for(final Object key
: classLabelPairs
.keySet()){
57 createMenuItem(menu
, key
);
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(){
70 public void widgetDefaultSelected(SelectionEvent e
) {}
73 public void widgetSelected(SelectionEvent ev
) {
74 Event event
= new Event();
76 event
.text
= classLabelPairs
.get(key
);
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
);
88 * Get class label pairs from Annotated Line Editor's entity creator.
91 private Map
<?
extends Object
, String
> getClassLabelPairs() {
92 IEditorPart editor
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
94 IEditorInput input
= editor
.getEditorInput();
96 if(input
instanceof AbstractBulkEditorInput
){
97 IEntityCreator
<?
> entityCreator
= ((AbstractBulkEditorInput
) input
).getEntityCreator();
98 if(entityCreator
!=null){
99 return entityCreator
.getKeyLabelPairs();
104 return Collections
.EMPTY_MAP
;