- fixed potential NPE (#4448)
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / handler / FixClassificationHierarchyHandler.java
1 package eu.etaxonomy.taxeditor.editor.handler;
2
3 import org.apache.log4j.Logger;
4 import org.eclipse.core.commands.AbstractHandler;
5 import org.eclipse.core.commands.ExecutionEvent;
6 import org.eclipse.core.commands.ExecutionException;
7 import org.eclipse.jface.viewers.ISelection;
8 import org.eclipse.jface.viewers.IStructuredSelection;
9 import org.eclipse.ui.handlers.HandlerUtil;
10
11 import eu.etaxonomy.cdm.api.service.IClassificationService;
12 import eu.etaxonomy.cdm.model.taxon.Classification;
13 import eu.etaxonomy.taxeditor.model.MessagingUtils;
14 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
15 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
16 import eu.etaxonomy.taxeditor.store.CdmStore;
17
18 public class FixClassificationHierarchyHandler extends AbstractHandler {
19
20 private static final Logger logger = Logger.getLogger(FixClassificationHierarchyHandler.class);
21
22 /*
23 * (non-Javadoc)
24 *
25 * @see
26 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
27 * ExecutionEvent)
28 */
29 @Override
30 public Object execute(ExecutionEvent event) throws ExecutionException {
31 boolean isChecklistEditorActivated = PreferencesUtil.getPreferenceStore().getBoolean(
32 IPreferenceKeys.DISTRIBUTION_AREA_PREFRENCES_ACTIVE);
33 if (isChecklistEditorActivated) {
34 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
35 if (currentSelection instanceof IStructuredSelection) {
36 Object selectedElement = ((IStructuredSelection) currentSelection).getFirstElement();
37 if (selectedElement instanceof Classification) {
38
39 Classification classification = (Classification) selectedElement;
40 try {
41 IClassificationService service = CdmStore.getService(IClassificationService.class);
42
43 // Map<String, List<TaxonNode>> sortedGenusList =
44 // service.getSortedGenusList(classification.getAllNodes());
45
46 Classification newClassification = service.createHierarchyInClassification(classification, null);
47
48 MessagingUtils.messageDialog("Fix Hierarchy successful",
49 FixClassificationHierarchyHandler.class, "Operation 'Fix Hierarchy' was successful. New classification is " + newClassification.getTitleCache());
50
51 } catch (Exception e) {
52 MessagingUtils.messageDialog("Failed to open Editor", FixClassificationHierarchyHandler.class,
53 "Could not open ChecklistView. The hierarchy is corrupted!", e);
54 }
55 }
56 }
57 }
58 return null;
59 }
60 }