performed javacscript:fix and worked on documentation
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / NavigationUtil.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.navigation;
11
12 import java.util.Set;
13 import java.util.UUID;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.NotEnabledException;
17 import org.eclipse.core.commands.NotHandledException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.core.commands.operations.IUndoContext;
20 import org.eclipse.core.commands.operations.UndoContext;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.widgets.Display;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorReference;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.PartInitException;
28 import org.eclipse.ui.handlers.IHandlerService;
29 import org.eclipse.ui.navigator.CommonViewer;
30
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33 import eu.etaxonomy.cdm.model.taxon.Synonym;
34 import eu.etaxonomy.cdm.model.taxon.Taxon;
35 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
37 import eu.etaxonomy.taxeditor.editor.EditorUtil;
38 import eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration;
39 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
40 import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
41 import eu.etaxonomy.taxeditor.model.AbstractUtility;
42 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
43 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator;
44
45 /**
46 * <p>NavigationUtil class.</p>
47 *
48 * @author n.hoffmann
49 * @created 24.03.2009
50 * @version 1.0
51 */
52 public class NavigationUtil extends AbstractUtility{
53 private static IUndoContext defaultUndoContext;
54
55 /**
56 * <p>executeEditHandler</p>
57 */
58 public static void executeEditHandler(){
59
60 String commandId = "eu.etaxonomy.taxeditor.navigation.command.editSelection";
61
62 IHandlerService handlerService = (IHandlerService) NavigationUtil.getService(IHandlerService.class);
63 try {
64 handlerService.executeCommand(commandId, null);
65 } catch (ExecutionException e) {
66 NavigationUtil.error(NavigationUtil.class, e);
67 } catch (NotDefinedException e) {
68 NavigationUtil.error(NavigationUtil.class, e);
69 } catch (NotEnabledException e) {
70 NavigationUtil.error(NavigationUtil.class, e);
71 } catch (NotHandledException e) {
72 NavigationUtil.error(NavigationUtil.class, e);
73 }
74 }
75
76 /**
77 * <p>openEditor</p>
78 *
79 * @param selectedObject a {@link eu.etaxonomy.cdm.model.common.CdmBase} object.
80 */
81 public static void openEditor(CdmBase selectedObject){
82 UUID entityUuid = selectedObject.getUuid();
83 try {
84 if(selectedObject instanceof TaxonNode){
85 EditorUtil.openTaxonNode(entityUuid);
86 }else if(selectedObject instanceof TaxonBase){
87 EditorUtil.openTaxonBase(entityUuid);
88 }else if(selectedObject instanceof TaxonNameBase){
89 // TODO open bulk editor
90 warningDialog("Not implemented yet", NavigationUtil.class, "You tried to open a name. This is not handled by the software yet.");
91 }else{
92 warningDialog("Unsupported Type", NavigationUtil.class, "No editor exists for the current selection: " + selectedObject);
93 }
94 } catch (PartInitException e) {
95 NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
96 } catch (Exception e) {
97 EditorUtil.warningDialog("Could not create Taxon", NavigationUtil.class, e.getMessage());
98 }
99 }
100
101 /**
102 * <p>openEmpty</p>
103 *
104 * @param parentNodeUuid a {@link java.util.UUID} object.
105 */
106 public static void openEmpty(UUID parentNodeUuid) {
107 try {
108 EditorUtil.openEmpty(parentNodeUuid);
109 } catch (PartInitException e) {
110 NavigationUtil.error(NavigationUtil.class, "Error opening the editor", e);
111 }
112 }
113
114 /**
115 * <p>openEditor</p>
116 *
117 * @param configuration a eu.etaxonomy.taxeditor.editor.OpenEditorConfiguration object.
118 */
119 public static void openEditor(OpenEditorConfiguration configuration) {
120 // TODO Auto-generated method stub
121
122 }
123
124 /**
125 * <p>getShell</p>
126 *
127 * @return a {@link org.eclipse.swt.widgets.Shell} object.
128 */
129 public static Shell getShell() {
130 return getActiveWindow().getShell();
131 }
132
133 /**
134 * <p>getActiveWindow</p>
135 *
136 * @return a {@link org.eclipse.ui.IWorkbenchWindow} object.
137 */
138 public static IWorkbenchWindow getActiveWindow() {
139 return TaxeditorNavigationPlugin.getDefault().getWorkbench().
140 getActiveWorkbenchWindow();
141 }
142
143 /**
144 * <p>getWorkbenchUndoContext</p>
145 *
146 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
147 */
148 public static IUndoContext getWorkbenchUndoContext() {
149 return TaxeditorEditorPlugin.getDefault().getWorkbench().
150 getOperationSupport().getUndoContext();
151 }
152
153 /**
154 * <p>getUndoContext</p>
155 *
156 * @return a {@link org.eclipse.core.commands.operations.IUndoContext} object.
157 */
158 public static IUndoContext getUndoContext() {
159 // FIXME this has to be more specific. Every widget has to have its own undo context
160 // return IOperationHistory.GLOBAL_UNDO_CONTEXT;
161
162 // Plug-ins that wish their operations to be undoable from workbench views
163 // such as the Navigator or Package Explorer should assign the workbench
164 // undo context to their operations.
165 if (defaultUndoContext == null) {
166 defaultUndoContext = new UndoContext();
167 }
168 return defaultUndoContext;
169 }
170
171 /**
172 * Whether a taxonNode has unsaved changes.
173 *
174 * @param taxonNode a {@link eu.etaxonomy.cdm.model.taxon.TaxonNode} object.
175 * @return a boolean.
176 */
177 public static boolean isDirty(TaxonNode taxonNode){
178
179 for (IEditorReference reference : getActivePage().getEditorReferences()) {
180
181 try {
182 if (reference.getEditorInput() instanceof TaxonEditorInput) {
183 TaxonEditorInput editorInput = (TaxonEditorInput) reference.getEditorInput();
184 if(editorInput.getTaxonNode().equals(taxonNode) && reference.isDirty()){
185 return true;
186 }
187 }
188 } catch (PartInitException e) {
189 NavigationUtil.error(NavigationUtil.class, e.getMessage(), e);
190 throw new RuntimeException(e);
191 }
192
193 }
194 return false;
195 }
196
197 /**
198 * <p>selectInNavigator</p>
199 *
200 * @param element a {@link java.lang.Object} object.
201 * @param parentElement a {@link java.lang.Object} object.
202 */
203 public static void selectInNavigator(final Object element, final Object parentElement) {
204 Display.getDefault().asyncExec(new Runnable(){
205
206 public void run() {
207 TaxonNavigator navigator = showNavigator();
208
209 if (navigator != null) {
210 CommonViewer viewer = navigator.getCommonViewer();
211 if (viewer != null) {
212 if (parentElement != null) {
213 viewer.setExpandedState(parentElement, true);
214 }
215 viewer.setSelection(new StructuredSelection((TaxonNode) element));
216 }
217 }
218 }
219
220 });
221 }
222
223 /**
224 * <p>openSearch</p>
225 *
226 * @param selection a {@link java.lang.Object} object.
227 */
228 public static void openSearch(Object selection) {
229 if(selection instanceof Taxon){
230 Taxon taxon = (Taxon) selection;
231
232 handleOpeningOfMultipleTaxonNodes(taxon.getTaxonNodes());
233
234 }else if(selection instanceof Synonym){
235 Synonym synonym = (Synonym) selection;
236
237 handleOpeningOfMultipleTaxa(synonym.getAcceptedTaxa());
238
239 }else{
240 warningDialog("Not implemented yet", NavigationUtil.class, "You chose to open a name that has no connection to a taxon. The Editor does not support editing of such a content type at the moment.");
241 }
242
243 }
244
245 private static void handleOpeningOfMultipleTaxa(Set<Taxon> acceptedTaxa) {
246 if(acceptedTaxa.size() == 1){
247 openEditor(acceptedTaxa.iterator().next());
248 }else if(acceptedTaxa.size() > 1){
249 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
250 warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
251 " This case is not handled yet by the software.");
252 }else if(acceptedTaxa.size() == 0){
253 // this is an undesired state
254 warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
255 }
256 }
257
258 /**
259 * @param taxonNodes
260 */
261 private static void handleOpeningOfMultipleTaxonNodes(
262 Set<TaxonNode> taxonNodes) {
263
264 if(taxonNodes.size() == 1){
265 openEditor(taxonNodes.iterator().next());
266 }else if(taxonNodes.size() > 1){
267 // FIXME implement a dialog that shows all possible taxa and let the user choose which he wants to open.
268 warningDialog("Not implemented yet", NavigationUtil.class, "The accepted taxon is in multiple taxonomic trees. We currently do not know which one you want to open." +
269 " This case is not handled yet by the software.");
270 }else if(taxonNodes.size() == 0){
271 // this is an undesired state
272 warningDialog("Incorrect state", NavigationUtil.class, "The accepted taxon is not in a taxonomic view. This should not have happened.");
273 }
274 }
275
276 /**
277 * <p>showNavigator</p>
278 *
279 * @return the TaxonNavigator instance if present
280 */
281 public static TaxonNavigator showNavigator() {
282 return (TaxonNavigator) showView(TaxonNavigator.ID);
283 }
284
285 /**
286 * <p>getNavigator</p>
287 *
288 * @param restore a boolean.
289 * @return a {@link eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigator} object.
290 */
291 public static TaxonNavigator getNavigator(boolean restore) {
292 return (TaxonNavigator) getView(TaxonNavigator.ID, restore);
293 }
294
295 /**
296 * <p>getOpenEditors</p>
297 *
298 * @return a {@link java.util.Set} object.
299 */
300 public static Set<IEditorPart> getOpenEditors() {
301 return EditorUtil.getOpenEditors();
302 }
303
304 /**
305 * <p>getPluginId</p>
306 *
307 * @return a {@link java.lang.String} object.
308 */
309 protected static String getPluginId(){
310 return TaxeditorNavigationPlugin.PLUGIN_ID;
311 }
312
313 }