3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
;
13 import java
.util
.HashSet
;
15 import java
.util
.UUID
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.ui
.IEditorInput
;
19 import org
.eclipse
.ui
.IEditorPart
;
20 import org
.eclipse
.ui
.IMemento
;
21 import org
.eclipse
.ui
.IPersistable
;
22 import org
.eclipse
.ui
.PartInitException
;
24 import eu
.etaxonomy
.taxeditor
.editor
.view
.concept
.ConceptViewPart
;
25 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveViewPart
;
26 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.MediaViewPart
;
27 import eu
.etaxonomy
.taxeditor
.model
.ContextListenerAdapter
;
28 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmEditorViewPart
;
31 * <p>EditorStateManager class.</p>
34 * @created Sep 30, 2010
37 public class EditorStateManager
extends ContextListenerAdapter
{
39 private static final String EDITORS
= "editors";
41 private static final String EDITOR
= "editor";
43 private TaxonEditorInputFactory inputFactory
;
46 * <p>Constructor for EditorStateManager.</p>
48 public EditorStateManager() {
49 inputFactory
= new TaxonEditorInputFactory();
53 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
57 public void contextAboutToStop(IMemento memento
, IProgressMonitor monitor
) {
58 saveEditorState(memento
);
62 * @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
66 public void contextStop(IMemento memento
, IProgressMonitor monitor
) {
67 EditorUtil
.closeAll();
74 private void resetConstantViews() {
75 AbstractCdmEditorViewPart descriptiveView
= (AbstractCdmEditorViewPart
) EditorUtil
.getView(DescriptiveViewPart
.ID
, false);
76 AbstractCdmEditorViewPart mediaView
= (AbstractCdmEditorViewPart
) EditorUtil
.getView(MediaViewPart
.ID
, false);
77 AbstractCdmEditorViewPart conceptView
= (AbstractCdmEditorViewPart
) EditorUtil
.getView(ConceptViewPart
.ID
, false);
79 if(descriptiveView
!= null){
80 descriptiveView
.showEmptyPage();
82 if(mediaView
!= null){
83 mediaView
.showEmptyPage();
85 if(conceptView
!= null){
86 conceptView
.showEmptyPage();
91 * @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
95 public void contextStart(IMemento memento
, IProgressMonitor monitor
) {
96 restoreEditors(memento
, monitor
);
100 * @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
103 public void contextRefresh(IProgressMonitor monitor
) {
104 refreshEditors(monitor
);
108 * @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
112 public void workbenchShutdown(IMemento memento
, IProgressMonitor monitor
) {
113 saveEditorState(memento
);
117 * Reads taxon node UUIDs from the given memento and tries to open an editor
118 * for each of the found UUIDs.
122 private void restoreEditors(final IMemento memento
, IProgressMonitor monitor
){
127 IProgressMonitor subProgressMonitor
= EditorUtil
.getSubProgressMonitor(monitor
, 1);
129 IMemento editorMementos
= memento
.getChild(EDITORS
);
130 IMemento
[] editorInputs
= editorMementos
.getChildren(EDITOR
);
132 subProgressMonitor
.beginTask("Restoring Editors", 1 + editorInputs
.length
* 2);
133 subProgressMonitor
.subTask("Restoring Editors");
134 subProgressMonitor
.worked(1);
136 for(IMemento editorInput
: editorInputs
){
137 TaxonEditorInput input
= (TaxonEditorInput
) inputFactory
.createElement(editorInput
);
138 subProgressMonitor
.worked(1);
139 if(!subProgressMonitor
.isCanceled() && input
!= null){
141 EditorUtil
.open(input
);
142 } catch (PartInitException e
) {
143 EditorUtil
.error(getClass(), "Error opening an editor window", e
);
146 subProgressMonitor
.worked(1);
148 subProgressMonitor
.done();
154 private void refreshEditors(IProgressMonitor monitor
) {
155 IProgressMonitor subProgressMonitor
= EditorUtil
.getSubProgressMonitor(monitor
, 1);
157 Set
<IEditorPart
> openEditors
= EditorUtil
.getOpenEditors();
160 subProgressMonitor
.beginTask("Refreshing Editors", openEditors
.size() * 2);
162 Set
<UUID
> uuids
= new HashSet
<UUID
>();
164 for(IEditorPart editor
: openEditors
){
165 if(editor
instanceof MultiPageTaxonEditor
){
167 uuids
.add(((TaxonEditorInput
) editor
.getEditorInput()).getTaxonNode().getUuid());
169 // ((MultiPageTaxonEditor) editor).close(false);
170 EditorUtil
.close((MultiPageTaxonEditor
)editor
);
171 subProgressMonitor
.worked(1);
172 } catch (Exception e
) {
173 // TODO Auto-generated catch block
179 for(UUID uuid
: uuids
){
181 EditorUtil
.openTaxonNode(uuid
);
182 } catch (Exception e
) {
183 // TODO Auto-generated catch block
186 subProgressMonitor
.worked(1);
189 subProgressMonitor
.done();
197 private void saveEditorState(IMemento memento
){
202 Set
<IEditorPart
> editors
= EditorUtil
.getOpenEditors();
204 IMemento editorMementos
= memento
.createChild(EDITORS
);
206 for(IEditorPart editor
: editors
){
207 IEditorInput input
= editor
.getEditorInput();
208 if(input
instanceof IPersistable
){
209 IMemento editorMemento
= editorMementos
.createChild(EDITOR
);
210 ((IPersistable
) input
).saveState(editorMemento
);