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
.Taxon
;
28 import eu
.etaxonomy
.taxeditor
.datasource
.CdmTransactionController
;
29 import eu
.etaxonomy
.taxeditor
.editor
.MultiPageTaxonEditor
;
30 import eu
.etaxonomy
.taxeditor
.editor
.name
.CdmParserController
;
31 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
32 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
33 import eu
.etaxonomy
.taxeditor
.model
.NameEditorInput
;
34 import eu
.etaxonomy
.taxeditor
.navigation
.RecentNamesView
;
35 import eu
.etaxonomy
.taxeditor
.navigation
.TaxonomicTreeViewer
;
42 public class EditorController
{
43 private static final Logger logger
= Logger
.getLogger(EditorController
.class);
46 private static Map
<Taxon
, TaxonNameEditor
> taxonNameEditors
;
49 private static boolean isSaving
= false;
51 private static IWorkbenchPage
getActivePage(){
52 return GlobalController
.getActivePage();
58 * @throws PartInitException
60 private static IEditorPart
getEditorByInput(IEditorInput input
)
61 throws PartInitException
{
62 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
63 if (reference
.getEditorInput().equals(input
)) {
64 IEditorPart editor
= reference
.getEditor(false);
71 public static boolean closeAll() {
72 for (IEditorPart editor
: getOpenEditors()) {
73 if (!getActivePage().closeEditor(editor
, true)) {
80 public static IEditorPart
getEditorByTaxon(Taxon taxon
)
81 throws PartInitException
{
82 IEditorInput input
= new NameEditorInput(taxon
);
83 return getEditorByInput(input
);
90 * @throws PartInitException
92 private static IEditorPart
open(IEditorInput input
, String editorId
)
93 throws PartInitException
{
94 return getActivePage().openEditor(input
, editorId
);
98 * Open an emtpy taxon editor
101 * @throws PartInitException
103 public static IEditorPart
open()
104 throws PartInitException
{
109 * Open a taxon editor for the given taxon
113 * @throws PartInitException
115 public static IEditorPart
open(Taxon taxon
){
117 TaxonNameBase name
= CdmParserController
.parseFullReference("", null, null);
118 name
.setFullTitleCache("", false);
119 name
.setTitleCache("", false);
120 taxon
= Taxon
.NewInstance(name
,
121 CdmSessionDataRepository
.getDefault().getDefaultSec());
123 // If this taxon is not visible in the tree, open node
124 TaxonomicTreeViewer treeViewer
= TreeController
.getTreeViewer();
125 if (treeViewer
!= null) {
126 treeViewer
.revealTaxon(taxon
);
129 // Add to recent names list
130 RecentNamesView
.addRecentName(taxon
);
133 IEditorInput input
= new NameEditorInput(taxon
);
136 return open(input
, MultiPageTaxonEditor
.ID
);
137 } catch (PartInitException e
) {
138 logger
.error("Error opening editor.", e
);
144 * Redraws an open editor if it exists for the given taxon
149 public static boolean redraw(Taxon taxon
){
151 TaxonNameEditor editor
= EditorController
.getEditor(taxon
);
153 if(editor
== null || editor
.redraw()){
155 // Mark editor as changed and unsaved
167 public static void close(Taxon taxon
, boolean save
) {
170 editor
= getEditorByTaxon(taxon
);
172 } catch (PartInitException e
) {
173 logger
.error("Error closing taxon editor", e
);
181 * @throws PartInitException
183 private static void close(IEditorPart editor
, boolean save
)
184 throws PartInitException
{
185 if (editor
!= null) {
186 getActivePage().closeEditor(editor
, save
);
190 public static void save(Taxon taxon
, boolean confirm
) {
191 IEditorPart editor
= null;
193 editor
= getEditorByTaxon(taxon
);
194 } catch (PartInitException e
) {
195 // TODO Auto-generated catch block
198 if (editor
!= null) {
199 GlobalController
.getActivePage().saveEditor(editor
, confirm
);
203 public static void saveAll(){
206 // Get all open windows
207 for (IEditorPart taxonEditor
: getOpenEditors()) {
209 // Save the dirty ones
210 if (taxonEditor
.isDirty()) {
213 IEditorInput input
= taxonEditor
.getEditorInput();
214 if (input
.getAdapter(Taxon
.class) != null) {
215 Taxon taxon
= (Taxon
) input
.getAdapter(Taxon
.class);
216 CdmSessionDataRepository
.getDefault().saveTaxon(taxon
);
217 if (taxonEditor
instanceof MultiPageTaxonEditor
) {
218 ((MultiPageTaxonEditor
) taxonEditor
).setDirtyExtern(false);
224 // Commit the transaction
225 CdmTransactionController
.commitTransaction();
227 // Force library objects to be associated with new transaction
228 CdmSessionDataRepository
.getDefault().clearNonTaxonData();
230 // Start a new transaction
231 CdmTransactionController
.startTransaction();
233 // Put all open taxa in the new transaction
234 CdmTransactionController
.addSessionTaxaToTransaction();
237 // TODO: delete undoHistory
240 public static void setSaving(boolean isSaving
) {
241 EditorController
.isSaving
= isSaving
;
244 public static boolean isSaving() {
248 public static void addEditor(
249 Taxon taxon
, TaxonNameEditor taxonNameEditor
) {
250 if (taxonNameEditors
== null) {
251 taxonNameEditors
= new HashMap
<Taxon
, TaxonNameEditor
>();
253 taxonNameEditors
.put(taxon
, taxonNameEditor
);
256 public static TaxonNameEditor
getEditor(Taxon taxon
) {
257 if (taxonNameEditors
== null) {
260 return taxonNameEditors
.get(taxon
);
264 * Returns a set of all currently open
265 * <code>MultiPageTaxonEditor</code>s.
269 public static Set
<IEditorPart
> getOpenEditors() {
271 Set
<IEditorPart
> taxonEditors
= new HashSet
<IEditorPart
>();
273 for (IEditorReference reference
: getActivePage().getEditorReferences()) {
274 IEditorPart editor
= reference
.getEditor(false);
275 if (editor
instanceof MultiPageTaxonEditor
) {
276 taxonEditors
.add(editor
);
282 public static IUndoContext
getUndoContext(Taxon taxon
) {
283 // TODO make this taxon name editor specific
284 // return getTaxonNameEditor(taxon).getUndoContext();
285 return IOperationHistory
.GLOBAL_UNDO_CONTEXT
;