1 |
98788db9
|
p.ciardelli
|
// $Id$
|
2 |
|
|
/**
|
3 |
|
|
* Copyright (C) 2007 EDIT
|
4 |
313a413f
|
Patric Plitzner
|
* European Distributed Institute of Taxonomy
|
5 |
98788db9
|
p.ciardelli
|
* http://www.e-taxonomy.eu
|
6 |
313a413f
|
Patric Plitzner
|
*
|
7 |
98788db9
|
p.ciardelli
|
* 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 |
db5e366d
|
n.hoffmann
|
import org.eclipse.ui.IEditorInput;
|
24 |
98788db9
|
p.ciardelli
|
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 |
db5e366d
|
n.hoffmann
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
|
31 |
98788db9
|
p.ciardelli
|
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
|
32 |
db5e366d
|
n.hoffmann
|
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
|
33 |
98788db9
|
p.ciardelli
|
|
34 |
|
|
/**
|
35 |
3be6ef3e
|
n.hoffmann
|
* <p>DynamicNewObjectMenu class.</p>
|
36 |
|
|
*
|
37 |
98788db9
|
p.ciardelli
|
* @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 |
313a413f
|
Patric Plitzner
|
|
45 |
|
|
private final IHandlerService handlerService = (IHandlerService) BulkEditorUtil.getService(IHandlerService.class);
|
46 |
db5e366d
|
n.hoffmann
|
private Map<Object, String> classLabelPairs;
|
47 |
98788db9
|
p.ciardelli
|
/* (non-Javadoc)
|
48 |
|
|
* @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
|
49 |
|
|
*/
|
50 |
3be6ef3e
|
n.hoffmann
|
/** {@inheritDoc} */
|
51 |
98788db9
|
p.ciardelli
|
@Override
|
52 |
|
|
protected IContributionItem[] getContributionItems() {
|
53 |
313a413f
|
Patric Plitzner
|
|
54 |
db5e366d
|
n.hoffmann
|
classLabelPairs = getClassLabelPairs();
|
55 |
313a413f
|
Patric Plitzner
|
|
56 |
98788db9
|
p.ciardelli
|
return new IContributionItem[] {
|
57 |
|
|
new ContributionItem() {
|
58 |
313a413f
|
Patric Plitzner
|
@Override
|
59 |
|
|
public void fill(Menu menu, int index){
|
60 |
98788db9
|
p.ciardelli
|
for(final Object key : classLabelPairs.keySet()){
|
61 |
db5e366d
|
n.hoffmann
|
createMenuItem(menu, key);
|
62 |
313a413f
|
Patric Plitzner
|
}
|
63 |
98788db9
|
p.ciardelli
|
}
|
64 |
|
|
}
|
65 |
|
|
};
|
66 |
|
|
}
|
67 |
|
|
|
68 |
db5e366d
|
n.hoffmann
|
private void createMenuItem(Menu menu, final Object key){
|
69 |
|
|
MenuItem menuItem = new MenuItem(menu, -1);
|
70 |
|
|
menuItem.setText(classLabelPairs.get(key));
|
71 |
|
|
menuItem.addSelectionListener(new SelectionListener(){
|
72 |
|
|
|
73 |
313a413f
|
Patric Plitzner
|
@Override
|
74 |
|
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
75 |
db5e366d
|
n.hoffmann
|
|
76 |
313a413f
|
Patric Plitzner
|
@Override
|
77 |
|
|
public void widgetSelected(SelectionEvent ev) {
|
78 |
db5e366d
|
n.hoffmann
|
Event event = new Event();
|
79 |
|
|
event.data = key;
|
80 |
|
|
event.text = classLabelPairs.get(key);
|
81 |
|
|
try {
|
82 |
|
|
handlerService.executeCommand(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID, event);
|
83 |
|
|
} catch (Exception e) {
|
84 |
|
|
logger.error("Error executing command", e);
|
85 |
|
|
throw new RuntimeException("Error executing command", e);
|
86 |
|
|
}
|
87 |
313a413f
|
Patric Plitzner
|
}
|
88 |
db5e366d
|
n.hoffmann
|
});
|
89 |
|
|
}
|
90 |
313a413f
|
Patric Plitzner
|
|
91 |
98788db9
|
p.ciardelli
|
/**
|
92 |
|
|
* Get class label pairs from Annotated Line Editor's entity creator.
|
93 |
|
|
* @return
|
94 |
|
|
*/
|
95 |
|
|
private Map<Object, String> getClassLabelPairs() {
|
96 |
|
|
IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
|
97 |
db5e366d
|
n.hoffmann
|
if (editor != null){
|
98 |
|
|
IEditorInput input = editor.getEditorInput();
|
99 |
313a413f
|
Patric Plitzner
|
|
100 |
db5e366d
|
n.hoffmann
|
if(input instanceof AbstractBulkEditorInput){
|
101 |
|
|
IEntityCreator<?> entityCreator = ((AbstractBulkEditorInput) input).getEntityCreator();
|
102 |
|
|
return entityCreator.getKeyLabelPairs();
|
103 |
|
|
}
|
104 |
98788db9
|
p.ciardelli
|
}
|
105 |
313a413f
|
Patric Plitzner
|
|
106 |
db5e366d
|
n.hoffmann
|
return null;
|
107 |
313a413f
|
Patric Plitzner
|
}
|
108 |
3be6ef3e
|
n.hoffmann
|
}
|