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
.controller
;
12 import java
.util
.HashMap
;
13 import java
.util
.HashSet
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.commands
.operations
.IOperationHistory
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
20 import org
.eclipse
.ui
.IEditorInput
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.IEditorReference
;
23 import org
.eclipse
.ui
.IWorkbenchPage
;
24 import org
.eclipse
.ui
.PartInitException
;
26 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
30 import eu
.etaxonomy
.taxeditor
.datasource
.CdmTransactionController
;
31 import eu
.etaxonomy
.taxeditor
.editor
.AbstractTaxonEditor
;
32 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
33 import eu
.etaxonomy
.taxeditor
.editor
.TemporaryTaxon
;
34 import eu
.etaxonomy
.taxeditor
.editor
.description
.TaxonDescriptionEditor
;
35 import eu
.etaxonomy
.taxeditor
.editor
.images
.TaxonImagesEditor
;
36 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
37 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
38 import eu
.etaxonomy
.taxeditor
.model
.CdmUtil
;
39 import eu
.etaxonomy
.taxeditor
.model
.NameEditorInput
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.RecentNamesView
;
41 import eu
.etaxonomy
.taxeditor
.navigation
.TaxonomicTreeViewer
;
48 public class EditorController
{
49 private static final Logger logger
= Logger
.getLogger(EditorController
.class);
51 private static Map
<Taxon
, TaxonNameEditor
> taxonNameEditors
;
52 private static Map
<Taxon
, TaxonDescriptionEditor
> taxonDescriptionEditors
;
54 private static boolean isSaving
= false;
56 private static HashMap
<Taxon
, TaxonImagesEditor
> taxonImagesEditors
;
58 private static IWorkbenchPage
getActivePage(){
59 return GlobalController
.getActivePage();
65 * @throws PartInitException
67 private static IEditorPart
getEditorByInput(IEditorInput input
)
68 throws PartInitException
{
69 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
70 if (reference
.getEditorInput().equals(input
)) {
71 IEditorPart editor
= reference
.getEditor(false);
78 public static boolean closeAll() {
79 for (IEditorPart editor
: getOpenEditors()) {
80 if (!getActivePage().closeEditor(editor
, true)) {
87 public static IEditorPart
getEditorByTaxon(Taxon taxon
)
88 throws PartInitException
{
89 IEditorInput input
= new NameEditorInput(taxon
);
90 return getEditorByInput(input
);
97 * @throws PartInitException
99 private static IEditorPart
open(IEditorInput input
, String editorId
)
100 throws PartInitException
{
101 return getActivePage().openEditor(input
, editorId
);
105 * Open a taxon editor for the given taxon. If the taxon has been saved before, it is
106 * considered initialized, and opening its editor will give it an entry in the list
107 * of recent names and open its node in the tree.
110 * @param isInitialized
113 public static IEditorPart
open(Taxon taxon
, boolean isInitialized
){
117 TaxonomicTreeViewer treeViewer
= TreeController
.getTreeViewer();
118 if (treeViewer
!= null) {
119 treeViewer
.revealTaxon(taxon
);
122 RecentNamesView
.addRecentName(taxon
);
125 IEditorInput input
= new NameEditorInput(taxon
);
128 return open(input
, MultiPageTaxonEditor
.ID
);
129 } catch (PartInitException e
) {
130 logger
.error("Error opening editor.", e
);
135 public static IEditorPart
open(Taxon taxon
){
137 // TemporaryTaxon taxonCopy = TemporaryTaxon.newInstance(taxon);
138 Taxon taxonCopy
= getEditorTaxon(taxon
);
140 return open(getEditorTaxon(taxon
), true);
147 private static Taxon
getEditorTaxon(Taxon taxon
) {
148 return (Taxon
) CdmUtil
.getTaxonService().getTaxonByUuid(taxon
.getUuid());
152 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
153 * the list of recent names nor in the taxonomic tree when opened.
158 public static IEditorPart
openUnitializedChildTaxon(Taxon parentTaxon
) {
160 TaxonNameBase name
= PreferencesController
.getInstanceOfPreferredNameClass();
161 Taxon taxon
= Taxon
.NewInstance(name
, parentTaxon
.getSec());
162 parentTaxon
.addTaxonomicChild(taxon
, null, null);
164 return open(taxon
, false);
168 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
169 * the list of recent names nor in the taxonomic tree when opened.
174 public static IEditorPart
openUnitializedRootTaxon() {
176 TaxonNameBase name
= PreferencesController
.getInstanceOfPreferredNameClass();
177 Taxon taxon
= Taxon
.NewInstance(name
,
178 CdmSessionDataRepository
.getDefault().getDefaultSec());
180 return open(taxon
, false);
183 public static IEditorPart
openTaxonEditor(TaxonBase taxonBase
) {
186 if (taxonBase
instanceof Synonym
) {
187 // TODO: in case of pro parte synonym or any other where we might have multiple
188 // accepted taxa we have to provide a mechanism that can deal with that
189 // TODO set focus to the synonym
190 taxon
= (Taxon
) ((Synonym
) taxonBase
).getAcceptedTaxa().toArray()[0];
192 taxon
= (Taxon
) taxonBase
;
195 // Open a taxon editor
196 return EditorController
.open(taxon
);
200 * Redraws an open editor if it exists for the given taxon
205 public static boolean redraw(Taxon taxon
){
206 // return redraw(EditorController.getNameEditor(taxon)) &&
207 // redraw(EditorController.getDescriptionEditor(taxon));
209 boolean returnCode
= redraw(EditorController
.getNameEditor(taxon
)) &&
210 redraw(EditorController
.getDescriptionEditor(taxon
)) &&
211 redraw(EditorController
.getImageEditor(taxon
));
212 getActiveEditor().setFocus();
216 private static boolean redraw(AbstractTaxonEditor editor
) {
217 if(editor
== null || editor
.redraw()){
219 // if (getActiveEditor().setFocus())
222 // Mark editor as changed and unsaved
233 public static void close(Taxon taxon
, boolean save
) {
236 editor
= getEditorByTaxon(taxon
);
238 } catch (PartInitException e
) {
239 logger
.error("Error closing taxon editor", e
);
247 * @throws PartInitException
249 private static void close(IEditorPart editor
, boolean save
)
250 throws PartInitException
{
251 if (editor
!= null) {
252 getActivePage().closeEditor(editor
, save
);
256 public static boolean save(Taxon taxon
, boolean confirm
) {
257 IEditorPart editor
= null;
259 editor
= getEditorByTaxon(taxon
);
260 } catch (PartInitException e
) {
261 logger
.error("Error saving taxon.", e
);
263 if (editor
!= null) {
264 return GlobalController
.getActivePage().saveEditor(editor
, confirm
);
269 public static void saveAll(){
272 // Get all open windows
273 for (IEditorPart taxonEditor
: getOpenEditors()) {
275 // Save the dirty ones
276 if (taxonEditor
.isDirty()) {
279 IEditorInput input
= taxonEditor
.getEditorInput();
280 if (input
.getAdapter(Taxon
.class) != null) {
281 Taxon taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
282 CdmSessionDataRepository
.getDefault().saveTaxon(taxon
);
283 if (taxonEditor
instanceof MultiPageTaxonEditor
) {
284 ((MultiPageTaxonEditor
) taxonEditor
).setDirtyExtern(false);
290 // Commit the transaction
291 CdmTransactionController
.commitTransaction();
293 // Force library objects to be associated with new transaction
294 CdmSessionDataRepository
.getDefault().clearNonTaxonData();
296 // Start a new transaction
297 CdmTransactionController
.startTransaction();
299 // Put all open taxa in the new transaction
300 CdmTransactionController
.addSessionTaxaToTransaction();
303 // TODO: delete undoHistory
306 public static void setSaving(boolean isSaving
) {
307 EditorController
.isSaving
= isSaving
;
310 public static boolean isSaving() {
314 public static void addNameEditor(
315 Taxon taxon
, TaxonNameEditor taxonNameEditor
) {
316 if (taxonNameEditors
== null) {
317 taxonNameEditors
= new HashMap
<Taxon
, TaxonNameEditor
>();
319 taxonNameEditors
.put(taxon
, taxonNameEditor
);
322 public static TaxonNameEditor
getNameEditor(Taxon taxon
) {
323 if (taxonNameEditors
== null) {
326 return taxonNameEditors
.get(taxon
);
329 public static void addDescriptionEditor(
330 Taxon taxon
, TaxonDescriptionEditor taxonDescriptionEditor
) {
331 if (taxonDescriptionEditors
== null) {
332 taxonDescriptionEditors
= new HashMap
<Taxon
, TaxonDescriptionEditor
>();
334 taxonDescriptionEditors
.put(taxon
, taxonDescriptionEditor
);
337 public static TaxonDescriptionEditor
getDescriptionEditor(Taxon taxon
) {
338 if (taxonDescriptionEditors
== null) {
341 return taxonDescriptionEditors
.get(taxon
);
345 * Returns a set of all currently open
346 * <code>MultiPageTaxonEditor</code>s.
350 public static Set
<IEditorPart
> getOpenEditors() {
352 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
354 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
355 IEditorPart editor
= reference
.getEditor(false);
356 if (editor
instanceof MultiPageTaxonEditor
) {
357 taxonEditors
.add(editor
);
364 * Returns the currently active taxon editor
366 * @return the taxon editor that has focus
368 public static MultiPageTaxonEditor
getActiveEditor(){
369 if(getActivePage().getActiveEditor() instanceof MultiPageTaxonEditor
){
370 return (MultiPageTaxonEditor
) getActivePage().getActiveEditor();
375 public static IUndoContext
getUndoContext(Taxon taxon
) {
376 // TODO make this taxon name editor specific
377 // return getTaxonNameEditor(taxon).getUndoContext();
378 return getUndoContext();
381 public static IUndoContext
getUndoContext() {
382 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;
389 public static TaxonImagesEditor
getImageEditor(Taxon taxon
) {
390 if (taxonImagesEditors
== null) {
393 return taxonImagesEditors
.get(taxon
);
398 * @param taxonImagesEditor
400 public static void addImagesEditor(Taxon taxon
,
401 TaxonImagesEditor taxonImagesEditor
) {
402 if (taxonImagesEditors
== null) {
403 taxonImagesEditors
= new HashMap
<Taxon
, TaxonImagesEditor
>();
405 taxonImagesEditors
.put(taxon
, taxonImagesEditor
);