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
.MultiPageTaxonEditor
;
32 import eu
.etaxonomy
.taxeditor
.editor
.name
.CdmParserController
;
33 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
34 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
35 import eu
.etaxonomy
.taxeditor
.model
.NameEditorInput
;
36 import eu
.etaxonomy
.taxeditor
.navigation
.RecentNamesView
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.TaxonomicTreeViewer
;
44 public class EditorController
{
45 private static final Logger logger
= Logger
.getLogger(EditorController
.class);
48 private static Map
<Taxon
, TaxonNameEditor
> taxonNameEditors
;
51 private static boolean isSaving
= false;
53 private static IWorkbenchPage
getActivePage(){
54 return GlobalController
.getActivePage();
60 * @throws PartInitException
62 private static IEditorPart
getEditorByInput(IEditorInput input
)
63 throws PartInitException
{
64 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
65 if (reference
.getEditorInput().equals(input
)) {
66 IEditorPart editor
= reference
.getEditor(false);
73 public static boolean closeAll() {
74 for (IEditorPart editor
: getOpenEditors()) {
75 if (!getActivePage().closeEditor(editor
, true)) {
82 public static IEditorPart
getEditorByTaxon(Taxon taxon
)
83 throws PartInitException
{
84 IEditorInput input
= new NameEditorInput(taxon
);
85 return getEditorByInput(input
);
92 * @throws PartInitException
94 private static IEditorPart
open(IEditorInput input
, String editorId
)
95 throws PartInitException
{
96 return getActivePage().openEditor(input
, editorId
);
100 * Open an empty taxon editor
103 * @throws PartInitException
105 public static IEditorPart
open()
106 throws PartInitException
{
111 * Open a taxon editor for the given taxon. If the taxon has been saved before, it is
112 * considered initialized, and opening its editor will give it an entry in the list
113 * of recent names and open its node in the tree.
116 * @param isInitialized
119 public static IEditorPart
open(Taxon taxon
, boolean isInitialized
){
121 TaxonNameBase name
= CdmParserController
.parseFullReference("", null, null);
122 name
.setFullTitleCache("", false);
123 name
.setTitleCache("", false);
124 taxon
= Taxon
.NewInstance(name
,
125 CdmSessionDataRepository
.getDefault().getDefaultSec());
130 TaxonomicTreeViewer treeViewer
= TreeController
.getTreeViewer();
131 if (treeViewer
!= null) {
132 treeViewer
.revealTaxon(taxon
);
135 RecentNamesView
.addRecentName(taxon
);
138 IEditorInput input
= new NameEditorInput(taxon
);
141 return open(input
, MultiPageTaxonEditor
.ID
);
142 } catch (PartInitException e
) {
143 logger
.error("Error opening editor.", e
);
148 public static IEditorPart
open(Taxon taxon
){
149 return open(taxon
, true);
153 * An unitialized taxon is one that hasn't been saved yet. As such, it should appear in neither
154 * the list of recent names nor in the taxonomic tree when opened.
159 public static IEditorPart
openUnitializedTaxon(Taxon taxon
) {
160 return open(taxon
, false);
163 public static IEditorPart
openTaxonEditor(TaxonBase taxonBase
) {
166 if (taxonBase
instanceof Synonym
) {
167 // TODO: in case of pro parte synonym or any other where we might have multiple
168 // accepted taxa we have to provide a mechanism that can deal with that
169 // TODO set focus to the synonym
170 taxon
= (Taxon
) ((Synonym
) taxonBase
).getAcceptedTaxa().toArray()[0];
172 taxon
= (Taxon
) taxonBase
;
175 // Open a taxon editor
176 return EditorController
.open(taxon
);
180 * Redraws an open editor if it exists for the given taxon
185 public static boolean redraw(Taxon taxon
){
187 TaxonNameEditor editor
= EditorController
.getEditor(taxon
);
189 if(editor
== null || editor
.redraw()){
191 // Mark editor as changed and unsaved
203 public static void close(Taxon taxon
, boolean save
) {
206 editor
= getEditorByTaxon(taxon
);
208 } catch (PartInitException e
) {
209 logger
.error("Error closing taxon editor", e
);
217 * @throws PartInitException
219 private static void close(IEditorPart editor
, boolean save
)
220 throws PartInitException
{
221 if (editor
!= null) {
222 getActivePage().closeEditor(editor
, save
);
226 public static boolean save(Taxon taxon
, boolean confirm
) {
227 IEditorPart editor
= null;
229 editor
= getEditorByTaxon(taxon
);
230 } catch (PartInitException e
) {
231 logger
.error("Error saving taxon.", e
);
233 if (editor
!= null) {
234 return GlobalController
.getActivePage().saveEditor(editor
, confirm
);
239 public static void saveAll(){
242 // Get all open windows
243 for (IEditorPart taxonEditor
: getOpenEditors()) {
245 // Save the dirty ones
246 if (taxonEditor
.isDirty()) {
249 IEditorInput input
= taxonEditor
.getEditorInput();
250 if (input
.getAdapter(Taxon
.class) != null) {
251 Taxon taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
252 CdmSessionDataRepository
.getDefault().saveTaxon(taxon
);
253 if (taxonEditor
instanceof MultiPageTaxonEditor
) {
254 ((MultiPageTaxonEditor
) taxonEditor
).setDirtyExtern(false);
260 // Commit the transaction
261 CdmTransactionController
.commitTransaction();
263 // Force library objects to be associated with new transaction
264 CdmSessionDataRepository
.getDefault().clearNonTaxonData();
266 // Start a new transaction
267 CdmTransactionController
.startTransaction();
269 // Put all open taxa in the new transaction
270 CdmTransactionController
.addSessionTaxaToTransaction();
273 // TODO: delete undoHistory
276 public static void setSaving(boolean isSaving
) {
277 EditorController
.isSaving
= isSaving
;
280 public static boolean isSaving() {
284 public static void addEditor(
285 Taxon taxon
, TaxonNameEditor taxonNameEditor
) {
286 if (taxonNameEditors
== null) {
287 taxonNameEditors
= new HashMap
<Taxon
, TaxonNameEditor
>();
289 taxonNameEditors
.put(taxon
, taxonNameEditor
);
292 public static TaxonNameEditor
getEditor(Taxon taxon
) {
293 if (taxonNameEditors
== null) {
296 return taxonNameEditors
.get(taxon
);
300 * Returns a set of all currently open
301 * <code>MultiPageTaxonEditor</code>s.
305 public static Set
<IEditorPart
> getOpenEditors() {
307 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
309 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
310 IEditorPart editor
= reference
.getEditor(false);
311 if (editor
instanceof MultiPageTaxonEditor
) {
312 taxonEditors
.add(editor
);
319 * Returns the currently active taxon editor
321 * @return the taxon editor that has focus
323 public static MultiPageTaxonEditor
getActiveEditor(){
324 if(getActivePage().getActiveEditor() instanceof MultiPageTaxonEditor
){
325 return (MultiPageTaxonEditor
) getActivePage().getActiveEditor();
330 public static IUndoContext
getUndoContext(Taxon taxon
) {
331 // TODO make this taxon name editor specific
332 // return getTaxonNameEditor(taxon).getUndoContext();
333 return getUndoContext();
336 public static IUndoContext
getUndoContext() {
337 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;