2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.HashSet
;
14 import java
.util
.UUID
;
16 import org
.eclipse
.core
.commands
.ExecutionEvent
;
17 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
22 import org
.eclipse
.jface
.viewers
.TreeNode
;
23 import org
.eclipse
.swt
.widgets
.Shell
;
24 import org
.eclipse
.ui
.IEditorInput
;
25 import org
.eclipse
.ui
.IEditorPart
;
26 import org
.eclipse
.ui
.IEditorReference
;
27 import org
.eclipse
.ui
.IWorkbenchPage
;
28 import org
.eclipse
.ui
.PartInitException
;
29 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
31 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
32 import eu
.etaxonomy
.cdm
.model
.common
.ITreeNode
;
33 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
34 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditor
;
39 import eu
.etaxonomy
.taxeditor
.editor
.group
.authority
.CdmAuthorityEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.editor
.internal
.TaxeditorEditorPlugin
;
41 import eu
.etaxonomy
.taxeditor
.editor
.key
.KeyEditor
;
42 import eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
.PolytomousKeyEditorInput
;
43 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditor
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.checklist
.ChecklistEditorInput
;
45 //import eu.etaxonomy.taxeditor.store.view.dataimport.BioCaseEditorInput;
46 //import eu.etaxonomy.taxeditor.view.dataimport.DataImportEditor;
47 //import eu.etaxonomy.taxeditor.view.dataimport.DataImportEditorInput;
48 //import eu.etaxonomy.taxeditor.view.dataimport.GbifImportEditor;
49 //import eu.etaxonomy.taxeditor.view.dataimport.GbifImportEditorInput;
50 //import eu.etaxonomy.taxeditor.view.dataimport.SpecimenImportEditor;
51 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
52 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateViewEditorInput
;
53 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
54 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
55 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
58 * Utility for the editor package
64 public class EditorUtil
extends AbstractUtility
{
66 private static boolean isSaving
= false;
69 * Opens a new editor window with the given input
75 * @throws PartInitException
77 private static IEditorPart
open(final IEditorInput input
,
78 final String editorId
) throws PartInitException
{
79 IEditorPart editor
= getActivePage().openEditor(input
, editorId
);
81 editor
.getEditorInput() != null &&
82 input
!= editor
.getEditorInput() &&
83 input
instanceof CdmEntitySessionInput
) {
84 ((CdmEntitySessionInput
)input
).dispose();
90 * Opens a new editor window with the given TaxonEditorInput
93 * a {@link eu.etaxonomy.taxeditor.editor.TaxonEditorInput}
95 * @throws org.eclipse.ui.PartInitException
98 public static void open(TaxonEditorInput input
) throws PartInitException
{
99 open(input
, MultiPageTaxonEditor
.ID
);
102 public static void open(PolytomousKeyEditorInput input
)
103 throws PartInitException
{
104 open(input
, KeyEditor
.ID
);
107 public static void open(CdmAuthorityEditorInput input
)
108 throws PartInitException
{
109 open(input
, CdmAuthorityEditor
.ID
);
113 * Opens a new DerivateView for the given input
114 * @param input a {@link DerivateViewEditorInput} representing the selected derivate
115 * @throws PartInitException
117 public static void open(DerivateViewEditorInput input
)
118 throws PartInitException
{
119 open(input
, DerivateView
.ID
);
124 * Opens a new ChecklistView for the given input
125 * @param input a {@link ChecklistEditorInput} representing the selected checklist
126 * @throws PartInitException
128 public static void open(ChecklistEditorInput input
)
129 throws PartInitException
{
130 open(input
, ChecklistEditor
.ID
);
134 * Opens a new {@link DataImportEditor} for the given input
135 * @param input a {@link DataImportEditorInput}
136 * @throws PartInitException
138 // public static void open(DataImportEditorInput<?> input)
139 // throws PartInitException {
140 // if(input instanceof BioCaseEditorInput){
141 // open(input, SpecimenImportEditor.ID);
143 // else if(input instanceof GbifImportEditorInput){
144 // open(input, GbifImportEditor.ID);
149 * Taxon Editors may be opened by supplying a taxon node uuid. Session gets
150 * initialised here and is passed to the editor
152 * @param taxonNodeUuid
153 * a {@link java.util.UUID} object.
154 * @throws java.lang.Exception
157 public static void openTaxonNode(UUID taxonNodeUuid
) throws Exception
{
158 TaxonEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
167 * @param taxonBaseUuid
168 * a {@link java.util.UUID} object.
169 * @throws org.eclipse.ui.PartInitException
172 public static void openTaxonBase(UUID taxonBaseUuid
)
173 throws PartInitException
{
174 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).find(taxonBaseUuid
);
175 if (taxonBase
!= null && taxonBase
.isOrphaned()) {
176 MessagingUtils
.warningDialog("Orphaned Taxon", TaxonEditorInput
.class, "This is an orphaned taxon i.e. a taxon that is not connected to a classification and not having any taxonomic relationships. Editing of orphaned taxon is currently not supported.");
179 TaxonEditorInput input
= TaxonEditorInput
180 .NewInstanceFromTaxonBase(taxonBaseUuid
);
188 * findEditorByTaxonNodeUuid
191 * @param taxonNodeUuid
192 * a {@link java.util.UUID} object.
193 * @return a {@link org.eclipse.ui.IEditorPart} object.
194 * @throws java.lang.Exception
197 public static IEditorPart
findEditorByTaxonNodeUuid(UUID taxonNodeUuid
)
199 IEditorInput input
= TaxonEditorInput
.NewInstance(taxonNodeUuid
);
200 return getActivePage().findEditor(input
);
204 * An uninitialized taxon is one that hasn't been saved yet. As such, it
205 * should appear in neither the list of recent names nor in the taxonomic
208 * @throws org.eclipse.ui.PartInitException
210 * @param parentNodeUuid
211 * a {@link java.util.UUID} object.
213 public static void openEmpty(UUID parentNodeUuid
) throws PartInitException
{
214 TaxonEditorInput input
= TaxonEditorInput
215 .NewEmptyInstance(parentNodeUuid
);
216 open(input
, MultiPageTaxonEditor
.ID
);
218 getActiveMultiPageTaxonEditor().changed(null);
230 public static void setSaving(boolean isSaving
) {
231 EditorUtil
.isSaving
= isSaving
;
241 public static boolean isSaving() {
246 * Returns a set of all currently open <code>MultiPageTaxonEditor</code>s.
248 * @return a {@link java.util.Set} object.
250 public static Set
<IEditorPart
> getOpenEditors() {
251 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
253 if (getActivePage() != null) {
254 for (IEditorReference reference
: getActivePage()
255 .getEditorReferences()) {
256 IEditorPart editor
= reference
.getEditor(false);
257 if (editor
instanceof MultiPageTaxonEditor
) {
258 taxonEditors
.add(editor
);
267 * Returns the currently active taxon editor
269 * @return the taxon editor that has focus
271 public static MultiPageTaxonEditor
getActiveMultiPageTaxonEditor() {
272 IEditorPart editorPart
= getActiveEditor();
273 if (editorPart
!= null && editorPart
instanceof MultiPageTaxonEditor
) {
274 MultiPageTaxonEditor editor
= (MultiPageTaxonEditor
) editorPart
;
275 editor
.getConversationHolder().bind();
283 * getActiveEditorPage
287 * a {@link eu.etaxonomy.taxeditor.editor.Page} object.
288 * @return a {@link org.eclipse.ui.IEditorPart} object.
290 public static IEditorPart
getActiveEditorPage(Page page
) {
291 MultiPageTaxonEditor editor
= getActiveMultiPageTaxonEditor();
293 return editor
!= null ? editor
.getPage(page
) : null;
297 * Returns the selection of the currently active taxon editor
299 * @return a {@link org.eclipse.jface.viewers.ISelection} object.
301 public static ISelection
getCurrentSelection() {
302 if (getActiveMultiPageTaxonEditor() == null) {
305 return getActiveMultiPageTaxonEditor().getSite()
306 .getSelectionProvider().getSelection();
316 * a {@link eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor}
318 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
321 public static IUndoContext
getUndoContext(MultiPageTaxonEditor editor
) {
322 return editor
.getUndoContext();
330 * @return a {@link org.eclipse.core.commands.operations.IUndoContext}
333 public static IUndoContext
getUndoContext() {
334 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
343 * a {@link org.eclipse.ui.IEditorPart} object.
345 * a {@link org.eclipse.swt.widgets.Shell} object.
348 public static boolean forceUserSave(IEditorPart editor
, Shell shell
) {
349 if (editor
.isDirty()) {
351 boolean doSave
= MessageDialog
352 .openConfirm(shell
, "Confirm save",
353 "Warning - this operation will save the editor. This will also save all other unsaved changes " +
354 "in your working editor to the database. Please 'Cancel' if you are not ready to do this.");
360 editor
.doSave(AbstractUtility
.getMonitor());
371 * a {@link org.eclipse.core.commands.ExecutionEvent} object.
372 * @return a {@link org.eclipse.jface.viewers.IStructuredSelection} object.
374 public static IStructuredSelection
getSelection(ExecutionEvent event
) {
375 IEditorPart activeEditor
= HandlerUtil
.getActiveEditor(event
);
377 return (IStructuredSelection
) activeEditor
.getSite()
378 .getSelectionProvider().getSelection();
386 * @return a {@link java.lang.String} object.
388 public static String
getPluginId() {
389 return TaxeditorEditorPlugin
.PLUGIN_ID
;
392 public static void openPolytomousKey(UUID polytomousKeyUuid
)
394 PolytomousKeyEditorInput input
= PolytomousKeyEditorInput
395 .NewInstance(polytomousKeyUuid
);
399 // public static void openPolytomousKeyEditor(UUID polytomousKeyUuid, String name)
400 // throws Exception {
401 // PolytomousKeyEditorInput input = new PolytomousKeyEditorInput(polytomousKeyUuid, name);
405 public static void openCdmAuthorities(UUID groupUuid
)
407 CdmAuthorityEditorInput input
= CdmAuthorityEditorInput
.NewInstance(groupUuid
);
412 * Iterates recursively over all originals having the given specimen as a derivate.
413 * The first {@link DerivedUnit} with no more originals or the first {@link FieldUnit} is returned
414 * @param specimen the start element for which the originals are iterated recursively
415 * @return either a FieldUnit or a the topmost DerivedUnit (which can be itself)
417 public static SpecimenOrObservationBase
<?
> getTopMostDerivate(SpecimenOrObservationBase
<?
> specimen
){
418 if(specimen
.isInstanceOf(FieldUnit
.class)){
421 else if(specimen
instanceof DerivedUnit
422 && ((DerivedUnit
) specimen
).getOriginals()!=null
423 && !((DerivedUnit
) specimen
).getOriginals().isEmpty()){
424 for(SpecimenOrObservationBase
<?
> original
:((DerivedUnit
) specimen
).getOriginals()){
425 return getTopMostDerivate(original
);
427 //needed to add this for compilation although this is unreachable
436 * If the current selection is a single {@link TreeNode} it will be returned.
437 * @param selection the selection to check
438 * @return the selected TreeNode or <code>null</code> if no TreeNode selected
440 public static TreeNode
getTreeNodeOfSelection(ISelection selection
){
441 if(selection
instanceof IStructuredSelection
442 && ((IStructuredSelection
) selection
).size()==1
443 && ((IStructuredSelection
) selection
).getFirstElement() instanceof TreeNode
){
444 return (TreeNode
) ((IStructuredSelection
) selection
).getFirstElement();
451 * Opens a taxon editor for the given object if the given object is a valid input.
452 * @param object the object for which the editor will be opened
453 * @throws PartInitException
455 public static void openTaxonEditor(Object object
) throws PartInitException
{
456 if(object
instanceof TaxonBase
<?
>){
457 openTaxonBase(((TaxonBase
<?
>) object
).getUuid());
461 public static boolean closeObsoleteEditor(TaxonNode taxonNode
, IWorkbenchPage activePage
){
462 boolean result
= true;
463 for (IEditorReference ref
: activePage
.getEditorReferences()) {
465 String treeIndex
= ((ITreeNode
)taxonNode
).treeIndex();
468 IEditorInput input
= ref
.getEditorInput();
469 if (input
instanceof TaxonEditorInput
) {
470 TaxonNode node
= ((TaxonEditorInput
) input
).getTaxonNode();
471 //if node is a child of taxonNode then close the editor
472 if( ((ITreeNode
) node
).treeIndex().startsWith(treeIndex
)){
473 //if (taxonNode.equals(node)) {
474 result
&= activePage
.closeEditor(ref
.getEditor(false), true);
478 } catch (PartInitException e
) {