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