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.HashSet;
|
14
|
import java.util.Set;
|
15
|
import java.util.UUID;
|
16
|
|
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;
|
23
|
|
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.model.MessagingUtils;
|
29
|
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
|
30
|
|
31
|
/**
|
32
|
* <p>EditorStateManager class.</p>
|
33
|
*
|
34
|
* @author n.hoffmann
|
35
|
* @created Sep 30, 2010
|
36
|
* @version 1.0
|
37
|
*/
|
38
|
public class EditorStateManager extends ContextListenerAdapter {
|
39
|
|
40
|
private static final String EDITORS = "editors";
|
41
|
|
42
|
private static final String EDITOR = "editor";
|
43
|
|
44
|
private TaxonEditorInputFactory inputFactory;
|
45
|
|
46
|
/**
|
47
|
* <p>Constructor for EditorStateManager.</p>
|
48
|
*/
|
49
|
public EditorStateManager() {
|
50
|
inputFactory = new TaxonEditorInputFactory();
|
51
|
}
|
52
|
|
53
|
/* (non-Javadoc)
|
54
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextAboutToStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
55
|
*/
|
56
|
/** {@inheritDoc} */
|
57
|
@Override
|
58
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
59
|
saveEditorState(memento);
|
60
|
}
|
61
|
|
62
|
/* (non-Javadoc)
|
63
|
* @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
64
|
*/
|
65
|
/** {@inheritDoc} */
|
66
|
@Override
|
67
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
68
|
EditorUtil.closeAll();
|
69
|
resetConstantViews();
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
*
|
74
|
*/
|
75
|
private void resetConstantViews() {
|
76
|
AbstractCdmEditorViewPart descriptiveView = (AbstractCdmEditorViewPart) EditorUtil.getView(DescriptiveViewPart.ID, false);
|
77
|
AbstractCdmEditorViewPart mediaView = (AbstractCdmEditorViewPart) EditorUtil.getView(MediaViewPart.ID, false);
|
78
|
AbstractCdmEditorViewPart conceptView = (AbstractCdmEditorViewPart) EditorUtil.getView(ConceptViewPart.ID, false);
|
79
|
|
80
|
if(descriptiveView != null){
|
81
|
descriptiveView.showEmptyPage();
|
82
|
}
|
83
|
if(mediaView != null){
|
84
|
mediaView.showEmptyPage();
|
85
|
}
|
86
|
if(conceptView != null){
|
87
|
conceptView.showEmptyPage();
|
88
|
}
|
89
|
}
|
90
|
|
91
|
/* (non-Javadoc)
|
92
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStart(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
93
|
*/
|
94
|
/** {@inheritDoc} */
|
95
|
@Override
|
96
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
97
|
restoreEditors(memento, monitor);
|
98
|
}
|
99
|
|
100
|
/* (non-Javadoc)
|
101
|
* @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#contextRefresh(org.eclipse.core.runtime.IProgressMonitor)
|
102
|
*/
|
103
|
@Override
|
104
|
public void contextRefresh(IProgressMonitor monitor) {
|
105
|
refreshEditors(monitor);
|
106
|
}
|
107
|
|
108
|
/* (non-Javadoc)
|
109
|
* @see eu.etaxonomy.taxeditor.model.ContextListenerAdapter#workbenchShutdown(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
110
|
*/
|
111
|
/** {@inheritDoc} */
|
112
|
@Override
|
113
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
114
|
saveEditorState(memento);
|
115
|
}
|
116
|
|
117
|
/**
|
118
|
* Reads taxon node UUIDs from the given memento and tries to open an editor
|
119
|
* for each of the found UUIDs.
|
120
|
*
|
121
|
* @param memento
|
122
|
*/
|
123
|
private void restoreEditors(final IMemento memento, IProgressMonitor monitor){
|
124
|
if(memento == null){
|
125
|
return;
|
126
|
}
|
127
|
|
128
|
IProgressMonitor subProgressMonitor = EditorUtil.getSubProgressMonitor(monitor, 1);
|
129
|
|
130
|
IMemento editorMementos = memento.getChild(EDITORS);
|
131
|
IMemento[] editorInputs = editorMementos.getChildren(EDITOR);
|
132
|
|
133
|
subProgressMonitor.beginTask("Restoring Editors", 1 + editorInputs.length * 2);
|
134
|
subProgressMonitor.subTask("Restoring Editors");
|
135
|
subProgressMonitor.worked(1);
|
136
|
|
137
|
for(IMemento editorInput : editorInputs){
|
138
|
TaxonEditorInput input = (TaxonEditorInput) inputFactory.createElement(editorInput);
|
139
|
subProgressMonitor.worked(1);
|
140
|
if(!subProgressMonitor.isCanceled() && input != null){
|
141
|
try {
|
142
|
EditorUtil.open(input);
|
143
|
} catch (PartInitException e) {
|
144
|
MessagingUtils.error(getClass(), "Error opening an editor window", e);
|
145
|
}
|
146
|
}
|
147
|
subProgressMonitor.worked(1);
|
148
|
}
|
149
|
subProgressMonitor.done();
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* @param monitor
|
154
|
*/
|
155
|
private void refreshEditors(IProgressMonitor monitor) {
|
156
|
IProgressMonitor subProgressMonitor = EditorUtil.getSubProgressMonitor(monitor, 1);
|
157
|
|
158
|
Set<IEditorPart> openEditors = EditorUtil.getOpenEditors();
|
159
|
|
160
|
|
161
|
subProgressMonitor.beginTask("Refreshing Editors", openEditors.size() * 2);
|
162
|
|
163
|
Set<UUID> uuids = new HashSet<UUID>();
|
164
|
|
165
|
for(IEditorPart editor : openEditors){
|
166
|
if(editor instanceof MultiPageTaxonEditor){
|
167
|
try {
|
168
|
uuids.add(((TaxonEditorInput) editor.getEditorInput()).getTaxonNode().getUuid());
|
169
|
|
170
|
// ((MultiPageTaxonEditor) editor).close(false);
|
171
|
EditorUtil.close((MultiPageTaxonEditor)editor);
|
172
|
subProgressMonitor.worked(1);
|
173
|
} catch (Exception e) {
|
174
|
// TODO Auto-generated catch block
|
175
|
e.printStackTrace();
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
|
180
|
for(UUID uuid : uuids){
|
181
|
try {
|
182
|
EditorUtil.openTaxonNode(uuid);
|
183
|
} catch (Exception e) {
|
184
|
// TODO Auto-generated catch block
|
185
|
e.printStackTrace();
|
186
|
}
|
187
|
subProgressMonitor.worked(1);
|
188
|
}
|
189
|
|
190
|
subProgressMonitor.done();
|
191
|
}
|
192
|
|
193
|
/**
|
194
|
* Saves the UUIDs
|
195
|
*
|
196
|
* @param memento
|
197
|
*/
|
198
|
private void saveEditorState(IMemento memento){
|
199
|
if(memento == null){
|
200
|
return;
|
201
|
}
|
202
|
|
203
|
Set<IEditorPart> editors = EditorUtil.getOpenEditors();
|
204
|
|
205
|
IMemento editorMementos = memento.createChild(EDITORS);
|
206
|
|
207
|
for(IEditorPart editor : editors){
|
208
|
IEditorInput input = editor.getEditorInput();
|
209
|
if(input instanceof IPersistable){
|
210
|
IMemento editorMemento = editorMementos.createChild(EDITOR);
|
211
|
((IPersistable) input).saveState(editorMemento);
|
212
|
}
|
213
|
}
|
214
|
}
|
215
|
}
|