Revision 1109c1c6
Added by Patrick Plitzner almost 10 years ago
- refactored DataImportEditor context menu
- removed extension point in plugin.xml
- added dynamic context menu
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/dataimport/DataImportEditorContextMenu.java | ||
---|---|---|
1 | 1 |
package eu.etaxonomy.taxeditor.editor.view.dataimport; |
2 | 2 |
|
3 |
import java.io.IOException; |
|
4 |
|
|
5 |
import org.apache.http.client.ClientProtocolException; |
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.eclipse.core.runtime.IProgressMonitor; |
|
8 |
import org.eclipse.core.runtime.IStatus; |
|
9 |
import org.eclipse.core.runtime.Status; |
|
10 |
import org.eclipse.core.runtime.jobs.Job; |
|
3 | 11 |
import org.eclipse.jface.action.ContributionItem; |
4 | 12 |
import org.eclipse.jface.action.IContributionItem; |
5 | 13 |
import org.eclipse.jface.viewers.ISelection; |
14 |
import org.eclipse.jface.viewers.StructuredSelection; |
|
6 | 15 |
import org.eclipse.swt.SWT; |
7 | 16 |
import org.eclipse.swt.widgets.Event; |
8 | 17 |
import org.eclipse.swt.widgets.Listener; |
... | ... | |
10 | 19 |
import org.eclipse.swt.widgets.MenuItem; |
11 | 20 |
import org.eclipse.ui.IEditorPart; |
12 | 21 |
import org.eclipse.ui.IWorkbenchWindow; |
22 |
import org.eclipse.ui.PartInitException; |
|
13 | 23 |
import org.eclipse.ui.PlatformUI; |
14 | 24 |
import org.eclipse.ui.actions.CompoundContributionItem; |
15 | 25 |
|
26 |
import eu.etaxonomy.cdm.api.service.IOccurrenceService; |
|
27 |
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery; |
|
28 |
import eu.etaxonomy.cdm.ext.occurrence.gbif.DataSetResponse; |
|
29 |
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifDataSetProtocol; |
|
30 |
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifQueryServiceWrapper; |
|
31 |
import eu.etaxonomy.cdm.ext.occurrence.gbif.GbifResponse; |
|
32 |
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase; |
|
33 |
import eu.etaxonomy.taxeditor.editor.EditorUtil; |
|
34 |
import eu.etaxonomy.taxeditor.store.CdmStore; |
|
35 |
|
|
16 | 36 |
public class DataImportEditorContextMenu extends CompoundContributionItem { |
17 | 37 |
|
38 |
private final Logger logger = Logger.getLogger(DataImportEditorContextMenu.class); |
|
39 |
|
|
18 | 40 |
/* (non-Javadoc) |
19 | 41 |
* @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems() |
20 | 42 |
*/ |
... | ... | |
27 | 49 |
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
28 | 50 |
final IEditorPart activeEditor = window.getActivePage().getActiveEditor(); |
29 | 51 |
final ISelection selection = window.getActivePage().getSelection(); |
30 |
MenuItem item = new MenuItem(menu, SWT.NONE); |
|
31 |
item.setText("Save Specimen"); |
|
32 |
item.addListener(SWT.Selection, new Listener() { |
|
52 |
if(selection instanceof StructuredSelection){ |
|
53 |
final StructuredSelection structuredSelection = (StructuredSelection)selection; |
|
54 |
|
|
55 |
//Save Specimen |
|
56 |
MenuItem saveSpecimenItem = new MenuItem(menu, SWT.NONE); |
|
57 |
saveSpecimenItem.setText("Save Specimen"); |
|
58 |
saveSpecimenItem.addListener(SWT.Selection, new Listener() { |
|
59 |
|
|
60 |
@Override |
|
61 |
public void handleEvent(Event event) { |
|
62 |
if(structuredSelection.iterator().hasNext()){ |
|
63 |
Object next = structuredSelection.iterator().next(); |
|
64 |
if(next instanceof GbifResponse){ |
|
65 |
//unwrap DerivedUnit from GbifResponse |
|
66 |
next = ((GbifResponse) next).getDerivedUnitFacade().innerDerivedUnit(); |
|
67 |
} |
|
68 |
if(next instanceof SpecimenOrObservationBase){ |
|
69 |
if(activeEditor instanceof DataImportEditor){ |
|
70 |
DataImportEditor<?> dataImportEditor = (DataImportEditor<?>)activeEditor; |
|
71 |
CdmStore.getService(IOccurrenceService.class).saveOrUpdate((SpecimenOrObservationBase<?>) next); |
|
72 |
dataImportEditor.getConversationHolder().commit(true); |
|
73 |
System.out.println("commit"); |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
} |
|
78 |
}); |
|
79 |
|
|
80 |
//Query original data provider |
|
81 |
if(structuredSelection.size()==1 |
|
82 |
&& structuredSelection.iterator().next() instanceof GbifResponse |
|
83 |
&& ((GbifResponse)structuredSelection.iterator().next()).getDataSetProtocol()!=null |
|
84 |
&& ((GbifResponse)structuredSelection.iterator().next()).getDataSetProtocol()==GbifDataSetProtocol.BIOCASE){ |
|
85 |
final GbifResponse gbifResponse = (GbifResponse)structuredSelection.iterator().next(); |
|
86 |
MenuItem queryOriginalItem = new MenuItem(menu, SWT.NONE); |
|
87 |
queryOriginalItem.setText("Query original data provider"); |
|
88 |
queryOriginalItem.addListener(SWT.Selection, new Listener() { |
|
89 |
|
|
90 |
@Override |
|
91 |
public void handleEvent(Event event) { |
|
92 |
try{ |
|
93 |
DataSetResponse response = new GbifQueryServiceWrapper().queryOriginalDataSet(gbifResponse); |
|
94 |
final BioCaseEditorInput input = new BioCaseEditorInput(new OccurenceQuery(response.getUnitId()), response.getEndpoint()); |
|
95 |
EditorUtil.open(input); |
|
96 |
Job queryJob = new Job("Querying for original record...") { |
|
33 | 97 |
|
34 |
@Override |
|
35 |
public void handleEvent(Event event) { |
|
36 |
System.out.println("listener"); |
|
98 |
@Override |
|
99 |
protected IStatus run(IProgressMonitor monitor) { |
|
100 |
input.query(); |
|
101 |
return Status.OK_STATUS; |
|
102 |
} |
|
103 |
}; |
|
104 |
queryJob.schedule(); |
|
105 |
} catch (ClientProtocolException e) { |
|
106 |
logger.error("Query for single record failed.", e); |
|
107 |
} catch (IOException e) { |
|
108 |
logger.error("Query for single record failed.", e); |
|
109 |
} catch (PartInitException e) { |
|
110 |
logger.error("Could not open editor.", e); |
|
111 |
} |
|
112 |
} |
|
113 |
}); |
|
37 | 114 |
} |
38 |
}); |
|
39 |
System.out.println(activeEditor); |
|
40 |
System.out.println(selection); |
|
115 |
} |
|
41 | 116 |
} |
42 |
} |
|
43 |
}; |
|
117 |
}}; |
|
44 | 118 |
return contributionItems; |
45 | 119 |
} |
46 | 120 |
|
Also available in: Unified diff