1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
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.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.taxeditor.editor;
|
12
|
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.eclipse.core.runtime.IProgressMonitor;
|
17
|
import org.eclipse.ui.IEditorInput;
|
18
|
import org.eclipse.ui.IEditorPart;
|
19
|
import org.eclipse.ui.IMemento;
|
20
|
import org.eclipse.ui.IPersistable;
|
21
|
import org.eclipse.ui.PartInitException;
|
22
|
|
23
|
import eu.etaxonomy.taxeditor.editor.view.AbstractCdmEditorViewPart;
|
24
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
|
25
|
import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
|
26
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
27
|
|
28
|
/**
|
29
|
* <p>EditorStateManager class.</p>
|
30
|
*
|
31
|
* @author n.hoffmann
|
32
|
* @created Sep 30, 2010
|
33
|
* @version 1.0
|
34
|
*/
|
35
|
public class EditorStateManager extends ContextListenerAdapter {
|
36
|
private static final Logger logger = Logger
|
37
|
.getLogger(EditorStateManager.class);
|
38
|
|
39
|
private static final String EDITORS = "editors";
|
40
|
|
41
|
private static final String EDITOR = "editor";
|
42
|
|
43
|
private TaxonEditorInputFactory inputFactory;
|
44
|
|
45
|
/**
|
46
|
* <p>Constructor for EditorStateManager.</p>
|
47
|
*/
|
48
|
public EditorStateManager() {
|
49
|
inputFactory = new TaxonEditorInputFactory();
|
50
|
}
|
51
|
|
52
|
/* (non-Javadoc)
|
53
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
54
|
*/
|
55
|
/** {@inheritDoc} */
|
56
|
@Override
|
57
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
58
|
saveEditorState(memento);
|
59
|
}
|
60
|
|
61
|
/* (non-Javadoc)
|
62
|
* @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
63
|
*/
|
64
|
/** {@inheritDoc} */
|
65
|
@Override
|
66
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
67
|
resetConstantViews();
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
*
|
72
|
*/
|
73
|
private void resetConstantViews() {
|
74
|
AbstractCdmEditorViewPart descriptiveView = (AbstractCdmEditorViewPart) EditorUtil.getView(DescriptiveViewPart.ID, false);
|
75
|
AbstractCdmEditorViewPart mediaView = (AbstractCdmEditorViewPart) EditorUtil.getView(MediaViewPart.ID, false);
|
76
|
|
77
|
if(descriptiveView != null){
|
78
|
descriptiveView.showEmptyPage();
|
79
|
}
|
80
|
|
81
|
if(mediaView != null){
|
82
|
mediaView.showEmptyPage();
|
83
|
}
|
84
|
}
|
85
|
|
86
|
/* (non-Javadoc)
|
87
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
88
|
*/
|
89
|
/** {@inheritDoc} */
|
90
|
@Override
|
91
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
92
|
restoreEditors(memento, monitor);
|
93
|
}
|
94
|
|
95
|
/* (non-Javadoc)
|
96
|
* @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
97
|
*/
|
98
|
/** {@inheritDoc} */
|
99
|
@Override
|
100
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
101
|
saveEditorState(memento);
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Reads taxon node UUIDs from the given memento and tries to open an editor
|
106
|
* for each of the found UUIDs.
|
107
|
*
|
108
|
* @param memento
|
109
|
*/
|
110
|
private void restoreEditors(final IMemento memento, IProgressMonitor monitor){
|
111
|
if(memento == null){
|
112
|
return;
|
113
|
}
|
114
|
|
115
|
IProgressMonitor subProgressMonitor = EditorUtil.getSubProgressMonitor(monitor, 1);
|
116
|
|
117
|
IMemento editorMementos = memento.getChild(EDITORS);
|
118
|
IMemento[] editorInputs = editorMementos.getChildren(EDITOR);
|
119
|
|
120
|
subProgressMonitor.beginTask("Restoring Editors", 1 + editorInputs.length * 2);
|
121
|
subProgressMonitor.subTask("");
|
122
|
subProgressMonitor.worked(1);
|
123
|
|
124
|
for(IMemento editorInput : editorInputs){
|
125
|
TaxonEditorInput input = (TaxonEditorInput) inputFactory.createElement(editorInput);
|
126
|
subProgressMonitor.worked(1);
|
127
|
if(!subProgressMonitor.isCanceled() && input != null){
|
128
|
try {
|
129
|
EditorUtil.open(input);
|
130
|
} catch (PartInitException e) {
|
131
|
logger.error("Error opening an editor window", e);
|
132
|
}
|
133
|
}
|
134
|
subProgressMonitor.worked(1);
|
135
|
}
|
136
|
subProgressMonitor.done();
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Saves the UUIDs
|
141
|
*
|
142
|
* @param memento
|
143
|
*/
|
144
|
private void saveEditorState(IMemento memento){
|
145
|
if(memento == null){
|
146
|
return;
|
147
|
}
|
148
|
|
149
|
Set<IEditorPart> editors = EditorUtil.getOpenEditors();
|
150
|
|
151
|
IMemento editorMementos = memento.createChild(EDITORS);
|
152
|
|
153
|
for(IEditorPart editor : editors){
|
154
|
IEditorInput input = editor.getEditorInput();
|
155
|
if(input instanceof IPersistable){
|
156
|
IMemento editorMemento = editorMementos.createChild(EDITOR);
|
157
|
((IPersistable) input).saveState(editorMemento);
|
158
|
}
|
159
|
}
|
160
|
}
|
161
|
}
|