ref #6190 removing svn property place holder in first line of code - java files
[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 java.net.URI;
13 import java.net.URISyntaxException;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.jface.dialogs.IInputValidator;
19 import org.eclipse.jface.dialogs.InputDialog;
20 import org.eclipse.jface.window.Window;
21 import org.eclipse.swt.widgets.Event;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.handlers.HandlerUtil;
25
26 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
27 import eu.etaxonomy.taxeditor.annotatedlineeditor.AnnotatedLineEditor;
28
29 /**
30 * <p>NewObjectHandler class.</p>
31 *
32 * @author p.ciardelli
33 * @created 14.08.2009
34 * @version 1.0
35 */
36 public class NewObjectHandler extends AbstractHandler {
37
38 /* (non-Javadoc)
39 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
40 */
41 /** {@inheritDoc} */
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("User") || text.equals("Group")) {
64 nonEmptyInputValidator = new IInputValidator() {
65 public String isValid(String text) {
66 if(text == null || text.isEmpty()) {
67 return "Input cannot be empty";
68 }
69 return null;
70 }
71 };
72 }
73 InputDialog dialog = new InputDialog(HandlerUtil.getActiveShell(event),
74 "Create " + text,
75 "Enter new " + 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 }