Merge branch 'release/4.6.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / annotatedlineeditor / handler / NewObjectHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.annotatedlineeditor.handler;
11
12 import org.eclipse.core.commands.AbstractHandler;
13 import org.eclipse.core.commands.ExecutionEvent;
14 import org.eclipse.core.commands.ExecutionException;
15 import org.eclipse.jface.dialogs.IInputValidator;
16 import org.eclipse.jface.dialogs.InputDialog;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.widgets.Event;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
24 import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor;
25 import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.GroupCreator;
26 import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.UserCreator;
27
28 /**
29 * <p>NewObjectHandler class.</p>
30 *
31 * @author p.ciardelli
32 * @created 14.08.2009
33 * @version 1.0
34 */
35 public class NewObjectHandler extends AbstractHandler {
36
37 /* (non-Javadoc)
38 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
39 */
40 /** {@inheritDoc} */
41 @Override
42 public Object execute(ExecutionEvent event) throws ExecutionException {
43
44 IEditorPart editor = HandlerUtil.getActiveEditor(event);
45 IEditorInput input = editor.getEditorInput();
46
47 if (editor instanceof AnnotatedLineEditor) {
48
49 Object key = ((Event)event.getTrigger()).data;
50 if (key != null) {
51 if(!(key instanceof SpecimenOrObservationType)) {
52 String text = ((Event)event.getTrigger()).text;
53
54 //FIXME : This should probably go into some ValidatorFactory
55 IInputValidator nonEmptyInputValidator = null;
56 //FIXME : This is a workaround to not allow empty strings in the
57 // input dialog for User and Group entities.
58 // Normally this should be default
59 // behaviour, so we need to discuss whether this handler
60 // should be used to handle the creating new entities of
61 // type other than User and Group.
62 // Once #4348 is fixed this check can be removed.
63 if(text.equals(UserCreator.USER) || text.equals(GroupCreator.GROUP)) {
64 nonEmptyInputValidator = new IInputValidator() {
65 @Override
66 public String isValid(String text) {
67 if(text == null || text.isEmpty()) {
68 return "Input cannot be empty";
69 }
70 return null;
71 }
72 };
73 }
74 InputDialog dialog = new InputDialog(HandlerUtil.getActiveShell(event),
75 String.format("Create %s", text), String.format("Enter new %s", text), "",
76 nonEmptyInputValidator);
77
78 if (dialog.open() != Window.CANCEL) {
79 ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject(key, dialog.getValue());
80 }
81 } else {
82 ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject(key, "Untitled");
83 }
84
85 } else {
86 ((AnnotatedLineEditor) editor).createAnnotatedLineNewObject();
87 }
88 }
89 return null;
90 }
91 }