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